Module: TypeStructTest

Defined in:
lib/type_struct_test.rb

Defined Under Namespace

Classes: Dummy, Sample

Constant Summary collapse

BoolClass =
TrueClass | FalseClass
C =
TypeStruct.new(
  a: ArrayOf(BoolClass),
)
B =
TypeStruct.new(
  a: Integer,
  b: BoolClass,
  c: ArrayOf(Integer),
  d: ArrayOf(BoolClass),
  e: C,
)
A =
TypeStruct.new(
  a: ArrayOf(Integer),
  b: ArrayOf(BoolClass),
  c: BoolClass,
  d: B,
  e: ArrayOf(B),
  f: HashOf(String, Integer),
)

Instance Method Summary collapse

Instance Method Details

#example_readmeObject



579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/type_struct_test.rb', line 579

def example_readme
  sample = Sample.new(
    str: "instance of String",
    reg: "match to regexp",
    num: 10,
    any: true,
  )
  p sample
  p sample.to_h
  # Output:
  # #<TypeStructTest::Sample str="instance of String", reg="match to regexp", num=10, any=true>
  # {:str=>"instance of String", :reg=>"match to regexp", :num=>10, :any=>true}
end

#test_array_of(t) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/type_struct_test.rb', line 151

def test_array_of(t)
  a = TypeStruct.new(a: Integer)
  b = TypeStruct.new(a: ArrayOf(a))
  bb = b.new(a: [a.new(a: 1), a.new(a: 2), a.new(a: 3)])
  unless b === bb
    t.error("type error")
  end

  unless bb == b.from_hash(a: [{ a: 1 }, { a: 2 }, { a: 3 }])
    t.error("from_hash error")
  end
end

#test_array_of_error(t) ⇒ Object



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
# File 'lib/type_struct_test.rb', line 164

def test_array_of_error(t)
  a = TypeStruct.new(a: ArrayOf(Integer))
  begin
    a.from_hash(a: [1.1])
  rescue TypeError
  rescue => e
    t.error("Unexpected error #{e.class}")
  else
    t.error("Nothing raised TypeError")
  end

  b = TypeStruct.new(a: ArrayOf(Integer) | NilClass)
  begin
    b.from_hash(a: [1.1])
  rescue TypeStruct::UnionNotFoundError
  rescue => e
    t.error("Unexpected error #{e.class}")
  else
    t.error("Nothing raised TypeStruct::UnionNotFoundError")
  end

  c = TypeStruct.new(c: Integer)
  d = TypeStruct.new(d: ArrayOf(c) | NilClass)
  begin
    d.from_hash(d: [{c: 1.1}])
  rescue TypeStruct::UnionNotFoundError
  rescue => e
    t.error("Unexpected error #{e.class}")
  else
    t.error("Nothing raised TypeStruct::UnionNotFoundError")
  end
end

#test_arrayof_s_valid?(t) ⇒ Boolean

Returns:

  • (Boolean)


430
431
432
433
434
# File 'lib/type_struct_test.rb', line 430

def test_arrayof_s_valid?(t)
  unless A.valid?(:a, [1, 2, 3])
    t.error("ArrayOf failed with [1,2,3]")
  end
end

#test_arrayof_type_struct_s_valid?(t) ⇒ Boolean

Returns:

  • (Boolean)


455
456
457
458
459
460
461
462
463
464
# File 'lib/type_struct_test.rb', line 455

def test_arrayof_type_struct_s_valid?(t)
  ary_b = [
    B.new(a: 1, b: false, c: [1, 2, 3], d: [false], e: C.new(a: [true])),
    B.new(a: 2, b: true, c: [1, 2, 3], d: [false], e: C.new(a: [true])),
    B.new(a: 3, b: false, c: [1, 2, 3], d: [false], e: C.new(a: [true])),
  ]
  unless A.valid?(:e, ary_b)
    t.error("ArrayOf with TypeStruct is invalid with #{ary_b}")
  end
end

#test_arrayof_union_s_valid?(t) ⇒ Boolean

Returns:

  • (Boolean)


436
437
438
439
440
# File 'lib/type_struct_test.rb', line 436

def test_arrayof_union_s_valid?(t)
  unless A.valid?(:b, [false, true, false])
    t.error("ArrayOf failed with Union")
  end
end

#test_eq(t) ⇒ Object



483
484
485
486
487
488
489
490
491
492
493
# File 'lib/type_struct_test.rb', line 483

def test_eq(t)
  dummy1 = Dummy.new(str: "aaa", num: 123, reg: "abc", ary: [1.1, 1], any: [1, "bbb"])
  dummy2 = Dummy.new(str: "aaa", num: 123, reg: "abc", ary: [1.1, 1], any: [1, "bbb"])
  dummy3 = Dummy.new(str: "bbb", num: 123, reg: "abc", ary: [1.1, 1], any: [1, "bbb"])
  unless dummy1 == dummy2
    t.error("members not equal")
  end
  unless dummy1 != dummy3
    t.error("members equal")
  end
end

#test_getter(t) ⇒ Object



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/type_struct_test.rb', line 519

def test_getter(t)
  dummy = Dummy.new(str: "aaa", num: 123, reg: "abc", any: [1, "bbb"])
  _, err = go { dummy[:str] }
  if err != nil
    t.error("expect not raise error when valid value get. got #{err}")
  end
  _, err = go { dummy.str }
  if err != nil
    t.error("expect not raise error when valid value get. got #{err}")
  end

  _, err = go { dummy[:nothing] }
  if err == nil
    t.error("expect not raise error when invalid value get")
  end
  _, err = go { dummy.nothing }
  if err == nil
    t.error("expect not raise error when invalid value get")
  end
end

#test_hash_of(t) ⇒ Object



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
# File 'lib/type_struct_test.rb', line 75

def test_hash_of(t)
  b = TypeStruct.new(b: Integer)
  hc = TypeStruct.new(
    a: HashOf(Symbol, b),
  )

  h = hc.new(
    a: { sym: b.new(b: 1) },
  )
  unless 1 === h.a[:sym].b
    t.error("assign failed")
  end

  begin
    hc.new(
      a: [],
    )
  rescue TypeError
  else
    t.error("TypeError was not railsed")
  end

  hh = hc.from_hash(
    a: { sym: { b: 1 } },
  )
  unless hh == h
    t.error("new and from_hash dose not make equal object")
  end

  begin
    hh = hc.from_hash(a: 1)
  rescue TypeError
  else
    t.error("TypeError dose not raise error")
  end

  hsbn = TypeStruct.new(
    a: HashOf(Symbol, b) | NilClass,
  )
  begin
    hsbn.from_hash(a: [])
  rescue TypeStruct::UnionNotFoundError
  rescue => e
    t.error("Unexpected error #{e.class}: #{e.message}")
  else
    t.error("Unexpected behavior")
  end

  begin
    hsbn.from_hash(a: {a: {b: 1.1}})
  rescue TypeStruct::UnionNotFoundError
  rescue => e
    t.error("Unexpected error #{e.class}: #{e.message}")
  else
    t.error("Unexpected behavior")
  end

  begin
    hsbn.from_hash(a: {"a" => {b: 1}})
  rescue TypeError
  rescue => e
    t.error("Unexpected error #{e.class}: #{e.message}")
  else
    t.error("Unexpected behavior")
  end

  begin
    hsbn.from_hash(a: {"a" => {b: 1.1}})
  rescue TypeStruct::UnionNotFoundError
  rescue => e
    t.error("Unexpected error #{e.class}: #{e.message}")
  else
    t.error("Unexpected behavior")
  end
end

#test_initialize(t) ⇒ Object



466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/type_struct_test.rb', line 466

def test_initialize(t)
  expects = { str: "aaa", num: 123, reg: "abc", ary: [1.1, 1], any: [1, "bbb"] }
  dummy = Dummy.new(str: "aaa", num: 123, reg: "abc", ary: [1.1, 1], any: [1, "bbb"])
  expects.each do |k, v|
    unless dummy[k] == v
      t.error("expect #{dummy[k]} got #{v}")
    end
  end

  dummy2 = Dummy.new("str" => "aaa", "num" => 123, "reg" => "abc", "ary" => [1.1, 1], "any" => [1, "bbb"])
  expects.each do |k, v|
    unless dummy2[k] == v
      t.error("expect #{dummy2[k]} got #{v}")
    end
  end
end

#test_initialize_invalid_type(t) ⇒ Object



502
503
504
505
506
507
# File 'lib/type_struct_test.rb', line 502

def test_initialize_invalid_type(t)
  value, err = go { Dummy.new(str: "aaa", num: 123, reg: "abb", any: nil) }
  if err == nil
    t.error("invalid value expect raise error")
  end
end

#test_initialize_not_enough(t) ⇒ Object



495
496
497
498
499
500
# File 'lib/type_struct_test.rb', line 495

def test_initialize_not_enough(t)
  _, err = go { Dummy.new(str: "aaa") }
  if err == nil
    t.error("in initialize, expect raise error since not enough members but nothing raised")
  end
end

#test_s_from_hash(t) ⇒ Object



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
# File 'lib/type_struct_test.rb', line 197

def test_s_from_hash(t)
  bar = TypeStruct.new(
    baz: ArrayOf(Integer | NilClass),
  )
  foo = TypeStruct.new(
    nil: NilClass,
    bar: bar,
  )

  f = foo.from_hash(bar: { baz: [1, 2, 3] })
  unless foo === f
    t.error("return value was break")
  end

  begin
    foo.from_hash(bar: { baz: [1, 2, "3"] })
  rescue TypeStruct::UnionNotFoundError
  else
    t.error("'3' is not valid value for Baz.baz:#{Bar.definition.fetch(:baz)}")
  end

  f = foo.from_hash("bar" => { "baz" => [1, 2, 3] })
  unless foo === f
    t.error("return value was break")
  end

  f = foo.from_hash(bar: { baz: [1, 2, 3] }, nil: nil)
  unless TypeStruct === f
    t.error("return value type was break")
  end
  unless foo === f
    t.error("return value type was break")
  end

  begin
    foo.from_hash(bar: { baz: [1, nil, 3] })
  rescue => e
    t.error("Bar.baz is able to nil but raise error #{e.class}: #{e.message}")
  end

  begin
    foo.from_hash(bar: { baz: nil })
  rescue TypeError
  else
    t.error("Bar.baz is not able to nil")
  end
end

#test_s_from_hash_a(t) ⇒ Object



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
# File 'lib/type_struct_test.rb', line 35

def test_s_from_hash_a(t)
  a = A.from_hash(
    a: [1, 2, 3],
    b: [false, true, false],
    c: false,
    d: { a: 1, b: false, c: [1, 2, 3], d: [false], e: { a: [true] } },
    e: [
      { a: 1, b: false, c: [1, 2, 3], d: [false], e: { a: [true] } },
      { a: 2, b: true, c: [1, 2, 3], d: [false], e: { a: [true] } },
      { a: 3, b: true, c: [1, 2, 3], d: [false], e: { a: [true] } },
    ],
    f: {
      "a" => 1,
      "c" => 2,
    },
  )
  aa = A.new(
    a: [1, 2, 3],
    b: [false, true, false],
    c: false,
    d: B.new(a: 1, b: false, c: [1, 2, 3], d: [false], e: C.new(a: [true])),
    e: [
      B.new(a: 1, b: false, c: [1, 2, 3], d: [false], e: C.new(a: [true])),
      B.new(a: 2, b: true, c: [1, 2, 3], d: [false], e: C.new(a: [true])),
      B.new(a: 3, b: true, c: [1, 2, 3], d: [false], e: C.new(a: [true])),
    ],
    f: {
      "a" => 1,
      "c" => 2,
    },
  )
  unless A === a
    t.error("instance type miss")
  end

  unless a == aa
    t.error("not same new and from_hash")
  end
end

#test_s_from_hash_equal(t) ⇒ Object



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
# File 'lib/type_struct_test.rb', line 348

def test_s_from_hash_equal(t)
  bar = TypeStruct.new(
    baz: ArrayOf(Integer | NilClass),
  )
  foo = TypeStruct.new(
    nil: NilClass,
    bar: bar,
  )

  expect = foo.new(bar: bar.new(baz: [1, 2, 3]))
  actual = foo.from_hash(bar: { baz: [1, 2, 3] })
  if expect != actual
    t.error("expect #{expect} got #{actual}")
  end

  noteq = foo.from_hash(bar: { baz: [1, 2, 4] })
  if expect == noteq
    t.error("expect #{expect} not equal #{noteq}")
  end

  noteq = foo.from_hash(bar: { baz: [1, 2, nil] })
  if expect == noteq
    t.error("expect #{expect} not equal #{noteq}")
  end
end

#test_s_from_hash_union(t) ⇒ Object



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
# File 'lib/type_struct_test.rb', line 305

def test_s_from_hash_union(t)
  a = TypeStruct.new(a: Integer)
  b = TypeStruct.new(b: Integer)
  c = TypeStruct.new(c: Integer)
  u = TypeStruct::Union.new(a, b, c)
  d = TypeStruct.new(d: u)

  begin
    d.from_hash(d: { b: 1 })
  rescue => e
    t.error("Unexpected error was raised #{e.class}: #{e.message}")
  end

  begin
    d.from_hash(d: [b: 1])
  rescue TypeStruct::UnionNotFoundError => err
    unless /is not found with value/ =~ err.message
      t.error("error message was changed: '#{err.message}'")
    end
  else
    t.error("error dose not raised")
  end

  begin
    d.from_hash(d: { b: "a" })
  rescue TypeStruct::UnionNotFoundError => err
    unless /is not found with value/ =~ err.message
      t.error("error message was changed")
    end
    unless /#a expect Integer got nil/ =~ err.message
      t.error("error message was changed")
    end
    unless /#b expect Integer got "a"/ =~ err.message
      t.error("error message was changed")
    end
    unless /#c expect Integer got nil/ =~ err.message
      t.error("error message was changed")
    end
  else
    t.error("error dose not raised")
  end
end

#test_s_from_hash_with_array_of(t) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/type_struct_test.rb', line 265

def test_s_from_hash_with_array_of(t)
  a = TypeStruct.new(a: ArrayOf(Integer))
  begin
    a.from_hash(a: 1)
  rescue TypeError => e
    unless /#a expect ArrayOf\(Integer\) got 1/ =~ e.message
      t.error("message was changed: #{e.message}")
    end
  rescue => e
    t.error("Unexpected error #{e}")
  else
    t.error("Unexpected behavior")
  end
end

#test_s_from_hash_with_hash_of(t) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/type_struct_test.rb', line 280

def test_s_from_hash_with_hash_of(t)
  a = TypeStruct.new(a: HashOf(String, Integer))
  begin
    a.from_hash(a: 1)
  rescue TypeError => e
    unless /#a expect HashOf\(String, Integer\) got 1/ =~ e.message
      t.error("message was changed: #{e.message}")
    end
  rescue => e
    t.error("Unexpected error #{e}")
  else
    t.error("Unexpected behavior")
  end
end

#test_s_from_hash_with_not_class(t) ⇒ Object



295
296
297
298
299
300
301
302
303
# File 'lib/type_struct_test.rb', line 295

def test_s_from_hash_with_not_class(t)
  a = TypeStruct.new(a: "a")
  begin
    a.from_hash(a: "b")
  rescue TypeError
  else
    t.error("Unexpected behavior")
  end
end

#test_s_from_hash_with_other_object(t) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/type_struct_test.rb', line 245

def test_s_from_hash_with_other_object(t)
  a = TypeStruct.new(a: Integer)
  o = Object.new
  begin
    a.from_hash(o)
  rescue TypeError => e
  rescue => e
    t.error("Unexpected error #{e.class}")
  else
    t.error("should raise TypeError")
  end

  def o.to_hash
    {a: 1}
  end
  unless a === a.from_hash(o)
    t.error("Unexpected behavior")
  end
end

#test_s_from_hash_with_struct(t) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/type_struct_test.rb', line 374

def test_s_from_hash_with_struct(t)
  quux = Struct.new(:q)

  qux = TypeStruct.new(
    quux1: quux,
    quux2: quux,
  )

  q = qux.from_hash(quux1: { q: 1 }, quux2: { q: nil })
  unless qux === q
    t.error("return value was break")
  end
  unless quux === q.quux1
    t.error("struct type was not applied")
  end
  unless 1 == q.quux1.q
    t.error("mapping failed #{q.quux.q} != 1")
  end
  unless nil == q.quux2.q
    t.error("mapping failed #{q.quux.q} != nil")
  end
end

#test_s_members(t) ⇒ Object



397
398
399
400
401
402
403
# File 'lib/type_struct_test.rb', line 397

def test_s_members(t)
  m = Dummy.members
  expect = [:str, :num, :reg, :ary, :any]
  unless m == expect
    t.error("expect #{expect} got #{m}")
  end
end

#test_s_type(t) ⇒ Object



405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/type_struct_test.rb', line 405

def test_s_type(t)
  Dummy.definition.each do |k, v|
    type = Dummy.type(k)
    if v.respond_to?(:[])
      if type != v[:type]
        t.error("expect #{v[:type]} got #{type}")
      end
    elsif type != v
      t.error("expect #{v} got #{type}")
    end
  end
end

#test_s_valid?(t) ⇒ Boolean

Returns:

  • (Boolean)


418
419
420
421
422
423
424
425
426
427
428
# File 'lib/type_struct_test.rb', line 418

def test_s_valid?(t)
  unless Dummy.valid?(:str, "abc")
    t.error('expect :str valid "abc"')
  end
  if Dummy.valid?(:str, 345)
    t.error("expect :str invalid 345")
  end
  unless Dummy.valid?(:reg, "abc")
    t.error('expect :reg valid "abc"')
  end
end

#test_setter(t) ⇒ Object



540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/type_struct_test.rb', line 540

def test_setter(t)
  dummy = Dummy.new(str: "aaa", num: 123, reg: "abc", any: [1, "bbb"])
  i(str num reg).each do |k|
    _, err = go { dummy[k] = nil }
    if err == nil
      t.error("expect raise error when invalid value set")
    end
    _, err = go { dummy.__send__("#{k}=", nil) }
    if err == nil
      t.error("expect raise error when invalid value set")
    end
  end

  _, err = go { dummy[:any] = nil }
  if err != nil
    t.error("expect not raise error when valid value set got #{err}")
  end
  _, err = go { dummy.any = nil }
  if err != nil
    t.error("expect not raise error when valid value set got #{err}")
  end

  _, err = go { dummy[:nothing] = nil }
  if err == nil
    t.error("expect not raise error when valid value set got #{err}")
  end
  _, err = go { dummy.nothing = nil }
  if err == nil
    t.error("expect not raise error when valid value set got #{err}")
  end
end

#test_to_h(t) ⇒ Object



509
510
511
512
513
514
515
516
517
# File 'lib/type_struct_test.rb', line 509

def test_to_h(t)
  expects = { str: "aaa", num: 123, reg: "abcde", any: [1, "bbb"] }
  dummy = Dummy.new(str: "aaa", num: 123, reg: "abcde", any: [1, "bbb"])
  expects.each do |k, v|
    unless dummy[k] == v
      t.error("expect #{dummy[k]} got #{v}")
    end
  end
end

#test_type_struct_s_valid?(t) ⇒ Boolean

Returns:

  • (Boolean)


448
449
450
451
452
453
# File 'lib/type_struct_test.rb', line 448

def test_type_struct_s_valid?(t)
  b = B.new(a: 1, b: false, c: [1, 2, 3], d: [false], e: C.new(a: [true]))
  unless A.valid?(:d, b)
    t.error("TypeStruct is invalid with #{b}")
  end
end

#test_union_s_valid?(t) ⇒ Boolean

Returns:

  • (Boolean)


442
443
444
445
446
# File 'lib/type_struct_test.rb', line 442

def test_union_s_valid?(t)
  unless A.valid?(:c, false)
    t.error("Union is invalid with false")
  end
end