striuct 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/History.rdoc +12 -0
- data/LICENSE +2 -2
- data/Manifest.txt +34 -24
- data/README.md +34 -5
- data/example/README.rb +20 -1
- data/lib/striuct/classmethods/README.md +5 -0
- data/lib/striuct/classmethods/adjustment.rb +5 -3
- data/lib/striuct/classmethods/default.rb +2 -2
- data/lib/striuct/classmethods/enum.rb +3 -3
- data/lib/striuct/classmethods/inner.rb +28 -49
- data/lib/striuct/classmethods/named.rb +22 -2
- data/lib/striuct/classmethods/object.rb +35 -5
- data/lib/striuct/instancemethods/README.md +5 -0
- data/lib/striuct/instancemethods/assign.rb +1 -1
- data/lib/striuct/instancemethods/cast.rb +35 -0
- data/lib/striuct/instancemethods/compare.rb +4 -5
- data/lib/striuct/instancemethods/delegate_class_methods.rb +12 -4
- data/lib/striuct/instancemethods/hashy.rb +0 -11
- data/lib/striuct/instancemethods/keyvalidatable.rb +14 -0
- data/lib/striuct/instancemethods/lock.rb +15 -4
- data/lib/striuct/instancemethods/object.rb +11 -6
- data/lib/striuct/instancemethods/requirements.rb +2 -2
- data/lib/striuct/instancemethods/subscript.rb +1 -1
- data/lib/striuct/instancemethods/values.rb +1 -8
- data/lib/striuct/singleton_class.rb +9 -36
- data/lib/striuct/version.rb +1 -1
- data/striuct.gemspec +4 -4
- data/test/{test_striuct_define.rb → test_striuct-singleton_class-define.rb} +1 -1
- data/test/test_striuct-subclass-class-cloning.rb +21 -0
- data/test/{test_striuct_subclass_classlock.rb → test_striuct-subclass-class-close.rb} +2 -2
- data/test/{test_striuct_subclass_classmethods.rb → test_striuct-subclass-class-constructor.rb} +15 -20
- data/test/test_striuct-subclass-class-freeze.rb +36 -0
- data/test/{test_striuct_subclass_inheritable.rb → test_striuct-subclass-class-inheritable.rb} +1 -1
- data/test/test_striuct-subclass-class-macro.rb +13 -0
- data/test/{test_striuct_subclass_safetynaming.rb → test_striuct-subclass-class-safety_naming.rb} +1 -1
- data/test/test_striuct-subclass-class-validation.rb +26 -0
- data/test/{test_striuct_subclass_alias_member.rb → test_striuct-subclass-feature-alias_member.rb} +1 -1
- data/test/{test_striuct_subclass_to_struct.rb → test_striuct-subclass-feature-to_struct.rb} +1 -1
- data/test/test_striuct-subclass-instance-accessor.rb +137 -0
- data/test/{test_striuct_subclass_adjuster.rb → test_striuct-subclass-instance-adjuster.rb} +28 -28
- data/test/{test_striuct_subclass_assign.rb → test_striuct-subclass-instance-assign.rb} +1 -1
- data/test/test_striuct-subclass-instance-basic.rb +32 -0
- data/test/test_striuct-subclass-instance-cloning.rb +23 -0
- data/test/test_striuct-subclass-instance-compare.rb +52 -0
- data/test/{test_striuct_subclass_default_value.rb → test_striuct-subclass-instance-default_value.rb} +2 -2
- data/test/test_striuct-subclass-instance-enum.rb +93 -0
- data/test/{test_striuct_subclass_freeze.rb → test_striuct-subclass-instance-freeze.rb} +1 -1
- data/test/{test_striuct_subclass_hashlike.rb → test_striuct-subclass-instance-hashlike.rb} +1 -1
- data/test/test_striuct-subclass-instance-keyvalidatable.rb +25 -0
- data/test/{test_striuct_subclass_lock.rb → test_striuct-subclass-instance-lock.rb} +1 -1
- data/test/test_striuct-subclass-instance-to_s_family.rb +25 -0
- data/test/{test_striuct_subclass_functional_condition.rb → test_striuct-subclass-instance-validation_functional_condition.rb} +0 -0
- data/test/{test_striuct_subclass_inference.rb → test_striuct-subclass-instance-validation_inference.rb} +1 -1
- data/test/{test_striuct_subclass_specific_conditions.rb → test_striuct-subclass-instance-validation_specific_conditions.rb} +1 -1
- data/test/{test_striuct_subclass_getter_validation.rb → test_striuct-subclass-instance-validation_with_getter.rb} +1 -1
- data/test/test_striuct-subclass-instance_names.rb +18 -0
- data/test/test_striuct-version.rb +11 -0
- metadata +68 -50
- data/lib/striuct/instancemethods/singleton_class.rb +0 -22
- data/lib/striuct/instancemethods/to_struct.rb +0 -12
- data/test/test_striuct.rb +0 -24
- data/test/test_striuct_subclass_cloning.rb +0 -36
- data/test/test_striuct_subclass_for_pairs.rb +0 -20
- data/test/test_striuct_subclass_instance.rb +0 -289
- data/test/test_striuct_version.rb +0 -9
@@ -1,289 +0,0 @@
|
|
1
|
-
require_relative 'helper'
|
2
|
-
|
3
|
-
class TestStriuctSubclassInstance1 < Test::Unit::TestCase
|
4
|
-
class User < Striuct.new
|
5
|
-
member :id, Integer
|
6
|
-
member :last_name, /\A\w+\z/
|
7
|
-
member :family_name, /\A\w+\z/
|
8
|
-
member :address, /\A((\w+) ?)+\z/
|
9
|
-
member :age, ->age{(20..140).include? age}
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_setter
|
13
|
-
user = User.new
|
14
|
-
user[:last_name] = 'foo'
|
15
|
-
assert_equal user.last_name, 'foo'
|
16
|
-
user.last_name = 'bar'
|
17
|
-
assert_equal user[:last_name], 'bar'
|
18
|
-
|
19
|
-
assert_raises Validation::InvalidWritingError do
|
20
|
-
user[:last_name] = 'foo s'
|
21
|
-
end
|
22
|
-
|
23
|
-
assert_raises Validation::InvalidWritingError do
|
24
|
-
User.new 'asda'
|
25
|
-
end
|
26
|
-
|
27
|
-
assert_raises Validation::InvalidWritingError do
|
28
|
-
user.age = 19
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_equal
|
33
|
-
user1 = User.new 11218, 'taro'
|
34
|
-
user2 = User.new 11218, 'taro'
|
35
|
-
assert_equal true, (user1 == user2)
|
36
|
-
user2.last_name = 'ichiro'
|
37
|
-
assert_equal false, (user1 == user2)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
class TestStriuctSubclassInstance2 < Test::Unit::TestCase
|
42
|
-
class User < Striuct.new
|
43
|
-
member :id, Integer
|
44
|
-
member :last_name, /\A\w+\z/
|
45
|
-
member :family_name, /\A\w+\z/
|
46
|
-
member :address, /\A((\w+) ?)+\z/
|
47
|
-
member :age, ->age{(20..140).include? age}
|
48
|
-
end
|
49
|
-
|
50
|
-
def setup
|
51
|
-
@user = User.new 9999, 'taro', 'yamada', 'Tokyo Japan', 30
|
52
|
-
end
|
53
|
-
|
54
|
-
def test_reader
|
55
|
-
assert_equal @user[1], 'taro'
|
56
|
-
assert_equal @user[:last_name], 'taro'
|
57
|
-
assert_equal @user.last_name, 'taro'
|
58
|
-
end
|
59
|
-
|
60
|
-
def test_setter_pass
|
61
|
-
assert_equal((@user.id = 2139203509295), 2139203509295)
|
62
|
-
assert_equal @user.id, 2139203509295
|
63
|
-
assert_equal((@user.last_name = 'jiro'), 'jiro')
|
64
|
-
assert_equal @user.last_name, 'jiro'
|
65
|
-
assert_equal((@user.age = 40), 40)
|
66
|
-
assert_equal @user.age, 40
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_setter_fail
|
70
|
-
assert_raises Validation::InvalidWritingError do
|
71
|
-
@user.id = 2139203509295.0
|
72
|
-
end
|
73
|
-
|
74
|
-
assert_raises Validation::InvalidWritingError do
|
75
|
-
@user.last_name = 'ignore name'
|
76
|
-
end
|
77
|
-
|
78
|
-
assert_raises Validation::InvalidWritingError do
|
79
|
-
@user.age = 19
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
def test_strict?
|
84
|
-
assert_same @user.sufficient?(:last_name), true
|
85
|
-
assert_same @user.strict?, true
|
86
|
-
assert_same @user.sufficient?(:last_name), true
|
87
|
-
@user.last_name.clear
|
88
|
-
assert_same @user.sufficient?(:last_name), false
|
89
|
-
assert_same @user.strict?, false
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
class TestStriuctSubclassInstance3 < Test::Unit::TestCase
|
94
|
-
class User < Striuct.new
|
95
|
-
member :id, Integer
|
96
|
-
member :last_name, /\A\w+\z/
|
97
|
-
member :family_name, /\A\w+\z/
|
98
|
-
member :address, /\A((\w+) ?)+\z/
|
99
|
-
member :age, ->age{(20..140).include? age}
|
100
|
-
end
|
101
|
-
|
102
|
-
def setup
|
103
|
-
@user = User.new 9999, 'taro', 'yamada', 'Tokyo Japan', 30
|
104
|
-
@user2 = User.new 9999, 'taro', 'yamada', 'Tokyo Japan', 30
|
105
|
-
end
|
106
|
-
|
107
|
-
def test_to_s
|
108
|
-
/\b(taro)\b/ =~ @user.to_s
|
109
|
-
assert_equal 'taro', $1
|
110
|
-
end
|
111
|
-
|
112
|
-
def test_inspect
|
113
|
-
/\b(taro)\b/ =~ @user.inspect
|
114
|
-
assert_equal 'taro', $1
|
115
|
-
end
|
116
|
-
|
117
|
-
def test_members
|
118
|
-
assert_equal @user.members, [:id, :last_name, :family_name, :address, :age]
|
119
|
-
end
|
120
|
-
|
121
|
-
def test_delegate_class_method
|
122
|
-
assert_equal @user.members, User.members
|
123
|
-
assert_equal @user.size, User.size
|
124
|
-
assert_equal @user.member?(:age), User.member?(:age)
|
125
|
-
end
|
126
|
-
|
127
|
-
def test_each
|
128
|
-
assert_same @user, @user.each{}
|
129
|
-
assert_kind_of Enumerator, enum = @user.each
|
130
|
-
assert_equal enum.next, 9999
|
131
|
-
assert_equal enum.next, 'taro'
|
132
|
-
end
|
133
|
-
|
134
|
-
def test_each_member
|
135
|
-
assert_same @user, @user.each_member{}
|
136
|
-
assert_kind_of Enumerator, enum = @user.each_member
|
137
|
-
assert_equal :id, enum.next
|
138
|
-
assert_equal :last_name, enum.next
|
139
|
-
assert_equal :family_name, enum.next
|
140
|
-
assert_equal :address, enum.next
|
141
|
-
assert_equal :age, enum.next
|
142
|
-
assert_raises StopIteration do
|
143
|
-
enum.next
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
def test_each_index
|
148
|
-
assert_same @user, @user.each_index{}
|
149
|
-
assert_kind_of Enumerator, enum = @user.each_index
|
150
|
-
assert_equal 0, enum.next
|
151
|
-
assert_equal 1, enum.next
|
152
|
-
assert_equal 2, enum.next
|
153
|
-
assert_equal 3, enum.next
|
154
|
-
assert_equal 4, enum.next
|
155
|
-
assert_raises StopIteration do
|
156
|
-
enum.next
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
def test_each_with_index
|
161
|
-
assert_same @user, @user.each_with_index{}
|
162
|
-
assert_kind_of Enumerator, @user.each_with_index
|
163
|
-
|
164
|
-
r = []
|
165
|
-
@user.each_with_index do |value, index|
|
166
|
-
r << [value, index]
|
167
|
-
end
|
168
|
-
|
169
|
-
assert_equal [@user.each_value.to_a, @user.each_index.to_a].transpose, r
|
170
|
-
end
|
171
|
-
|
172
|
-
def test_each_member_with_index
|
173
|
-
assert_same @user, @user.each_member_with_index{}
|
174
|
-
assert_kind_of Enumerator, @user.each_member_with_index
|
175
|
-
|
176
|
-
r = []
|
177
|
-
@user.each_member_with_index do |name, index|
|
178
|
-
r << [name, index]
|
179
|
-
end
|
180
|
-
|
181
|
-
assert_equal [@user.each_key.to_a, @user.each_index.to_a].transpose, r
|
182
|
-
end
|
183
|
-
|
184
|
-
def test_values
|
185
|
-
assert_equal @user.values, [9999, 'taro', 'yamada', 'Tokyo Japan', 30]
|
186
|
-
end
|
187
|
-
|
188
|
-
def test_values_at
|
189
|
-
assert_equal(@user.values_at(4, 0, (2..4)), [30, 9999, 'yamada', 'Tokyo Japan', 30])
|
190
|
-
end
|
191
|
-
|
192
|
-
def test_hash
|
193
|
-
assert_kind_of Integer, @user.hash
|
194
|
-
assert_equal @user.hash, @user2.hash
|
195
|
-
end
|
196
|
-
|
197
|
-
def test_eql?
|
198
|
-
assert_equal true, @user.eql?(@user2)
|
199
|
-
assert_equal true, @user2.eql?(@user)
|
200
|
-
assert_equal false, @user.eql?(User.new 9999, 'taro', 'yamada', 'Tokyo Japan', 31)
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
|
205
|
-
class TestStriuctSubclassInstance4 < Test::Unit::TestCase
|
206
|
-
class Sth < Striuct.new
|
207
|
-
member :bool, OR(true, false)
|
208
|
-
member :sth
|
209
|
-
protect_level :struct
|
210
|
-
member :lambda, OR(->v{v % 3 == 2}, ->v{v.kind_of? Float})
|
211
|
-
end
|
212
|
-
|
213
|
-
def setup
|
214
|
-
@sth = Sth.new
|
215
|
-
end
|
216
|
-
|
217
|
-
def test_accessor
|
218
|
-
@sth.bool = true
|
219
|
-
assert_same true, @sth.bool
|
220
|
-
@sth.bool = false
|
221
|
-
assert_same false, @sth.bool
|
222
|
-
|
223
|
-
assert_raises Validation::InvalidWritingError do
|
224
|
-
@sth.bool = nil
|
225
|
-
end
|
226
|
-
|
227
|
-
@sth.sth = 1
|
228
|
-
assert_same 1, @sth.sth
|
229
|
-
|
230
|
-
@sth.sth = 'String'
|
231
|
-
assert_equal 'String', @sth.sth
|
232
|
-
|
233
|
-
@sth.sth = Class.class
|
234
|
-
assert_same Class.class, @sth.sth
|
235
|
-
|
236
|
-
assert_raises Validation::InvalidWritingError do
|
237
|
-
@sth.lambda = 9
|
238
|
-
end
|
239
|
-
|
240
|
-
assert_raises Validation::InvalidWritingError do
|
241
|
-
@sth.lambda = 7
|
242
|
-
end
|
243
|
-
|
244
|
-
@sth.lambda = 8
|
245
|
-
assert_same 8, @sth.lambda
|
246
|
-
|
247
|
-
@sth.lambda = 9.0
|
248
|
-
assert_equal 9.0, @sth.lambda
|
249
|
-
end
|
250
|
-
end
|
251
|
-
|
252
|
-
class TestStriuctHashKeyable < Test::Unit::TestCase
|
253
|
-
|
254
|
-
Sth = Striuct.new :foo, :bar, :hoge
|
255
|
-
|
256
|
-
def test_hash
|
257
|
-
sth1 = Sth[hoge: 7, foo: 8]
|
258
|
-
sth2 = Sth[hoge: 7, foo: 8]
|
259
|
-
assert_equal true, sth1.eql?(sth2)
|
260
|
-
assert_equal true, sth2.eql?(sth1)
|
261
|
-
assert_equal sth1.hash, sth2.hash
|
262
|
-
assert_equal true, {sth1 => 1}.has_key?(sth2)
|
263
|
-
assert_equal true, {sth2 => 1}.has_key?(sth1)
|
264
|
-
assert_equal 1, {sth1 => 1}[sth2]
|
265
|
-
assert_equal 1, {sth2 => 1}[sth1]
|
266
|
-
end
|
267
|
-
|
268
|
-
end
|
269
|
-
|
270
|
-
class TestStriuctEachPair < Test::Unit::TestCase
|
271
|
-
|
272
|
-
Sth = Striuct.define do
|
273
|
-
member :name
|
274
|
-
member :age
|
275
|
-
end
|
276
|
-
|
277
|
-
def test_each_pair_with_index
|
278
|
-
sth = Sth.new 'a', 10
|
279
|
-
assert_same sth, sth.each_pair_with_index{}
|
280
|
-
|
281
|
-
enum = sth.each_pair_with_index
|
282
|
-
assert_equal [:name, 'a', 0], enum.next
|
283
|
-
assert_equal [:age, 10, 1], enum.next
|
284
|
-
assert_raises StopIteration do
|
285
|
-
enum.next
|
286
|
-
end
|
287
|
-
end
|
288
|
-
|
289
|
-
end
|