type_struct 0.1.1 → 0.2.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.
- checksums.yaml +4 -4
- data/.travis.yml +3 -3
- data/README.md +77 -0
- data/lib/type_struct.rb +137 -44
- data/lib/type_struct/arrayof.rb +24 -0
- data/lib/type_struct/ext.rb +16 -0
- data/lib/type_struct/union.rb +25 -0
- data/lib/type_struct/union_test.rb +45 -0
- data/lib/type_struct/version.rb +1 -1
- data/lib/type_struct_test.rb +201 -11
- metadata +17 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e4ad3ee2f907ec9b4607626b3708183e99d97a6a
|
4
|
+
data.tar.gz: 55840f29943e7263fd0671eca5fd00286f765673
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66b8e9b9a352b38f9751e65bd933ccc7dc477dcdc49a6c12e51cf7ea5e0377f6e608f4fbca3f9758cc679ee8d38a761102ee1d53b333039032af4ad2106bf7ab
|
7
|
+
data.tar.gz: b5d4fb2c7980a1ca40fbb463a224d079b11bc95683ff5bca68b0fb2655ad22360d89b17d33c09f9a78b5aa1fdf41d2f8dde4f07e3b8f7a7e8980ab551761f901
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -6,6 +6,8 @@ Imitating static typed struct.
|
|
6
6
|
|
7
7
|
## Usage
|
8
8
|
|
9
|
+
### Check type
|
10
|
+
|
9
11
|
```ruby
|
10
12
|
class Sample < TypeStruct.new(
|
11
13
|
str: String,
|
@@ -26,6 +28,81 @@ p sample
|
|
26
28
|
|
27
29
|
p sample.to_h
|
28
30
|
#=> {:str=>"instance of String", :reg=>"not match to regexp", :num=>10, :any=>true}
|
31
|
+
|
32
|
+
p sample.str
|
33
|
+
#=> "instance of String"
|
34
|
+
|
35
|
+
sample.string #=> NoMethodError
|
36
|
+
sample.str = 1 #=> TypeError
|
37
|
+
```
|
38
|
+
|
39
|
+
### Mapping from Hash
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
Point = TypeStruct.new(
|
43
|
+
x: Integer,
|
44
|
+
y: Integer,
|
45
|
+
)
|
46
|
+
Color = Struct.new(:code)
|
47
|
+
Line = TypeStruct.new(
|
48
|
+
start: Point,
|
49
|
+
end: Point,
|
50
|
+
color: Color,
|
51
|
+
)
|
52
|
+
|
53
|
+
hash = JSON.parse(%({"start":{"x":3,"y":10},"end":{"x":5,"y":9},"color":{"code":"#CAFE00"}}))
|
54
|
+
line = Line.from_hash(hash)
|
55
|
+
|
56
|
+
p line
|
57
|
+
#=> #<Line start=#<Point x=3, y=10>, end=#<Point x=5, y=9>, color=#<struct Color code="#CAFE00">>
|
58
|
+
p line.start.y
|
59
|
+
#=> 10
|
60
|
+
line.stort
|
61
|
+
#=> NoMethodError
|
62
|
+
```
|
63
|
+
|
64
|
+
## Two special notation
|
65
|
+
|
66
|
+
### Union
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
require "union"
|
70
|
+
Foo = TypeStruct.new(
|
71
|
+
bar: Union.new(TrueClass, FalseClass)
|
72
|
+
)
|
73
|
+
p Foo.new(bar: false) #=> #<Foo bar=false>
|
74
|
+
```
|
75
|
+
|
76
|
+
or
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
require "union_ext"
|
80
|
+
using UnionExt
|
81
|
+
Foo = TypeStruct.new(
|
82
|
+
bar: TrueClass | FalseClass,
|
83
|
+
)
|
84
|
+
```
|
85
|
+
|
86
|
+
### ArrayOf
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
Bar = TypeStruct.new(
|
90
|
+
baz: ArrayOf.new(Integer),
|
91
|
+
)
|
92
|
+
p Bar.new(baz: [1, 2, 3]) #=> #<Bar baz=[1, 2, 3]>
|
93
|
+
```
|
94
|
+
|
95
|
+
### Mix
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
using UnionExt
|
99
|
+
Baz = TypeStruct.new(
|
100
|
+
qux: ArrayOf.new(Integer | TrueClass | FalseClass) | NilClass
|
101
|
+
)
|
102
|
+
p Baz.new(qux: [1]) #=> #<AAA::Baz qux=[1]>
|
103
|
+
p Baz.new(qux: [true, false]) #=> #<AAA::Baz qux=[true, false]>
|
104
|
+
p Baz.new(qux: nil) #=> #<AAA::Baz qux=nil>
|
105
|
+
p Baz.new(qux: 1) #=> TypeError
|
29
106
|
```
|
30
107
|
|
31
108
|
## Installation
|
data/lib/type_struct.rb
CHANGED
@@ -1,29 +1,148 @@
|
|
1
|
+
require "type_struct/union"
|
2
|
+
require "type_struct/arrayof"
|
3
|
+
|
1
4
|
class TypeStruct
|
2
5
|
require "type_struct/version"
|
3
6
|
|
4
7
|
class NoMemberError < StandardError
|
5
8
|
end
|
6
9
|
|
10
|
+
def initialize(arg)
|
11
|
+
sym_arg = {}
|
12
|
+
arg.each do |k, v|
|
13
|
+
sym_arg[k.to_sym] = v
|
14
|
+
end
|
15
|
+
self.class.members.each do |k|
|
16
|
+
self[k] = sym_arg[k]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def ==(other)
|
21
|
+
return false unless TypeStruct === other
|
22
|
+
return false unless to_h == other.to_h
|
23
|
+
true
|
24
|
+
end
|
25
|
+
|
26
|
+
def []=(k, v)
|
27
|
+
__send__("#{k}=", v)
|
28
|
+
end
|
29
|
+
|
30
|
+
def [](k)
|
31
|
+
__send__(k)
|
32
|
+
end
|
33
|
+
|
34
|
+
def inspect
|
35
|
+
m = to_h.map do |k, v|
|
36
|
+
"#{k}=#{v.inspect}"
|
37
|
+
end
|
38
|
+
"#<#{self.class} #{m.join(', ')}>"
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_h
|
42
|
+
m = {}
|
43
|
+
self.class.members.each do |k|
|
44
|
+
m[k] = self[k]
|
45
|
+
end
|
46
|
+
m
|
47
|
+
end
|
48
|
+
|
7
49
|
class << self
|
8
|
-
def
|
9
|
-
|
10
|
-
const_set :MEMBERS, args
|
50
|
+
def try_convert(klass, value)
|
51
|
+
return nil unless klass
|
11
52
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
53
|
+
if Union === klass
|
54
|
+
klass.each do |k|
|
55
|
+
t = try_convert(k, value)
|
56
|
+
return t if !t.nil?
|
57
|
+
end
|
58
|
+
nil
|
59
|
+
elsif ArrayOf === klass
|
60
|
+
value.map { |v| try_convert(klass.type, v) }
|
61
|
+
elsif klass.ancestors.include?(TypeStruct)
|
62
|
+
klass.from_hash(value)
|
63
|
+
elsif klass.ancestors.include?(Struct)
|
64
|
+
struct = klass.new
|
65
|
+
value.each { |k, v| struct[k] = v }
|
66
|
+
struct
|
67
|
+
else
|
68
|
+
value
|
69
|
+
end
|
70
|
+
rescue
|
71
|
+
nil
|
72
|
+
end
|
16
73
|
|
17
|
-
|
18
|
-
|
74
|
+
def from_hash(h)
|
75
|
+
args = {}
|
76
|
+
h.each do |key, value|
|
77
|
+
key = key.to_sym
|
78
|
+
t = type(key)
|
79
|
+
if Class === t
|
80
|
+
case
|
81
|
+
when t.ancestors.include?(TypeStruct)
|
82
|
+
args[key] = t.from_hash(value)
|
83
|
+
when t.ancestors.include?(Struct)
|
84
|
+
struct = t.new
|
85
|
+
value.each { |k, v| struct[k] = v }
|
86
|
+
args[key] = struct
|
87
|
+
when t.respond_to?(:new)
|
88
|
+
args[key] = t.new(value)
|
89
|
+
else
|
90
|
+
args[key] = value
|
19
91
|
end
|
20
|
-
|
21
|
-
|
22
|
-
type
|
92
|
+
elsif ArrayOf === t
|
93
|
+
args[key] = if value.respond_to?(:map)
|
94
|
+
value.map { |v| try_convert(t.type, v) }
|
95
|
+
else
|
96
|
+
value
|
23
97
|
end
|
98
|
+
else
|
99
|
+
args[key] = try_convert(t, value)
|
24
100
|
end
|
101
|
+
end
|
102
|
+
new(args)
|
103
|
+
end
|
104
|
+
|
105
|
+
def definition
|
106
|
+
const_get(:DEFINITION)
|
107
|
+
end
|
25
108
|
|
26
|
-
|
109
|
+
def members
|
110
|
+
definition.keys
|
111
|
+
end
|
112
|
+
|
113
|
+
def type(k)
|
114
|
+
t = definition[k]
|
115
|
+
if Hash === t
|
116
|
+
t[:type]
|
117
|
+
else
|
118
|
+
t
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def valid?(k, v)
|
123
|
+
t = definition[k]
|
124
|
+
if ArrayOf === t && Array === v
|
125
|
+
v.all? { |vv| t.type === vv }
|
126
|
+
elsif Array === t
|
127
|
+
return false if v.nil?
|
128
|
+
v.all? { |i| t.any? { |c| c === i } }
|
129
|
+
elsif TypeStruct === v
|
130
|
+
t == v.class
|
131
|
+
else
|
132
|
+
t === v
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
alias original_new new
|
137
|
+
def new(**args, &block)
|
138
|
+
c = Class.new(TypeStruct) do
|
139
|
+
const_set :DEFINITION, args
|
140
|
+
|
141
|
+
class << self
|
142
|
+
alias_method :new, :original_new
|
143
|
+
end
|
144
|
+
|
145
|
+
args.each do |k, _|
|
27
146
|
define_method(k) do
|
28
147
|
instance_variable_get("@#{k}")
|
29
148
|
end
|
@@ -31,42 +150,16 @@ class TypeStruct
|
|
31
150
|
define_method("#{k}=") do |v|
|
32
151
|
raise TypeStruct::NoMemberError unless respond_to?(k)
|
33
152
|
unless self.class.valid?(k, v)
|
34
|
-
raise TypeError, "expect #{self.class.type(k)} got #{v.
|
153
|
+
raise TypeError, "#{self.class}##{k} expect #{self.class.type(k)} got #{v.inspect}"
|
35
154
|
end
|
36
155
|
instance_variable_set("@#{k}", v)
|
37
156
|
end
|
38
157
|
end
|
39
|
-
|
40
|
-
def initialize(**arg)
|
41
|
-
self.class.members.each do |k, _|
|
42
|
-
self[k] = arg[k]
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def []=(k, v)
|
47
|
-
__send__("#{k}=", v)
|
48
|
-
end
|
49
|
-
|
50
|
-
def [](k)
|
51
|
-
__send__(k)
|
52
|
-
end
|
53
|
-
|
54
|
-
def inspect
|
55
|
-
m = to_h.map do |k, v|
|
56
|
-
"#{k}=#{v.inspect}"
|
57
|
-
end
|
58
|
-
"#<#{self.class.to_s} #{m.join(', ')}>"
|
59
|
-
end
|
60
|
-
|
61
|
-
def to_h
|
62
|
-
m = {}
|
63
|
-
self.class.members.each do |k, _|
|
64
|
-
m[k] = self[k]
|
65
|
-
end
|
66
|
-
m
|
67
|
-
end
|
68
|
-
alias to_hash to_h
|
69
158
|
end
|
159
|
+
if block_given?
|
160
|
+
c.module_eval(&block)
|
161
|
+
end
|
162
|
+
c
|
70
163
|
end
|
71
164
|
end
|
72
165
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require "type_struct/union"
|
2
|
+
|
3
|
+
class TypeStruct
|
4
|
+
class ArrayOf
|
5
|
+
attr_reader :type
|
6
|
+
def initialize(type)
|
7
|
+
@type = type
|
8
|
+
end
|
9
|
+
|
10
|
+
def |(other)
|
11
|
+
Union.new(self, other)
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_s
|
15
|
+
"#<#{self.class} #{@type}>"
|
16
|
+
end
|
17
|
+
alias inspect to_s
|
18
|
+
|
19
|
+
def ===(other)
|
20
|
+
return false unless other.respond_to?(:any?)
|
21
|
+
other.any? { |o| @type === o }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require "type_struct/union"
|
2
|
+
require "type_struct/arrayof"
|
3
|
+
|
4
|
+
class TypeStruct
|
5
|
+
module UnionExt
|
6
|
+
refine Class do
|
7
|
+
def |(other)
|
8
|
+
Union.new(self, other)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
ArrayOf = TypeStruct::ArrayOf
|
15
|
+
Union = TypeStruct::Union
|
16
|
+
UnionExt = TypeStruct::UnionExt
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "forwardable"
|
2
|
+
|
3
|
+
class TypeStruct
|
4
|
+
class Union
|
5
|
+
extend Forwardable
|
6
|
+
def_delegators :@classes, :each
|
7
|
+
include Enumerable
|
8
|
+
def initialize(*classes)
|
9
|
+
@classes = classes
|
10
|
+
end
|
11
|
+
|
12
|
+
def |(other)
|
13
|
+
Union.new(*@classes, other)
|
14
|
+
end
|
15
|
+
|
16
|
+
def ===(other)
|
17
|
+
@classes.any? { |c| c === other }
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_s
|
21
|
+
"#<#{self.class} #{@classes.join('|')}>"
|
22
|
+
end
|
23
|
+
alias inspect to_s
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "type_struct/ext"
|
2
|
+
|
3
|
+
module UnionTest
|
4
|
+
U = Union.new(TrueClass, FalseClass)
|
5
|
+
def test_union(t)
|
6
|
+
unless Union === U
|
7
|
+
t.error("union error")
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_or(t)
|
12
|
+
if U === nil
|
13
|
+
t.error("nil")
|
14
|
+
end
|
15
|
+
|
16
|
+
n = U | NilClass
|
17
|
+
if U === nil
|
18
|
+
t.error("nil")
|
19
|
+
end
|
20
|
+
unless n === nil
|
21
|
+
t.error("nil")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_equal(t)
|
26
|
+
unless U === true
|
27
|
+
t.error("union error")
|
28
|
+
end
|
29
|
+
unless U === false
|
30
|
+
t.error("union error")
|
31
|
+
end
|
32
|
+
if U === nil
|
33
|
+
t.error("union error")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
using UnionExt
|
38
|
+
|
39
|
+
def test_class_or(t)
|
40
|
+
u = TrueClass | FalseClass
|
41
|
+
unless u === true
|
42
|
+
t.error("error")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/type_struct/version.rb
CHANGED
data/lib/type_struct_test.rb
CHANGED
@@ -1,26 +1,161 @@
|
|
1
|
-
require
|
1
|
+
require "type_struct"
|
2
|
+
require "type_struct/ext"
|
2
3
|
|
3
4
|
module TypeStructTest
|
5
|
+
using UnionExt
|
6
|
+
|
4
7
|
class Dummy < TypeStruct.new(
|
5
8
|
str: String,
|
6
9
|
num: Integer,
|
7
10
|
reg: /abc/,
|
11
|
+
ary: ArrayOf.new(Integer | Float) | NilClass,
|
8
12
|
any: Object,
|
9
13
|
); end
|
10
14
|
|
15
|
+
class Quux < Struct.new(:q)
|
16
|
+
end
|
17
|
+
|
18
|
+
class Qux < TypeStruct.new(
|
19
|
+
quux1: Quux,
|
20
|
+
quux2: Quux,
|
21
|
+
); end
|
22
|
+
|
23
|
+
class Bar < TypeStruct.new(
|
24
|
+
baz: ArrayOf.new(Integer | NilClass),
|
25
|
+
); end
|
26
|
+
|
27
|
+
class Foo < TypeStruct.new(
|
28
|
+
nil: NilClass,
|
29
|
+
bar: Bar,
|
30
|
+
); end
|
31
|
+
|
32
|
+
BoolClass = TrueClass | FalseClass
|
33
|
+
C = TypeStruct.new(
|
34
|
+
a: ArrayOf.new(BoolClass),
|
35
|
+
)
|
36
|
+
B = TypeStruct.new(
|
37
|
+
a: Integer,
|
38
|
+
b: BoolClass,
|
39
|
+
c: ArrayOf.new(Integer),
|
40
|
+
d: ArrayOf.new(BoolClass),
|
41
|
+
e: C,
|
42
|
+
)
|
43
|
+
A = TypeStruct.new(
|
44
|
+
a: ArrayOf.new(Integer),
|
45
|
+
b: ArrayOf.new(BoolClass),
|
46
|
+
c: BoolClass,
|
47
|
+
d: B,
|
48
|
+
e: ArrayOf.new(B),
|
49
|
+
)
|
50
|
+
|
51
|
+
def test_s_from_hash_a(t)
|
52
|
+
a = A.from_hash(
|
53
|
+
a: [1, 2, 3],
|
54
|
+
b: [false, true, false],
|
55
|
+
c: false,
|
56
|
+
d: { a: 1, b: false, c: [1, 2, 3], d: [false], e: { a: [true] } },
|
57
|
+
e: [
|
58
|
+
{ a: 1, b: false, c: [1, 2, 3], d: [false], e: { a: [true] } },
|
59
|
+
{ a: 2, b: true, c: [1, 2, 3], d: [false], e: { a: [true] } },
|
60
|
+
{ a: 3, b: true, c: [1, 2, 3], d: [false], e: { a: [true] } },
|
61
|
+
],
|
62
|
+
)
|
63
|
+
unless A === a
|
64
|
+
t.error("failed")
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_s_from_hash(t)
|
69
|
+
foo = Foo.from_hash(bar: { baz: [1, 2, 3] })
|
70
|
+
unless Foo === foo
|
71
|
+
t.error("return value was break")
|
72
|
+
end
|
73
|
+
|
74
|
+
foo = Foo.from_hash("bar" => { "baz" => [1, 2, 3] })
|
75
|
+
unless Foo === foo
|
76
|
+
t.error("return value was break")
|
77
|
+
end
|
78
|
+
|
79
|
+
begin
|
80
|
+
Foo.from_hash(bar: { baz: [1, 2, 3] }, nil: 1)
|
81
|
+
rescue TypeError
|
82
|
+
else
|
83
|
+
t.error("Bar.qux is not able to nil but accepted")
|
84
|
+
end
|
85
|
+
|
86
|
+
foo = Foo.from_hash(bar: { baz: [1, 2, 3] }, nil: nil)
|
87
|
+
unless TypeStruct === foo
|
88
|
+
t.error("return value type was break")
|
89
|
+
end
|
90
|
+
unless Foo === foo
|
91
|
+
t.error("return value type was break")
|
92
|
+
end
|
93
|
+
|
94
|
+
begin
|
95
|
+
Foo.from_hash(bar: { baz: [1, nil, 3] })
|
96
|
+
rescue => e
|
97
|
+
t.error("Bar.baz is able to nil but raise error #{e.class}: #{e.message}")
|
98
|
+
end
|
99
|
+
|
100
|
+
begin
|
101
|
+
Foo.from_hash(bar: { baz: nil })
|
102
|
+
rescue TypeError
|
103
|
+
else
|
104
|
+
t.error("Bar.baz is not able to nil")
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_s_from_hash_equal(t)
|
109
|
+
expect = Foo.new(bar: Bar.new(baz: [1, 2, 3]))
|
110
|
+
actual = Foo.from_hash(bar: { baz: [1, 2, 3] })
|
111
|
+
if expect != actual
|
112
|
+
t.error("expect #{expect} got #{actual}")
|
113
|
+
end
|
114
|
+
|
115
|
+
noteq = Foo.from_hash(bar: { baz: [1, 2, 4] })
|
116
|
+
if expect == noteq
|
117
|
+
t.error("expect #{expect} not equal #{noteq}")
|
118
|
+
end
|
119
|
+
|
120
|
+
noteq = Foo.from_hash(bar: { baz: [1, 2, nil] })
|
121
|
+
if expect == noteq
|
122
|
+
t.error("expect #{expect} not equal #{noteq}")
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_s_from_hash_with_struct(t)
|
127
|
+
qux = Qux.from_hash(quux1: { q: 1 }, quux2: { q: nil })
|
128
|
+
unless Qux === qux
|
129
|
+
t.error("return value was break")
|
130
|
+
end
|
131
|
+
unless Quux === qux.quux1
|
132
|
+
t.error("struct type was not applied")
|
133
|
+
end
|
134
|
+
unless 1 == qux.quux1.q
|
135
|
+
t.error("mapping failed #{qux.quux.q} != 1")
|
136
|
+
end
|
137
|
+
unless nil == qux.quux2.q
|
138
|
+
t.error("mapping failed #{qux.quux.q} != nil")
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
11
142
|
def test_s_members(t)
|
12
143
|
m = Dummy.members
|
13
|
-
expect =
|
144
|
+
expect = [:str, :num, :reg, :ary, :any]
|
14
145
|
unless m == expect
|
15
146
|
t.error("expect #{expect} got #{m}")
|
16
147
|
end
|
17
148
|
end
|
18
149
|
|
19
150
|
def test_s_type(t)
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
151
|
+
Dummy.definition.each do |k, v|
|
152
|
+
type = Dummy.type(k)
|
153
|
+
if v.respond_to?(:[])
|
154
|
+
if type != v[:type]
|
155
|
+
t.error("expect #{v[:type]} got #{type}")
|
156
|
+
end
|
157
|
+
elsif type != v
|
158
|
+
t.error("expect #{v} got #{type}")
|
24
159
|
end
|
25
160
|
end
|
26
161
|
end
|
@@ -37,14 +172,69 @@ module TypeStructTest
|
|
37
172
|
end
|
38
173
|
end
|
39
174
|
|
175
|
+
def test_arrayof_s_valid?(t)
|
176
|
+
unless A.valid?(:a, [1, 2, 3])
|
177
|
+
t.error("ArrayOf failed with [1,2,3]")
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
def test_arrayof_union_s_valid?(t)
|
182
|
+
unless A.valid?(:b, [false, true, false])
|
183
|
+
t.error("ArrayOf failed with Union")
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
def test_union_s_valid?(t)
|
188
|
+
unless A.valid?(:c, false)
|
189
|
+
t.error("Union is invalid with false")
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
def test_type_struct_s_valid?(t)
|
194
|
+
b = B.new(a: 1, b: false, c: [1, 2, 3], d: [false], e: C.new(a: [true]))
|
195
|
+
unless A.valid?(:d, b)
|
196
|
+
t.error("TypeStruct is invalid with #{b}")
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
def test_arrayof_type_struct_s_valid?(t)
|
201
|
+
ary_b = [
|
202
|
+
B.new(a: 1, b: false, c: [1, 2, 3], d: [false], e: C.new(a: [true])),
|
203
|
+
B.new(a: 2, b: true, c: [1, 2, 3], d: [false], e: C.new(a: [true])),
|
204
|
+
B.new(a: 3, b: false, c: [1, 2, 3], d: [false], e: C.new(a: [true])),
|
205
|
+
]
|
206
|
+
unless A.valid?(:e, ary_b)
|
207
|
+
t.error("ArrayOf with TypeStruct is invalid with #{ary_b}")
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
40
211
|
def test_initialize(t)
|
41
|
-
expects = {str: "aaa", num: 123, reg: "abc", any: [1, "bbb"]}
|
42
|
-
dummy = Dummy.new(str: "aaa", num: 123, reg: "abc", any: [1, "bbb"])
|
212
|
+
expects = { str: "aaa", num: 123, reg: "abc", ary: [1.1, 1], any: [1, "bbb"] }
|
213
|
+
dummy = Dummy.new(str: "aaa", num: 123, reg: "abc", ary: [1.1, 1], any: [1, "bbb"])
|
43
214
|
expects.each do |k, v|
|
44
215
|
unless dummy[k] == v
|
45
216
|
t.error("expect #{dummy[k]} got #{v}")
|
46
217
|
end
|
47
218
|
end
|
219
|
+
|
220
|
+
dummy2 = Dummy.new("str" => "aaa", "num" => 123, "reg" => "abc", "ary" => [1.1, 1], "any" => [1, "bbb"])
|
221
|
+
expects.each do |k, v|
|
222
|
+
unless dummy2[k] == v
|
223
|
+
t.error("expect #{dummy2[k]} got #{v}")
|
224
|
+
end
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
def test_eq(t)
|
229
|
+
dummy1 = Dummy.new(str: "aaa", num: 123, reg: "abc", ary: [1.1, 1], any: [1, "bbb"])
|
230
|
+
dummy2 = Dummy.new(str: "aaa", num: 123, reg: "abc", ary: [1.1, 1], any: [1, "bbb"])
|
231
|
+
dummy3 = Dummy.new(str: "bbb", num: 123, reg: "abc", ary: [1.1, 1], any: [1, "bbb"])
|
232
|
+
unless dummy1 == dummy2
|
233
|
+
t.error("members not equal")
|
234
|
+
end
|
235
|
+
unless dummy1 != dummy3
|
236
|
+
t.error("members equal")
|
237
|
+
end
|
48
238
|
end
|
49
239
|
|
50
240
|
def test_initialize_not_enough(t)
|
@@ -62,7 +252,7 @@ module TypeStructTest
|
|
62
252
|
end
|
63
253
|
|
64
254
|
def test_to_h(t)
|
65
|
-
expects = {str: "aaa", num: 123, reg: "abcde", any: [1, "bbb"]}
|
255
|
+
expects = { str: "aaa", num: 123, reg: "abcde", any: [1, "bbb"] }
|
66
256
|
dummy = Dummy.new(str: "aaa", num: 123, reg: "abcde", any: [1, "bbb"])
|
67
257
|
expects.each do |k, v|
|
68
258
|
unless dummy[k] == v
|
@@ -94,7 +284,7 @@ module TypeStructTest
|
|
94
284
|
|
95
285
|
def test_setter(t)
|
96
286
|
dummy = Dummy.new(str: "aaa", num: 123, reg: "abc", any: [1, "bbb"])
|
97
|
-
%i(str num reg).each do |k
|
287
|
+
%i(str num reg).each do |k|
|
98
288
|
_, err = go { dummy[k] = nil }
|
99
289
|
if err == nil
|
100
290
|
t.error("expect raise error when invalid value set")
|
@@ -128,7 +318,7 @@ module TypeStructTest
|
|
128
318
|
str: String,
|
129
319
|
reg: /exp/,
|
130
320
|
num: Integer,
|
131
|
-
any: Object
|
321
|
+
any: Object,
|
132
322
|
); end
|
133
323
|
|
134
324
|
def example_readme
|
metadata
CHANGED
@@ -1,55 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: type_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ksss
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-02-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.10'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.10'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '10.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rgot
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
description: Imitating static typed struct.
|
@@ -59,13 +59,17 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
-
- .gitignore
|
63
|
-
- .travis.yml
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
64
|
- Gemfile
|
65
65
|
- LICENSE.txt
|
66
66
|
- README.md
|
67
67
|
- Rakefile
|
68
68
|
- lib/type_struct.rb
|
69
|
+
- lib/type_struct/arrayof.rb
|
70
|
+
- lib/type_struct/ext.rb
|
71
|
+
- lib/type_struct/union.rb
|
72
|
+
- lib/type_struct/union_test.rb
|
69
73
|
- lib/type_struct/version.rb
|
70
74
|
- lib/type_struct_test.rb
|
71
75
|
- type_struct.gemspec
|
@@ -79,17 +83,17 @@ require_paths:
|
|
79
83
|
- lib
|
80
84
|
required_ruby_version: !ruby/object:Gem::Requirement
|
81
85
|
requirements:
|
82
|
-
- -
|
86
|
+
- - ">="
|
83
87
|
- !ruby/object:Gem::Version
|
84
88
|
version: '0'
|
85
89
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
90
|
requirements:
|
87
|
-
- -
|
91
|
+
- - ">="
|
88
92
|
- !ruby/object:Gem::Version
|
89
93
|
version: '0'
|
90
94
|
requirements: []
|
91
95
|
rubyforge_project:
|
92
|
-
rubygems_version: 2.
|
96
|
+
rubygems_version: 2.5.1
|
93
97
|
signing_key:
|
94
98
|
specification_version: 4
|
95
99
|
summary: Imitating static typed struct.
|