File: arguments_spec.rb

package info (click to toggle)
ruby-thor 0.15.3-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 608 kB
  • sloc: ruby: 6,481; makefile: 2
file content (66 lines) | stat: -rw-r--r-- 2,339 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'thor/parser'

describe Thor::Arguments do
  def create(opts={})
    arguments = opts.map do |type, default|
      options = {:required => default.nil?, :type => type, :default => default}
      Thor::Argument.new(type.to_s, options)
    end

    arguments.sort!{ |a,b| b.name <=> a.name }
    @opt = Thor::Arguments.new(arguments)
  end

  def parse(*args)
    @opt.parse(args)
  end

  describe "#parse" do
    it "parses arguments in the given order" do
      create :string => nil, :numeric => nil
      parse("name", "13")["string"].should == "name"
      parse("name", "13")["numeric"].should == 13
    end

    it "accepts hashes" do
      create :string => nil, :hash => nil
      parse("product", "title:string", "age:integer")["string"].should == "product"
      parse("product", "title:string", "age:integer")["hash"].should == { "title" => "string", "age" => "integer"}
      parse("product", "url:http://www.amazon.com/gp/product/123")["hash"].should == { "url" => "http://www.amazon.com/gp/product/123" }
    end

    it "accepts arrays" do
      create :string => nil, :array => nil
      parse("product", "title", "age")["string"].should == "product"
      parse("product", "title", "age")["array"].should == %w(title age)
    end

    describe "with no inputs" do
      it "and no arguments returns an empty hash" do
        create
        parse.should == {}
      end

      it "and required arguments raises an error" do
        create :string => nil, :numeric => nil
        lambda { parse }.should raise_error(Thor::RequiredArgumentMissingError, "No value provided for required arguments 'string', 'numeric'")
      end

      it "and default arguments returns default values" do
        create :string => "name", :numeric => 13
        parse.should == { "string" => "name", "numeric" => 13 }
      end
    end

    it "returns the input if it's already parsed" do
      create :string => nil, :hash => nil, :array => nil, :numeric => nil
      parse("", 0, {}, []).should == { "string" => "", "numeric" => 0, "hash" => {}, "array" => [] }
    end

    it "returns the default value if none is provided" do
      create :string => "foo", :numeric => 3.0
      parse("bar").should == { "string" => "bar", "numeric" => 3.0 }
    end
  end
end