filigree 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,66 +18,66 @@ require 'filigree/match'
18
18
  #######################
19
19
 
20
20
  class MatchTester < Minitest::Test
21
-
21
+
22
22
  ####################
23
23
  # Internal Classes #
24
24
  ####################
25
-
25
+
26
26
  class Foo
27
27
  extend Filigree::Destructurable
28
-
28
+
29
29
  attr_reader :a
30
-
30
+
31
31
  def initialize(a)
32
32
  @a = a
33
33
  end
34
-
34
+
35
35
  def destructure(_)
36
36
  [@a]
37
37
  end
38
38
  end
39
-
39
+
40
40
  class Bar
41
41
  extend Filigree::Destructurable
42
-
42
+
43
43
  def initialize(a, b)
44
44
  @a = a
45
45
  @b = b
46
46
  end
47
-
47
+
48
48
  def destructure(_)
49
49
  [@a, @b]
50
50
  end
51
51
  end
52
-
52
+
53
53
  def setup
54
-
54
+
55
55
  end
56
-
56
+
57
57
  ###########
58
58
  # Helpers #
59
59
  ###########
60
-
60
+
61
61
  def match_tester_array_destructure(o)
62
62
  match o do
63
63
  with(Array.(x, [])) { x }
64
64
  with(Array.(x, xs)) { [x, xs] }
65
65
  end
66
66
  end
67
-
67
+
68
68
  def match_tester_destructure(o)
69
69
  match o do
70
70
  with(Foo.( 1)) { :one }
71
71
  with(Foo.(:a)) { |a| a.a }
72
72
  with(Foo.(Foo.(:b))) { :b }
73
-
73
+
74
74
  with(Foo.(Foo.(a).as b)) { [a, b] }
75
-
75
+
76
76
  with(Foo.(a))
77
77
  with(Bar.(a, _)) { a }
78
78
  end
79
79
  end
80
-
80
+
81
81
  def match_tester_deferred(o)
82
82
  match o do
83
83
  with(1)
@@ -86,7 +86,7 @@ class MatchTester < Minitest::Test
86
86
  with(:b) { :SYM }
87
87
  end
88
88
  end
89
-
89
+
90
90
  def match_tester_guard(o)
91
91
  match o do
92
92
  with(n, -> { n < 0 }) { :NEG }
@@ -94,7 +94,7 @@ class MatchTester < Minitest::Test
94
94
  with(n, -> { n > 0 }) { :POS }
95
95
  end
96
96
  end
97
-
97
+
98
98
  def match_tester_class_pattern(o)
99
99
  match o do
100
100
  with(Fixnum.as a) { [:Fixnum, a] }
@@ -102,7 +102,7 @@ class MatchTester < Minitest::Test
102
102
  with(String.as a) { [:String, a] }
103
103
  end
104
104
  end
105
-
105
+
106
106
  def match_tester_literal(o)
107
107
  match o do
108
108
  with(Literal(Fixnum)) { :Fixnum }
@@ -110,7 +110,15 @@ class MatchTester < Minitest::Test
110
110
  with(Literal(/a/)) { :Regexp }
111
111
  end
112
112
  end
113
-
113
+
114
+ def match_tester_bang_bind(o)
115
+ match o do
116
+ with(Fixnum.as(!:a)) { [:Fixnum, a] }
117
+ with(Float.as(!:a)) { [:Float, a] }
118
+ with(String.as(!:a)) { [:String, a] }
119
+ end
120
+ end
121
+
114
122
  def match_tester_manual_bind(o)
115
123
  match o do
116
124
  with(Fixnum.as(Bind(:a))) { [:Fixnum, a] }
@@ -118,7 +126,7 @@ class MatchTester < Minitest::Test
118
126
  with(String.as(Bind(:a))) { [:String, a] }
119
127
  end
120
128
  end
121
-
129
+
122
130
  def match_tester_mixed(o)
123
131
  match o do
124
132
  with('hello') { :hello0 }
@@ -127,14 +135,14 @@ class MatchTester < Minitest::Test
127
135
  with(1) { :one }
128
136
  end
129
137
  end
130
-
138
+
131
139
  def match_tester_regexp(s)
132
140
  match s do
133
141
  with(/(ab)+/) { :a }
134
142
  with(/[abc]+/) { :b }
135
143
  end
136
144
  end
137
-
145
+
138
146
  def match_tester_simple(o)
139
147
  match o do
140
148
  with(1) { :one }
@@ -142,7 +150,7 @@ class MatchTester < Minitest::Test
142
150
  with(3) { :three }
143
151
  end
144
152
  end
145
-
153
+
146
154
  def match_tester_tuple(*touple)
147
155
  match *touple do
148
156
  with(1, 2) { :FOO }
@@ -150,7 +158,7 @@ class MatchTester < Minitest::Test
150
158
  with(5) { :BAF }
151
159
  end
152
160
  end
153
-
161
+
154
162
  def match_tester_tuple_wildcard(*touple)
155
163
  match *touple do
156
164
  with(1)
@@ -159,7 +167,7 @@ class MatchTester < Minitest::Test
159
167
  with(_) { :WILD }
160
168
  end
161
169
  end
162
-
170
+
163
171
  def match_tester_wildcard(o)
164
172
  match o do
165
173
  with(1) { 1 }
@@ -167,140 +175,146 @@ class MatchTester < Minitest::Test
167
175
  with(n) { n }
168
176
  end
169
177
  end
170
-
178
+
171
179
  #########
172
180
  # Tests #
173
181
  #########
174
-
182
+
175
183
  def test_array_destructure
176
184
  assert_equal 42, match_tester_array_destructure([42])
177
185
  assert_equal [1, [2, 3, 4]], match_tester_array_destructure([1, 2, 3, 4])
178
186
  end
179
-
187
+
180
188
  def test_as
181
189
  v0 = Foo.new(:dog)
182
190
  v1 = Foo.new(v0)
183
-
191
+
184
192
  assert_equal([:dog, v0], match_tester_destructure(v1))
185
193
  end
186
-
194
+
187
195
  def test_class_pattern
188
196
  assert_equal [:Fixnum, 42], match_tester_class_pattern(42)
189
197
  assert_equal [:Float, 42.0], match_tester_class_pattern(42.0)
190
198
  assert_equal [:String, 'foo'], match_tester_class_pattern('foo')
191
199
  end
192
-
200
+
193
201
  def test_constants
194
202
  assert_equal :one, match_tester_simple(1)
195
203
  assert_equal :two, match_tester_simple(2)
196
204
  assert_equal :three, match_tester_simple(3)
197
-
205
+
198
206
  assert_raises(MatchError) { match_tester_simple(4) }
199
-
207
+
200
208
  assert_equal :hello0, match_tester_mixed('hello')
201
209
  assert_equal :world, match_tester_mixed(:world)
202
210
  assert_equal :one, match_tester_mixed(1)
203
211
  end
204
-
212
+
205
213
  def test_deconstructor
206
214
  assert_equal :one, match_tester_destructure(Foo.new( 1))
207
215
  assert_equal :a, match_tester_destructure(Foo.new( :a))
208
216
  assert_equal 42.0, match_tester_destructure(Foo.new(42.0))
209
-
217
+
210
218
  assert_equal :b, match_tester_destructure(Foo.new(Foo.new(:b)))
211
219
  assert_equal 42, match_tester_destructure(Bar.new(42, nil))
212
220
  end
213
-
221
+
214
222
  def test_deferred_block
215
223
  assert_equal :NUM, match_tester_deferred(1)
216
224
  assert_equal :NUM, match_tester_deferred(2)
217
-
225
+
218
226
  assert_equal :SYM, match_tester_deferred(:a)
219
227
  assert_equal :SYM, match_tester_deferred(:b)
220
228
  end
221
-
229
+
222
230
  def test_guards
223
231
  assert_equal :NEG, match_tester_guard(-5)
224
232
  assert_equal :ZERO, match_tester_guard(0)
225
233
  assert_equal :POS, match_tester_guard(6)
226
234
  end
227
-
235
+
228
236
  def test_literals
229
237
  assert_equal :Fixnum, match_tester_literal(Fixnum)
230
238
  assert_equal :Float, match_tester_literal(Float)
231
239
  assert_equal :Regexp, match_tester_literal(/a/)
232
240
  end
233
-
241
+
242
+ def test_bang_bind
243
+ assert_equal [:Fixnum, 42], match_tester_bang_bind(42)
244
+ assert_equal [:Float, 42.0], match_tester_bang_bind(42.0)
245
+ assert_equal [:String, 'foo'], match_tester_bang_bind('foo')
246
+ end
247
+
234
248
  def test_manual_bind
235
249
  assert_equal [:Fixnum, 42], match_tester_manual_bind(42)
236
250
  assert_equal [:Float, 42.0], match_tester_manual_bind(42.0)
237
251
  assert_equal [:String, 'foo'], match_tester_manual_bind('foo')
238
252
  end
239
-
253
+
240
254
  def test_match_array
241
255
  result =
242
256
  match [1,2,3,4] do
243
257
  with(Array.(a, b, c)) { [a, b, c] }
244
258
  end
245
-
259
+
246
260
  assert_equal [1, 2, [3, 4]], result
247
261
  end
248
-
262
+
249
263
  def test_regexp
250
264
  assert_equal :a, match_tester_regexp('abab')
251
265
  assert_equal :b, match_tester_regexp('acba')
252
-
266
+
253
267
  assert_raises(MatchError) { match_tester_regexp('def') }
254
-
268
+
255
269
  actual =
256
270
  match 'a42' do
257
271
  with(/a([0-9]+)/) { match_data[1].to_i }
258
272
  end
259
-
273
+
260
274
  assert_equal 42, actual
261
275
  end
262
-
276
+
263
277
  def test_tuple_wildcard
264
278
  assert_equal :DEF, match_tester_tuple_wildcard(1)
265
279
  assert_equal :DEF, match_tester_tuple_wildcard(2, 3)
266
-
280
+
267
281
  assert_equal :PART_WILD, match_tester_tuple_wildcard(4, 1)
268
282
  assert_equal :PART_WILD, match_tester_tuple_wildcard(4, :cat)
269
-
283
+
270
284
  assert_equal :WILD, match_tester_tuple_wildcard(5)
271
285
  assert_equal :WILD, match_tester_tuple_wildcard(5, 6)
272
286
  assert_equal :WILD, match_tester_tuple_wildcard(5, 6, 7)
273
287
  end
274
-
288
+
275
289
  def test_tuples
276
290
  assert_equal :FOO, match_tester_tuple(1, 2)
277
291
  assert_equal :BAR, match_tester_tuple(3, 4)
278
292
  assert_equal :BAF, match_tester_tuple(5)
279
-
293
+
280
294
  assert_raises(MatchError) { match_tester_tuple(1, 2, 3) }
281
295
  assert_raises(MatchError) { match_tester_tuple(6) }
282
296
  end
283
-
297
+
284
298
  def test_variable_comparison
285
299
  var = 42
286
-
300
+
287
301
  actual =
288
302
  match 42 do
289
303
  with(var) { :hoopy }
290
304
  with(_) { :other }
291
305
  end
292
-
306
+
293
307
  assert_equal :hoopy, actual
294
308
  end
295
-
309
+
296
310
  def test_wildcard_pattern
297
311
  result =
298
312
  match 42 do
299
313
  with(n) { n }
300
314
  end
301
-
315
+
302
316
  assert_equal 42, result
303
-
317
+
304
318
  assert_equal 1, match_tester_wildcard(1)
305
319
  assert_equal 42, match_tester_wildcard(42)
306
320
  end
@@ -19,25 +19,25 @@ require 'filigree/object'
19
19
 
20
20
  class ObjectTester < Minitest::Test
21
21
  Foo = Struct.new :a, :b
22
-
22
+
23
23
  def setup
24
-
24
+
25
25
  end
26
-
26
+
27
27
  def test_returning
28
28
  assert( returning(true) { false } )
29
29
  end
30
-
30
+
31
31
  def test_with
32
32
  v0 = Foo.new(1, 2)
33
33
  v1 = v0.clone_with { self.a = 3 }
34
-
34
+
35
35
  assert_equal 1, v0.a
36
36
  assert_equal 2, v0.b
37
-
37
+
38
38
  assert_equal 3, v1.a
39
39
  assert_equal 2, v1.b
40
-
40
+
41
41
  refute_equal v0.object_id, v1.object_id
42
42
  end
43
43
  end
@@ -18,7 +18,7 @@ require 'filigree/string'
18
18
  #######################
19
19
 
20
20
  class ObjectTester < Minitest::Test
21
-
21
+
22
22
  ORIGINAL = 'Hello, I am a test string. I am really long so that the string segmentation code can be tested.'
23
23
  SEGMENTED = <<eos
24
24
  Hello, I am a test string.
@@ -26,10 +26,10 @@ Hello, I am a test string.
26
26
  the string segmentation
27
27
  code can be tested.
28
28
  eos
29
-
29
+
30
30
  def setup
31
31
  end
32
-
32
+
33
33
  def test_segmentation
34
34
  assert_equal SEGMENTED.chomp, ORIGINAL.segment(2, 30)
35
35
  end
@@ -18,99 +18,99 @@ require 'filigree/types'
18
18
  #######################
19
19
 
20
20
  class TypeTester < Minitest::Test
21
-
21
+
22
22
  class Foo
23
23
  include Filigree::TypedClass
24
-
24
+
25
25
  typed_ivar :foo, Integer
26
26
  typed_ivar :bar, String
27
27
  typed_ivar :baf, [Integer]
28
28
  end
29
-
29
+
30
30
  class Bar
31
31
  include Filigree::TypedClass
32
-
32
+
33
33
  typed_ivar :foo, Integer
34
34
  typed_ivar :bar, String
35
-
35
+
36
36
  default_constructor
37
37
  end
38
-
38
+
39
39
  class Baf
40
40
  include Filigree::TypedClass
41
-
41
+
42
42
  typed_ivar :foo, Integer
43
43
  typed_ivar :bar, String, true
44
-
44
+
45
45
  default_constructor
46
46
  end
47
-
47
+
48
48
  class Baz
49
49
  include Filigree::TypedClass
50
-
50
+
51
51
  typed_ivar :foo, Integer
52
52
  typed_ivar :bar, String
53
-
53
+
54
54
  default_constructor true
55
55
  end
56
-
56
+
57
57
  def setup
58
-
58
+
59
59
  end
60
-
60
+
61
61
  def test_check_type
62
62
  assert check_type([], Array)
63
-
63
+
64
64
  assert check_type(1, Fixnum)
65
65
  assert check_type(nil, Fixnum, nil, true).nil?
66
66
  assert check_type(1, Fixnum, nil, false, true)
67
67
  assert check_type(nil, Fixnum, nil, true, true).nil?
68
68
  assert check_type(1, Integer)
69
-
69
+
70
70
  assert_raises(TypeError) { check_type(1, Integer, nil, false, true) }
71
71
  assert_raises(TypeError) { check_type(1, Array) }
72
72
  end
73
-
73
+
74
74
  def test_check_array_type
75
75
  assert check_array_type([1, 2, 3], Fixnum)
76
76
  assert check_array_type([1, 2, 3], Fixnum, nil, true)
77
77
  assert check_array_type([1, 2, 3], Integer)
78
-
78
+
79
79
  assert_raises(TypeError) { check_array_type([1, 2, 3], Integer, nil, false, true) }
80
80
  assert_raises(TypeError) { check_array_type([1, :hello, 'world'], Fixnum) }
81
81
  assert_raises(TypeError) { check_array_type([1, 2, 3], Float, 'foo') }
82
82
  end
83
-
83
+
84
84
  def test_default_constructor
85
85
  v0 = Bar.new(1, 'hello')
86
-
86
+
87
87
  assert_equal 1, v0.foo
88
88
  assert_equal 'hello', v0.bar
89
-
89
+
90
90
  v1 = Baf.new(2)
91
-
91
+
92
92
  assert_equal 2, v1.foo
93
93
  assert_nil v1.bar
94
-
94
+
95
95
  assert_raises(ArgumentError) { Baz.new(3) }
96
-
96
+
97
97
  v2 = Baz.new(2, 'world')
98
-
98
+
99
99
  assert_equal 2, v2.foo
100
100
  assert_equal 'world', v2.bar
101
101
  end
102
-
102
+
103
103
  def test_typed_instance_vars
104
104
  v0 = Foo.new
105
-
105
+
106
106
  v0.foo = 1
107
107
  v0.bar = 'hello'
108
108
  v0.baf = [1,2,3]
109
-
109
+
110
110
  assert_equal 1, v0.foo
111
111
  assert_equal 'hello', v0.bar
112
112
  assert_equal [1,2,3], v0.baf
113
-
113
+
114
114
  assert_raises(TypeError) { v0.foo = 'world' }
115
115
  end
116
116
  end