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/options_test.rb CHANGED
@@ -1,189 +1,189 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
- module Boson
4
- class OptionsTest < Test::Unit::TestCase
5
- include OptionTestHelper
3
+ describe "Options" do
4
+ def create(opts)
5
+ @opt = OptionParser.new(opts)
6
+ end
7
+
8
+ def parse(*args)
9
+ @non_opts = []
10
+ @opt.parse(args.flatten)
11
+ end
6
12
 
7
- context ":string type" do
8
- before :each do
9
- create "--foo" => :string, "--bar" => :string, :blah=>{:type=>:string, :default=>:huh}
10
- end
13
+ describe ":string type" do
14
+ before {
15
+ create "--foo" => :string, "--bar" => :string, :blah=>{:type=>:string, :default=>:huh}
16
+ }
11
17
 
12
- it "doesn't set nonexistant options" do
13
- parse("--bling")[:bar].should == nil
14
- end
18
+ it "doesn't set nonexistant options" do
19
+ parse("--bling")[:bar].should == nil
20
+ end
15
21
 
16
- it "sets values correctly" do
17
- parse("--foo", "12")[:foo].should == "12"
18
- parse("--bar", "12")[:bar].should == "12"
19
- end
22
+ it "sets values correctly" do
23
+ parse("--foo", "12")[:foo].should == "12"
24
+ parse("--bar", "12")[:bar].should == "12"
25
+ end
20
26
 
21
- it "raises error if passed another valid option" do
22
- assert_error(OptionParser::Error, "cannot pass.*'foo'") { parse("--foo", "--bar") }
23
- end
27
+ it "raises error if passed another valid option" do
28
+ assert_error(OptionParser::Error, "cannot pass.*'foo'") { parse("--foo", "--bar") }
29
+ end
24
30
 
25
- it "raises error if not passed a value" do
26
- assert_error(OptionParser::Error, "no value.*'foo'") { parse("--foo") }
27
- end
31
+ it "raises error if not passed a value" do
32
+ assert_error(OptionParser::Error, "no value.*'foo'") { parse("--foo") }
33
+ end
28
34
 
29
- it "overwrites earlier values with later values" do
30
- parse("--foo", "12", "--foo", "13")[:foo].should == "13"
31
- end
35
+ it "overwrites earlier values with later values" do
36
+ parse("--foo", "12", "--foo", "13")[:foo].should == "13"
37
+ end
32
38
 
33
- it "can have symbolic default value" do
34
- parse('--blah','ok')[:blah].should == 'ok'
35
- end
39
+ it "can have symbolic default value" do
40
+ parse('--blah','ok')[:blah].should == 'ok'
41
+ end
42
+ end
43
+
44
+ describe ":string type with :values attribute" do
45
+ before_all { create :foo=>{:type=>:string, :values=>%w{angola abu abib}} }
46
+ it "auto aliases if a match exists" do
47
+ parse("-f", "an")[:foo].should == 'angola'
36
48
  end
37
-
38
- context ":string type with :values attribute" do
39
- before(:all ) { create :foo=>{:type=>:string, :values=>%w{angola abu abib}} }
40
- it "auto aliases if a match exists" do
41
- parse("-f", "an")[:foo].should == 'angola'
42
- end
43
49
 
44
- it "auto aliases first sorted match" do
45
- parse("-f", "a")[:foo].should == 'abib'
46
- end
50
+ it "auto aliases first sorted match" do
51
+ parse("-f", "a")[:foo].should == 'abib'
52
+ end
47
53
 
48
- it "raises error if option doesn't auto alias or match given values" do
49
- assert_error(OptionParser::Error, "invalid.*'z'") { parse("-f", "z") }
50
- end
54
+ it "raises error if option doesn't auto alias or match given values" do
55
+ assert_error(OptionParser::Error, "invalid.*'z'") { parse("-f", "z") }
56
+ end
51
57
 
52
- it "doesn't raise error for a nonmatch if enum is false" do
53
- create :foo=>{:type=>:string, :values=>%w{angola abu abib}, :enum=>false}
54
- parse("-f", "z")[:foo].should == 'z'
55
- end
58
+ it "doesn't raise error for a nonmatch if enum is false" do
59
+ create :foo=>{:type=>:string, :values=>%w{angola abu abib}, :enum=>false}
60
+ parse("-f", "z")[:foo].should == 'z'
56
61
  end
62
+ end
63
+
64
+ describe ":string type with default value" do
65
+ before { create "--branch" => "master" }
57
66
 
58
- context ":string type with default value" do
59
- before(:each) do
60
- create "--branch" => "master"
61
- end
62
-
63
- it "should get the specified value" do
64
- parse("--branch", "bugfix").should == { :branch => "bugfix" }
65
- end
67
+ it "should get the specified value" do
68
+ parse("--branch", "bugfix").should == { :branch => "bugfix" }
69
+ end
70
+
71
+ it "should get the default value when not specified" do
72
+ parse.should == { :branch => "master" }
73
+ end
74
+ end
75
+
76
+ describe ":numeric type" do
77
+ before { create "n" => :numeric, "m" => 5 }
78
+
79
+ it "supports numeric defaults" do
80
+ parse["m"].should == 5
81
+ end
82
+
83
+ it "converts values to numeric types" do
84
+ parse("-n", "3", "-m", ".5").should == {:n => 3, :m => 0.5}
85
+ end
66
86
 
67
- it "should get the default value when not specified" do
68
- parse.should == { :branch => "master" }
69
- end
87
+ it "raises error when value isn't numeric" do
88
+ assert_error(OptionParser::Error, "expected numeric value for.*'n'") { parse("-n", "foo") }
70
89
  end
71
90
 
72
- context ":numeric type" do
73
- before(:each) do
74
- create "n" => :numeric, "m" => 5
75
- end
76
-
77
- it "supports numeric defaults" do
78
- parse["m"].should == 5
79
- end
80
-
81
- it "converts values to numeric types" do
82
- parse("-n", "3", "-m", ".5").should == {:n => 3, :m => 0.5}
83
- end
84
-
85
- it "raises error when value isn't numeric" do
86
- assert_error(OptionParser::Error, "expected numeric value for.*'n'") { parse("-n", "foo") }
87
- end
88
-
89
- it "raises error when opt is present without value" do
90
- assert_error(OptionParser::Error, "no value.*'n'") { parse("-n") }
91
- end
92
- end
93
-
94
- context ":array type" do
95
- before(:all) {
96
- create :a=>:array, :b=>[1,2,3], :c=>{:type=>:array, :values=>%w{foo fa bar zebra}, :enum=>false},
97
- :d=>{:type=>:array, :split=>" ", :values=>[:ab, :bc, :cd], :enum=>false},
98
- :e=>{:type=>:array, :values=>%w{some so silly}, :regexp=>true}
99
- }
100
-
101
- it "supports array defaults" do
102
- parse[:b].should == [1,2,3]
103
- end
104
-
105
- it "converts comma delimited values to an array" do
106
- parse("-a","1,2,5")[:a].should == %w{1 2 5}
107
- end
108
-
109
- it "raises error when option has no value" do
110
- assert_error(OptionParser::Error, "no value.*'a'") { parse("-a") }
111
- end
112
-
113
- it "auto aliases :values attribute" do
114
- parse("-c","f,b")[:c].should == %w{fa bar}
115
- end
116
-
117
- it "auto aliases symbolic :values" do
118
- parse("-d","a c")[:d].should == [:ab,:cd]
119
- end
120
-
121
- it "supports a configurable splitter" do
122
- parse("-d", "yogi berra")[:d].should == %w{yogi berra}
123
- end
124
-
125
- it "aliases * to all values" do
126
- parse("-c", '*')[:c].sort.should == %w{bar fa foo zebra}
127
- parse("-c", '*,ok')[:c].sort.should == %w{bar fa foo ok zebra}
128
- end
129
-
130
- it "aliases correctly with :regexp on" do
131
- parse("-e", 'so')[:e].sort.should == %w{so some}
132
- end
133
- end
134
-
135
- context ":hash type" do
136
- before(:all) {
137
- create :a=>:hash, :b=>{:default=>{:a=>'b'}}, :c=>{:type=>:hash, :keys=>%w{one two three}},
138
- :e=>{:type=>:hash, :keys=>[:one, :two, :three], :default_keys=>:three},
139
- :d=>{:type=>:hash, :split=>" "}
140
- }
141
-
142
- it "converts comma delimited pairs to hash" do
143
- parse("-a", "f:3,g:4")[:a].should == {'f'=>'3', 'g'=>'4'}
144
- end
145
-
146
- it "supports hash defaults" do
147
- parse[:b].should == {:a=>'b'}
148
- end
149
-
150
- it "raises error when option has no value" do
151
- assert_error(OptionParser::Error, "no value.*'a'") { parse("-a") }
152
- end
153
-
154
- it "raises error if invalid key-value pair given for unknown keys" do
155
- assert_error(OptionParser::Error, "invalid.*pair.*'a'") { parse("-a", 'b') }
156
- end
157
-
158
- it "auto aliases :keys attribute" do
159
- parse("-c","t:3,o:1")[:c].should == {'three'=>'3', 'one'=>'1'}
160
- end
161
-
162
- it "adds in explicit default keys with value only argument" do
163
- parse('-e', 'whoop')[:e].should == {:three=>'whoop'}
164
- end
165
-
166
- it "adds in default keys from known :keys with value only argument" do
167
- parse("-c","okay")[:c].should == {'one'=>'okay'}
168
- end
169
-
170
- it "auto aliases symbolic :keys" do
171
- parse("-e","t:3,o:1")[:e].should == {:three=>'3', :one=>'1'}
172
- end
173
-
174
- it "supports a configurable splitter" do
175
- parse("-d","a:ab b:bc")[:d].should == {'a'=>'ab', 'b'=>'bc'}
176
- end
177
-
178
- it "supports grouping keys" do
179
- parse("-c", "t,tw:foo,o:bar")[:c].should == {'three'=>'foo','two'=>'foo', 'one'=>'bar'}
180
- end
181
-
182
- it "aliases * to all keys" do
183
- parse("-c", "*:foo")[:c].should == {'three'=>'foo', 'two'=>'foo', 'one'=>'foo'}
184
- parse('-a', '*:foo')[:a].should == {'*'=>'foo'}
185
- end
91
+ it "raises error when opt is present without value" do
92
+ assert_error(OptionParser::Error, "no value.*'n'") { parse("-n") }
93
+ end
94
+ end
95
+
96
+ describe ":array type" do
97
+ before_all {
98
+ create :a=>:array, :b=>[1,2,3], :c=>{:type=>:array, :values=>%w{foo fa bar zebra}, :enum=>false},
99
+ :d=>{:type=>:array, :split=>" ", :values=>[:ab, :bc, :cd], :enum=>false},
100
+ :e=>{:type=>:array, :values=>%w{some so silly}, :regexp=>true}
101
+ }
102
+
103
+ it "supports array defaults" do
104
+ parse[:b].should == [1,2,3]
105
+ end
106
+
107
+ it "converts comma delimited values to an array" do
108
+ parse("-a","1,2,5")[:a].should == %w{1 2 5}
109
+ end
110
+
111
+ it "raises error when option has no value" do
112
+ assert_error(OptionParser::Error, "no value.*'a'") { parse("-a") }
186
113
  end
187
114
 
115
+ it "auto aliases :values attribute" do
116
+ parse("-c","f,b")[:c].should == %w{fa bar}
117
+ end
118
+
119
+ it "auto aliases symbolic :values" do
120
+ parse("-d","a c")[:d].should == [:ab,:cd]
121
+ end
122
+
123
+ it "supports a configurable splitter" do
124
+ parse("-d", "yogi berra")[:d].should == %w{yogi berra}
125
+ end
126
+
127
+ it "aliases * to all values" do
128
+ parse("-c", '*')[:c].sort.should == %w{bar fa foo zebra}
129
+ parse("-c", '*,ok')[:c].sort.should == %w{bar fa foo ok zebra}
130
+ end
131
+
132
+ it "aliases correctly with :regexp on" do
133
+ parse("-e", 'so')[:e].sort.should == %w{so some}
134
+ end
135
+ end
136
+
137
+ describe ":hash type" do
138
+ before_all {
139
+ create :a=>:hash, :b=>{:default=>{:a=>'b'}}, :c=>{:type=>:hash, :keys=>%w{one two three}},
140
+ :e=>{:type=>:hash, :keys=>[:one, :two, :three], :default_keys=>:three},
141
+ :d=>{:type=>:hash, :split=>" "}
142
+ }
143
+
144
+ it "converts comma delimited pairs to hash" do
145
+ parse("-a", "f:3,g:4")[:a].should == {'f'=>'3', 'g'=>'4'}
146
+ end
147
+
148
+ it "supports hash defaults" do
149
+ parse[:b].should == {:a=>'b'}
150
+ end
151
+
152
+ it "raises error when option has no value" do
153
+ assert_error(OptionParser::Error, "no value.*'a'") { parse("-a") }
154
+ end
155
+
156
+ it "raises error if invalid key-value pair given for unknown keys" do
157
+ assert_error(OptionParser::Error, "invalid.*pair.*'a'") { parse("-a", 'b') }
158
+ end
159
+
160
+ it "auto aliases :keys attribute" do
161
+ parse("-c","t:3,o:1")[:c].should == {'three'=>'3', 'one'=>'1'}
162
+ end
163
+
164
+ it "adds in explicit default keys with value only argument" do
165
+ parse('-e', 'whoop')[:e].should == {:three=>'whoop'}
166
+ end
167
+
168
+ it "adds in default keys from known :keys with value only argument" do
169
+ parse("-c","okay")[:c].should == {'one'=>'okay'}
170
+ end
171
+
172
+ it "auto aliases symbolic :keys" do
173
+ parse("-e","t:3,o:1")[:e].should == {:three=>'3', :one=>'1'}
174
+ end
175
+
176
+ it "supports a configurable splitter" do
177
+ parse("-d","a:ab b:bc")[:d].should == {'a'=>'ab', 'b'=>'bc'}
178
+ end
179
+
180
+ it "supports grouping keys" do
181
+ parse("-c", "t,tw:foo,o:bar")[:c].should == {'three'=>'foo','two'=>'foo', 'one'=>'bar'}
182
+ end
183
+
184
+ it "aliases * to all keys" do
185
+ parse("-c", "*:foo")[:c].should == {'three'=>'foo', 'two'=>'foo', 'one'=>'foo'}
186
+ parse('-a', '*:foo')[:a].should == {'*'=>'foo'}
187
+ end
188
188
  end
189
- end
189
+ end
data/test/pipes_test.rb CHANGED
@@ -1,54 +1,56 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
- module Boson
4
- class PipeTest < Test::Unit::TestCase
5
- before(:all) {
3
+ describe "Pipes" do
4
+ before_all { ::Ab = Struct.new(:a, :b) }
5
+ describe "query_pipe" do
6
+ before_all {
6
7
  @hashes = [{:a=>'some', :b=>'thing'}, {:a=>:more, :b=>:yep}]
7
- Ab = Struct.new(:a, :b) unless PipeTest.const_defined?(:Ab)
8
8
  @objects = [Ab.new('some', 'thing'), Ab.new(:more, :yep)]
9
9
  }
10
- context "query_pipe" do
11
-
12
- test "searches one query" do
13
- [@hashes, @objects].each {|e|
14
- Pipes.query_pipe(e, :a=>'some').should == e[0,1]
15
- }
16
- end
17
-
18
- test "searches non-string values" do
19
- [@hashes, @objects].each {|e|
20
- Pipes.query_pipe(e, :a=>'more').should == e[1,1]
21
- }
22
- end
23
-
24
- test "searches multiple search terms" do
25
- [@hashes, @objects].each {|e|
26
- Pipes.query_pipe(e, :a=>'some', :b=>'yep').size.should == 2
27
- }
28
- end
29
-
30
- test "prints error for invalid search field" do
31
- capture_stderr { Pipes.query_pipe(@objects, :blah=>'blah') }.should =~ /failed.*'blah'/
32
- end
33
- end
34
-
35
- context "sort_pipe" do
36
- test "sorts objects with values of different types" do
37
- Pipes.sort_pipe(@objects, :a).should == @objects.reverse
38
- end
39
-
40
- test "sorts hashes with values of different types" do
41
- Pipes.sort_pipe(@hashes, :a).should == @hashes.reverse
42
- end
43
-
44
- test "sorts numeric values" do
45
- hashes = [{:a=>10, :b=>4}, {:a=>5, :b=>3}]
46
- Pipes.sort_pipe(hashes, :a).should == hashes.reverse
47
- end
48
-
49
- test "prints error for invalid sort field" do
50
- capture_stderr { Pipes.sort_pipe(@objects, :blah)}.should =~ /failed.*'blah'/
51
- end
10
+
11
+ it "searches one query" do
12
+ [@hashes, @objects].each {|e|
13
+ Pipes.query_pipe(e, :a=>'some').should == e[0,1]
14
+ }
15
+ end
16
+
17
+ it "searches non-string values" do
18
+ [@hashes, @objects].each {|e|
19
+ Pipes.query_pipe(e, :a=>'more').should == e[1,1]
20
+ }
21
+ end
22
+
23
+ it "searches multiple search terms" do
24
+ [@hashes, @objects].each {|e|
25
+ Pipes.query_pipe(e, :a=>'some', :b=>'yep').size.should == 2
26
+ }
27
+ end
28
+
29
+ it "prints error for invalid search field" do
30
+ capture_stderr { Pipes.query_pipe(@objects, :blah=>'blah') }.should =~ /failed.*'blah'/
31
+ end
32
+ end
33
+
34
+ describe "sort_pipe" do
35
+ before_all {
36
+ @hashes = [{:a=>'some', :b=>'thing'}, {:a=>:more, :b=>:yep}]
37
+ @objects = [Ab.new('some', 'thing'), Ab.new(:more, :yep)]
38
+ }
39
+ it "sorts objects with values of different types" do
40
+ Pipes.sort_pipe(@objects, :a).should == @objects.reverse
41
+ end
42
+
43
+ it "sorts hashes with values of different types" do
44
+ Pipes.sort_pipe(@hashes, :a).should == @hashes.reverse
45
+ end
46
+
47
+ it "sorts numeric values" do
48
+ hashes = [{:a=>10, :b=>4}, {:a=>5, :b=>3}]
49
+ Pipes.sort_pipe(hashes, :a).should == hashes.reverse
50
+ end
51
+
52
+ it "prints error for invalid sort field" do
53
+ capture_stderr { Pipes.sort_pipe(@objects, :blah)}.should =~ /failed.*'blah'/
52
54
  end
53
55
  end
54
56
  end