Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 280 lines (231 sloc) 8.435 kB
3965f04 @josevalim Improving test coverage.
josevalim authored
1 require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
5c87fd4 @josevalim More parser refactoring.
josevalim authored
2 require 'thor/base'
3965f04 @josevalim Improving test coverage.
josevalim authored
3
30b0ca6 @josevalim More specs.
josevalim authored
4 class Amazing
5 desc "hello", "say hello"
6 def hello
7 puts "Hello"
8 end
3965f04 @josevalim Improving test coverage.
josevalim authored
9 end
10
6761560 @josevalim Moved everything to base.
josevalim authored
11 describe Thor::Base do
1120bf1 @josevalim Spec'ing initializer.
josevalim authored
12 describe "#initialize" do
752c458 @josevalim Allow default options on initilization.
josevalim authored
13 it "sets arguments array" do
1120bf1 @josevalim Spec'ing initializer.
josevalim authored
14 base = MyCounter.new [1, 2]
35b14a9 @sferik Add rcov to bundle
sferik authored
15 base.first.should == 1
16 base.second.should == 2
1120bf1 @josevalim Spec'ing initializer.
josevalim authored
17 end
18
752c458 @josevalim Allow default options on initilization.
josevalim authored
19 it "sets arguments default values" do
5c87fd4 @josevalim More parser refactoring.
josevalim authored
20 base = MyCounter.new [1]
35b14a9 @sferik Add rcov to bundle
sferik authored
21 base.second.should == 2
752c458 @josevalim Allow default options on initilization.
josevalim authored
22 end
23
24 it "sets options default values" do
5c87fd4 @josevalim More parser refactoring.
josevalim authored
25 base = MyCounter.new [1, 2]
35b14a9 @sferik Add rcov to bundle
sferik authored
26 base.options[:third].should == 3
752c458 @josevalim Allow default options on initilization.
josevalim authored
27 end
28
6cb7714 @josevalim Ensure that options hash has indifferent access.
josevalim authored
29 it "allows options to be given as symbols or strings" do
30 base = MyCounter.new [1, 2], :third => 4
35b14a9 @sferik Add rcov to bundle
sferik authored
31 base.options[:third].should == 4
6cb7714 @josevalim Ensure that options hash has indifferent access.
josevalim authored
32
33 base = MyCounter.new [1, 2], "third" => 4
35b14a9 @sferik Add rcov to bundle
sferik authored
34 base.options[:third].should == 4
6cb7714 @josevalim Ensure that options hash has indifferent access.
josevalim authored
35 end
36
752c458 @josevalim Allow default options on initilization.
josevalim authored
37 it "creates options with indifferent access" do
1120bf1 @josevalim Spec'ing initializer.
josevalim authored
38 base = MyCounter.new [1, 2], :third => 3
35b14a9 @sferik Add rcov to bundle
sferik authored
39 base.options['third'].should == 3
1120bf1 @josevalim Spec'ing initializer.
josevalim authored
40 end
41
752c458 @josevalim Allow default options on initilization.
josevalim authored
42 it "creates options with magic predicates" do
1120bf1 @josevalim Spec'ing initializer.
josevalim authored
43 base = MyCounter.new [1, 2], :third => 3
35b14a9 @sferik Add rcov to bundle
sferik authored
44 base.options.third.should == 3
1120bf1 @josevalim Spec'ing initializer.
josevalim authored
45 end
ca57d97 @josevalim Created methods to invoke tasks. You can still run a task directly, b…
josevalim authored
46 end
47
76383c7 @josevalim More specs, more documentation.
josevalim authored
48 describe "#no_tasks" do
49 it "avoids methods being added as tasks" do
35b14a9 @sferik Add rcov to bundle
sferik authored
50 MyScript.tasks.keys.should include("animal")
51 MyScript.tasks.keys.should_not include("this_is_not_a_task")
9d13f97 @josevalim Added :for to arguments and options. This allow to change a behavior …
josevalim authored
52 end
3965f04 @josevalim Improving test coverage.
josevalim authored
53 end
54
174f019 @josevalim If an argument is supplied as switch, it should put previous assigned…
josevalim authored
55 describe "#argument" do
56 it "sets a value as required and creates an accessor for it" do
35b14a9 @sferik Add rcov to bundle
sferik authored
57 MyCounter.start(["1", "2", "--third", "3"])[0].should == 1
58 Scripts::MyScript.start(["zoo", "my_special_param", "--param=normal_param"]).should == "my_special_param"
174f019 @josevalim If an argument is supplied as switch, it should put previous assigned…
josevalim authored
59 end
60
61 it "does not set a value in the options hash" do
35b14a9 @sferik Add rcov to bundle
sferik authored
62 BrokenCounter.start(["1", "2", "--third", "3"])[0].should be_nil
174f019 @josevalim If an argument is supplied as switch, it should put previous assigned…
josevalim authored
63 end
64 end
65
3acd153 @josevalim Moving task list responsabilities to one place.
josevalim authored
66 describe "#arguments" do
67 it "returns the arguments for the class" do
35b14a9 @sferik Add rcov to bundle
sferik authored
68 MyCounter.arguments.should have(2).items
3acd153 @josevalim Moving task list responsabilities to one place.
josevalim authored
69 end
70 end
71
174f019 @josevalim If an argument is supplied as switch, it should put previous assigned…
josevalim authored
72 describe "#class_option" do
73 it "sets options class wise" do
35b14a9 @sferik Add rcov to bundle
sferik authored
74 MyCounter.start(["1", "2", "--third", "3"])[2].should == 3
174f019 @josevalim If an argument is supplied as switch, it should put previous assigned…
josevalim authored
75 end
76
14bcf81 @joshbuddy typo in spec
joshbuddy authored
77 it "does not create an accessor for it" do
35b14a9 @sferik Add rcov to bundle
sferik authored
78 BrokenCounter.start(["1", "2", "--third", "3"])[3].should be_false
174f019 @josevalim If an argument is supplied as switch, it should put previous assigned…
josevalim authored
79 end
80 end
81
82 describe "#class_options" do
83 it "sets default options overwriting superclass definitions" do
1b475f5 @josevalim Added specs for source paths.
josevalim authored
84 options = Scripts::MyScript.class_options
35b14a9 @sferik Add rcov to bundle
sferik authored
85 options[:force].should_not be_required
174f019 @josevalim If an argument is supplied as switch, it should put previous assigned…
josevalim authored
86 end
87 end
88
243b51a @josevalim Allow attr_accessor to be undefined as well.
josevalim authored
89 describe "#remove_argument" do
90 it "removes previous defined arguments from class" do
35b14a9 @sferik Add rcov to bundle
sferik authored
91 ClearCounter.arguments.should be_empty
243b51a @josevalim Allow attr_accessor to be undefined as well.
josevalim authored
92 end
93
94 it "undefine accessors if required" do
35b14a9 @sferik Add rcov to bundle
sferik authored
95 ClearCounter.new.should_not respond_to(:first)
96 ClearCounter.new.should_not respond_to(:second)
243b51a @josevalim Allow attr_accessor to be undefined as well.
josevalim authored
97 end
98 end
99
100 describe "#remove_class_option" do
101 it "removes previous defined class option" do
35b14a9 @sferik Add rcov to bundle
sferik authored
102 ClearCounter.class_options[:third].should be_nil
243b51a @josevalim Allow attr_accessor to be undefined as well.
josevalim authored
103 end
104 end
105
2a2b77b @josevalim Does not inline default values in class options. Show them below the …
josevalim authored
106 describe "#class_options_help" do
509b87b @sferik Replace before(:each) with before
sferik authored
107 before do
9f45c21 @josevalim Improving shell capabilities and add Thor::Shell::Color.
josevalim authored
108 @content = capture(:stdout) { MyCounter.help(Thor::Base.shell.new) }
2a2b77b @josevalim Does not inline default values in class options. Show them below the …
josevalim authored
109 end
110
111 it "shows options description" do
35b14a9 @sferik Add rcov to bundle
sferik authored
112 @content.should =~ /# The third argument/
2a2b77b @josevalim Does not inline default values in class options. Show them below the …
josevalim authored
113 end
114
ffdd72e @josevalim Add banner to options.
josevalim authored
115 it "shows usage with banner content" do
35b14a9 @sferik Add rcov to bundle
sferik authored
116 @content.should =~ /\[\-\-third=THREE\]/
2a2b77b @josevalim Does not inline default values in class options. Show them below the …
josevalim authored
117 end
118
119 it "shows default values below description" do
35b14a9 @sferik Add rcov to bundle
sferik authored
120 @content.should =~ /# Default: 3/
2a2b77b @josevalim Does not inline default values in class options. Show them below the …
josevalim authored
121 end
122
123 it "shows options in different groups" do
35b14a9 @sferik Add rcov to bundle
sferik authored
124 @content.should =~ /Options\:/
125 @content.should =~ /Runtime options\:/
126 @content.should =~ /\-p, \[\-\-pretend\]/
2a2b77b @josevalim Does not inline default values in class options. Show them below the …
josevalim authored
127 end
8bbc6c6 @josevalim Allow extra options to be given to class_options_help.
josevalim authored
128
40d6950 @josevalim Use padding in options that does not have aliases.
josevalim authored
129 it "use padding in options that does not have aliases" do
35b14a9 @sferik Add rcov to bundle
sferik authored
130 @content.should =~ /^ -t, \[--third/
131 @content.should =~ /^ \[--fourth/
40d6950 @josevalim Use padding in options that does not have aliases.
josevalim authored
132 end
133
8bbc6c6 @josevalim Allow extra options to be given to class_options_help.
josevalim authored
134 it "allows extra options to be given" do
fd8f9ab @josevalim Finished moving code from Rails to Thor.
josevalim authored
135 hash = { "Foo" => B.class_options.values }
8bbc6c6 @josevalim Allow extra options to be given to class_options_help.
josevalim authored
136
094cd62 @josevalim Fix specs.
josevalim authored
137 content = capture(:stdout) { MyCounter.send(:class_options_help, Thor::Base.shell.new, hash) }
35b14a9 @sferik Add rcov to bundle
sferik authored
138 content.should =~ /Foo options\:/
139 content.should =~ /--last-name=LAST_NAME/
8bbc6c6 @josevalim Allow extra options to be given to class_options_help.
josevalim authored
140 end
0150b86 @sferik Add enum to string arguments
sferik authored
141
142 it "displays choices for enums" do
143 content = capture(:stdout) { Enum.help(Thor::Base.shell.new) }
144 content.should =~ /Possible values\: apple, banana/
145 end
2a2b77b @josevalim Does not inline default values in class options. Show them below the …
josevalim authored
146 end
147
0e8485c @josevalim Remove most of invokation logic from tasks and bumped up tasks tests.
josevalim authored
148 describe "#namespace" do
05eef24 @josevalim You can now set the namespace.
josevalim authored
149 it "returns the default class namespace" do
35b14a9 @sferik Add rcov to bundle
sferik authored
150 Scripts::MyScript.namespace.should == "scripts:my_script"
0e8485c @josevalim Remove most of invokation logic from tasks and bumped up tasks tests.
josevalim authored
151 end
05eef24 @josevalim You can now set the namespace.
josevalim authored
152
153 it "sets a namespace to the class" do
35b14a9 @sferik Add rcov to bundle
sferik authored
154 Scripts::MyDefaults.namespace.should == "default"
05eef24 @josevalim You can now set the namespace.
josevalim authored
155 end
0e8485c @josevalim Remove most of invokation logic from tasks and bumped up tasks tests.
josevalim authored
156 end
157
3965f04 @josevalim Improving test coverage.
josevalim authored
158 describe "#group" do
cdb73e6 @josevalim Remove group name accessor. You can get the group calling group.
josevalim authored
159 it "sets a group" do
35b14a9 @sferik Add rcov to bundle
sferik authored
160 MyScript.group.should == "script"
3965f04 @josevalim Improving test coverage.
josevalim authored
161 end
162
cdb73e6 @josevalim Remove group name accessor. You can get the group calling group.
josevalim authored
163 it "inherits the group from parent" do
35b14a9 @sferik Add rcov to bundle
sferik authored
164 MyChildScript.group.should == "script"
3965f04 @josevalim Improving test coverage.
josevalim authored
165 end
166
cdb73e6 @josevalim Remove group name accessor. You can get the group calling group.
josevalim authored
167 it "defaults to standard if no group is given" do
35b14a9 @sferik Add rcov to bundle
sferik authored
168 Amazing.group.should == "standard"
3965f04 @josevalim Improving test coverage.
josevalim authored
169 end
170 end
171
30b0ca6 @josevalim More specs.
josevalim authored
172 describe "#subclasses" do
173 it "tracks its subclasses in an Array" do
35b14a9 @sferik Add rcov to bundle
sferik authored
174 Thor::Base.subclasses.should include(MyScript)
175 Thor::Base.subclasses.should include(MyChildScript)
176 Thor::Base.subclasses.should include(Scripts::MyScript)
30b0ca6 @josevalim More specs.
josevalim authored
177 end
178 end
179
180 describe "#subclass_files" do
181 it "returns tracked subclasses, grouped by the files they come from" do
30ae344 @josevalim Improving more specs structure.
josevalim authored
182 thorfile = File.join(File.dirname(__FILE__), "fixtures", "script.thor")
35b14a9 @sferik Add rcov to bundle
sferik authored
183 Thor::Base.subclass_files[File.expand_path(thorfile)].should == [
5bb798a @indirect Fix subcommand help to display subcommands instead of tasks
indirect authored
184 MyScript, MyScript::AnotherScript, MyChildScript, Barn,
185 Scripts::MyScript, Scripts::MyDefaults, Scripts::ChildDefault
dfb5a14 @josevalim Stub at rake compat layer.
josevalim authored
186 ]
30b0ca6 @josevalim More specs.
josevalim authored
187 end
188
189 it "tracks a single subclass across multiple files" do
190 thorfile = File.join(File.dirname(__FILE__), "fixtures", "task.thor")
35b14a9 @sferik Add rcov to bundle
sferik authored
191 Thor::Base.subclass_files[File.expand_path(thorfile)].should include(Amazing)
192 Thor::Base.subclass_files[File.expand_path(__FILE__)].should include(Amazing)
30b0ca6 @josevalim More specs.
josevalim authored
193 end
194 end
195
d42a3c9 @josevalim Added the hability to remove tasks.
josevalim authored
196 describe "#tasks" do
197 it "returns a list with all tasks defined in this class" do
35b14a9 @sferik Add rcov to bundle
sferik authored
198 MyChildScript.new.should respond_to("animal")
199 MyChildScript.tasks.keys.should include("animal")
d42a3c9 @josevalim Added the hability to remove tasks.
josevalim authored
200 end
2324d70 @josevalim Underscore private instance methods and raise an error if the user tr…
josevalim authored
201
202 it "raises an error if a task with reserved word is defined" do
203 lambda {
e93949e @josevalim Remove all from reserved words.
josevalim authored
204 klass = Class.new(Thor::Group)
205 klass.class_eval "def shell; end"
35b14a9 @sferik Add rcov to bundle
sferik authored
206 }.should raise_error(RuntimeError, /"shell" is a Thor reserved word and cannot be defined as task/)
2324d70 @josevalim Underscore private instance methods and raise an error if the user tr…
josevalim authored
207 end
d42a3c9 @josevalim Added the hability to remove tasks.
josevalim authored
208 end
209
210 describe "#all_tasks" do
211 it "returns a list with all tasks defined in this class plus superclasses" do
35b14a9 @sferik Add rcov to bundle
sferik authored
212 MyChildScript.new.should respond_to("foo")
213 MyChildScript.all_tasks.keys.should include("foo")
d42a3c9 @josevalim Added the hability to remove tasks.
josevalim authored
214 end
215 end
216
217 describe "#remove_task" do
218 it "removes the task from its tasks hash" do
35b14a9 @sferik Add rcov to bundle
sferik authored
219 MyChildScript.tasks.keys.should_not include("bar")
220 MyChildScript.tasks.keys.should_not include("boom")
d42a3c9 @josevalim Added the hability to remove tasks.
josevalim authored
221 end
222
223 it "undefines the method if desired" do
35b14a9 @sferik Add rcov to bundle
sferik authored
224 MyChildScript.new.should_not respond_to("boom")
d42a3c9 @josevalim Added the hability to remove tasks.
josevalim authored
225 end
226 end
b883fc6 @josevalim Does not invoke a method on the superclass if it does not respond to …
josevalim authored
227
228 describe "#from_superclass" do
229 it "does not send a method to the superclass if the superclass does not respond to it" do
35b14a9 @sferik Add rcov to bundle
sferik authored
230 MyCounter.get_from_super.should == 13
b883fc6 @josevalim Does not invoke a method on the superclass if it does not respond to …
josevalim authored
231 end
232 end
2abff2b @josevalim Show debug information if --debug is supplied.
josevalim authored
233
234 describe "#start" do
8bd8eef @josevalim Do not steal --debug.
josevalim authored
235 it "raises an error instead of rescueing if THOR_DEBUG=1 is given" do
236 begin
237 ENV["THOR_DEBUG"] = 1
238 lambda {
239 MyScript.start ["what", "--debug"]
240 }.should raise_error(Thor::UndefinedTaskError, 'Could not find task "what" in "my_script" namespace.')
241 rescue
242 ENV["THOR_DEBUG"] = nil
243 end
2abff2b @josevalim Show debug information if --debug is supplied.
josevalim authored
244 end
dca8f99 @josevalim Allow to check and raise error on unknown options.
josevalim authored
245
636e496 @josevalim Thor should not steal ARGV.
josevalim authored
246 it "does not steal args" do
247 args = ["foo", "bar", "--force", "true"]
248 MyScript.start(args)
35b14a9 @sferik Add rcov to bundle
sferik authored
249 args.should == ["foo", "bar", "--force", "true"]
636e496 @josevalim Thor should not steal ARGV.
josevalim authored
250 end
251
dca8f99 @josevalim Allow to check and raise error on unknown options.
josevalim authored
252 it "checks unknown options" do
253 capture(:stderr) {
254 MyScript.start(["foo", "bar", "--force", "true", "--unknown", "baz"])
35b14a9 @sferik Add rcov to bundle
sferik authored
255 }.strip.should == "Unknown switches '--unknown'"
dca8f99 @josevalim Allow to check and raise error on unknown options.
josevalim authored
256 end
9c00e55 @josevalim Add :only and :except to check_unknown_options.
josevalim authored
257
258 it "checks unknown options except specified" do
259 capture(:stderr) {
0cfe4af @wycats Changes in semantics:
wycats authored
260 MyScript.start(["with_optional", "NAME", "--omg", "--invalid"]).should == ["NAME", {}, ["--omg", "--invalid"]]
35b14a9 @sferik Add rcov to bundle
sferik authored
261 }.strip.should be_empty
9c00e55 @josevalim Add :only and :except to check_unknown_options.
josevalim authored
262 end
2abff2b @josevalim Show debug information if --debug is supplied.
josevalim authored
263 end
1aa9eab @josevalim attr_* are automatically not marked as tasks.
josevalim authored
264
265 describe "attr_*" do
266 it "should not add attr_reader as a task" do
35b14a9 @sferik Add rcov to bundle
sferik authored
267 capture(:stderr){ MyScript.start(["another_attribute"]) }.should =~ /Could not find/
1aa9eab @josevalim attr_* are automatically not marked as tasks.
josevalim authored
268 end
269
270 it "should not add attr_writer as a task" do
35b14a9 @sferik Add rcov to bundle
sferik authored
271 capture(:stderr){ MyScript.start(["another_attribute=", "foo"]) }.should =~ /Could not find/
1aa9eab @josevalim attr_* are automatically not marked as tasks.
josevalim authored
272 end
273
274 it "should not add attr_accessor as a task" do
35b14a9 @sferik Add rcov to bundle
sferik authored
275 capture(:stderr){ MyScript.start(["some_attribute"]) }.should =~ /Could not find/
276 capture(:stderr){ MyScript.start(["some_attribute=", "foo"]) }.should =~ /Could not find/
1aa9eab @josevalim attr_* are automatically not marked as tasks.
josevalim authored
277 end
278 end
3965f04 @josevalim Improving test coverage.
josevalim authored
279 end
Something went wrong with that request. Please try again.