thor 0.14.4 → 0.14.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +11 -0
- data/Thorfile +9 -55
- data/lib/thor.rb +17 -0
- data/lib/thor/actions.rb +11 -6
- data/lib/thor/actions/create_file.rb +2 -2
- data/lib/thor/actions/create_link.rb +57 -0
- data/lib/thor/actions/file_manipulation.rb +36 -12
- data/lib/thor/actions/inject_into_file.rb +15 -10
- data/lib/thor/parser/option.rb +1 -1
- data/lib/thor/task.rb +1 -1
- data/lib/thor/version.rb +1 -1
- data/spec/actions/create_file_spec.rb +27 -27
- data/spec/actions/directory_spec.rb +23 -23
- data/spec/actions/empty_directory_spec.rb +12 -12
- data/spec/actions/file_manipulation_spec.rb +65 -42
- data/spec/actions/inject_into_file_spec.rb +24 -24
- data/spec/actions_spec.rb +63 -58
- data/spec/base_spec.rb +60 -60
- data/spec/core_ext/hash_with_indifferent_access_spec.rb +14 -14
- data/spec/core_ext/ordered_hash_spec.rb +28 -28
- data/spec/group_spec.rb +39 -39
- data/spec/invocation_spec.rb +21 -21
- data/spec/parser/argument_spec.rb +7 -7
- data/spec/parser/arguments_spec.rb +11 -11
- data/spec/parser/option_spec.rb +45 -45
- data/spec/parser/options_spec.rb +75 -75
- data/spec/rake_compat_spec.rb +10 -10
- data/spec/register_spec.rb +92 -0
- data/spec/runner_spec.rb +32 -32
- data/spec/shell/basic_spec.rb +18 -18
- data/spec/shell/color_spec.rb +3 -3
- data/spec/shell_spec.rb +10 -10
- data/spec/spec_helper.rb +9 -10
- data/spec/task_spec.rb +8 -8
- data/spec/thor_spec.rb +62 -62
- data/spec/util_spec.rb +35 -35
- metadata +100 -68
- data/spec/spec.opts +0 -1
data/spec/group_spec.rb
CHANGED
@@ -3,36 +3,36 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
3
3
|
describe Thor::Group do
|
4
4
|
describe "task" do
|
5
5
|
it "allows to use private methods from parent class as tasks" do
|
6
|
-
ChildGroup.start.
|
7
|
-
ChildGroup.new.baz("bar").
|
6
|
+
ChildGroup.start.should == ["bar", "foo", "baz"]
|
7
|
+
ChildGroup.new.baz("bar").should == "bar"
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
11
|
describe "#start" do
|
12
12
|
it "invokes all the tasks under the Thor group" do
|
13
|
-
MyCounter.start(["1", "2", "--third", "3"]).
|
13
|
+
MyCounter.start(["1", "2", "--third", "3"]).should == [ 1, 2, 3 ]
|
14
14
|
end
|
15
15
|
|
16
16
|
it "uses argument default value" do
|
17
|
-
MyCounter.start(["1", "--third", "3"]).
|
17
|
+
MyCounter.start(["1", "--third", "3"]).should == [ 1, 2, 3 ]
|
18
18
|
end
|
19
19
|
|
20
20
|
it "invokes all the tasks in the Thor group and his parents" do
|
21
|
-
BrokenCounter.start(["1", "2", "--third", "3"]).
|
21
|
+
BrokenCounter.start(["1", "2", "--third", "3"]).should == [ nil, 2, 3, false, 5 ]
|
22
22
|
end
|
23
23
|
|
24
24
|
it "raises an error if a required argument is added after a non-required" do
|
25
25
|
lambda {
|
26
26
|
MyCounter.argument(:foo, :type => :string)
|
27
|
-
}.
|
27
|
+
}.should raise_error(ArgumentError, 'You cannot have "foo" as required argument after the non-required argument "second".')
|
28
28
|
end
|
29
29
|
|
30
30
|
it "raises when an exception happens within the task call" do
|
31
|
-
lambda { BrokenCounter.start(["1", "2", "--fail"]) }.
|
31
|
+
lambda { BrokenCounter.start(["1", "2", "--fail"]) }.should raise_error
|
32
32
|
end
|
33
33
|
|
34
34
|
it "raises an error when a Thor group task expects arguments" do
|
35
|
-
lambda { WhinyGenerator.start }.
|
35
|
+
lambda { WhinyGenerator.start }.should raise_error(ArgumentError, /Are you sure it has arity equals to 0\?/)
|
36
36
|
end
|
37
37
|
|
38
38
|
it "invokes help message if any of the shortcuts is given" do
|
@@ -43,15 +43,15 @@ describe Thor::Group do
|
|
43
43
|
|
44
44
|
describe "#desc" do
|
45
45
|
it "sets the description for a given class" do
|
46
|
-
MyCounter.desc.
|
46
|
+
MyCounter.desc.should == "Description:\n This generator runs three tasks: one, two and three.\n"
|
47
47
|
end
|
48
48
|
|
49
49
|
it "can be inherited" do
|
50
|
-
BrokenCounter.desc.
|
50
|
+
BrokenCounter.desc.should == "Description:\n This generator runs three tasks: one, two and three.\n"
|
51
51
|
end
|
52
52
|
|
53
53
|
it "can be nil" do
|
54
|
-
WhinyGenerator.desc.
|
54
|
+
WhinyGenerator.desc.should be_nil
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
@@ -61,17 +61,17 @@ describe Thor::Group do
|
|
61
61
|
end
|
62
62
|
|
63
63
|
it "provides usage information" do
|
64
|
-
@content.
|
64
|
+
@content.should =~ /my_counter N \[N\]/
|
65
65
|
end
|
66
66
|
|
67
67
|
it "shows description" do
|
68
|
-
@content.
|
69
|
-
@content.
|
68
|
+
@content.should =~ /Description:/
|
69
|
+
@content.should =~ /This generator runs three tasks: one, two and three./
|
70
70
|
end
|
71
71
|
|
72
72
|
it "shows options information" do
|
73
|
-
@content.
|
74
|
-
@content.
|
73
|
+
@content.should =~ /Options/
|
74
|
+
@content.should =~ /\[\-\-third=THREE\]/
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
@@ -81,28 +81,28 @@ describe Thor::Group do
|
|
81
81
|
end
|
82
82
|
|
83
83
|
it "allows to invoke a class from the class binding" do
|
84
|
-
@content.
|
84
|
+
@content.should =~ /1\n2\n3\n4\n5\n/
|
85
85
|
end
|
86
86
|
|
87
87
|
it "shows invocation information to the user" do
|
88
|
-
@content.
|
88
|
+
@content.should =~ /invoke Defined/
|
89
89
|
end
|
90
90
|
|
91
91
|
it "uses padding on status generated by the invoked class" do
|
92
|
-
@content.
|
92
|
+
@content.should =~ /finished counting/
|
93
93
|
end
|
94
94
|
|
95
95
|
it "allows invocation to be configured with blocks" do
|
96
96
|
capture(:stdout) do
|
97
|
-
F.start.
|
97
|
+
F.start.should == ["Valim, Jose"]
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
101
|
it "shows invoked options on help" do
|
102
102
|
content = capture(:stdout){ E.help(Thor::Base.shell.new) }
|
103
|
-
content.
|
104
|
-
content.
|
105
|
-
content.
|
103
|
+
content.should =~ /Defined options:/
|
104
|
+
content.should =~ /\[--unused\]/
|
105
|
+
content.should =~ /# This option has no use/
|
106
106
|
end
|
107
107
|
end
|
108
108
|
|
@@ -113,36 +113,36 @@ describe Thor::Group do
|
|
113
113
|
end
|
114
114
|
|
115
115
|
it "allows to invoke a class from the class binding by a default option" do
|
116
|
-
@content.
|
116
|
+
@content.should =~ /1\n2\n3\n4\n5\n/
|
117
117
|
end
|
118
118
|
|
119
119
|
it "does not invoke if the option is nil" do
|
120
|
-
capture(:stdout){ G.start(["--skip-invoked"]) }.
|
120
|
+
capture(:stdout){ G.start(["--skip-invoked"]) }.should_not =~ /invoke/
|
121
121
|
end
|
122
122
|
|
123
123
|
it "prints a message if invocation cannot be found" do
|
124
124
|
content = capture(:stdout){ G.start(["--invoked", "unknown"]) }
|
125
|
-
content.
|
125
|
+
content.should =~ /error unknown \[not found\]/
|
126
126
|
end
|
127
127
|
|
128
128
|
it "allows to invoke a class from the class binding by the given option" do
|
129
129
|
content = capture(:stdout){ G.start(["--invoked", "e"]) }
|
130
|
-
content.
|
130
|
+
content.should =~ /invoke e/
|
131
131
|
end
|
132
132
|
|
133
133
|
it "shows invocation information to the user" do
|
134
|
-
@content.
|
134
|
+
@content.should =~ /invoke defined/
|
135
135
|
end
|
136
136
|
|
137
137
|
it "uses padding on status generated by the invoked class" do
|
138
|
-
@content.
|
138
|
+
@content.should =~ /finished counting/
|
139
139
|
end
|
140
140
|
|
141
141
|
it "shows invoked options on help" do
|
142
142
|
content = capture(:stdout){ G.help(Thor::Base.shell.new) }
|
143
|
-
content.
|
144
|
-
content.
|
145
|
-
content.
|
143
|
+
content.should =~ /defined options:/
|
144
|
+
content.should =~ /\[--unused\]/
|
145
|
+
content.should =~ /# This option has no use/
|
146
146
|
end
|
147
147
|
end
|
148
148
|
|
@@ -152,26 +152,26 @@ describe Thor::Group do
|
|
152
152
|
end
|
153
153
|
|
154
154
|
it "allows to invoke a class from the class binding by a default option" do
|
155
|
-
@content.
|
155
|
+
@content.should =~ /1\n2\n3\n4\n5\n/
|
156
156
|
end
|
157
157
|
|
158
158
|
it "does not invoke if the option is false" do
|
159
|
-
capture(:stdout){ H.start(["--no-defined"]) }.
|
159
|
+
capture(:stdout){ H.start(["--no-defined"]) }.should_not =~ /invoke/
|
160
160
|
end
|
161
161
|
|
162
162
|
it "shows invocation information to the user" do
|
163
|
-
@content.
|
163
|
+
@content.should =~ /invoke defined/
|
164
164
|
end
|
165
165
|
|
166
166
|
it "uses padding on status generated by the invoked class" do
|
167
|
-
@content.
|
167
|
+
@content.should =~ /finished counting/
|
168
168
|
end
|
169
169
|
|
170
170
|
it "shows invoked options on help" do
|
171
171
|
content = capture(:stdout){ H.help(Thor::Base.shell.new) }
|
172
|
-
content.
|
173
|
-
content.
|
174
|
-
content.
|
172
|
+
content.should =~ /defined options:/
|
173
|
+
content.should =~ /\[--unused\]/
|
174
|
+
content.should =~ /# This option has no use/
|
175
175
|
end
|
176
176
|
end
|
177
177
|
end
|
data/spec/invocation_spec.rb
CHANGED
@@ -4,82 +4,82 @@ require 'thor/base'
|
|
4
4
|
describe Thor::Invocation do
|
5
5
|
describe "#invoke" do
|
6
6
|
it "invokes a task inside another task" do
|
7
|
-
capture(:stdout){ A.new.invoke(:two) }.
|
7
|
+
capture(:stdout){ A.new.invoke(:two) }.should == "2\n3\n"
|
8
8
|
end
|
9
9
|
|
10
10
|
it "invokes a task just once" do
|
11
|
-
capture(:stdout){ A.new.invoke(:one) }.
|
11
|
+
capture(:stdout){ A.new.invoke(:one) }.should == "1\n2\n3\n"
|
12
12
|
end
|
13
13
|
|
14
14
|
it "invokes a task just once even if they belongs to different classes" do
|
15
|
-
capture(:stdout){ Defined.new.invoke(:one) }.
|
15
|
+
capture(:stdout){ Defined.new.invoke(:one) }.should == "1\n2\n3\n4\n5\n"
|
16
16
|
end
|
17
17
|
|
18
18
|
it "invokes a task with arguments" do
|
19
|
-
A.new.invoke(:five, [5]).
|
20
|
-
A.new.invoke(:five, [7]).
|
19
|
+
A.new.invoke(:five, [5]).should be_true
|
20
|
+
A.new.invoke(:five, [7]).should be_false
|
21
21
|
end
|
22
22
|
|
23
23
|
it "invokes the default task if none is given to a Thor class" do
|
24
24
|
content = capture(:stdout){ A.new.invoke("b") }
|
25
|
-
content.
|
26
|
-
content.
|
25
|
+
content.should =~ /Tasks/
|
26
|
+
content.should =~ /LAST_NAME/
|
27
27
|
end
|
28
28
|
|
29
29
|
it "accepts a class as argument without a task to invoke" do
|
30
30
|
content = capture(:stdout){ A.new.invoke(B) }
|
31
|
-
content.
|
32
|
-
content.
|
31
|
+
content.should =~ /Tasks/
|
32
|
+
content.should =~ /LAST_NAME/
|
33
33
|
end
|
34
34
|
|
35
35
|
it "accepts a class as argument with a task to invoke" do
|
36
36
|
base = A.new([], :last_name => "Valim")
|
37
|
-
base.invoke(B, :one, ["Jose"]).
|
37
|
+
base.invoke(B, :one, ["Jose"]).should == "Valim, Jose"
|
38
38
|
end
|
39
39
|
|
40
40
|
it "allows customized options to be given" do
|
41
41
|
base = A.new([], :last_name => "Wrong")
|
42
|
-
base.invoke(B, :one, ["Jose"], :last_name => "Valim").
|
42
|
+
base.invoke(B, :one, ["Jose"], :last_name => "Valim").should == "Valim, Jose"
|
43
43
|
end
|
44
44
|
|
45
45
|
it "reparses options in the new class" do
|
46
|
-
A.start(["invoker", "--last-name", "Valim"]).
|
46
|
+
A.start(["invoker", "--last-name", "Valim"]).should == "Valim, Jose"
|
47
47
|
end
|
48
48
|
|
49
49
|
it "shares initialize options with invoked class" do
|
50
|
-
A.new([], :foo => :bar).invoke("b:two").
|
50
|
+
A.new([], :foo => :bar).invoke("b:two").should == { "foo" => :bar }
|
51
51
|
end
|
52
52
|
|
53
53
|
it "dump configuration values to be used in the invoked class" do
|
54
54
|
base = A.new
|
55
|
-
base.invoke("b:three").shell.
|
55
|
+
base.invoke("b:three").shell.should == base.shell
|
56
56
|
end
|
57
57
|
|
58
58
|
it "allow extra configuration values to be given" do
|
59
59
|
base, shell = A.new, Thor::Base.shell.new
|
60
|
-
base.invoke("b:three", [], {}, :shell => shell).shell.
|
60
|
+
base.invoke("b:three", [], {}, :shell => shell).shell.should == shell
|
61
61
|
end
|
62
62
|
|
63
63
|
it "invokes a Thor::Group and all of its tasks" do
|
64
|
-
capture(:stdout){ A.new.invoke(:c) }.
|
64
|
+
capture(:stdout){ A.new.invoke(:c) }.should == "1\n2\n3\n"
|
65
65
|
end
|
66
66
|
|
67
67
|
it "does not invoke a Thor::Group twice" do
|
68
68
|
base = A.new
|
69
69
|
silence(:stdout){ base.invoke(:c) }
|
70
|
-
capture(:stdout){ base.invoke(:c) }.
|
70
|
+
capture(:stdout){ base.invoke(:c) }.should be_empty
|
71
71
|
end
|
72
72
|
|
73
73
|
it "does not invoke any of Thor::Group tasks twice" do
|
74
74
|
base = A.new
|
75
75
|
silence(:stdout){ base.invoke(:c) }
|
76
|
-
capture(:stdout){ base.invoke("c:one") }.
|
76
|
+
capture(:stdout){ base.invoke("c:one") }.should be_empty
|
77
77
|
end
|
78
78
|
|
79
79
|
it "raises Thor::UndefinedTaskError if the task can't be found" do
|
80
80
|
lambda do
|
81
81
|
A.new.invoke("foo:bar")
|
82
|
-
end.
|
82
|
+
end.should raise_error(Thor::UndefinedTaskError)
|
83
83
|
end
|
84
84
|
|
85
85
|
it "raises Thor::UndefinedTaskError if the task can't be found even if all tasks where already executed" do
|
@@ -88,13 +88,13 @@ describe Thor::Invocation do
|
|
88
88
|
|
89
89
|
lambda do
|
90
90
|
base.invoke("foo:bar")
|
91
|
-
end.
|
91
|
+
end.should raise_error(Thor::UndefinedTaskError)
|
92
92
|
end
|
93
93
|
|
94
94
|
it "raises an error if a non Thor class is given" do
|
95
95
|
lambda do
|
96
96
|
A.new.invoke(Object)
|
97
|
-
end.
|
97
|
+
end.should raise_error(RuntimeError, "Expected Thor class, got Object")
|
98
98
|
end
|
99
99
|
end
|
100
100
|
end
|
@@ -11,37 +11,37 @@ describe Thor::Argument do
|
|
11
11
|
it "raises an error if name is not supplied" do
|
12
12
|
lambda {
|
13
13
|
argument(nil)
|
14
|
-
}.
|
14
|
+
}.should raise_error(ArgumentError, "Argument name can't be nil.")
|
15
15
|
end
|
16
16
|
|
17
17
|
it "raises an error if type is unknown" do
|
18
18
|
lambda {
|
19
19
|
argument(:task, :unknown)
|
20
|
-
}.
|
20
|
+
}.should raise_error(ArgumentError, "Type :unknown is not valid for arguments.")
|
21
21
|
end
|
22
22
|
|
23
23
|
it "raises an error if argument is required and have default values" do
|
24
24
|
lambda {
|
25
25
|
argument(:task, :string, "bar", true)
|
26
|
-
}.
|
26
|
+
}.should raise_error(ArgumentError, "An argument cannot be required and have default value.")
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
30
|
describe "#usage" do
|
31
31
|
it "returns usage for string types" do
|
32
|
-
argument(:foo, :string).usage.
|
32
|
+
argument(:foo, :string).usage.should == "FOO"
|
33
33
|
end
|
34
34
|
|
35
35
|
it "returns usage for numeric types" do
|
36
|
-
argument(:foo, :numeric).usage.
|
36
|
+
argument(:foo, :numeric).usage.should == "N"
|
37
37
|
end
|
38
38
|
|
39
39
|
it "returns usage for array types" do
|
40
|
-
argument(:foo, :array).usage.
|
40
|
+
argument(:foo, :array).usage.should == "one two three"
|
41
41
|
end
|
42
42
|
|
43
43
|
it "returns usage for hash types" do
|
44
|
-
argument(:foo, :hash).usage.
|
44
|
+
argument(:foo, :hash).usage.should == "key:value"
|
45
45
|
end
|
46
46
|
end
|
47
47
|
end
|
@@ -18,47 +18,47 @@ describe Thor::Arguments do
|
|
18
18
|
describe "#parse" do
|
19
19
|
it "parses arguments in the given order" do
|
20
20
|
create :string => nil, :numeric => nil
|
21
|
-
parse("name", "13")["string"].
|
22
|
-
parse("name", "13")["numeric"].
|
21
|
+
parse("name", "13")["string"].should == "name"
|
22
|
+
parse("name", "13")["numeric"].should == 13
|
23
23
|
end
|
24
24
|
|
25
25
|
it "accepts hashes" do
|
26
26
|
create :string => nil, :hash => nil
|
27
|
-
parse("product", "title:string", "age:integer")["string"].
|
28
|
-
parse("product", "title:string", "age:integer")["hash"].
|
27
|
+
parse("product", "title:string", "age:integer")["string"].should == "product"
|
28
|
+
parse("product", "title:string", "age:integer")["hash"].should == { "title" => "string", "age" => "integer"}
|
29
29
|
end
|
30
30
|
|
31
31
|
it "accepts arrays" do
|
32
32
|
create :string => nil, :array => nil
|
33
|
-
parse("product", "title", "age")["string"].
|
34
|
-
parse("product", "title", "age")["array"].
|
33
|
+
parse("product", "title", "age")["string"].should == "product"
|
34
|
+
parse("product", "title", "age")["array"].should == %w(title age)
|
35
35
|
end
|
36
36
|
|
37
37
|
describe "with no inputs" do
|
38
38
|
it "and no arguments returns an empty hash" do
|
39
39
|
create
|
40
|
-
parse.
|
40
|
+
parse.should == {}
|
41
41
|
end
|
42
42
|
|
43
43
|
it "and required arguments raises an error" do
|
44
44
|
create :string => nil, :numeric => nil
|
45
|
-
lambda { parse }.
|
45
|
+
lambda { parse }.should raise_error(Thor::RequiredArgumentMissingError, "No value provided for required arguments 'string', 'numeric'")
|
46
46
|
end
|
47
47
|
|
48
48
|
it "and default arguments returns default values" do
|
49
49
|
create :string => "name", :numeric => 13
|
50
|
-
parse.
|
50
|
+
parse.should == { "string" => "name", "numeric" => 13 }
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
54
|
it "returns the input if it's already parsed" do
|
55
55
|
create :string => nil, :hash => nil, :array => nil, :numeric => nil
|
56
|
-
parse("", 0, {}, []).
|
56
|
+
parse("", 0, {}, []).should == { "string" => "", "numeric" => 0, "hash" => {}, "array" => [] }
|
57
57
|
end
|
58
58
|
|
59
59
|
it "returns the default value if none is provided" do
|
60
60
|
create :string => "foo", :numeric => 3.0
|
61
|
-
parse("bar").
|
61
|
+
parse("bar").should == { "string" => "bar", "numeric" => 3.0 }
|
62
62
|
end
|
63
63
|
end
|
64
64
|
end
|
data/spec/parser/option_spec.rb
CHANGED
@@ -15,187 +15,187 @@ describe Thor::Option do
|
|
15
15
|
describe "with value as a symbol" do
|
16
16
|
describe "and symbol is a valid type" do
|
17
17
|
it "has type equals to the symbol" do
|
18
|
-
parse(:foo, :string).type.
|
19
|
-
parse(:foo, :numeric).type.
|
18
|
+
parse(:foo, :string).type.should == :string
|
19
|
+
parse(:foo, :numeric).type.should == :numeric
|
20
20
|
end
|
21
21
|
|
22
22
|
it "has not default value" do
|
23
|
-
parse(:foo, :string).default.
|
24
|
-
parse(:foo, :numeric).default.
|
23
|
+
parse(:foo, :string).default.should be_nil
|
24
|
+
parse(:foo, :numeric).default.should be_nil
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
28
|
describe "equals to :required" do
|
29
29
|
it "has type equals to :string" do
|
30
|
-
parse(:foo, :required).type.
|
30
|
+
parse(:foo, :required).type.should == :string
|
31
31
|
end
|
32
32
|
|
33
33
|
it "has no default value" do
|
34
|
-
parse(:foo, :required).default.
|
34
|
+
parse(:foo, :required).default.should be_nil
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
38
|
describe "and symbol is not a reserved key" do
|
39
39
|
it "has type equals to :string" do
|
40
|
-
parse(:foo, :bar).type.
|
40
|
+
parse(:foo, :bar).type.should == :string
|
41
41
|
end
|
42
42
|
|
43
43
|
it "has no default value" do
|
44
|
-
parse(:foo, :bar).default.
|
44
|
+
parse(:foo, :bar).default.should be_nil
|
45
45
|
end
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
49
|
describe "with value as hash" do
|
50
50
|
it "has default type :hash" do
|
51
|
-
parse(:foo, :a => :b).type.
|
51
|
+
parse(:foo, :a => :b).type.should == :hash
|
52
52
|
end
|
53
53
|
|
54
54
|
it "has default value equals to the hash" do
|
55
|
-
parse(:foo, :a => :b).default.
|
55
|
+
parse(:foo, :a => :b).default.should == { :a => :b }
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
59
|
describe "with value as array" do
|
60
60
|
it "has default type :array" do
|
61
|
-
parse(:foo, [:a, :b]).type.
|
61
|
+
parse(:foo, [:a, :b]).type.should == :array
|
62
62
|
end
|
63
63
|
|
64
64
|
it "has default value equals to the array" do
|
65
|
-
parse(:foo, [:a, :b]).default.
|
65
|
+
parse(:foo, [:a, :b]).default.should == [:a, :b]
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
69
|
describe "with value as string" do
|
70
70
|
it "has default type :string" do
|
71
|
-
parse(:foo, "bar").type.
|
71
|
+
parse(:foo, "bar").type.should == :string
|
72
72
|
end
|
73
73
|
|
74
74
|
it "has default value equals to the string" do
|
75
|
-
parse(:foo, "bar").default.
|
75
|
+
parse(:foo, "bar").default.should == "bar"
|
76
76
|
end
|
77
77
|
end
|
78
78
|
|
79
79
|
describe "with value as numeric" do
|
80
80
|
it "has default type :numeric" do
|
81
|
-
parse(:foo, 2.0).type.
|
81
|
+
parse(:foo, 2.0).type.should == :numeric
|
82
82
|
end
|
83
83
|
|
84
84
|
it "has default value equals to the numeric" do
|
85
|
-
parse(:foo, 2.0).default.
|
85
|
+
parse(:foo, 2.0).default.should == 2.0
|
86
86
|
end
|
87
87
|
end
|
88
88
|
|
89
89
|
describe "with value as boolean" do
|
90
90
|
it "has default type :boolean" do
|
91
|
-
parse(:foo, true).type.
|
92
|
-
parse(:foo, false).type.
|
91
|
+
parse(:foo, true).type.should == :boolean
|
92
|
+
parse(:foo, false).type.should == :boolean
|
93
93
|
end
|
94
94
|
|
95
95
|
it "has default value equals to the boolean" do
|
96
|
-
parse(:foo, true).default.
|
97
|
-
parse(:foo, false).default.
|
96
|
+
parse(:foo, true).default.should == true
|
97
|
+
parse(:foo, false).default.should == false
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
101
|
describe "with key as a symbol" do
|
102
102
|
it "sets the name equals to the key" do
|
103
|
-
parse(:foo, true).name.
|
103
|
+
parse(:foo, true).name.should == "foo"
|
104
104
|
end
|
105
105
|
end
|
106
106
|
|
107
107
|
describe "with key as an array" do
|
108
108
|
it "sets the first items in the array to the name" do
|
109
|
-
parse([:foo, :bar, :baz], true).name.
|
109
|
+
parse([:foo, :bar, :baz], true).name.should == "foo"
|
110
110
|
end
|
111
111
|
|
112
112
|
it "sets all other items as aliases" do
|
113
|
-
parse([:foo, :bar, :baz], true).aliases.
|
113
|
+
parse([:foo, :bar, :baz], true).aliases.should == [:bar, :baz]
|
114
114
|
end
|
115
115
|
end
|
116
116
|
end
|
117
117
|
|
118
118
|
it "returns the switch name" do
|
119
|
-
option("foo").switch_name.
|
120
|
-
option("--foo").switch_name.
|
119
|
+
option("foo").switch_name.should == "--foo"
|
120
|
+
option("--foo").switch_name.should == "--foo"
|
121
121
|
end
|
122
122
|
|
123
123
|
it "returns the human name" do
|
124
|
-
option("foo").human_name.
|
125
|
-
option("--foo").human_name.
|
124
|
+
option("foo").human_name.should == "foo"
|
125
|
+
option("--foo").human_name.should == "foo"
|
126
126
|
end
|
127
127
|
|
128
128
|
it "converts underscores to dashes" do
|
129
|
-
option("foo_bar").switch_name.
|
129
|
+
option("foo_bar").switch_name.should == "--foo-bar"
|
130
130
|
end
|
131
131
|
|
132
132
|
it "can be required and have default values" do
|
133
133
|
option = option("foo", nil, true, :string, "bar")
|
134
|
-
option.default.
|
135
|
-
option.
|
134
|
+
option.default.should == "bar"
|
135
|
+
option.should be_required
|
136
136
|
end
|
137
137
|
|
138
138
|
it "cannot be required and have type boolean" do
|
139
139
|
lambda {
|
140
140
|
option("foo", nil, true, :boolean)
|
141
|
-
}.
|
141
|
+
}.should raise_error(ArgumentError, "An option cannot be boolean and required.")
|
142
142
|
end
|
143
143
|
|
144
144
|
it "allows type predicates" do
|
145
|
-
parse(:foo, :string).
|
146
|
-
parse(:foo, :boolean).
|
147
|
-
parse(:foo, :numeric).
|
145
|
+
parse(:foo, :string).should be_string
|
146
|
+
parse(:foo, :boolean).should be_boolean
|
147
|
+
parse(:foo, :numeric).should be_numeric
|
148
148
|
end
|
149
149
|
|
150
150
|
it "raises an error on method missing" do
|
151
151
|
lambda {
|
152
152
|
parse(:foo, :string).unknown?
|
153
|
-
}.
|
153
|
+
}.should raise_error(NoMethodError)
|
154
154
|
end
|
155
155
|
|
156
156
|
describe "#usage" do
|
157
157
|
|
158
158
|
it "returns usage for string types" do
|
159
|
-
parse(:foo, :string).usage.
|
159
|
+
parse(:foo, :string).usage.should == "[--foo=FOO]"
|
160
160
|
end
|
161
161
|
|
162
162
|
it "returns usage for numeric types" do
|
163
|
-
parse(:foo, :numeric).usage.
|
163
|
+
parse(:foo, :numeric).usage.should == "[--foo=N]"
|
164
164
|
end
|
165
165
|
|
166
166
|
it "returns usage for array types" do
|
167
|
-
parse(:foo, :array).usage.
|
167
|
+
parse(:foo, :array).usage.should == "[--foo=one two three]"
|
168
168
|
end
|
169
169
|
|
170
170
|
it "returns usage for hash types" do
|
171
|
-
parse(:foo, :hash).usage.
|
171
|
+
parse(:foo, :hash).usage.should == "[--foo=key:value]"
|
172
172
|
end
|
173
173
|
|
174
174
|
it "returns usage for boolean types" do
|
175
|
-
parse(:foo, :boolean).usage.
|
175
|
+
parse(:foo, :boolean).usage.should == "[--foo]"
|
176
176
|
end
|
177
177
|
|
178
178
|
it "uses padding when no aliases is given" do
|
179
|
-
parse(:foo, :boolean).usage(4).
|
179
|
+
parse(:foo, :boolean).usage(4).should == " [--foo]"
|
180
180
|
end
|
181
181
|
|
182
182
|
it "uses banner when supplied" do
|
183
|
-
option(:foo, nil, false, :string, nil, "BAR").usage.
|
183
|
+
option(:foo, nil, false, :string, nil, "BAR").usage.should == "[--foo=BAR]"
|
184
184
|
end
|
185
185
|
|
186
186
|
it "checkes when banner is an empty string" do
|
187
|
-
option(:foo, nil, false, :string, nil, "").usage.
|
187
|
+
option(:foo, nil, false, :string, nil, "").usage.should == "[--foo]"
|
188
188
|
end
|
189
189
|
|
190
190
|
describe "with required values" do
|
191
191
|
it "does not show the usage between brackets" do
|
192
|
-
parse(:foo, :required).usage.
|
192
|
+
parse(:foo, :required).usage.should == "--foo=FOO"
|
193
193
|
end
|
194
194
|
end
|
195
195
|
|
196
196
|
describe "with aliases" do
|
197
197
|
it "does not show the usage between brackets" do
|
198
|
-
parse([:foo, "-f", "-b"], :required).usage.
|
198
|
+
parse([:foo, "-f", "-b"], :required).usage.should == "-f, -b, --foo=FOO"
|
199
199
|
end
|
200
200
|
end
|
201
201
|
end
|