Module: TypeStructTest

Defined in:
lib/type_struct_test.rb

Defined Under Namespace

Classes: Bar, Dummy, Foo, Quux, Qux, Sample

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#example_readmeObject



324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/type_struct_test.rb', line 324

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_arrayof_s_valid?(t) ⇒ Boolean

Returns:

  • (Boolean)


175
176
177
178
179
# File 'lib/type_struct_test.rb', line 175

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)


200
201
202
203
204
205
206
207
208
209
# File 'lib/type_struct_test.rb', line 200

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)


181
182
183
184
185
# File 'lib/type_struct_test.rb', line 181

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



228
229
230
231
232
233
234
235
236
237
238
# File 'lib/type_struct_test.rb', line 228

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



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

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_initialize(t) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/type_struct_test.rb', line 211

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



247
248
249
250
251
252
# File 'lib/type_struct_test.rb', line 247

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



240
241
242
243
244
245
# File 'lib/type_struct_test.rb', line 240

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



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

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

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

  begin
    Foo.from_hash(bar: { baz: [1, 2, 3] }, nil: 1)
  rescue TypeError
  else
    t.error("Bar.qux is not able to nil but accepted")
  end

  foo = Foo.from_hash(bar: { baz: [1, 2, 3] }, nil: nil)
  unless TypeStruct === foo
    t.error("return value type was break")
  end
  unless Foo === foo
    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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/type_struct_test.rb', line 51

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] } },
    ],
  )
  unless A === a
    t.error("failed")
  end
end

#test_s_from_hash_equal(t) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/type_struct_test.rb', line 108

def test_s_from_hash_equal(t)
  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_with_struct(t) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/type_struct_test.rb', line 126

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

#test_s_members(t) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/type_struct_test.rb', line 142

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



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

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)


163
164
165
166
167
168
169
170
171
172
173
# File 'lib/type_struct_test.rb', line 163

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



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

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



254
255
256
257
258
259
260
261
262
# File 'lib/type_struct_test.rb', line 254

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)


193
194
195
196
197
198
# File 'lib/type_struct_test.rb', line 193

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)


187
188
189
190
191
# File 'lib/type_struct_test.rb', line 187

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