-
Notifications
You must be signed in to change notification settings - Fork 416
/
struct_shared.rb
490 lines (391 loc) · 15.8 KB
/
struct_shared.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
RSpec.shared_examples :struct do
context 'definition' do
it 'registers the class when given a class name' do
class_name = 'ValidClassName'
clazz = described_class.new(class_name)
expect{ described_class.const_get(class_name) }.to_not raise_error
expect(clazz).to be_a Class
expect(clazz.ancestors).to include described_class
end
it 'creates an anonymous class when given at least one member' do
clazz = described_class.new(:foo)
expect{ described_class.const_get(clazz.to_s) }.to raise_error(NameError)
expect(clazz).to be_a Class
expect(clazz.ancestors).to include described_class
end
it 'raises an exception when given an invalid class name' do
expect{ described_class.new('lowercase') }.to raise_error(NameError)
expect{ described_class.new('_') }.to raise_error(NameError)
expect{ described_class.new('1') }.to raise_error(NameError)
end
it 'defines a getter for each member' do
members = [:Foo, :bar, 'baz']
structs = [
described_class.new(*members).new,
described_class.new(*members, keyword_init: true).new,
described_class.new('ClassForCheckingGetterDefinition', *members).new,
]
structs.each do |struct|
members.each do |member|
expect(struct).to respond_to member
method = struct.method(member)
expect(method.arity).to eq 0
end
end
end
it 'raises an exception when given no members' do
expect{ described_class.new() }.to raise_error(ArgumentError)
end
it 'raise an exception when given an invalid member' do
expect{ described_class.new('ClassForCheckingValidFieldNames1', 1) }.to raise_error(TypeError)
end
it 'evalues a given block against the new class' do
clazz1 = described_class.new('ClassForCheckingBlockProcessing', :foo, :bar) do
def baz(foo, bar) foo + bar; end
end
clazz2 = described_class.new(:foo, :bar) do
def baz(foo, bar) foo + bar; end
end
clazz3 = described_class.new(:foo, :bar, keyword_init: true) do
def baz(foo, bar) foo + bar; end
end
[clazz1, clazz2, clazz3].each do |clazz|
struct = clazz.new
expect(struct).to respond_to :baz
expect(struct.method(:baz).arity).to eq 2
expect(struct.baz(40, 2)).to eq 42
end
end
end
context 'construction' do
let!(:members){ [:Foo, :bar, 'baz'] }
let!(:values){ [42, '42', :fortytwo] }
let!(:kw_values){ {Foo: 42, bar: '42', baz: :fortytwo} }
let!(:classes) do
[
described_class.new(*members),
described_class.new(*members, keyword_init: true),
described_class.new('StructConstructionTester', *members)
]
end
it 'sets all absent members to nil' do
classes.each do |clazz|
struct = clazz.new
members.each do |member|
expect(struct.send(member)).to be_nil
end
end
end
it 'sets all given members in order' do
classes.each do |clazz|
struct = clazz::KEYWORD_INIT ? clazz.new(**kw_values) : clazz.new(*values)
members.each_with_index do |member, index|
expect(struct.send(member)).to eq values[index]
end
end
end
it 'raises an exception when extra members are given' do
classes.each do |clazz|
if clazz::KEYWORD_INIT
extra_values = kw_values.merge({FortyTwo: 'forty two'})
expect{ clazz.new(**extra_values) }.to raise_error(ArgumentError)
else
extra_values = values << 'forty two'
expect{ clazz.new(*extra_values) }.to raise_error(ArgumentError)
end
end
end
end
context 'properties' do
let!(:anon_struct_members) { [:name, :address, :zip] }
let(:anon_struct) { described_class.new(*anon_struct_members) }
let(:anon_struct_with_keyword_init) { described_class.new(*anon_struct_members, keyword_init: true) }
let!(:named_struct_members) { [:left, :right] }
let(:named_struct) do
described_class.new("Test#{described_class}Properties".gsub(/::/, ''),
*named_struct_members)
end
context '#length' do
it 'returns the number of struct members' do
expect(anon_struct.new.length).to eq anon_struct_members.length
expect(anon_struct_with_keyword_init.new.length).to eq anon_struct_members.length
expect(named_struct.new.length).to eq named_struct_members.length
end
end
context '#keyword_init?' do
it 'returns a boolean indicating if keywords are enabled for initialization' do
expect(anon_struct.new.keyword_init?).to eq false
expect(anon_struct_with_keyword_init.new.keyword_init?).to eq true
expect(named_struct.new.keyword_init?).to eq false
end
end
context '#members' do
it 'returns the struct members as an array of symbols' do
expect(anon_struct.new.members).to eq anon_struct_members
expect(anon_struct_with_keyword_init.new.members).to eq anon_struct_members
expect(named_struct.new.members).to eq named_struct_members
end
it 'returns a different object than the array passed at definition' do
expect(anon_struct.new.members.object_id).to_not eq anon_struct_members.object_id
expect(anon_struct_with_keyword_init.new.members.object_id).to_not eq anon_struct_members.object_id
expect(named_struct.new.members.object_id).to_not eq named_struct_members.object_id
end
end
context '#size' do
it 'returns the number of struct members' do
expect(anon_struct.new.size).to eq anon_struct_members.size
expect(anon_struct_with_keyword_init.new.size).to eq anon_struct_members.size
expect(named_struct.new.size).to eq named_struct_members.size
end
end
context '#values' do
it 'returns the values of the struct as an array in order' do
expect(anon_struct.new().values).to eq [nil, nil, nil]
expect(anon_struct_with_keyword_init.new().values).to eq [nil, nil, nil]
expect(named_struct.new().values).to eq [nil, nil]
expect(anon_struct.new(:foo, :bar, :baz).values).to eq [:foo, :bar, :baz]
expect(anon_struct_with_keyword_init.new(name: :foo, address: :bar, zip: :baz).values).to eq [:foo, :bar, :baz]
expect(named_struct.new(:yes, :no).values).to eq [:yes, :no]
end
end
context '#values_at' do
let(:anon_struct) do
described_class.new(:zero, :one, :two, :three, :four, :five, :six, :seven, :eight, :nine).
new(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
end
let!(:named_struct) do
described_class.new("Test#{described_class}ValuesAtAccessor".gsub(/::/, ''),
:zero, :one, :two, :three, :four, :five, :six, :seven, :eight, :nine).
new(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
end
it 'returns the value at the given offset' do
expect(anon_struct.values_at(3)).to eq [3]
expect(named_struct.values_at(7)).to eq [7]
end
it 'returns the values at multiple given offsets' do
expect(anon_struct.values_at(4, 1, 7)).to eq [4, 1, 7]
expect(named_struct.values_at(2, 4, 6)).to eq [2, 4, 6]
end
it 'returns values at offsets in a given range' do
expect(anon_struct.values_at(4..7)).to eq [4, 5, 6, 7]
expect(named_struct.values_at(1..3)).to eq [1, 2, 3]
end
it 'returns values for multiple ranges' do
expect(anon_struct.values_at(1..3, 4..7)).to eq [1, 2, 3, 4, 5, 6, 7]
expect(named_struct.values_at(1..3, 4..7)).to eq [1, 2, 3, 4, 5, 6, 7]
end
it 'returns values for ranges and offsets' do
expect(anon_struct.values_at(1, 2, 3, 4..7)).to eq [1, 2, 3, 4, 5, 6, 7]
expect(named_struct.values_at(1, 2, 3, 4..7)).to eq [1, 2, 3, 4, 5, 6, 7]
end
end
end
context 'accessors' do
let!(:anon_struct_members) { [:name, :address, :zip] }
let(:anon_struct) { described_class.new(*anon_struct_members) }
let!(:named_struct_members) { [:left, :right] }
let(:named_struct) do
described_class.new("Test#{described_class}Properties".gsub(/::/, ''),
*named_struct_members)
end
let(:anon_instance){ anon_struct.new('Douglass Adams', 'Earth', 42) }
let(:named_instance){ named_struct.new('up', 'down') }
context '#[member]' do
it 'retrieves the value when given a valid symbol member' do
expect(anon_instance[:address]).to eq 'Earth'
expect(named_instance[:right]).to eq 'down'
end
it 'retrieves the value when given a valid string member' do
expect(anon_instance['address']).to eq 'Earth'
expect(named_instance['right']).to eq 'down'
end
it 'raises an exception when given a non-existent symbol member' do
expect{anon_instance[:foo]}.to raise_error(NameError)
expect{named_instance[:bar]}.to raise_error(NameError)
end
it 'raises an exception when given a non-existent string member' do
expect{anon_instance['foo']}.to raise_error(NameError)
expect{named_instance['bar']}.to raise_error(NameError)
end
end
context '#[index]' do
it 'retrieves the value when given a valid index' do
expect(anon_instance[1]).to eq 'Earth'
expect(named_instance[1]).to eq 'down'
end
it 'raises an exception when given an out-of-bound index' do
expect{anon_instance[100]}.to raise_error(IndexError)
expect{named_instance[100]}.to raise_error(IndexError)
end
end
end
context 'comparison' do
let(:customer) { described_class.new(:name, :address, :zip) }
let(:employer) { described_class.new(:name, :address, :zip) }
let!(:joe) { customer.new('Joe Smith', '123 Maple, Anytown NC', 12345) }
let!(:joejr) { customer.new('Joe Smith', '123 Maple, Anytown NC', 12345) }
let!(:jane) { customer.new('Jane Doe', '456 Elm, Anytown NC', 12345) }
let!(:janejr){ employer.new('Jane Doe', '456 Elm, Anytown NC', 12345) }
context '#==' do
it 'returns true if other has same struct subclass and equal values' do
expect(joe == joejr).to be true
end
it 'returns false if other has different struct subclass' do
expect(jane == janejr).to be false
end
it 'returns false if other has different values' do
expect(jane == joe).to be false
end
end
context '#!=' do
it 'returns false if other has same struct subclass and equal values' do
expect(joe != joejr).to be false
end
it 'returns true if other has different struct subclass' do
expect(jane != janejr).to be true
end
it 'returns true if other has different values' do
expect(jane != joe).to be true
end
end
end
context 'enumeration' do
let(:members) { [:name, :address, :zip] }
let(:values) { ['Joe Smith', '123 Maple, Anytown NC', 12345] }
let(:customer) { described_class.new(*members) }
let!(:joe) { customer.new(*values) }
context '#each' do
it 'yields the value of each struct member in order' do
index = 0
joe.each do |value|
expect(joe[index]).to eq value
index += 1
end
expect(index).to eq 3
end
it 'returns an enumerator when no block is given' do
expect(joe.each).to be_a Enumerator
end
end
context '#each_pair' do
it 'yields the name and value of each struct member in order' do
index = 0
joe.each_pair do |name, value|
expect(joe.members[index]).to eq name
expect(joe[index]).to eq value
index += 1
end
expect(index).to eq 3
end
it 'returns an enumerator when no block is given' do
expect(joe.each_pair).to be_a Enumerator
end
end
context '#select' do
it 'yields each value' do
index = 0
joe.select do |value|
expect(joe[index]).to eq value
index += 1
end
expect(index).to eq 3
end
it 'returns an Array with the values from for which the block returns true' do
result = joe.select{|value| value.is_a?(String) }
expect(result).to eq ['Joe Smith', '123 Maple, Anytown NC']
end
it 'returns an enumerator when no block is given' do
expect(joe.select).to be_a Enumerator
end
end
end
context 'conversion' do
let!(:anon_struct_members) { [:name, :address, :zip] }
let(:anon_struct) { described_class.new(*anon_struct_members) }
let!(:named_struct_members) { [:left, :right] }
let(:named_struct) do
described_class.new("Test#{described_class}Properties".gsub(/::/, ''),
*named_struct_members)
end
context '#to_s' do
it 'includes the name of the class when registered' do
expect(named_struct.new.to_s).to match(/#{named_struct}/)
end
it 'includes the names of all members' do
string = anon_struct.new.to_s
anon_struct_members.each do |member|
expect(string).to match(/#{member}/)
end
string = named_struct.new.to_s
named_struct_members.each do |member|
expect(string).to match(/#{member}/)
end
end
it 'includes all values' do
values = [:foo, 'bar', 42]
string = anon_struct.new(*values).to_s
values.each do |value|
expect(string).to match(/#{value}/)
end
values = ['bar', 42]
string = named_struct.new(*values).to_s
values.each do |value|
expect(string).to match(/#{value}/)
end
end
it 'returns the same string as #inspect' do
values = [:foo, 'bar', 42]
struct = anon_struct.new(*values)
expect(struct.to_s).to eq struct.inspect
values = ['bar', 42]
struct = named_struct.new(*values)
expect(struct.to_s).to eq struct.inspect
end
end
context '#to_a' do
it 'returns the to_a for this struct as an array' do
expect(anon_struct.new().to_a).to eq [nil, nil, nil]
expect(named_struct.new().to_a).to eq [nil, nil]
expect(anon_struct.new(:foo, :bar, :baz).to_a).to eq [:foo, :bar, :baz]
expect(named_struct.new(:yes, :no).to_a).to eq [:yes, :no]
end
end
context '#to_h' do
it 'returns a Hash containing the names and values in order' do
expected = {name: nil, address: nil, zip: nil}
expect(anon_struct.new().to_h).to eq expected
expected = {left: nil, right: nil}
expect(named_struct.new().to_h).to eq expected
expected = {name: :foo, address: :bar, zip: :baz}
expect(anon_struct.new(:foo, :bar, :baz).to_h).to eq expected
expected = {left: :yes, right: :no}
expect(named_struct.new(:yes, :no).to_h).to eq expected
end
end
end
end
RSpec.shared_examples :mergeable_struct do
let(:this){ described_class.new(:foo, :bar, :baz).new('foo', nil, nil)}
let(:other){ {baz: 42} }
context '#merge' do
it 'updates all members with the new values from a given hash' do
expect(this.merge(other).baz).to eq 42
end
it 'calls the given block for each key in `other`' do
actual = 0
this = described_class.new(:foo, :bar, :baz).new('foo', :bar, 42)
this.merge(bar: :yes, baz: :no){|member, thisval, otherval| actual += 1 }
expect(actual).to eq 2
end
it 'retains the value for all members not without values in the given hash' do
expect(this.merge(other).foo).to eq 'foo'
end
it 'raises an exception when given a hash with members not in the struct' do
expect{this.merge(bogus: true)}.to raise_exception(ArgumentError)
end
it 'returns a new object' do
expect(this.merge(other).object_id).to_not eq this.object_id
expect(this.merge(other).object_id).to_not eq other.object_id
end
end
end