filigree 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,78 @@
1
+ # Author: Chris Wailes <chris.wailes@gmail.com>
2
+ # Project: Filigree
3
+ # Date: 2013/05/17
4
+ # Description: Test cases the Commands module.
5
+
6
+ ############
7
+ # Requires #
8
+ ############
9
+
10
+ # Standard Library
11
+ require 'minitest/autorun'
12
+
13
+ # Filigree
14
+ require 'filigree/commands'
15
+
16
+ #######################
17
+ # Classes and Modules #
18
+ #######################
19
+
20
+ class CommandTester < Minitest::Test
21
+ class TestCommands
22
+ include Filigree::Commands
23
+
24
+ command 'foo' do
25
+ :foo
26
+ end
27
+
28
+ command 'foo bar' do
29
+ :foobar
30
+ end
31
+
32
+ command 'hello1' do |subject|
33
+ "hello #{subject}"
34
+ end
35
+
36
+ config do
37
+ default 'world'
38
+ option 'subject', 's', conversions: [:to_s]
39
+ end
40
+ command 'hello2' do
41
+ "hello #{subject}"
42
+ end
43
+
44
+ command 'add' do |x, y|
45
+ x.to_i + y.to_i
46
+ end
47
+ end
48
+
49
+ def setup
50
+ @commander = TestCommands.new
51
+ end
52
+
53
+ def test_command_not_found
54
+ assert_raises(CommandNotFoundError) { @commander.('bar') }
55
+ end
56
+
57
+ def test_command_args
58
+ assert_equal 'hello world', @commander.('hello1 world')
59
+ assert_equal 42, @commander.('add 27 15')
60
+ end
61
+
62
+ def test_command_with_wrong_args
63
+ assert_raises(ArgumentError) { @commander.('hello1 cat dog') }
64
+ end
65
+
66
+ def test_configured_command
67
+ assert_equal 'hello world', @commander.('hello2')
68
+ assert_equal 'hello dog', @commander.('hello2 -s dog')
69
+ end
70
+
71
+ def test_subcommand
72
+ assert_equal :foobar, @commander.('foo bar')
73
+ end
74
+
75
+ def test_zero_arg_command
76
+ assert_equal :foo, @commander.('foo')
77
+ end
78
+ end
@@ -0,0 +1,173 @@
1
+ # Author: Chris Wailes <chris.wailes@gmail.com>
2
+ # Project: Filigree
3
+ # Date: 2013/05/15
4
+ # Description: Test cases the Configuration module.
5
+
6
+ ############
7
+ # Requires #
8
+ ############
9
+
10
+ # Gems
11
+ require 'minitest/autorun'
12
+
13
+ # Filigree
14
+ require 'filigree/configuration'
15
+
16
+ #######################
17
+ # Classes and Modules #
18
+ #######################
19
+
20
+ class ConfigurationTester < Minitest::Test
21
+ class TestConfig
22
+ include Filigree::Configuration
23
+
24
+ help 'Does foo.'
25
+ default { moo.to_s }
26
+ string_option 'foo', 'f'
27
+
28
+ help 'This is a longer help message to test and see how the string segmentation works. I hope it is long enough.'
29
+ default 42
30
+ option 'bar', 'b', conversions: [:to_i]
31
+
32
+ help 'Does baz.'
33
+ option 'baz', 'z', conversions: [:to_sym, :to_sym]
34
+
35
+ help 'Does moo.'
36
+ required
37
+ option 'moo', 'm' do |i|
38
+ i.to_i * 6
39
+ end
40
+
41
+ help 'This does daf'
42
+ option 'daf', 'd' do |*syms|
43
+ syms.map { |syms| syms.to_sym }
44
+ end
45
+
46
+ bool_option :bool
47
+
48
+ auto :cow do
49
+ self.moo / 3
50
+ end
51
+ end
52
+
53
+ def setup
54
+ @defaults = ['--moo', '10']
55
+ end
56
+
57
+ def test_auto
58
+ conf = TestConfig.new @defaults
59
+
60
+ assert_equal 20, conf.cow
61
+ end
62
+
63
+ def test_bool_option
64
+ conf = TestConfig.new @defaults.clone
65
+ assert !conf.bool, 'Boolean flag set in config object'
66
+
67
+ conf = TestConfig.new(@defaults.clone << '--bool')
68
+ assert conf.bool, 'Boolean flag not set in config object'
69
+ end
70
+
71
+ def test_defaults
72
+ conf = TestConfig.new @defaults
73
+
74
+ assert_equal 42, conf.bar
75
+ assert_equal '60', conf.foo
76
+ end
77
+
78
+ def test_long_option
79
+ conf = TestConfig.new @defaults
80
+
81
+ assert_equal 60, conf.moo
82
+ end
83
+
84
+ def test_proc_handler
85
+ conf = TestConfig.new (@defaults + ['--foo', 'hello world'])
86
+
87
+ assert_equal 'hello world', conf.foo
88
+ end
89
+
90
+ def test_required
91
+ assert_raises(ArgumentError) { TestConfig.new([]) }
92
+ TestConfig.new(@defaults)
93
+ end
94
+
95
+ def test_serialization
96
+ require 'tempfile'
97
+
98
+ conf = TestConfig.new (@defaults + ['-f', 'hello world', '-b', '32', '-z', 'a', 'b'])
99
+
100
+ yaml_string = conf.dump
101
+
102
+ conf = TestConfig.new yaml_string
103
+
104
+ assert_equal 60, conf.moo
105
+ assert_equal 'hello world', conf.foo
106
+ assert_equal 32, conf.bar
107
+ assert_equal [:a, :b], conf.baz
108
+
109
+ yaml_file = Tempfile.new 'tc_configuration_serialization'
110
+ yaml_file_path = yaml_file.path
111
+ yaml_file.close
112
+
113
+ # Dump the file.
114
+ File.open(yaml_file_path, 'w') { |f| conf.dump f }
115
+
116
+ # Load the configuration from the file.
117
+ conf = File.open(yaml_file_path, 'r') { |f| TestConfig.new f }
118
+
119
+ assert_equal 60, conf.moo
120
+ assert_equal 'hello world', conf.foo
121
+ assert_equal 32, conf.bar
122
+ assert_equal [:a, :b], conf.baz
123
+
124
+ # Remove the file.
125
+ FileUtils.rm yaml_file_path
126
+
127
+ # Re-create an empty file.
128
+ FileUtils.touch yaml_file_path
129
+
130
+ # Dump the config again.
131
+ conf.dump yaml_file_path
132
+
133
+ # Load the configuration again.
134
+ conf = TestConfig.new yaml_file_path
135
+
136
+ assert_equal 60, conf.moo
137
+ assert_equal 'hello world', conf.foo
138
+ assert_equal 32, conf.bar
139
+ assert_equal [:a, :b], conf.baz
140
+
141
+ #########################
142
+ # Partial Serialization #
143
+ #########################
144
+
145
+ yaml_string = conf.dump nil, :moo, :foo, :bar
146
+
147
+ conf = TestConfig.new yaml_string
148
+
149
+ assert_equal 60, conf.moo
150
+ assert_equal 'hello world', conf.foo
151
+ assert_equal 32, conf.bar
152
+ end
153
+
154
+ def test_short_option
155
+ conf = TestConfig.new ['-m', 10]
156
+
157
+ assert_equal 60, conf.moo
158
+ end
159
+
160
+ def test_splat
161
+ conf = TestConfig.new (['-d', 'a', 'b', 'c'] + @defaults)
162
+
163
+ assert_equal [:a, :b, :c], conf.daf
164
+ end
165
+
166
+ def test_symbol_handler
167
+ conf = TestConfig.new (@defaults + ['-b', '32'])
168
+ assert_equal 32, conf.bar
169
+
170
+ conf = TestConfig.new (@defaults + ['-z', 'a', 'b'])
171
+ assert_equal [:a, :b], conf.baz
172
+ end
173
+ end
data/test/tc_match.rb ADDED
@@ -0,0 +1,307 @@
1
+ # Author: Chris Wailes <chris.wailes@gmail.com>
2
+ # Project: Filigree
3
+ # Date: 2013/05/04
4
+ # Description: Test cases for the match method.
5
+
6
+ ############
7
+ # Requires #
8
+ ############
9
+
10
+ # Gems
11
+ require 'minitest/autorun'
12
+
13
+ # Filigree
14
+ require 'filigree/match'
15
+
16
+ #######################
17
+ # Classes and Modules #
18
+ #######################
19
+
20
+ class MatchTester < Minitest::Test
21
+
22
+ ####################
23
+ # Internal Classes #
24
+ ####################
25
+
26
+ class Foo
27
+ extend Filigree::Destructurable
28
+
29
+ attr_reader :a
30
+
31
+ def initialize(a)
32
+ @a = a
33
+ end
34
+
35
+ def destructure(_)
36
+ [@a]
37
+ end
38
+ end
39
+
40
+ class Bar
41
+ extend Filigree::Destructurable
42
+
43
+ def initialize(a, b)
44
+ @a = a
45
+ @b = b
46
+ end
47
+
48
+ def destructure(_)
49
+ [@a, @b]
50
+ end
51
+ end
52
+
53
+ def setup
54
+
55
+ end
56
+
57
+ ###########
58
+ # Helpers #
59
+ ###########
60
+
61
+ def match_tester_array_destructure(o)
62
+ match o do
63
+ with(Array.(x, [])) { x }
64
+ with(Array.(x, xs)) { [x, xs] }
65
+ end
66
+ end
67
+
68
+ def match_tester_destructure(o)
69
+ match o do
70
+ with(Foo.( 1)) { :one }
71
+ with(Foo.(:a)) { |a| a.a }
72
+ with(Foo.(Foo.(:b))) { :b }
73
+
74
+ with(Foo.(Foo.(a).as b)) { [a, b] }
75
+
76
+ with(Foo.(a))
77
+ with(Bar.(a, _)) { a }
78
+ end
79
+ end
80
+
81
+ def match_tester_deferred(o)
82
+ match o do
83
+ with(1)
84
+ with(2) { :NUM }
85
+ with(:a)
86
+ with(:b) { :SYM }
87
+ end
88
+ end
89
+
90
+ def match_tester_guard(o)
91
+ match o do
92
+ with(n, -> { n < 0 }) { :NEG }
93
+ with(0) { :ZERO }
94
+ with(n, -> { n > 0 }) { :POS }
95
+ end
96
+ end
97
+
98
+ def match_tester_class_pattern(o)
99
+ match o do
100
+ with(Fixnum.as a) { [:Fixnum, a] }
101
+ with(Float.as a) { [:Float, a] }
102
+ with(String.as a) { [:String, a] }
103
+ end
104
+ end
105
+
106
+ def match_tester_literal(o)
107
+ match o do
108
+ with(Literal(Fixnum)) { :Fixnum }
109
+ with(Literal(Float)) { :Float }
110
+ with(Literal(/a/)) { :Regexp }
111
+ end
112
+ end
113
+
114
+ def match_tester_manual_bind(o)
115
+ match o do
116
+ with(Fixnum.as(Bind(:a))) { [:Fixnum, a] }
117
+ with(Float.as(Bind(:a))) { [:Float, a] }
118
+ with(String.as(Bind(:a))) { [:String, a] }
119
+ end
120
+ end
121
+
122
+ def match_tester_mixed(o)
123
+ match o do
124
+ with('hello') { :hello0 }
125
+ with('hello') { :hello1 }
126
+ with(:world) { :world }
127
+ with(1) { :one }
128
+ end
129
+ end
130
+
131
+ def match_tester_regexp(s)
132
+ match s do
133
+ with(/(ab)+/) { :a }
134
+ with(/[abc]+/) { :b }
135
+ end
136
+ end
137
+
138
+ def match_tester_simple(o)
139
+ match o do
140
+ with(1) { :one }
141
+ with(2) { :two }
142
+ with(3) { :three }
143
+ end
144
+ end
145
+
146
+ def match_tester_tuple(*touple)
147
+ match *touple do
148
+ with(1, 2) { :FOO }
149
+ with(3, 4) { :BAR }
150
+ with(5) { :BAF }
151
+ end
152
+ end
153
+
154
+ def match_tester_tuple_wildcard(*touple)
155
+ match *touple do
156
+ with(1)
157
+ with(2, 3) { :DEF }
158
+ with(4, _) { :PART_WILD }
159
+ with(_) { :WILD }
160
+ end
161
+ end
162
+
163
+ def match_tester_wildcard(o)
164
+ match o do
165
+ with(1) { 1 }
166
+ with(2) { 2 }
167
+ with(n) { n }
168
+ end
169
+ end
170
+
171
+ #########
172
+ # Tests #
173
+ #########
174
+
175
+ def test_array_destructure
176
+ assert_equal 42, match_tester_array_destructure([42])
177
+ assert_equal [1, [2, 3, 4]], match_tester_array_destructure([1, 2, 3, 4])
178
+ end
179
+
180
+ def test_as
181
+ v0 = Foo.new(:dog)
182
+ v1 = Foo.new(v0)
183
+
184
+ assert_equal([:dog, v0], match_tester_destructure(v1))
185
+ end
186
+
187
+ def test_class_pattern
188
+ assert_equal [:Fixnum, 42], match_tester_class_pattern(42)
189
+ assert_equal [:Float, 42.0], match_tester_class_pattern(42.0)
190
+ assert_equal [:String, 'foo'], match_tester_class_pattern('foo')
191
+ end
192
+
193
+ def test_constants
194
+ assert_equal :one, match_tester_simple(1)
195
+ assert_equal :two, match_tester_simple(2)
196
+ assert_equal :three, match_tester_simple(3)
197
+
198
+ assert_raises(MatchError) { match_tester_simple(4) }
199
+
200
+ assert_equal :hello0, match_tester_mixed('hello')
201
+ assert_equal :world, match_tester_mixed(:world)
202
+ assert_equal :one, match_tester_mixed(1)
203
+ end
204
+
205
+ def test_deconstructor
206
+ assert_equal :one, match_tester_destructure(Foo.new( 1))
207
+ assert_equal :a, match_tester_destructure(Foo.new( :a))
208
+ assert_equal 42.0, match_tester_destructure(Foo.new(42.0))
209
+
210
+ assert_equal :b, match_tester_destructure(Foo.new(Foo.new(:b)))
211
+ assert_equal 42, match_tester_destructure(Bar.new(42, nil))
212
+ end
213
+
214
+ def test_deferred_block
215
+ assert_equal :NUM, match_tester_deferred(1)
216
+ assert_equal :NUM, match_tester_deferred(2)
217
+
218
+ assert_equal :SYM, match_tester_deferred(:a)
219
+ assert_equal :SYM, match_tester_deferred(:b)
220
+ end
221
+
222
+ def test_guards
223
+ assert_equal :NEG, match_tester_guard(-5)
224
+ assert_equal :ZERO, match_tester_guard(0)
225
+ assert_equal :POS, match_tester_guard(6)
226
+ end
227
+
228
+ def test_literals
229
+ assert_equal :Fixnum, match_tester_literal(Fixnum)
230
+ assert_equal :Float, match_tester_literal(Float)
231
+ assert_equal :Regexp, match_tester_literal(/a/)
232
+ end
233
+
234
+ def test_manual_bind
235
+ assert_equal [:Fixnum, 42], match_tester_manual_bind(42)
236
+ assert_equal [:Float, 42.0], match_tester_manual_bind(42.0)
237
+ assert_equal [:String, 'foo'], match_tester_manual_bind('foo')
238
+ end
239
+
240
+ def test_match_array
241
+ result =
242
+ match [1,2,3,4] do
243
+ with(Array.(a, b, c)) { [a, b, c] }
244
+ end
245
+
246
+ assert_equal [1, 2, [3, 4]], result
247
+ end
248
+
249
+ def test_regexp
250
+ assert_equal :a, match_tester_regexp('abab')
251
+ assert_equal :b, match_tester_regexp('acba')
252
+
253
+ assert_raises(MatchError) { match_tester_regexp('def') }
254
+
255
+ actual =
256
+ match 'a42' do
257
+ with(/a([0-9]+)/) { match_data[1].to_i }
258
+ end
259
+
260
+ assert_equal 42, actual
261
+ end
262
+
263
+ def test_tuple_wildcard
264
+ assert_equal :DEF, match_tester_tuple_wildcard(1)
265
+ assert_equal :DEF, match_tester_tuple_wildcard(2, 3)
266
+
267
+ assert_equal :PART_WILD, match_tester_tuple_wildcard(4, 1)
268
+ assert_equal :PART_WILD, match_tester_tuple_wildcard(4, :cat)
269
+
270
+ assert_equal :WILD, match_tester_tuple_wildcard(5)
271
+ assert_equal :WILD, match_tester_tuple_wildcard(5, 6)
272
+ assert_equal :WILD, match_tester_tuple_wildcard(5, 6, 7)
273
+ end
274
+
275
+ def test_tuples
276
+ assert_equal :FOO, match_tester_tuple(1, 2)
277
+ assert_equal :BAR, match_tester_tuple(3, 4)
278
+ assert_equal :BAF, match_tester_tuple(5)
279
+
280
+ assert_raises(MatchError) { match_tester_tuple(1, 2, 3) }
281
+ assert_raises(MatchError) { match_tester_tuple(6) }
282
+ end
283
+
284
+ def test_variable_comparison
285
+ var = 42
286
+
287
+ actual =
288
+ match 42 do
289
+ with(var) { :hoopy }
290
+ with(_) { :other }
291
+ end
292
+
293
+ assert_equal :hoopy, actual
294
+ end
295
+
296
+ def test_wildcard_pattern
297
+ result =
298
+ match 42 do
299
+ with(n) { n }
300
+ end
301
+
302
+ assert_equal 42, result
303
+
304
+ assert_equal 1, match_tester_wildcard(1)
305
+ assert_equal 42, match_tester_wildcard(42)
306
+ end
307
+ end
data/test/tc_object.rb ADDED
@@ -0,0 +1,43 @@
1
+ # Author: Chris Wailes <chris.wailes@gmail.com>
2
+ # Project: Filigree
3
+ # Date: 2013/05/04
4
+ # Description: Test cases for the Object extensions.
5
+
6
+ ############
7
+ # Requires #
8
+ ############
9
+
10
+ # Gems
11
+ require 'minitest/autorun'
12
+
13
+ # Filigree
14
+ require 'filigree/object'
15
+
16
+ #######################
17
+ # Classes and Modules #
18
+ #######################
19
+
20
+ class ObjectTester < Minitest::Test
21
+ Foo = Struct.new :a, :b
22
+
23
+ def setup
24
+
25
+ end
26
+
27
+ def test_returning
28
+ assert( returning(true) { false } )
29
+ end
30
+
31
+ def test_with
32
+ v0 = Foo.new(1, 2)
33
+ v1 = v0.clone_with { self.a = 3 }
34
+
35
+ assert_equal 1, v0.a
36
+ assert_equal 2, v0.b
37
+
38
+ assert_equal 3, v1.a
39
+ assert_equal 2, v1.b
40
+
41
+ refute_equal v0.object_id, v1.object_id
42
+ end
43
+ end
data/test/tc_string.rb ADDED
@@ -0,0 +1,36 @@
1
+ # Author: Chris Wailes <chris.wailes@gmail.com>
2
+ # Project: Filigree
3
+ # Date: 2014/02/05
4
+ # Description: Test cases for the String extensions.
5
+
6
+ ############
7
+ # Requires #
8
+ ############
9
+
10
+ # Gems
11
+ require 'minitest/autorun'
12
+
13
+ # Filigree
14
+ require 'filigree/string'
15
+
16
+ #######################
17
+ # Classes and Modules #
18
+ #######################
19
+
20
+ class ObjectTester < Minitest::Test
21
+
22
+ ORIGINAL = 'Hello, I am a test string. I am really long so that the string segmentation code can be tested.'
23
+ SEGMENTED = <<eos
24
+ Hello, I am a test string.
25
+ I am really long so that
26
+ the string segmentation
27
+ code can be tested.
28
+ eos
29
+
30
+ def setup
31
+ end
32
+
33
+ def test_segmentation
34
+ assert_equal SEGMENTED.chomp, ORIGINAL.segment(2, 30)
35
+ end
36
+ end