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), f: HashOf.new(String, Integer), )
Instance Method Summary collapse
- #example_readme ⇒ Object
- #test_array_of(t) ⇒ Object
- #test_arrayof_s_valid?(t) ⇒ Boolean
- #test_arrayof_type_struct_s_valid?(t) ⇒ Boolean
- #test_arrayof_union_s_valid?(t) ⇒ Boolean
- #test_eq(t) ⇒ Object
- #test_getter(t) ⇒ Object
- #test_hash_of(t) ⇒ Object
- #test_initialize(t) ⇒ Object
- #test_initialize_invalid_type(t) ⇒ Object
- #test_initialize_not_enough(t) ⇒ Object
- #test_s_from_hash(t) ⇒ Object
- #test_s_from_hash_a(t) ⇒ Object
- #test_s_from_hash_equal(t) ⇒ Object
- #test_s_from_hash_union(t) ⇒ Object
- #test_s_from_hash_with_struct(t) ⇒ Object
- #test_s_members(t) ⇒ Object
- #test_s_type(t) ⇒ Object
- #test_s_valid?(t) ⇒ Boolean
- #test_setter(t) ⇒ Object
- #test_to_h(t) ⇒ Object
- #test_type_struct_s_valid?(t) ⇒ Boolean
- #test_union_s_valid?(t) ⇒ Boolean
Instance Method Details
#example_readme ⇒ Object
423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/type_struct_test.rb', line 423 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
130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/type_struct_test.rb', line 130 def test_array_of(t) a = TypeStruct.new(a: Integer) b = TypeStruct.new(a: ArrayOf.new(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_arrayof_s_valid?(t) ⇒ Boolean
274 275 276 277 278 |
# File 'lib/type_struct_test.rb', line 274 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
299 300 301 302 303 304 305 306 307 308 |
# File 'lib/type_struct_test.rb', line 299 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
280 281 282 283 284 |
# File 'lib/type_struct_test.rb', line 280 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
327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/type_struct_test.rb', line 327 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
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/type_struct_test.rb', line 363 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
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 |
# File 'lib/type_struct_test.rb', line 92 def test_hash_of(t) b = TypeStruct.new(b: Integer) hc = TypeStruct.new( a: HashOf.new(Symbol, b), ) h = hc.new( a: { sym: b.new(b: 1) }, ) unless 1 === h.a[:sym].b t.error("assign failed") end p HashOf.new(Symbol, b) === [] 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 end |
#test_initialize(t) ⇒ Object
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/type_struct_test.rb', line 310 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
346 347 348 349 350 351 |
# File 'lib/type_struct_test.rb', line 346 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
339 340 341 342 343 344 |
# File 'lib/type_struct_test.rb', line 339 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
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 |
# File 'lib/type_struct_test.rb', line 143 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 begin Foo.from_hash(bar: { baz: [1, 2, "3"] }) rescue TypeError else t.error("'3' is not valid value for Baz.baz:#{Bar.definition.fetch(:baz)}") end 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] }, 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
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 |
# File 'lib/type_struct_test.rb', line 52 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
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/type_struct_test.rb', line 207 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_union(t) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/type_struct_test.rb', line 183 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) d.from_hash(d: { b: 1 }) begin d.from_hash(d: [b: 1]) rescue TypeStruct::UnionNotFoundError else t.error("error dose not raised") end begin d.from_hash(d: { b: "a" }) rescue TypeStruct::UnionNotFoundError else t.error("error dose not raised") end end |
#test_s_from_hash_with_struct(t) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/type_struct_test.rb', line 225 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
241 242 243 244 245 246 247 |
# File 'lib/type_struct_test.rb', line 241 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
249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/type_struct_test.rb', line 249 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
262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/type_struct_test.rb', line 262 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
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 |
# File 'lib/type_struct_test.rb', line 384 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
353 354 355 356 357 358 359 360 361 |
# File 'lib/type_struct_test.rb', line 353 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
292 293 294 295 296 297 |
# File 'lib/type_struct_test.rb', line 292 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
286 287 288 289 290 |
# File 'lib/type_struct_test.rb', line 286 def test_union_s_valid?(t) unless A.valid?(:c, false) t.error("Union is invalid with false") end end |