boson 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
data/test/manager_test.rb CHANGED
@@ -1,102 +1,100 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
- module Boson
4
- class ManagerTest < Test::Unit::TestCase
5
- context "after_load" do
6
- def load_library(hash)
7
- new_attributes = {:name=>hash.delete(:name), :commands=>[], :created_dependencies=>[], :loaded=>true}
8
- [:module, :commands].each {|e| new_attributes[e] = hash.delete(e) if hash[e] }
9
- Manager.expects(:load_once).returns(Library.new(new_attributes))
10
- Manager.load([hash[:name]])
11
- end
3
+ describe "Manager" do
4
+ describe "after_load" do
5
+ def load_library(hash)
6
+ new_attributes = {:name=>hash[:name], :commands=>[], :created_dependencies=>[], :loaded=>true}
7
+ [:module, :commands].each {|e| new_attributes[e] = hash.delete(e) if hash[e] }
8
+ Manager.expects(:rescue_load_action).returns(Library.new(new_attributes))
9
+ Manager.load([hash[:name]])
10
+ end
12
11
 
13
- before(:each) { reset_boson }
12
+ before { reset_boson }
14
13
 
15
- test "loads basic library" do
16
- load_library :name=>'blah'
17
- library_loaded? 'blah'
18
- end
14
+ it "loads basic library" do
15
+ load_library :name=>'blah'
16
+ library_loaded? 'blah'
17
+ end
19
18
 
20
- test "loads library with commands" do
21
- load_library :name=>'blah', :commands=>['frylock','meatwad']
22
- library_loaded? 'blah'
23
- command_exists?('frylock')
24
- command_exists?('meatwad')
25
- end
19
+ it "loads library with commands" do
20
+ load_library :name=>'blah', :commands=>['frylock','meatwad']
21
+ library_loaded? 'blah'
22
+ command_exists?('frylock')
23
+ command_exists?('meatwad')
24
+ end
26
25
 
27
- context "command aliases" do
28
- before(:each) { eval %[module ::Aquateen; def frylock; end; end] }
29
- after(:each) { Object.send(:remove_const, "Aquateen") }
26
+ describe "command aliases" do
27
+ before { eval %[module ::Aquateen; def frylock; end; end] }
28
+ after { Object.send(:remove_const, "Aquateen") }
30
29
 
31
- test "created with command specific config" do
32
- with_config(:command_aliases=>{'frylock'=>'fr'}) do
33
- Manager.expects(:create_instance_aliases).with({"Aquateen"=>{"frylock"=>"fr"}})
34
- load_library :name=>'aquateen', :commands=>['frylock'], :module=>Aquateen
35
- library_loaded? 'aquateen'
36
- end
30
+ it "created with command specific config" do
31
+ with_config(:command_aliases=>{'frylock'=>'fr'}) do
32
+ Manager.expects(:create_instance_aliases).with({"Aquateen"=>{"frylock"=>"fr"}})
33
+ load_library :name=>'aquateen', :commands=>['frylock'], :module=>Aquateen
34
+ library_loaded? 'aquateen'
37
35
  end
36
+ end
38
37
 
39
- test "created with config command_aliases" do
40
- with_config(:command_aliases=>{"frylock"=>"fr"}) do
41
- Manager.expects(:create_instance_aliases).with({"Aquateen"=>{"frylock"=>"fr"}})
42
- load_library :name=>'aquateen', :commands=>['frylock'], :module=>Aquateen
43
- library_loaded? 'aquateen'
44
- end
38
+ it "created with config command_aliases" do
39
+ with_config(:command_aliases=>{"frylock"=>"fr"}) do
40
+ Manager.expects(:create_instance_aliases).with({"Aquateen"=>{"frylock"=>"fr"}})
41
+ load_library :name=>'aquateen', :commands=>['frylock'], :module=>Aquateen
42
+ library_loaded? 'aquateen'
45
43
  end
44
+ end
46
45
 
47
- test "not created and warns for commands with no module" do
48
- with_config(:command_aliases=>{'frylock'=>'fr'}) do
49
- capture_stderr {
50
- load_library(:name=>'aquateen', :commands=>['frylock'])
51
- }.should =~ /No aliases/
52
- library_loaded? 'aquateen'
53
- Aquateen.method_defined?(:fr).should == false
54
- end
46
+ it "not created and warns for commands with no module" do
47
+ with_config(:command_aliases=>{'frylock'=>'fr'}) do
48
+ capture_stderr {
49
+ load_library(:name=>'aquateen', :commands=>['frylock'])
50
+ }.should =~ /No aliases/
51
+ library_loaded? 'aquateen'
52
+ Aquateen.method_defined?(:fr).should == false
55
53
  end
56
54
  end
55
+ end
57
56
 
58
- test "merges with existing created library" do
59
- create_library('blah')
60
- load_library :name=>'blah'
61
- library_loaded? 'blah'
62
- Boson.libraries.size.should == 1
63
- end
57
+ it "merges with existing created library" do
58
+ create_library('blah')
59
+ load_library :name=>'blah'
60
+ library_loaded? 'blah'
61
+ Boson.libraries.size.should == 1
64
62
  end
63
+ end
65
64
 
66
- context "option commands without args" do
67
- before(:all) {
68
- reset_boson
69
- @library = Library.new(:name=>'blah', :commands=>['foo', 'bar'])
70
- Boson.libraries << @library
71
- @foo = Command.new(:name=>'foo', :lib=>'blah', :options=>{:fool=>:string}, :args=>'*')
72
- Boson.commands << @foo
73
- Boson.commands << Command.new(:name=>'bar', :lib=>'blah', :options=>{:bah=>:string})
74
- }
65
+ describe "option commands without args" do
66
+ before_all {
67
+ reset_boson
68
+ @library = Library.new(:name=>'blah', :commands=>['foo', 'bar'])
69
+ Boson.libraries << @library
70
+ @foo = Command.new(:name=>'foo', :lib=>'blah', :options=>{:fool=>:string}, :args=>'*')
71
+ Boson.commands << @foo
72
+ Boson.commands << Command.new(:name=>'bar', :lib=>'blah', :options=>{:bah=>:string})
73
+ }
75
74
 
76
- test "are deleted" do
77
- Scientist.expects(:redefine_command).with(anything, @foo)
78
- Manager.redefine_commands(@library, @library.commands)
79
- end
75
+ it "are deleted" do
76
+ Scientist.expects(:redefine_command).with(anything, @foo)
77
+ Manager.redefine_commands(@library, @library.commands)
78
+ end
80
79
 
81
- test "are deleted and printed when verbose" do
82
- Scientist.expects(:redefine_command).with(anything, @foo)
83
- @library.instance_eval("@options = {:verbose=>true}")
84
- capture_stdout { Manager.redefine_commands(@library, @library.commands) } =~ /options.*blah/
85
- end
80
+ it "are deleted and printed when verbose" do
81
+ Scientist.expects(:redefine_command).with(anything, @foo)
82
+ @library.instance_eval("@options = {:verbose=>true}")
83
+ capture_stdout { Manager.redefine_commands(@library, @library.commands) } =~ /options.*blah/
86
84
  end
85
+ end
87
86
 
88
- context "loaded" do
89
- before(:each) { reset_libraries }
87
+ describe "loaded" do
88
+ before { reset_libraries }
90
89
 
91
- test "returns false when library isn't loaded" do
92
- create_library('blah')
93
- Manager.loaded?('blah').should be(false)
94
- end
90
+ it "returns false when library isn't loaded" do
91
+ create_library('blah')
92
+ Manager.loaded?('blah').should == false
93
+ end
95
94
 
96
- test "returns true when library is loaded" do
97
- create_library('blah', :loaded=>true)
98
- Manager.loaded?('blah').should be(true)
99
- end
95
+ it "returns true when library is loaded" do
96
+ create_library('blah', :loaded=>true)
97
+ Manager.loaded?('blah').should == true
100
98
  end
101
99
  end
102
- end
100
+ end
@@ -1,69 +1,67 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
- module Boson
4
- class MethodInspectorTest < Test::Unit::TestCase
5
- test "non commands module can't set anything" do
6
- eval "module Blah; end"
7
- MethodInspector.current_module = Blah
3
+ describe "MethodInspector" do
4
+ it "non commands module can't set anything" do
5
+ eval "module Blah; end"
6
+ MethodInspector.current_module = Blah
7
+ Inspector.enable
8
+ Blah.module_eval("desc 'test'; def test; end; options :a=>1; def test2; end")
9
+ Inspector.disable
10
+ MethodInspector.store[:desc].empty?.should == true
11
+ MethodInspector.store[:options].empty?.should == true
12
+ end
13
+
14
+ describe "commands module with" do
15
+ def parse(string)
8
16
  Inspector.enable
9
- Blah.module_eval("desc 'test'; def test; end; options :a=>1; def test2; end")
17
+ ::Boson::Commands::Zzz.module_eval(string)
10
18
  Inspector.disable
11
- MethodInspector.store[:desc].empty?.should == true
12
- MethodInspector.store[:options].empty?.should == true
19
+ MethodInspector.store
13
20
  end
14
21
 
15
- context "commands module with" do
16
- def parse(string)
17
- Inspector.enable
18
- ::Boson::Commands::Zzz.module_eval(string)
19
- Inspector.disable
20
- MethodInspector.store
21
- end
22
-
23
- before(:all) { eval "module ::Boson::Commands::Zzz; end" }
24
- before(:each) { MethodInspector.mod_store.delete(::Boson::Commands::Zzz) }
22
+ before_all { eval "module ::Boson::Commands::Zzz; end" }
23
+ before { MethodInspector.mod_store.delete(::Boson::Commands::Zzz) }
25
24
 
26
- test "desc sets descriptions" do
27
- parsed = parse "desc 'test'; def m1; end; desc 'one'; desc 'more'; def m2; end"
28
- parsed[:desc].should == {"m1"=>"test", "m2"=>"more"}
29
- end
25
+ it "desc sets descriptions" do
26
+ parsed = parse "desc 'test'; def m1; end; desc 'one'; desc 'more'; def m2; end"
27
+ parsed[:desc].should == {"m1"=>"test", "m2"=>"more"}
28
+ end
30
29
 
31
- test "options sets options" do
32
- parse("options :z=>'b'; def zee; end")[:options].should == {"zee"=>{:z=>'b'}}
33
- end
30
+ it "options sets options" do
31
+ parse("options :z=>'b'; def zee; end")[:options].should == {"zee"=>{:z=>'b'}}
32
+ end
34
33
 
35
- test "render_options sets render_options" do
36
- parse("render_options :z=>true; def zee; end")[:render_options].should == {"zee"=>{:z=>true}}
37
- end
34
+ it "render_options sets render_options" do
35
+ parse("render_options :z=>true; def zee; end")[:render_options].should == {"zee"=>{:z=>true}}
36
+ end
38
37
 
39
- test "config sets config" do
40
- parse("config :z=>true; def zee; end")[:config].should == {"zee"=>{:z=>true}}
41
- end
38
+ it "config sets config" do
39
+ parse("config :z=>true; def zee; end")[:config].should == {"zee"=>{:z=>true}}
40
+ end
42
41
 
43
- test "not all method attributes set causes method_locations to be set" do
44
- MethodInspector.stubs(:find_method_locations).returns(["/some/path", 10])
45
- parsed = parse "desc 'yo'; def yo; end; options :yep=>1; def yep; end; " +
46
- "render_options :a=>1; config :a=>1; desc 'z'; options :a=>1; def az; end"
47
- parsed[:method_locations].key?('yo').should == true
48
- parsed[:method_locations].key?('yep').should == true
49
- parsed[:method_locations].key?('az').should == false
50
- end
42
+ it "not all method attributes set causes method_locations to be set" do
43
+ MethodInspector.stubs(:find_method_locations).returns(["/some/path", 10])
44
+ parsed = parse "desc 'yo'; def yo; end; options :yep=>1; def yep; end; " +
45
+ "render_options :a=>1; config :a=>1; desc 'z'; options :a=>1; def az; end"
46
+ parsed[:method_locations].key?('yo').should == true
47
+ parsed[:method_locations].key?('yep').should == true
48
+ parsed[:method_locations].key?('az').should == false
49
+ end
51
50
 
52
- test "no find_method_locations doesn't set method_locations" do
53
- MethodInspector.stubs(:find_method_locations).returns(nil)
54
- parse("def bluh; end")[:method_locations].key?('bluh').should == false
55
- end
51
+ it "no find_method_locations doesn't set method_locations" do
52
+ MethodInspector.stubs(:find_method_locations).returns(nil)
53
+ parse("def bluh; end")[:method_locations].key?('bluh').should == false
54
+ end
56
55
 
57
- test "options calls scrape_with_eval" do
58
- ArgumentInspector.expects(:scrape_with_eval).returns([['arg1']])
59
- parse("desc 'desc'; options :some=>:opts; def doy(arg1); end")[:args]['doy'].should == [['arg1']]
60
- end
56
+ it "options calls scrape_with_eval" do
57
+ ArgumentInspector.expects(:scrape_with_eval).returns([['arg1']])
58
+ parse("desc 'desc'; options :some=>:opts; def doy(arg1); end")[:args]['doy'].should == [['arg1']]
59
+ end
61
60
 
62
- test "options in file calls scrape_with_eval" do
63
- MethodInspector.expects(:inspector_in_file?).returns(true)
64
- ArgumentInspector.expects(:scrape_with_eval).returns([['arg1']])
65
- parse("desc 'desc'; def doz(arg1); end")[:args]['doz'].should == [['arg1']]
66
- end
61
+ it "options in file calls scrape_with_eval" do
62
+ MethodInspector.expects(:inspector_in_file?).returns(true)
63
+ ArgumentInspector.expects(:scrape_with_eval).returns([['arg1']])
64
+ parse("desc 'desc'; def doz(arg1); end")[:args]['doz'].should == [['arg1']]
67
65
  end
68
66
  end
69
- end
67
+ end
@@ -1,11 +1,19 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
- module Boson
4
- class OptionParserTest < Test::Unit::TestCase
5
- include OptionTestHelper
3
+ describe "OptionParser" do
4
+ def create(opts)
5
+ @opt = OptionParser.new(opts)
6
+ end
7
+
8
+ def opt; @opt; end
9
+
10
+ def parse(*args)
11
+ @non_opts = []
12
+ opt.parse(args.flatten)
13
+ end
6
14
 
7
- context "IndifferentAccessHash" do
8
- before(:each) {
15
+ describe "IndifferentAccessHash" do
16
+ before {
9
17
  @hash = IndifferentAccessHash.new 'foo' => 'bar', 'baz' => 'bee'
10
18
  }
11
19
  it "can access values indifferently" do
@@ -31,7 +39,7 @@ module Boson
31
39
  end
32
40
  end
33
41
 
34
- context "naming" do
42
+ describe "naming" do
35
43
  it "automatically aliases long options with their first letter" do
36
44
  create "--foo" => true
37
45
  parse("-f")["foo"].should == true
@@ -122,7 +130,7 @@ module Boson
122
130
 
123
131
  end
124
132
 
125
- context "option values can be set with" do
133
+ describe "option values can be set with" do
126
134
  it "a opt=<value> assignment" do
127
135
  create :foo => :string
128
136
  parse("--foo=12")["foo"].should == "12"
@@ -153,51 +161,51 @@ module Boson
153
161
  end
154
162
  end
155
163
 
156
- context "parse" do
164
+ describe "parse" do
157
165
  it "extracts non-option arguments" do
158
166
  create "--foo" => :string, "--bar" => true
159
167
  parse("foo", "bar", "--baz", "--foo", "12", "--bar", "-T", "bang").should == {
160
168
  :foo => "12", :bar => true
161
169
  }
162
- @opt.leading_non_opts.should == ["foo", "bar", "--baz"]
163
- @opt.trailing_non_opts.should == ["-T", "bang"]
164
- @opt.non_opts.should == ["foo", "bar", "--baz", "-T", "bang"]
170
+ opt.leading_non_opts.should == ["foo", "bar", "--baz"]
171
+ opt.trailing_non_opts.should == ["-T", "bang"]
172
+ opt.non_opts.should == ["foo", "bar", "--baz", "-T", "bang"]
165
173
  end
166
174
 
167
175
  it "stopped by --" do
168
176
  create :foo=>:boolean, :dude=>:boolean
169
177
  parse("foo", "bar", "--", "-f").should == {}
170
- @opt.leading_non_opts.should == %w{foo bar}
171
- @opt.trailing_non_opts.should == %w{-- -f}
178
+ opt.leading_non_opts.should == %w{foo bar}
179
+ opt.trailing_non_opts.should == %w{-- -f}
172
180
  end
173
181
 
174
- context "with parse flag" do
182
+ describe "with parse flag" do
175
183
  it ":delete_invalid_opts deletes and warns of invalid options" do
176
184
  create(:foo=>:boolean)
177
185
  capture_stderr {
178
- @opt.parse(%w{-f -d ok}, :delete_invalid_opts=>true)
186
+ opt.parse(%w{-f -d ok}, :delete_invalid_opts=>true)
179
187
  }.should =~ /Deleted invalid option '-d'/
180
- @opt.non_opts.should == ['ok']
188
+ opt.non_opts.should == ['ok']
181
189
  end
182
190
 
183
191
  it ":delete_invalid_opts deletes until - or --" do
184
192
  create(:foo=>:boolean, :bar=>:boolean)
185
193
  %w{- --}.each do |stop_char|
186
194
  capture_stderr {
187
- @opt.parse(%w{ok -b -d} << stop_char << '-f', :delete_invalid_opts=>true)
195
+ opt.parse(%w{ok -b -d} << stop_char << '-f', :delete_invalid_opts=>true)
188
196
  }.should =~ /'-d'/
189
- @opt.non_opts.should == %w{ok -d} << stop_char << '-f'
197
+ opt.non_opts.should == %w{ok -d} << stop_char << '-f'
190
198
  end
191
199
  end
192
200
 
193
201
  it ":opts_before_args only allows options before args" do
194
202
  create(:foo=>:boolean)
195
- @opt.parse(%w{ok -f}, :opts_before_args=>true).should == {}
196
- @opt.parse(%w{-f ok}, :opts_before_args=>true).should == {:foo=>true}
203
+ opt.parse(%w{ok -f}, :opts_before_args=>true).should == {}
204
+ opt.parse(%w{-f ok}, :opts_before_args=>true).should == {:foo=>true}
197
205
  end
198
206
  end
199
207
 
200
- context "with no arguments" do
208
+ describe "with no arguments" do
201
209
  it "and no options returns an empty hash" do
202
210
  create({})
203
211
  parse.should == {}
@@ -210,7 +218,7 @@ module Boson
210
218
  end
211
219
  end
212
220
 
213
- context "option hashes" do
221
+ describe "option hashes" do
214
222
  it "make hash keys available as symbols as well" do
215
223
  create "--foo" => :string
216
224
  parse("--foo", "12")[:foo].should == "12"
@@ -224,8 +232,8 @@ module Boson
224
232
  end
225
233
  end
226
234
 
227
- context ":required option attribute" do
228
- before(:all) {
235
+ describe ":required option attribute" do
236
+ before_all {
229
237
  create "--foo" => {:type=>:string, :required=>true}, :bar => {:type=>:hash, :required=>true}
230
238
  }
231
239
 
@@ -242,8 +250,8 @@ module Boson
242
250
  end
243
251
  end
244
252
 
245
- context ":bool_default option attribute" do
246
- before(:all) {
253
+ describe ":bool_default option attribute" do
254
+ before_all {
247
255
  create :foo=>{:type=>:string, :bool_default=>'whoop'}, :bar=>{:type=>:array, :bool_default=>'1'},
248
256
  :verbose=>:boolean, :yep=>{:type=>:string, :bool_default=>true}
249
257
  }
@@ -270,10 +278,10 @@ module Boson
270
278
  end
271
279
  end
272
280
 
273
- context "option with attributes" do
281
+ describe "option with attributes" do
274
282
  it "can get type from :type" do
275
283
  create :foo=>{:type=>:numeric}
276
- parse("-f", '3')[:foo] == 3
284
+ parse("-f", '3')[:foo].should == 3
277
285
  end
278
286
 
279
287
  it "can get type and default from :default" do
@@ -289,10 +297,10 @@ module Boson
289
297
  end
290
298
 
291
299
  def usage
292
- @opt.formatted_usage.split(" ").sort
300
+ opt.formatted_usage.split(" ").sort
293
301
  end
294
302
 
295
- context "#formatted_usage" do
303
+ describe "#formatted_usage" do
296
304
  it "outputs string args with sample values" do
297
305
  create "--repo" => :string, "--branch" => "bugfix", "-n" => 6
298
306
  usage.should == %w([--branch=bugfix] [--repo=REPO] [-n=6])
@@ -314,47 +322,46 @@ module Boson
314
322
  end
315
323
  end
316
324
 
317
- context "user defined option class" do
318
- before(:all) {
325
+ describe "user defined option class" do
326
+ before_all {
319
327
  ::FooBoo = Struct.new(:name)
320
- module ::Boson::Options::FooBoo
328
+ module Options::FooBoo
321
329
  def create_foo_boo(value)
322
330
  ::FooBoo.new(value)
323
331
  end
324
332
  def validate_foo_boo(value); end
325
333
  end
326
- ::Boson::OptionParser.send :include, ::Boson::Options::FooBoo
334
+ ::OptionParser.send :include, Options::FooBoo
327
335
  create :a=>:foo_boo, :b=>::FooBoo.new('blah'), :c=>:blah_blah,
328
336
  :d=>{:type=>:foo_boo, :type=>::FooBoo.new('bling')}
329
337
  }
330
338
 
331
- test "created from symbol" do
339
+ it "created from symbol" do
332
340
  (obj = parse('-a', 'whoop')[:a]).class.should == ::FooBoo
333
341
  obj.name.should == 'whoop'
334
342
  end
335
343
 
336
- test "created from default" do
344
+ it "created from default" do
337
345
  (obj = parse[:b]).class.should == ::FooBoo
338
346
  obj.name.should == 'blah'
339
347
  end
340
348
 
341
- test "created from type attribute" do
349
+ it "created from type attribute" do
342
350
  (obj = parse('-d', 'whoop')[:d]).class.should == ::FooBoo
343
351
  obj.name.should == 'whoop'
344
352
  end
345
353
 
346
- test "has its validation called" do
347
- @opt.expects(:validate_foo_boo)
354
+ it "has its validation called" do
355
+ opt.expects(:validate_foo_boo)
348
356
  parse("-a", 'blah')
349
357
  end
350
358
 
351
- test "has default usage" do
359
+ it "has default usage" do
352
360
  usage[0].should == "[-a=:foo_boo]"
353
361
  end
354
362
 
355
- test "when nonexistant raises error" do
363
+ it "when nonexistant raises error" do
356
364
  assert_error(OptionParser::Error, "invalid.*:blah_blah") { parse("-c", 'ok') }
357
365
  end
358
366
  end
359
- end
360
367
  end