striuct 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +32 -0
- data/.travis.yml +6 -0
- data/Gemfile +12 -0
- data/History.rdoc +5 -0
- data/Manifest.txt +53 -25
- data/README.md +97 -0
- data/Rakefile +7 -21
- data/examples/README.rb +25 -0
- data/{example → examples}/example1.rb +1 -1
- data/{example → examples}/example2.rb +1 -1
- data/{example → examples}/see_trace.rb +4 -0
- data/lib/striuct/{containable/eigen → classmethods}/basic.rb +13 -5
- data/lib/striuct/classmethods/constants.rb +23 -0
- data/lib/striuct/{containable/eigen → classmethods}/constructor.rb +20 -14
- data/lib/striuct/{containable/eigen → classmethods}/handy.rb +19 -20
- data/lib/striuct/{containable/eigen → classmethods}/inner.rb +10 -32
- data/lib/striuct/{containable/eigen → classmethods}/macro.rb +37 -7
- data/lib/striuct/classmethods/requiremnets.rb +7 -0
- data/lib/striuct/{containable/eigen → classmethods}/safety.rb +6 -6
- data/lib/striuct/classmethods.rb +1 -0
- data/lib/striuct/{containable → instancemethods}/basic.rb +6 -6
- data/lib/striuct/{containable → instancemethods}/handy.rb +3 -3
- data/lib/striuct/{containable → instancemethods}/hashlike.rb +12 -6
- data/lib/striuct/{containable → instancemethods}/inner.rb +9 -8
- data/lib/striuct/instancemethods/requirements.rb +6 -0
- data/lib/striuct/{containable → instancemethods}/safety.rb +2 -2
- data/lib/striuct/instancemethods/singleton_class.rb +19 -0
- data/lib/striuct/instancemethods.rb +1 -0
- data/lib/striuct/requirements.rb +6 -0
- data/lib/striuct/singleton_class.rb +93 -0
- data/lib/striuct/specificcontainer.rb +17 -0
- data/lib/striuct/structs.rb +7 -0
- data/lib/striuct/version.rb +3 -2
- data/lib/striuct.rb +4 -9
- data/striuct.gemspec +24 -0
- data/test/{test_helper.rb → helper.rb} +3 -0
- data/test/test_striuct.rb +23 -1409
- data/test/test_striuct_define.rb +19 -0
- data/test/test_striuct_subclass_adjuster.rb +95 -0
- data/test/test_striuct_subclass_alias_member.rb +54 -0
- data/test/test_striuct_subclass_assign.rb +30 -0
- data/test/test_striuct_subclass_classlock.rb +36 -0
- data/test/test_striuct_subclass_classmethods.rb +87 -0
- data/test/test_striuct_subclass_cloning.rb +36 -0
- data/test/test_striuct_subclass_default_value.rb +129 -0
- data/test/test_striuct_subclass_for_pairs.rb +13 -0
- data/test/test_striuct_subclass_freeze.rb +19 -0
- data/test/test_striuct_subclass_functional_condition.rb +51 -0
- data/test/test_striuct_subclass_getter_validation.rb +33 -0
- data/test/test_striuct_subclass_hashlike.rb +153 -0
- data/test/test_striuct_subclass_inference.rb +51 -0
- data/test/test_striuct_subclass_inheritable.rb +58 -0
- data/test/test_striuct_subclass_instance.rb +289 -0
- data/test/test_striuct_subclass_lock.rb +40 -0
- data/test/test_striuct_subclass_safetynaming.rb +72 -0
- data/test/test_striuct_subclass_specific_conditions.rb +247 -0
- data/test/test_striuct_subclass_to_struct.rb +25 -0
- data/test/test_striuct_version.rb +9 -0
- metadata +106 -68
- data/.gemtest +0 -0
- data/README.rdoc +0 -63
- data/example/example.rb +0 -17
- data/lib/striuct/abstract.rb +0 -74
- data/lib/striuct/containable/classutil.rb +0 -22
- data/lib/striuct/containable/eigen.rb +0 -29
- data/lib/striuct/containable.rb +0 -38
- /data/{example/benchmarks.rb → benchmarks/basics.rb} +0 -0
- /data/{example → examples}/example.old.rdoc +0 -0
data/test/test_striuct.rb
CHANGED
@@ -1,1410 +1,24 @@
|
|
1
1
|
$VERBOSE = true
|
2
|
-
require_relative '
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
klass = Striuct.new :foo do
|
27
|
-
member :var
|
28
|
-
end
|
29
|
-
|
30
|
-
assert_equal klass.members, [:foo, :var]
|
31
|
-
assert_equal User.members, [:id, :last_name, :family_name, :address, :age]
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_define
|
35
|
-
user = User2.define{|r|r.age = 1; r.name = 'a'}
|
36
|
-
assert_same 1, user.age
|
37
|
-
|
38
|
-
assert_raises RuntimeError do
|
39
|
-
user.age = 1
|
40
|
-
end
|
41
|
-
|
42
|
-
user = User2.define{|r|r.age = 1; r.name = 'a'}
|
43
|
-
assert_same 1, user.age
|
44
|
-
assert_same true, user.lock?
|
45
|
-
assert_same false, user.frozen?
|
46
|
-
|
47
|
-
assert_raises RuntimeError do
|
48
|
-
User2.define{|r|r.age = 1}
|
49
|
-
end
|
50
|
-
|
51
|
-
user = User2.define(lock: true){|r|r.age = 1; r.name = 'a'}
|
52
|
-
assert_same 1, user.age
|
53
|
-
assert_same true, user.lock?
|
54
|
-
user = User2.define(lock: false){|r|r.age = 1; r.name = 'a'}
|
55
|
-
assert_same false, user.lock?
|
56
|
-
assert_equal true, user.strict?
|
57
|
-
|
58
|
-
assert_raises Validation::InvalidWritingError do
|
59
|
-
User2.define{|r|r.age = 1; r.name = 'a'; r.name.clear}
|
60
|
-
end
|
61
|
-
|
62
|
-
user = User2.define(strict: false){|r|r.age = 1; r.name = 'a'; r.name.clear}
|
63
|
-
assert_equal '', user.name
|
64
|
-
assert_equal false, user.strict?
|
65
|
-
end
|
66
|
-
|
67
|
-
def test_restrict?
|
68
|
-
klass = Striuct.new :foo do
|
69
|
-
member :var, //
|
70
|
-
member :hoge
|
71
|
-
member :moge, nil
|
72
|
-
end
|
73
|
-
|
74
|
-
assert_equal false, klass.restrict?(:foo)
|
75
|
-
assert_equal true, klass.restrict?(:var)
|
76
|
-
assert_equal false, klass.restrict?(:hoge)
|
77
|
-
assert_equal true, klass.restrict?(:moge)
|
78
|
-
end
|
79
|
-
|
80
|
-
def test_add_members
|
81
|
-
klass = Striuct.new :foo do
|
82
|
-
add_members :aaa, 'bbb', :ccc
|
83
|
-
end
|
84
|
-
|
85
|
-
assert_equal [:foo, :aaa, :bbb, :ccc], klass.members
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
class TestStriuctSubclassInstance1 < Test::Unit::TestCase
|
90
|
-
class User < Striuct.new
|
91
|
-
member :id, Integer
|
92
|
-
member :last_name, /\A\w+\z/
|
93
|
-
member :family_name, /\A\w+\z/
|
94
|
-
member :address, /\A((\w+) ?)+\z/
|
95
|
-
member :age, ->age{(20..140).include? age}
|
96
|
-
end
|
97
|
-
|
98
|
-
def test_setter
|
99
|
-
user = User.new
|
100
|
-
user[:last_name] = 'foo'
|
101
|
-
assert_equal user.last_name, 'foo'
|
102
|
-
user.last_name = 'bar'
|
103
|
-
assert_equal user[:last_name], 'bar'
|
104
|
-
|
105
|
-
assert_raises Validation::InvalidWritingError do
|
106
|
-
user[:last_name] = 'foo s'
|
107
|
-
end
|
108
|
-
|
109
|
-
assert_raises Validation::InvalidWritingError do
|
110
|
-
User.new 'asda'
|
111
|
-
end
|
112
|
-
|
113
|
-
assert_raises Validation::InvalidWritingError do
|
114
|
-
user.age = 19
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
def test_equal
|
119
|
-
user1 = User.new 11218, 'taro'
|
120
|
-
user2 = User.new 11218, 'taro'
|
121
|
-
assert_equal true, (user1 == user2)
|
122
|
-
user2.last_name = 'ichiro'
|
123
|
-
assert_equal false, (user1 == user2)
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
class TestStriuctSubclassInstance2 < Test::Unit::TestCase
|
128
|
-
class User < Striuct.new
|
129
|
-
member :id, Integer
|
130
|
-
member :last_name, /\A\w+\z/
|
131
|
-
member :family_name, /\A\w+\z/
|
132
|
-
member :address, /\A((\w+) ?)+\z/
|
133
|
-
member :age, ->age{(20..140).include? age}
|
134
|
-
end
|
135
|
-
|
136
|
-
def setup
|
137
|
-
@user = User.new 9999, 'taro', 'yamada', 'Tokyo Japan', 30
|
138
|
-
end
|
139
|
-
|
140
|
-
def test_reader
|
141
|
-
assert_equal @user[1], 'taro'
|
142
|
-
assert_equal @user[:last_name], 'taro'
|
143
|
-
assert_equal @user.last_name, 'taro'
|
144
|
-
end
|
145
|
-
|
146
|
-
def test_setter_pass
|
147
|
-
assert_equal (@user.id = 2139203509295), 2139203509295
|
148
|
-
assert_equal @user.id, 2139203509295
|
149
|
-
assert_equal (@user.last_name = 'jiro'), 'jiro'
|
150
|
-
assert_equal @user.last_name, 'jiro'
|
151
|
-
assert_equal (@user.age = 40), 40
|
152
|
-
assert_equal @user.age, 40
|
153
|
-
end
|
154
|
-
|
155
|
-
def test_setter_fail
|
156
|
-
assert_raises Validation::InvalidWritingError do
|
157
|
-
@user.id = 2139203509295.0
|
158
|
-
end
|
159
|
-
|
160
|
-
assert_raises Validation::InvalidWritingError do
|
161
|
-
@user.last_name = 'ignore name'
|
162
|
-
end
|
163
|
-
|
164
|
-
assert_raises Validation::InvalidWritingError do
|
165
|
-
@user.age = 19
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
def test_strict?
|
170
|
-
assert_same @user.sufficient?(:last_name), true
|
171
|
-
assert_same @user.strict?, true
|
172
|
-
assert_same @user.sufficient?(:last_name), true
|
173
|
-
@user.last_name.clear
|
174
|
-
assert_same @user.sufficient?(:last_name), false
|
175
|
-
assert_same @user.strict?, false
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
|
-
class TestStriuctSubclassInstance3 < Test::Unit::TestCase
|
180
|
-
class User < Striuct.new
|
181
|
-
member :id, Integer
|
182
|
-
member :last_name, /\A\w+\z/
|
183
|
-
member :family_name, /\A\w+\z/
|
184
|
-
member :address, /\A((\w+) ?)+\z/
|
185
|
-
member :age, ->age{(20..140).include? age}
|
186
|
-
end
|
187
|
-
|
188
|
-
def setup
|
189
|
-
@user = User.new 9999, 'taro', 'yamada', 'Tokyo Japan', 30
|
190
|
-
@user2 = User.new 9999, 'taro', 'yamada', 'Tokyo Japan', 30
|
191
|
-
end
|
192
|
-
|
193
|
-
def test_to_s
|
194
|
-
/\b(taro)\b/ =~ @user.to_s
|
195
|
-
assert_equal 'taro', $1
|
196
|
-
end
|
197
|
-
|
198
|
-
def test_inspect
|
199
|
-
/\b(taro)\b/ =~ @user.inspect
|
200
|
-
assert_equal 'taro', $1
|
201
|
-
end
|
202
|
-
|
203
|
-
def test_members
|
204
|
-
assert_equal @user.members, [:id, :last_name, :family_name, :address, :age]
|
205
|
-
end
|
206
|
-
|
207
|
-
def test_delegate_class_method
|
208
|
-
assert_equal @user.members, User.members
|
209
|
-
assert_equal @user.size, User.size
|
210
|
-
assert_equal @user.member?(:age), User.member?(:age)
|
211
|
-
end
|
212
|
-
|
213
|
-
def test_each
|
214
|
-
assert_same @user, @user.each{}
|
215
|
-
assert_kind_of Enumerator, enum = @user.each
|
216
|
-
assert_equal enum.next, 9999
|
217
|
-
assert_equal enum.next, 'taro'
|
218
|
-
end
|
219
|
-
|
220
|
-
def test_each_member
|
221
|
-
assert_same @user, @user.each_member{}
|
222
|
-
assert_kind_of Enumerator, enum = @user.each_member
|
223
|
-
assert_equal :id, enum.next
|
224
|
-
assert_equal :last_name, enum.next
|
225
|
-
assert_equal :family_name, enum.next
|
226
|
-
assert_equal :address, enum.next
|
227
|
-
assert_equal :age, enum.next
|
228
|
-
assert_raises StopIteration do
|
229
|
-
enum.next
|
230
|
-
end
|
231
|
-
end
|
232
|
-
|
233
|
-
def test_each_index
|
234
|
-
assert_same @user, @user.each_index{}
|
235
|
-
assert_kind_of Enumerator, enum = @user.each_index
|
236
|
-
assert_equal 0, enum.next
|
237
|
-
assert_equal 1, enum.next
|
238
|
-
assert_equal 2, enum.next
|
239
|
-
assert_equal 3, enum.next
|
240
|
-
assert_equal 4, enum.next
|
241
|
-
assert_raises StopIteration do
|
242
|
-
enum.next
|
243
|
-
end
|
244
|
-
end
|
245
|
-
|
246
|
-
def test_each_with_index
|
247
|
-
assert_same @user, @user.each_with_index{}
|
248
|
-
assert_kind_of Enumerator, enum = @user.each_with_index
|
249
|
-
|
250
|
-
r = []
|
251
|
-
@user.each_with_index do |value, index|
|
252
|
-
r << [value, index]
|
253
|
-
end
|
254
|
-
|
255
|
-
assert_equal [@user.each_value.to_a, @user.each_index.to_a].transpose, r
|
256
|
-
end
|
257
|
-
|
258
|
-
def test_each_member_with_index
|
259
|
-
assert_same @user, @user.each_member_with_index{}
|
260
|
-
assert_kind_of Enumerator, enum = @user.each_member_with_index
|
261
|
-
|
262
|
-
r = []
|
263
|
-
@user.each_member_with_index do |name, index|
|
264
|
-
r << [name, index]
|
265
|
-
end
|
266
|
-
|
267
|
-
assert_equal [@user.each_key.to_a, @user.each_index.to_a].transpose, r
|
268
|
-
end
|
269
|
-
|
270
|
-
def test_values
|
271
|
-
assert_equal @user.values, [9999, 'taro', 'yamada', 'Tokyo Japan', 30]
|
272
|
-
end
|
273
|
-
|
274
|
-
def test_values_at
|
275
|
-
assert_equal(@user.values_at(4, 0, (2..4)), [30, 9999, 'yamada', 'Tokyo Japan', 30])
|
276
|
-
end
|
277
|
-
|
278
|
-
def test_hash
|
279
|
-
assert_kind_of Integer, @user.hash
|
280
|
-
assert_equal @user.hash, @user2.hash
|
281
|
-
end
|
282
|
-
|
283
|
-
def test_eql?
|
284
|
-
assert_equal true, @user.eql?(@user2)
|
285
|
-
assert_equal true, @user2.eql?(@user)
|
286
|
-
assert_equal false, @user.eql?(User.new 9999, 'taro', 'yamada', 'Tokyo Japan', 31)
|
287
|
-
end
|
288
|
-
end
|
289
|
-
|
290
|
-
|
291
|
-
class TestStriuctSubclassInstance4 < Test::Unit::TestCase
|
292
|
-
class Sth < Striuct.new
|
293
|
-
member :bool, OR(true, false)
|
294
|
-
member :sth
|
295
|
-
protect_level :struct
|
296
|
-
member :lambda, OR(->v{v % 3 == 2}, ->v{v.kind_of? Float})
|
297
|
-
end
|
298
|
-
|
299
|
-
def setup
|
300
|
-
@sth = Sth.new
|
301
|
-
end
|
302
|
-
|
303
|
-
def test_accessor
|
304
|
-
@sth.bool = true
|
305
|
-
assert_same true, @sth.bool
|
306
|
-
@sth.bool = false
|
307
|
-
assert_same false, @sth.bool
|
308
|
-
|
309
|
-
assert_raises Validation::InvalidWritingError do
|
310
|
-
@sth.bool = nil
|
311
|
-
end
|
312
|
-
|
313
|
-
@sth.sth = 1
|
314
|
-
assert_same 1, @sth.sth
|
315
|
-
|
316
|
-
@sth.sth = 'String'
|
317
|
-
assert_equal 'String', @sth.sth
|
318
|
-
|
319
|
-
@sth.sth = Class.class
|
320
|
-
assert_same Class.class, @sth.sth
|
321
|
-
|
322
|
-
assert_raises Validation::InvalidWritingError do
|
323
|
-
@sth.lambda = 9
|
324
|
-
end
|
325
|
-
|
326
|
-
assert_raises Validation::InvalidWritingError do
|
327
|
-
@sth.lambda = 7
|
328
|
-
end
|
329
|
-
|
330
|
-
@sth.lambda = 8
|
331
|
-
assert_same 8, @sth.lambda
|
332
|
-
|
333
|
-
@sth.lambda = 9.0
|
334
|
-
assert_equal 9.0, @sth.lambda
|
335
|
-
end
|
336
|
-
end
|
337
|
-
|
338
|
-
class TestStriuctCloning < Test::Unit::TestCase
|
339
|
-
class Sth < Striuct.new
|
340
|
-
member :sth
|
341
|
-
end
|
342
|
-
|
343
|
-
def setup
|
344
|
-
@sth = Sth.new
|
345
|
-
end
|
346
|
-
|
347
|
-
def test_clone
|
348
|
-
sth2 = @sth.dup
|
349
|
-
@sth.sth = 1
|
350
|
-
assert_equal false, (@sth.sth == sth2.sth)
|
351
|
-
|
352
|
-
sth2 = @sth.clone
|
353
|
-
@sth.sth = 2
|
354
|
-
assert_equal false, (@sth.sth == sth2.sth)
|
355
|
-
end
|
356
|
-
|
357
|
-
def test_class_dup
|
358
|
-
klass = Sth.dup
|
359
|
-
Sth.__send__ :member, :dummy1
|
360
|
-
assert_equal false, klass.member?(:dummy1)
|
361
|
-
end
|
362
|
-
|
363
|
-
def test_class_clone
|
364
|
-
klass = Sth.clone
|
365
|
-
Sth.__send__ :member, :dummy2
|
366
|
-
assert_equal false, klass.member?(:dummy2)
|
367
|
-
end
|
368
|
-
end
|
369
|
-
|
370
|
-
class TestStriuctProcedure < Test::Unit::TestCase
|
371
|
-
Sth = Striuct.new do
|
372
|
-
member :age, Numeric, &->arg{Integer arg}
|
373
|
-
end
|
374
|
-
|
375
|
-
def setup
|
376
|
-
@sth = Sth.new
|
377
|
-
assert_same nil, @sth.age
|
378
|
-
end
|
379
|
-
|
380
|
-
def test_procedure
|
381
|
-
@sth.age = 10
|
382
|
-
assert_same 10, @sth.age
|
383
|
-
@sth.age = 10.0
|
384
|
-
assert_same 10, @sth.age
|
385
|
-
|
386
|
-
assert_raises Validation::UnmanagebleError do
|
387
|
-
@sth.age = '10.0'
|
388
|
-
end
|
389
|
-
|
390
|
-
@sth.age = '10'
|
391
|
-
assert_same 10, @sth.age
|
392
|
-
end
|
393
|
-
end
|
394
|
-
|
395
|
-
class TestStriuctDefaultValue < Test::Unit::TestCase
|
396
|
-
Sth = Striuct.new do
|
397
|
-
member :lank, OR(Bignum, Fixnum)
|
398
|
-
default :lank, 1
|
399
|
-
end
|
400
|
-
|
401
|
-
def test_default
|
402
|
-
sth = Sth.new 2
|
403
|
-
assert_equal 2, sth.lank
|
404
|
-
sth = Sth.new
|
405
|
-
assert_equal 1, sth.lank
|
406
|
-
assert_equal true, sth.default?(:lank)
|
407
|
-
sth.lank = 2
|
408
|
-
assert_equal false, sth.default?(:lank)
|
409
|
-
end
|
410
|
-
|
411
|
-
def test_define_default
|
412
|
-
assert_raises NameError do
|
413
|
-
Sth.class_eval do
|
414
|
-
default :anything, 10
|
415
|
-
end
|
416
|
-
end
|
417
|
-
|
418
|
-
klass = Striuct.define do
|
419
|
-
member :lank2, Integer
|
420
|
-
default :lank2, '10'
|
421
|
-
end
|
422
|
-
|
423
|
-
assert_raises Validation::InvalidWritingError do
|
424
|
-
klass.new
|
425
|
-
end
|
426
|
-
|
427
|
-
scope = self
|
428
|
-
seef = nil
|
429
|
-
klass = Striuct.define do
|
430
|
-
member :lank, Integer
|
431
|
-
scope.assert_raises ArgumentError do
|
432
|
-
default :lank, '10', &->own, name{rand}
|
433
|
-
end
|
434
|
-
|
435
|
-
scope.assert_raises ArgumentError do
|
436
|
-
default :lank, '10', &->own{rand}
|
437
|
-
end
|
438
|
-
|
439
|
-
scope.assert_raises ArgumentError do
|
440
|
-
default :lank, '10', &->{rand}
|
441
|
-
end
|
442
|
-
|
443
|
-
default :lank, &->own, name{(seef = own); rand}
|
444
|
-
end
|
445
|
-
|
446
|
-
assert_raises Validation::InvalidWritingError do
|
447
|
-
klass.new
|
448
|
-
end
|
449
|
-
|
450
|
-
klass = Striuct.define do
|
451
|
-
member :lank, Integer
|
452
|
-
default :lank, &->own, name{(seef = own); 10 - name.length}
|
453
|
-
end
|
454
|
-
|
455
|
-
assert_equal 6, klass.new.lank
|
456
|
-
assert_equal seef, klass.new
|
457
|
-
end
|
458
|
-
end
|
459
|
-
|
460
|
-
class TestStriuctFunctionalCondition < Test::Unit::TestCase
|
461
|
-
Sthlambda = Striuct.new do
|
462
|
-
member :lank, ->lank{lanks.include? lank}
|
463
|
-
member :lanks
|
464
|
-
default :lanks, 1..30
|
465
|
-
end
|
466
|
-
|
467
|
-
def test_lambda
|
468
|
-
sth = Sthlambda.new
|
469
|
-
sth.lank = 2
|
470
|
-
assert_equal 2, sth.lank
|
471
|
-
|
472
|
-
assert_raises Validation::InvalidWritingError do
|
473
|
-
sth.lank = 31
|
474
|
-
end
|
475
|
-
end
|
476
|
-
|
477
|
-
max = 9
|
478
|
-
|
479
|
-
SthProc = Striuct.new do
|
480
|
-
member :lank, Proc.new{|n|(3..max) === n}
|
481
|
-
end
|
482
|
-
|
483
|
-
def test_Proc
|
484
|
-
sth = SthProc.new
|
485
|
-
sth.lank = 8
|
486
|
-
assert_equal 8, sth.lank
|
487
|
-
|
488
|
-
assert_raises Validation::InvalidWritingError do
|
489
|
-
sth.lank = 2
|
490
|
-
end
|
491
|
-
end
|
492
|
-
|
493
|
-
SthMethod = Striuct.new do
|
494
|
-
member :lank, 7.method(:<)
|
495
|
-
end
|
496
|
-
|
497
|
-
def test_Method
|
498
|
-
sth = SthMethod.new
|
499
|
-
sth.lank = 8
|
500
|
-
assert_equal 8, sth.lank
|
501
|
-
|
502
|
-
assert_raises Validation::InvalidWritingError do
|
503
|
-
sth.lank = 6
|
504
|
-
end
|
505
|
-
end
|
506
|
-
end
|
507
|
-
|
508
|
-
class TestStriuctAssign < Test::Unit::TestCase
|
509
|
-
Sth = Striuct.new do
|
510
|
-
member :foo
|
511
|
-
end
|
512
|
-
|
513
|
-
def test_unassign
|
514
|
-
sth = Sth.new
|
515
|
-
assert_equal false, sth.assign?(:foo)
|
516
|
-
sth.foo = nil
|
517
|
-
assert_equal true, sth.assign?(:foo)
|
518
|
-
sth.unassign :foo
|
519
|
-
assert_equal false, sth.assign?(:foo)
|
520
|
-
sth.foo = nil
|
521
|
-
assert_equal true, sth.assign?(:foo)
|
522
|
-
sth.clear_at 0
|
523
|
-
assert_equal false, sth.assign?(:foo)
|
524
|
-
|
525
|
-
assert_raises NameError do
|
526
|
-
sth.unassign :var
|
527
|
-
end
|
528
|
-
|
529
|
-
assert_raises IndexError do
|
530
|
-
sth.unassign 1
|
531
|
-
end
|
532
|
-
end
|
533
|
-
end
|
534
|
-
|
535
|
-
class TestStriuctClassLock < Test::Unit::TestCase
|
536
|
-
Sth = Striuct.new do
|
537
|
-
member :foo
|
538
|
-
end
|
539
|
-
|
540
|
-
def test_class_lock
|
541
|
-
sth = Sth.new
|
542
|
-
|
543
|
-
assert_equal true, sth.member?(:foo)
|
544
|
-
|
545
|
-
Sth.class_eval do
|
546
|
-
member :bar
|
547
|
-
end
|
548
|
-
|
549
|
-
assert_equal true, sth.member?(:bar)
|
550
|
-
assert_equal [:foo, :bar], sth.members
|
551
|
-
|
552
|
-
assert_equal false, Sth.closed?
|
553
|
-
|
554
|
-
Sth.__send__ :close
|
555
|
-
|
556
|
-
assert_equal true, Sth.closed?
|
557
|
-
|
558
|
-
assert_raises RuntimeError do
|
559
|
-
Sth.class_eval do
|
560
|
-
member :var2
|
561
|
-
end
|
562
|
-
end
|
563
|
-
|
564
|
-
assert_equal false, sth.member?(:var2)
|
565
|
-
end
|
566
|
-
end
|
567
|
-
|
568
|
-
class TestStriuctDefine < Test::Unit::TestCase
|
569
|
-
def test_define
|
570
|
-
assert_raises RuntimeError do
|
571
|
-
Striuct.define do
|
572
|
-
end
|
573
|
-
end
|
574
|
-
|
575
|
-
klass = Striuct.define do
|
576
|
-
member :foo
|
577
|
-
end
|
578
|
-
|
579
|
-
assert_equal true, klass.closed?
|
580
|
-
end
|
581
|
-
end
|
582
|
-
|
583
|
-
class TestStriuctFreeze < Test::Unit::TestCase
|
584
|
-
Sth = Striuct.new :foo
|
585
|
-
|
586
|
-
def test_freeze
|
587
|
-
sth = Sth.new
|
588
|
-
sth.freeze
|
589
|
-
|
590
|
-
assert_raises RuntimeError do
|
591
|
-
sth.foo = 8
|
592
|
-
end
|
593
|
-
|
594
|
-
assert_equal true, sth.frozen?
|
595
|
-
end
|
596
|
-
end
|
597
|
-
|
598
|
-
|
599
|
-
class TestStriuctSafetyNaming < Test::Unit::TestCase
|
600
|
-
def test_cname?
|
601
|
-
klass = Striuct.new
|
602
|
-
assert_same false, klass.cname?(Object)
|
603
|
-
assert_same false, klass.cname?('m?')
|
604
|
-
assert_same false, klass.cname?('__foo__')
|
605
|
-
assert_same false, klass.cname?('a b')
|
606
|
-
assert_same false, klass.cname?('object_id')
|
607
|
-
assert_same false, klass.cname?('to_ary')
|
608
|
-
assert_same true, klass.cname?('foo')
|
609
|
-
klass.__send__ :protect_level, :warning
|
610
|
-
assert_same false, klass.cname?('m?')
|
611
|
-
assert_same false, klass.cname?('__foo__')
|
612
|
-
assert_same false, klass.cname?('a b')
|
613
|
-
assert_same false, klass.cname?('object_id')
|
614
|
-
assert_same false, klass.cname?('to_ary')
|
615
|
-
assert_same true, klass.cname?('foo')
|
616
|
-
klass.__send__ :protect_level, :struct
|
617
|
-
assert_same true, klass.cname?('m?')
|
618
|
-
assert_same true, klass.cname?('__foo__')
|
619
|
-
assert_same true, klass.cname?('a b')
|
620
|
-
assert_same true, klass.cname?('object_id')
|
621
|
-
assert_same true, klass.cname?('to_ary')
|
622
|
-
assert_same true, klass.cname?('foo')
|
623
|
-
end
|
624
|
-
|
625
|
-
def test_protect
|
626
|
-
klass = Striuct.new
|
627
|
-
assert_raises NameError do
|
628
|
-
klass.class_eval do
|
629
|
-
member ''
|
630
|
-
end
|
631
|
-
end
|
632
|
-
|
633
|
-
assert_raises NameError do
|
634
|
-
klass.class_eval do
|
635
|
-
member :'a b'
|
636
|
-
end
|
637
|
-
end
|
638
|
-
|
639
|
-
assert_raises NameError do
|
640
|
-
klass.class_eval do
|
641
|
-
member :'__send__'
|
642
|
-
end
|
643
|
-
end
|
644
|
-
|
645
|
-
assert_raises NameError do
|
646
|
-
klass.class_eval do
|
647
|
-
member :'__foo__'
|
648
|
-
end
|
649
|
-
end
|
650
|
-
|
651
|
-
assert_raises NameError do
|
652
|
-
klass.class_eval do
|
653
|
-
member :'m?'
|
654
|
-
end
|
655
|
-
end
|
656
|
-
|
657
|
-
assert_same false, klass.member?('m?')
|
658
|
-
|
659
|
-
klass.class_eval do
|
660
|
-
protect_level :struct
|
661
|
-
member :'m?'
|
662
|
-
end
|
663
|
-
|
664
|
-
assert_same true, klass.member?('m?')
|
665
|
-
end
|
666
|
-
end
|
667
|
-
|
668
|
-
class TestStriuctInference < Test::Unit::TestCase
|
669
|
-
def test_inference
|
670
|
-
klass = Striuct.define do
|
671
|
-
member :n, Numeric, inference: true
|
672
|
-
member :m, ANYTHING?, inference: true
|
673
|
-
end
|
674
|
-
|
675
|
-
sth, sth2 = klass.new, klass.new
|
676
|
-
|
677
|
-
assert_raises Validation::InvalidWritingError do
|
678
|
-
sth.n = '1'
|
679
|
-
end
|
680
|
-
|
681
|
-
sth.n = 1.1
|
682
|
-
|
683
|
-
assert_equal 1.1, sth.n
|
684
|
-
|
685
|
-
assert_raises Validation::InvalidWritingError do
|
686
|
-
sth.n = 1
|
687
|
-
end
|
688
|
-
|
689
|
-
assert_raises Validation::InvalidWritingError do
|
690
|
-
sth2.n = 1
|
691
|
-
end
|
692
|
-
|
693
|
-
sth.n = 2.1
|
694
|
-
|
695
|
-
assert_equal 2.1, sth.n
|
696
|
-
|
697
|
-
|
698
|
-
sth2.m = 1
|
699
|
-
|
700
|
-
assert_equal 1, sth2.m
|
701
|
-
|
702
|
-
assert_raises Validation::InvalidWritingError do
|
703
|
-
sth.m = 1.0
|
704
|
-
end
|
705
|
-
|
706
|
-
assert_raises Validation::InvalidWritingError do
|
707
|
-
sth2.m = 1.0
|
708
|
-
end
|
709
|
-
|
710
|
-
sth.m = 2
|
711
|
-
|
712
|
-
assert_equal 2, sth.m
|
713
|
-
end
|
714
|
-
end
|
715
|
-
|
716
|
-
class TestStriuctLoadPairs < Test::Unit::TestCase
|
717
|
-
Sth = Striuct.new :foo, :bar, :hoge
|
718
|
-
|
719
|
-
def test_load_pairs
|
720
|
-
sth = Sth[hoge: 7, foo: 8]
|
721
|
-
|
722
|
-
assert_equal [8, nil, 7], sth.values
|
723
|
-
end
|
724
|
-
end
|
725
|
-
|
726
|
-
class TestStriuctObject < Test::Unit::TestCase
|
727
|
-
Sth = Striuct.new :foo, :bar, :hoge
|
728
|
-
|
729
|
-
def test_hash
|
730
|
-
sth1 = Sth[hoge: 7, foo: 8]
|
731
|
-
sth2 = Sth[hoge: 7, foo: 8]
|
732
|
-
assert_equal true, sth1.eql?(sth2)
|
733
|
-
assert_equal true, sth2.eql?(sth1)
|
734
|
-
assert_equal sth1.hash, sth2.hash
|
735
|
-
assert_equal true, {sth1 => 1}.has_key?(sth2)
|
736
|
-
assert_equal true, {sth2 => 1}.has_key?(sth1)
|
737
|
-
assert_equal 1, {sth1 => 1}[sth2]
|
738
|
-
assert_equal 1, {sth2 => 1}[sth1]
|
739
|
-
end
|
740
|
-
end
|
741
|
-
|
742
|
-
class TestStriuctHashLike < Test::Unit::TestCase
|
743
|
-
Sth = Striuct.new :foo, :bar, :hoge
|
744
|
-
|
745
|
-
def test_empty?
|
746
|
-
sth = Sth[hoge: 7, foo: 8]
|
747
|
-
assert_equal false, sth.empty?
|
748
|
-
sth.each_member{|name|sth[name] = nil}
|
749
|
-
assert_equal false, sth.empty?
|
750
|
-
sth.each_member{|name|sth.unassign name}
|
751
|
-
assert_equal true, sth.empty?
|
752
|
-
end
|
753
|
-
|
754
|
-
def test_has_value?
|
755
|
-
sth = Sth[hoge: 7, foo: 8]
|
756
|
-
assert_equal true, sth.value?(7)
|
757
|
-
assert_equal true, sth.value?(8)
|
758
|
-
assert_equal false, sth.value?(9)
|
759
|
-
assert_equal false, sth.value?(nil)
|
760
|
-
end
|
761
|
-
|
762
|
-
def test_select!
|
763
|
-
sth = Sth[hoge: 7, foo: 8]
|
764
|
-
sth2, sth3 = sth.dup, sth.dup
|
765
|
-
|
766
|
-
assert_kind_of Enumerator, enum = sth.select!
|
767
|
-
assert_equal [:foo, 8], enum.next
|
768
|
-
assert_equal [:bar, nil], enum.next
|
769
|
-
assert_equal [:hoge, 7], enum.next
|
770
|
-
assert_raises StopIteration do
|
771
|
-
enum.next
|
772
|
-
end
|
773
|
-
|
774
|
-
assert_equal nil, sth2.select!{|k, v|true}
|
775
|
-
assert_equal sth3, sth3.select!{|k, v|k == :hoge && v == 7}
|
776
|
-
assert_equal nil, sth3.foo
|
777
|
-
assert_equal true, sth3.assign?(:hoge)
|
778
|
-
assert_equal false, sth3.assign?(:foo)
|
779
|
-
assert_equal 7, sth3.hoge
|
780
|
-
end
|
781
|
-
|
782
|
-
def test_keep_if
|
783
|
-
sth = Sth[hoge: 7, foo: 8]
|
784
|
-
sth2, sth3 = sth.dup, sth.dup
|
785
|
-
|
786
|
-
assert_kind_of Enumerator, enum = sth.keep_if
|
787
|
-
assert_equal [:foo, 8], enum.next
|
788
|
-
assert_equal [:bar, nil], enum.next
|
789
|
-
assert_equal [:hoge, 7], enum.next
|
790
|
-
assert_raises StopIteration do
|
791
|
-
enum.next
|
792
|
-
end
|
793
|
-
|
794
|
-
assert_equal sth2, sth2.keep_if{|k, v|true}
|
795
|
-
assert_equal sth3, sth3.keep_if{|k, v|k == :hoge && v == 7}
|
796
|
-
assert_equal nil, sth3.foo
|
797
|
-
assert_equal true, sth3.assign?(:hoge)
|
798
|
-
assert_equal false, sth3.assign?(:foo)
|
799
|
-
assert_equal 7, sth3.hoge
|
800
|
-
end
|
801
|
-
|
802
|
-
def test_reject!
|
803
|
-
sth = Sth[hoge: 7, foo: 8]
|
804
|
-
sth2, sth3 = sth.dup, sth.dup
|
805
|
-
|
806
|
-
assert_kind_of Enumerator, enum = sth.reject!
|
807
|
-
assert_equal [:foo, 8], enum.next
|
808
|
-
assert_equal [:bar, nil], enum.next
|
809
|
-
assert_equal [:hoge, 7], enum.next
|
810
|
-
assert_raises StopIteration do
|
811
|
-
enum.next
|
812
|
-
end
|
813
|
-
|
814
|
-
enum.rewind
|
815
|
-
assert_equal [:foo, 8], enum.next
|
816
|
-
|
817
|
-
assert_equal nil, sth2.reject!{|k, v|false}
|
818
|
-
assert_equal sth3, sth3.reject!{|k, v|k == :hoge && v == 7}
|
819
|
-
assert_equal nil, sth3.hoge
|
820
|
-
assert_equal false, sth3.assign?(:hoge)
|
821
|
-
assert_equal true, sth3.assign?(:foo)
|
822
|
-
assert_equal 8, sth3.foo
|
823
|
-
end
|
824
|
-
|
825
|
-
def test_delete_if
|
826
|
-
sth = Sth[hoge: 7, foo: 8]
|
827
|
-
sth2, sth3 = sth.dup, sth.dup
|
828
|
-
|
829
|
-
assert_kind_of Enumerator, enum = sth.delete_if
|
830
|
-
|
831
|
-
assert_equal [:foo, 8], enum.next
|
832
|
-
assert_equal [:bar, nil], enum.next
|
833
|
-
assert_equal [:hoge, 7], enum.next
|
834
|
-
assert_raises StopIteration do
|
835
|
-
enum.next
|
836
|
-
end
|
837
|
-
|
838
|
-
assert_equal sth2, sth2.delete_if{|k, v|false}
|
839
|
-
assert_equal sth3, sth3.delete_if{|k, v|k == :hoge && v == 7}
|
840
|
-
assert_equal nil, sth3.hoge
|
841
|
-
assert_equal false, sth3.assign?(:hoge)
|
842
|
-
assert_equal true, sth3.assign?(:foo)
|
843
|
-
assert_equal 8, sth3.foo
|
844
|
-
end
|
845
|
-
|
846
|
-
def test_assoc
|
847
|
-
sth = Sth[hoge: 7, foo: 8]
|
848
|
-
|
849
|
-
assert_equal [:foo, 8], sth.assoc(:foo)
|
850
|
-
assert_equal [:bar, nil], sth.assoc(:bar)
|
851
|
-
assert_equal [:hoge, 7], sth.assoc(:hoge)
|
852
|
-
|
853
|
-
assert_raises NameError do
|
854
|
-
sth.assoc(:dummy)
|
855
|
-
end
|
856
|
-
end
|
857
|
-
|
858
|
-
def test_rassoc
|
859
|
-
sth = Sth[hoge: 7, foo: 7]
|
860
|
-
|
861
|
-
assert_equal [:foo, 7], sth.rassoc(7)
|
862
|
-
assert_equal [:bar, nil], sth.rassoc(nil)
|
863
|
-
assert_equal nil, sth.rassoc(9)
|
864
|
-
end
|
865
|
-
|
866
|
-
def test_flatten
|
867
|
-
sth = Sth[hoge: 7, foo: 8, bar: [1, 2]]
|
868
|
-
assert_equal [:foo, 8, :bar, [1, 2], :hoge, 7], sth.flatten
|
869
|
-
assert_equal [:foo, 8, :bar, [1, 2], :hoge, 7], sth.flatten(1)
|
870
|
-
assert_equal [:foo, 8, :bar, 1, 2, :hoge, 7], sth.flatten(2)
|
871
|
-
assert_equal [:foo, 8, :bar, 1, 2, :hoge, 7], sth.flatten(3)
|
872
|
-
assert_equal [:foo, 8, :bar, 1, 2, :hoge, 7], sth.flatten(-1)
|
873
|
-
end
|
874
|
-
|
875
|
-
def test_select
|
876
|
-
sth = Sth[hoge: 7, foo: 8]
|
877
|
-
|
878
|
-
assert_kind_of Enumerator, enum = sth.select
|
879
|
-
|
880
|
-
assert_equal Sth[hoge: 7], sth.select{|k, v|k == :hoge}
|
881
|
-
end
|
882
|
-
|
883
|
-
def test_reject
|
884
|
-
sth = Sth[hoge: 7, foo: 8]
|
885
|
-
|
886
|
-
assert_kind_of Enumerator, enum = sth.reject
|
887
|
-
|
888
|
-
assert_equal Sth[foo: 8], sth.reject{|k, v|k == :hoge}
|
889
|
-
end
|
890
|
-
end
|
891
|
-
|
892
|
-
|
893
|
-
class TestStriuctAliasMember < Test::Unit::TestCase
|
894
|
-
class Sth < Striuct.new
|
895
|
-
member :foo, String
|
896
|
-
|
897
|
-
member :bar, Integer do |v|
|
898
|
-
v
|
899
|
-
end
|
900
|
-
|
901
|
-
member :hoge, Symbol
|
902
|
-
default :hoge, :'Z'
|
903
|
-
alias_member :abc, :bar
|
904
|
-
default :abc, 8
|
905
|
-
end
|
906
|
-
|
907
|
-
def test_alias_member
|
908
|
-
sth = Sth.new 'A'
|
909
|
-
assert_equal [:foo, :bar, :hoge], sth.members
|
910
|
-
|
911
|
-
assert_equal 8, sth[:abc]
|
912
|
-
flavor = Sth.__send__(:flavor_for, :abc)
|
913
|
-
assert_kind_of Proc, flavor
|
914
|
-
assert_same flavor, Sth.__send__(:flavor_for, :bar)
|
915
|
-
assert_equal 8, sth.abc
|
916
|
-
sth.abc = 5
|
917
|
-
assert_equal 5, sth.bar
|
918
|
-
sth[:abc] = 6
|
919
|
-
assert_equal 6, sth.bar
|
920
|
-
|
921
|
-
assert_raises Validation::InvalidWritingError do
|
922
|
-
sth[:abc] = 'a'
|
923
|
-
end
|
924
|
-
|
925
|
-
assert_raises Validation::InvalidWritingError do
|
926
|
-
sth.abc = 'a'
|
927
|
-
end
|
928
|
-
|
929
|
-
assert_raises Validation::InvalidWritingError do
|
930
|
-
sth.bar = 'a'
|
931
|
-
end
|
932
|
-
|
933
|
-
assert_raises ArgumentError do
|
934
|
-
Sth.class_eval do
|
935
|
-
member :abc
|
936
|
-
end
|
937
|
-
end
|
938
|
-
end
|
939
|
-
end
|
940
|
-
|
941
|
-
class TestStriuctSpecificConditions < Test::Unit::TestCase
|
942
|
-
Sth = Striuct.define do
|
943
|
-
member :list_only_int, GENERICS(Integer)
|
944
|
-
member :true_or_false, BOOL?
|
945
|
-
member :like_str, STRINGABLE?
|
946
|
-
member :has_x, CAN(:x)
|
947
|
-
member :has_x_and_y, CAN(:x, :y)
|
948
|
-
member :one_of_member, MEMBER_OF([1, 3])
|
949
|
-
member :has_ignore, AND(1..5, 3..10)
|
950
|
-
member :nand, NAND(1..5, 3..10)
|
951
|
-
member :all_pass, OR(1..5, 3..10)
|
952
|
-
member :catch_error, CATCH(NoMethodError){|v|v.no_name!}
|
953
|
-
member :no_exception, QUIET(->v{v.class})
|
954
|
-
member :not_integer, NOT(Integer)
|
955
|
-
end
|
956
|
-
|
957
|
-
def test_not
|
958
|
-
sth = Sth.new
|
959
|
-
|
960
|
-
obj = Object.new
|
961
|
-
|
962
|
-
sth.not_integer = obj
|
963
|
-
assert_same obj, sth.not_integer
|
964
|
-
|
965
|
-
assert_raises Validation::InvalidWritingError do
|
966
|
-
sth.not_integer = 1
|
967
|
-
end
|
968
|
-
end
|
969
|
-
|
970
|
-
|
971
|
-
def test_still
|
972
|
-
sth = Sth.new
|
973
|
-
|
974
|
-
obj = Object.new
|
975
|
-
|
976
|
-
sth.no_exception = obj
|
977
|
-
assert_same obj, sth.no_exception
|
978
|
-
sth.no_exception = false
|
979
|
-
|
980
|
-
obj.singleton_class.class_eval do
|
981
|
-
undef_method :class
|
982
|
-
end
|
983
|
-
|
984
|
-
assert_raises Validation::InvalidWritingError do
|
985
|
-
sth.no_exception = obj
|
986
|
-
end
|
987
|
-
end
|
988
|
-
|
989
|
-
def test_catch
|
990
|
-
sth = Sth.new
|
991
|
-
|
992
|
-
obj = Object.new
|
993
|
-
|
994
|
-
sth.catch_error = obj
|
995
|
-
assert_same obj, sth.catch_error
|
996
|
-
sth.catch_error = false
|
997
|
-
|
998
|
-
obj.singleton_class.class_eval do
|
999
|
-
def no_name!
|
1000
|
-
end
|
1001
|
-
end
|
1002
|
-
|
1003
|
-
assert_raises Validation::InvalidWritingError do
|
1004
|
-
sth.catch_error = obj
|
1005
|
-
end
|
1006
|
-
end
|
1007
|
-
|
1008
|
-
def test_or
|
1009
|
-
sth = Sth.new
|
1010
|
-
|
1011
|
-
assert_raises Validation::InvalidWritingError do
|
1012
|
-
sth.all_pass = 11
|
1013
|
-
end
|
1014
|
-
|
1015
|
-
sth.all_pass = 1
|
1016
|
-
assert_equal 1, sth.all_pass
|
1017
|
-
sth.all_pass = 4
|
1018
|
-
assert_equal 4, sth.all_pass
|
1019
|
-
assert_equal true, sth.valid?(:all_pass)
|
1020
|
-
end
|
1021
|
-
|
1022
|
-
def test_and
|
1023
|
-
sth = Sth.new
|
1024
|
-
|
1025
|
-
assert_raises Validation::InvalidWritingError do
|
1026
|
-
sth.has_ignore = 1
|
1027
|
-
end
|
1028
|
-
|
1029
|
-
assert_raises Validation::InvalidWritingError do
|
1030
|
-
sth.has_ignore= 2
|
1031
|
-
end
|
1032
|
-
|
1033
|
-
sth.has_ignore = 3
|
1034
|
-
assert_equal 3, sth.has_ignore
|
1035
|
-
assert_equal true, sth.valid?(:has_ignore)
|
1036
|
-
|
1037
|
-
assert_raises Validation::InvalidWritingError do
|
1038
|
-
sth.has_ignore = []
|
1039
|
-
end
|
1040
|
-
end
|
1041
|
-
|
1042
|
-
def test_nand
|
1043
|
-
sth = Sth.new
|
1044
|
-
|
1045
|
-
assert_raises Validation::InvalidWritingError do
|
1046
|
-
sth.nand = 4
|
1047
|
-
end
|
1048
|
-
|
1049
|
-
assert_raises Validation::InvalidWritingError do
|
1050
|
-
sth.nand = 4.5
|
1051
|
-
end
|
1052
|
-
|
1053
|
-
sth.nand = 2
|
1054
|
-
assert_equal 2, sth.nand
|
1055
|
-
assert_equal true, sth.valid?(:nand)
|
1056
|
-
sth.nand = []
|
1057
|
-
assert_equal [], sth.nand
|
1058
|
-
end
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1062
|
-
def test_member_of
|
1063
|
-
sth = Sth.new
|
1064
|
-
|
1065
|
-
assert_raises Validation::InvalidWritingError do
|
1066
|
-
sth.one_of_member = 4
|
1067
|
-
end
|
1068
|
-
|
1069
|
-
sth.one_of_member = 3
|
1070
|
-
assert_equal 3, sth.one_of_member
|
1071
|
-
assert_equal true, sth.valid?(:one_of_member)
|
1072
|
-
end
|
1073
|
-
|
1074
|
-
def test_generics
|
1075
|
-
sth = Sth.new
|
1076
|
-
|
1077
|
-
assert_raises Validation::InvalidWritingError do
|
1078
|
-
sth.list_only_int = [1, '2']
|
1079
|
-
end
|
1080
|
-
|
1081
|
-
sth.list_only_int = [1, 2]
|
1082
|
-
assert_equal [1, 2], sth.list_only_int
|
1083
|
-
assert_equal true, sth.valid?(:list_only_int)
|
1084
|
-
sth.list_only_int = []
|
1085
|
-
assert_equal [], sth.list_only_int
|
1086
|
-
assert_equal true, sth.valid?(:list_only_int)
|
1087
|
-
sth.list_only_int << '2'
|
1088
|
-
assert_equal false, sth.valid?(:list_only_int)
|
1089
|
-
end
|
1090
|
-
|
1091
|
-
def test_boolean
|
1092
|
-
sth = Sth.new
|
1093
|
-
|
1094
|
-
assert_raises Validation::InvalidWritingError do
|
1095
|
-
sth.true_or_false = nil
|
1096
|
-
end
|
1097
|
-
|
1098
|
-
assert_equal false, sth.valid?(:true_or_false)
|
1099
|
-
|
1100
|
-
sth.true_or_false = true
|
1101
|
-
assert_equal true, sth.true_or_false
|
1102
|
-
assert_equal true, sth.valid?(:true_or_false)
|
1103
|
-
sth.true_or_false = false
|
1104
|
-
assert_equal false, sth.true_or_false
|
1105
|
-
assert_equal true, sth.valid?(:true_or_false)
|
1106
|
-
end
|
1107
|
-
|
1108
|
-
def test_stringable
|
1109
|
-
sth = Sth.new
|
1110
|
-
obj = Object.new
|
1111
|
-
|
1112
|
-
assert_raises Validation::InvalidWritingError do
|
1113
|
-
sth.like_str = obj
|
1114
|
-
end
|
1115
|
-
|
1116
|
-
sth.like_str = 'str'
|
1117
|
-
assert_equal true, sth.valid?(:like_str)
|
1118
|
-
sth.like_str = :sym
|
1119
|
-
assert_equal true, sth.valid?(:like_str)
|
1120
|
-
|
1121
|
-
obj.singleton_class.class_eval do
|
1122
|
-
def to_str
|
1123
|
-
end
|
1124
|
-
end
|
1125
|
-
|
1126
|
-
sth.like_str = obj
|
1127
|
-
assert_equal true, sth.valid?(:like_str)
|
1128
|
-
end
|
1129
|
-
|
1130
|
-
def test_responsible_arg1
|
1131
|
-
sth = Sth.new
|
1132
|
-
obj = Object.new
|
1133
|
-
|
1134
|
-
assert_raises Validation::InvalidWritingError do
|
1135
|
-
sth.has_x = obj
|
1136
|
-
end
|
1137
|
-
|
1138
|
-
obj.singleton_class.class_eval do
|
1139
|
-
def x
|
1140
|
-
end
|
1141
|
-
end
|
1142
|
-
|
1143
|
-
sth.has_x = obj
|
1144
|
-
assert_equal obj, sth.has_x
|
1145
|
-
assert_equal true, sth.valid?(:has_x)
|
1146
|
-
end
|
1147
|
-
|
1148
|
-
def test_responsible_arg2
|
1149
|
-
sth = Sth.new
|
1150
|
-
obj = Object.new
|
1151
|
-
|
1152
|
-
assert_raises Validation::InvalidWritingError do
|
1153
|
-
sth.has_x_and_y = obj
|
1154
|
-
end
|
1155
|
-
|
1156
|
-
obj.singleton_class.class_eval do
|
1157
|
-
def x
|
1158
|
-
end
|
1159
|
-
end
|
1160
|
-
|
1161
|
-
assert_raises Validation::InvalidWritingError do
|
1162
|
-
sth.has_x_and_y = obj
|
1163
|
-
end
|
1164
|
-
|
1165
|
-
obj.singleton_class.class_eval do
|
1166
|
-
def y
|
1167
|
-
end
|
1168
|
-
end
|
1169
|
-
|
1170
|
-
sth.has_x_and_y = obj
|
1171
|
-
assert_equal obj, sth.has_x_and_y
|
1172
|
-
assert_equal true, sth.valid?(:has_x_and_y)
|
1173
|
-
end
|
1174
|
-
end
|
1175
|
-
|
1176
|
-
|
1177
|
-
class TestStriuctLock < Test::Unit::TestCase
|
1178
|
-
Sth = Striuct.new :foo, :bar
|
1179
|
-
Sth.__send__ :close
|
1180
|
-
|
1181
|
-
def test_lock
|
1182
|
-
sth = Sth.new
|
1183
|
-
assert_equal false, sth.lock?(:foo)
|
1184
|
-
assert_equal false, sth.lock?(:bar)
|
1185
|
-
assert_equal false, sth.secure?
|
1186
|
-
sth.lock :bar
|
1187
|
-
assert_equal true, sth.lock?(:bar)
|
1188
|
-
assert_equal false, sth.secure?
|
1189
|
-
|
1190
|
-
assert_raises RuntimeError do
|
1191
|
-
sth.bar = 1
|
1192
|
-
end
|
1193
|
-
|
1194
|
-
sth.__send__ :unlock, :bar
|
1195
|
-
|
1196
|
-
assert_equal false, sth.lock?(:bar)
|
1197
|
-
|
1198
|
-
sth.bar = 1
|
1199
|
-
assert_equal 1, sth.bar
|
1200
|
-
|
1201
|
-
sth.lock
|
1202
|
-
assert_equal true, sth.lock?(:foo)
|
1203
|
-
assert_equal true, sth.lock?(:bar)
|
1204
|
-
assert_equal true, sth.lock?
|
1205
|
-
|
1206
|
-
assert_raises RuntimeError do
|
1207
|
-
sth.foo = 1
|
1208
|
-
end
|
1209
|
-
|
1210
|
-
assert_equal true, sth.secure?
|
1211
|
-
end
|
1212
|
-
end
|
1213
|
-
|
1214
|
-
class TestStriuctInherit < Test::Unit::TestCase
|
1215
|
-
Sth = Striuct.define do
|
1216
|
-
member :foo, String
|
1217
|
-
member :bar, OR(nil, Fixnum)
|
1218
|
-
end
|
1219
|
-
|
1220
|
-
class SubSth < Sth
|
1221
|
-
member :hoge, OR(nil, 1..3)
|
1222
|
-
end
|
1223
|
-
|
1224
|
-
class SubSubSth < SubSth
|
1225
|
-
member :rest, AND(/\d/, Symbol)
|
1226
|
-
end
|
1227
|
-
|
1228
|
-
def test_inherit
|
1229
|
-
assert_equal [*Sth.members, :hoge], SubSth.members
|
1230
|
-
sth = Sth.new
|
1231
|
-
substh = SubSth.new
|
1232
|
-
|
1233
|
-
assert_raises Validation::InvalidWritingError do
|
1234
|
-
substh.bar = 'str'
|
1235
|
-
end
|
1236
|
-
|
1237
|
-
assert_raises Validation::InvalidWritingError do
|
1238
|
-
substh.hoge = 4
|
1239
|
-
end
|
1240
|
-
|
1241
|
-
assert_raises NoMethodError do
|
1242
|
-
sth.hoge = 3
|
1243
|
-
end
|
1244
|
-
|
1245
|
-
assert_raises NoMethodError do
|
1246
|
-
substh.rest = :a4
|
1247
|
-
end
|
1248
|
-
|
1249
|
-
subsubsth = SubSubSth.new
|
1250
|
-
|
1251
|
-
assert_raises Validation::InvalidWritingError do
|
1252
|
-
subsubsth.rest = 4
|
1253
|
-
end
|
1254
|
-
|
1255
|
-
subsubsth.rest = :a4
|
1256
|
-
|
1257
|
-
assert_equal :a4, subsubsth[:rest]
|
1258
|
-
|
1259
|
-
assert_equal true, Sth.__send__(:closed?)
|
1260
|
-
assert_equal false, SubSth.__send__(:closed?)
|
1261
|
-
SubSth.__send__(:close)
|
1262
|
-
assert_equal true, SubSth.__send__(:closed?)
|
1263
|
-
assert_equal false, SubSubSth.__send__(:closed?)
|
1264
|
-
SubSubSth.__send__(:close)
|
1265
|
-
assert_equal true, SubSubSth.__send__(:closed?)
|
1266
|
-
end
|
1267
|
-
end
|
1268
|
-
|
1269
|
-
class TestStriuctConstants < Test::Unit::TestCase
|
1270
|
-
def test_const_version
|
1271
|
-
assert_equal '0.3.0', Striuct::VERSION
|
1272
|
-
assert_equal true, Striuct::VERSION.frozen?
|
1273
|
-
assert_same Striuct::VERSION, Striuct::Version
|
1274
|
-
end
|
1275
|
-
end
|
1276
|
-
|
1277
|
-
class TestStriuctConstants < Test::Unit::TestCase
|
1278
|
-
Sth = Striuct.define do
|
1279
|
-
member :name
|
1280
|
-
member :age
|
1281
|
-
end
|
1282
|
-
|
1283
|
-
def test_each_pair_with_index
|
1284
|
-
sth = Sth.new 'a', 10
|
1285
|
-
assert_same sth, sth.each_pair_with_index{}
|
1286
|
-
|
1287
|
-
enum = sth.each_pair_with_index
|
1288
|
-
assert_equal [:name, 'a', 0], enum.next
|
1289
|
-
assert_equal [:age, 10, 1], enum.next
|
1290
|
-
assert_raises StopIteration do
|
1291
|
-
enum.next
|
1292
|
-
end
|
1293
|
-
end
|
1294
|
-
end
|
1295
|
-
|
1296
|
-
class TestStriuct_to_Struct < Test::Unit::TestCase
|
1297
|
-
Sth = Striuct.define do
|
1298
|
-
member :name, String
|
1299
|
-
member :age, Integer
|
1300
|
-
end
|
1301
|
-
|
1302
|
-
def test_to_struct_class
|
1303
|
-
klass = Sth.to_struct_class
|
1304
|
-
assert_equal 'Striuct::Structs::Sth', klass.to_s
|
1305
|
-
assert_same klass, Sth.to_struct_class
|
1306
|
-
assert_kind_of Struct, Sth.new.to_struct
|
1307
|
-
assert_kind_of Striuct::Structs::Sth, Sth.new.to_struct
|
1308
|
-
|
1309
|
-
assert_raises RuntimeError do
|
1310
|
-
Striuct.new.new.to_struct
|
1311
|
-
end
|
1312
|
-
|
1313
|
-
Striuct.new(:a, :b, :c).new.to_struct
|
1314
|
-
assert_equal 1, Striuct::Structs.constants.length
|
1315
|
-
end
|
1316
|
-
end
|
1317
|
-
|
1318
|
-
class TestStriuctFlavors < Test::Unit::TestCase
|
1319
|
-
class MyClass
|
1320
|
-
def self.parse(v)
|
1321
|
-
raise unless /\A[a-z]+\z/ =~ v
|
1322
|
-
new
|
1323
|
-
end
|
1324
|
-
end
|
1325
|
-
|
1326
|
-
Sth = Striuct.define do
|
1327
|
-
member :chomped, AND(Symbol, /[^\n]\z/), &WHEN(String, ->v{v.chomp.to_sym})
|
1328
|
-
member :no_reduced, Symbol, &->v{v.to_sym}
|
1329
|
-
member :reduced, Symbol, &INJECT(->v{v.to_s}, ->v{v.to_sym})
|
1330
|
-
member :integer, Integer, &PARSE(Integer)
|
1331
|
-
member :myobj, ->v{v.instance_of? MyClass}, &PARSE(MyClass)
|
1332
|
-
end
|
1333
|
-
|
1334
|
-
def test_WHEN
|
1335
|
-
sth = Sth.new
|
1336
|
-
|
1337
|
-
assert_raises Validation::InvalidWritingError do
|
1338
|
-
sth.chomped = :"a\n"
|
1339
|
-
end
|
1340
|
-
|
1341
|
-
sth.chomped = "a\n"
|
1342
|
-
|
1343
|
-
assert_equal :a, sth.chomped
|
1344
|
-
|
1345
|
-
sth.chomped = :b
|
1346
|
-
assert_equal :b, sth.chomped
|
1347
|
-
end
|
1348
|
-
|
1349
|
-
def test_INJECT
|
1350
|
-
sth = Sth.new
|
1351
|
-
|
1352
|
-
assert_raises Validation::UnmanagebleError do
|
1353
|
-
sth.no_reduced = 1
|
1354
|
-
end
|
1355
|
-
|
1356
|
-
sth.reduced = 1
|
1357
|
-
|
1358
|
-
assert_equal :'1', sth.reduced
|
1359
|
-
end
|
1360
|
-
|
1361
|
-
def test_PARSE
|
1362
|
-
sth = Sth.new
|
1363
|
-
|
1364
|
-
assert_raises Validation::UnmanagebleError do
|
1365
|
-
sth.integer = '1.0'
|
1366
|
-
end
|
1367
|
-
|
1368
|
-
sth.integer = '1'
|
1369
|
-
|
1370
|
-
assert_equal 1, sth.integer
|
1371
|
-
|
1372
|
-
assert_raises Validation::UnmanagebleError do
|
1373
|
-
sth.myobj = '1'
|
1374
|
-
end
|
1375
|
-
|
1376
|
-
sth.myobj = 'a'
|
1377
|
-
|
1378
|
-
assert_kind_of MyClass, sth.myobj
|
1379
|
-
end
|
1380
|
-
end
|
1381
|
-
|
1382
|
-
class TestGetterValidation < Test::Unit::TestCase
|
1383
|
-
Sth = Striuct.define do
|
1384
|
-
member :plus_getter, /./, getter_validation: true
|
1385
|
-
member :only_getter, /./, getter_validation: true,
|
1386
|
-
setter_validation: false
|
1387
|
-
end
|
1388
|
-
|
1389
|
-
def test_getter_validation
|
1390
|
-
sth = Sth.new
|
1391
|
-
|
1392
|
-
assert_raises Validation::InvalidWritingError do
|
1393
|
-
sth.plus_getter = ''
|
1394
|
-
end
|
1395
|
-
|
1396
|
-
sth.plus_getter = 'abc'
|
1397
|
-
assert_equal 'abc', sth.plus_getter
|
1398
|
-
sth.plus_getter.clear
|
1399
|
-
|
1400
|
-
assert_raises Validation::InvalidReadingError do
|
1401
|
-
sth.plus_getter
|
1402
|
-
end
|
1403
|
-
|
1404
|
-
sth.only_getter = ''
|
1405
|
-
|
1406
|
-
assert_raises Validation::InvalidReadingError do
|
1407
|
-
sth.only_getter
|
1408
|
-
end
|
1409
|
-
end
|
1410
|
-
end
|
2
|
+
require_relative 'helper'
|
3
|
+
|
4
|
+
require_relative 'test_striuct_define'
|
5
|
+
require_relative 'test_striuct_subclass_adjuster'
|
6
|
+
require_relative 'test_striuct_subclass_alias_member'
|
7
|
+
require_relative 'test_striuct_subclass_assign'
|
8
|
+
require_relative 'test_striuct_subclass_classlock'
|
9
|
+
require_relative 'test_striuct_subclass_classmethods'
|
10
|
+
require_relative 'test_striuct_subclass_cloning'
|
11
|
+
require_relative 'test_striuct_subclass_default_value'
|
12
|
+
require_relative 'test_striuct_subclass_for_pairs'
|
13
|
+
require_relative 'test_striuct_subclass_freeze'
|
14
|
+
require_relative 'test_striuct_subclass_functional_condition'
|
15
|
+
require_relative 'test_striuct_subclass_getter_validation'
|
16
|
+
require_relative 'test_striuct_subclass_hashlike'
|
17
|
+
require_relative 'test_striuct_subclass_inference'
|
18
|
+
require_relative 'test_striuct_subclass_inheritable'
|
19
|
+
require_relative 'test_striuct_subclass_instance'
|
20
|
+
require_relative 'test_striuct_subclass_lock'
|
21
|
+
require_relative 'test_striuct_subclass_safetynaming'
|
22
|
+
require_relative 'test_striuct_subclass_specific_conditions'
|
23
|
+
require_relative 'test_striuct_subclass_to_struct'
|
24
|
+
require_relative 'test_striuct_version'
|