striuct 0.6.1 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE.txt +21 -0
- data/README.md +19 -39
- data/lib/striuct.rb +3 -3
- data/lib/striuct/classmethods/README.md +2 -2
- data/lib/striuct/classmethods/adjustment.rb +15 -13
- data/lib/striuct/classmethods/attributes.rb +94 -92
- data/lib/striuct/classmethods/conflict_management.rb +109 -106
- data/lib/striuct/classmethods/constructor.rb +47 -54
- data/lib/striuct/classmethods/copy.rb +45 -43
- data/lib/striuct/classmethods/default.rb +23 -21
- data/lib/striuct/classmethods/enum.rb +46 -44
- data/lib/striuct/classmethods/fix.rb +23 -21
- data/lib/striuct/classmethods/hashdeepdupulicatable.rb +15 -15
- data/lib/striuct/classmethods/inner.rb +57 -55
- data/lib/striuct/classmethods/length.rb +12 -10
- data/lib/striuct/classmethods/macro.rb +110 -106
- data/lib/striuct/classmethods/names.rb +69 -65
- data/lib/striuct/classmethods/predicate.rb +131 -129
- data/lib/striuct/classmethods/requirements.rb +3 -1
- data/lib/striuct/classmethods/to_struct.rb +21 -18
- data/lib/striuct/classmethods/validation.rb +15 -13
- data/lib/striuct/instancemethods/README.md +2 -2
- data/lib/striuct/instancemethods/assign.rb +29 -27
- data/lib/striuct/instancemethods/cast.rb +28 -26
- data/lib/striuct/instancemethods/compare.rb +26 -24
- data/lib/striuct/instancemethods/default.rb +31 -29
- data/lib/striuct/instancemethods/delegate_class_methods.rb +35 -33
- data/lib/striuct/instancemethods/enum.rb +86 -84
- data/lib/striuct/instancemethods/getter.rb +39 -37
- data/lib/striuct/instancemethods/hashy.rb +99 -97
- data/lib/striuct/instancemethods/keyvalidatable.rb +8 -6
- data/lib/striuct/instancemethods/lock.rb +46 -44
- data/lib/striuct/instancemethods/object.rb +50 -48
- data/lib/striuct/instancemethods/requirements.rb +11 -9
- data/lib/striuct/instancemethods/safety.rb +11 -9
- data/lib/striuct/instancemethods/setter.rb +46 -45
- data/lib/striuct/instancemethods/to_s.rb +39 -37
- data/lib/striuct/instancemethods/validation.rb +31 -29
- data/lib/striuct/instancemethods/values.rb +50 -48
- data/lib/striuct/requirements.rb +3 -1
- data/lib/striuct/singleton_class.rb +12 -17
- data/lib/striuct/structs.rb +3 -3
- metadata +33 -178
- data/.gitignore +0 -35
- data/.travis.yml +0 -17
- data/.yardopts +0 -1
- data/Gemfile +0 -11
- data/MIT-LICENSE +0 -22
- data/Rakefile +0 -11
- data/benchmark/basics.rb +0 -56
- data/example/README.rb +0 -50
- data/example/example1.rb +0 -195
- data/example/example2.rb +0 -20
- data/example/see_trace.rb +0 -32
- data/striuct.gemspec +0 -45
- data/test/helper.rb +0 -3
- data/test/test_sglc-constructor.rb +0 -36
- data/test/test_subc-c-add_members.rb +0 -13
- data/test/test_subc-c-close_member.rb +0 -36
- data/test/test_subc-c-constructor.rb +0 -92
- data/test/test_subc-c-copy.rb +0 -71
- data/test/test_subc-c-freeze.rb +0 -36
- data/test/test_subc-c-inheritable.rb +0 -58
- data/test/test_subc-c-safety_naming.rb +0 -73
- data/test/test_subc-f-alias_member.rb +0 -54
- data/test/test_subc-f-enum.rb +0 -159
- data/test/test_subc-f-predicate.rb +0 -647
- data/test/test_subc-f-to_struct.rb +0 -61
- data/test/test_subc-f-validation_util.rb +0 -59
- data/test/test_subc-f_debug.rb +0 -39
- data/test/test_subc-f_name.rb +0 -525
- data/test/test_subc-i-accessor.rb +0 -138
- data/test/test_subc-i-adjuster.rb +0 -95
- data/test/test_subc-i-assign.rb +0 -53
- data/test/test_subc-i-basic.rb +0 -43
- data/test/test_subc-i-cast.rb +0 -27
- data/test/test_subc-i-compare.rb +0 -52
- data/test/test_subc-i-copy.rb +0 -23
- data/test/test_subc-i-default_value.rb +0 -129
- data/test/test_subc-i-freeze.rb +0 -19
- data/test/test_subc-i-hashlike.rb +0 -144
- data/test/test_subc-i-keyvalidatable.rb +0 -25
- data/test/test_subc-i-lock.rb +0 -40
- data/test/test_subc-i-must.rb +0 -30
- data/test/test_subc-i-to_s_family.rb +0 -26
- data/test/test_subc-i-validation_specific_conditions.rb +0 -297
- data/test/test_subc-i-validation_with_getter.rb +0 -33
- data/test/test_version.rb +0 -13
data/test/test_subc-i-freeze.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
require_relative 'helper'
|
2
|
-
|
3
|
-
class Test_Striuct_Subclass_Instance__Freeze < Test::Unit::TestCase
|
4
|
-
|
5
|
-
Sth = Striuct.new :foo
|
6
|
-
|
7
|
-
def test_freeze
|
8
|
-
sth = Sth.new
|
9
|
-
sth.freeze
|
10
|
-
|
11
|
-
assert_raises RuntimeError do
|
12
|
-
sth.foo = 8
|
13
|
-
end
|
14
|
-
|
15
|
-
assert_equal true, sth.frozen?
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
|
@@ -1,144 +0,0 @@
|
|
1
|
-
require_relative 'helper'
|
2
|
-
|
3
|
-
class Test_Striuct_Subclass_Instance_HashLike < Test::Unit::TestCase
|
4
|
-
|
5
|
-
Sth = Striuct.new :foo, :bar, :hoge
|
6
|
-
|
7
|
-
def test_has_value?
|
8
|
-
sth = Sth[hoge: 7, foo: 8]
|
9
|
-
assert_equal true, sth.value?(7)
|
10
|
-
assert_equal true, sth.value?(8)
|
11
|
-
assert_equal false, sth.value?(9)
|
12
|
-
assert_equal false, sth.value?(nil)
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_select!
|
16
|
-
sth = Sth[hoge: 7, foo: 8]
|
17
|
-
sth2, sth3 = sth.dup, sth.dup
|
18
|
-
|
19
|
-
assert_kind_of Enumerator, enum = sth.select!
|
20
|
-
assert_equal [:foo, 8], enum.next
|
21
|
-
assert_equal [:bar, nil], enum.next
|
22
|
-
assert_equal [:hoge, 7], enum.next
|
23
|
-
assert_raises StopIteration do
|
24
|
-
enum.next
|
25
|
-
end
|
26
|
-
|
27
|
-
assert_equal nil, sth2.select!{|k, v|true}
|
28
|
-
assert_equal sth3, sth3.select!{|k, v|k == :hoge && v == 7}
|
29
|
-
assert_equal nil, sth3.foo
|
30
|
-
assert_equal true, sth3.assign?(:hoge)
|
31
|
-
assert_equal false, sth3.assign?(:foo)
|
32
|
-
assert_equal 7, sth3.hoge
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_keep_if
|
36
|
-
sth = Sth[hoge: 7, foo: 8]
|
37
|
-
sth2, sth3 = sth.dup, sth.dup
|
38
|
-
|
39
|
-
assert_kind_of Enumerator, enum = sth.keep_if
|
40
|
-
assert_equal [:foo, 8], enum.next
|
41
|
-
assert_equal [:bar, nil], enum.next
|
42
|
-
assert_equal [:hoge, 7], enum.next
|
43
|
-
assert_raises StopIteration do
|
44
|
-
enum.next
|
45
|
-
end
|
46
|
-
|
47
|
-
assert_equal sth2, sth2.keep_if{|k, v|true}
|
48
|
-
assert_equal sth3, sth3.keep_if{|k, v|k == :hoge && v == 7}
|
49
|
-
assert_equal nil, sth3.foo
|
50
|
-
assert_equal true, sth3.assign?(:hoge)
|
51
|
-
assert_equal false, sth3.assign?(:foo)
|
52
|
-
assert_equal 7, sth3.hoge
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_reject!
|
56
|
-
sth = Sth[hoge: 7, foo: 8]
|
57
|
-
sth2, sth3 = sth.dup, sth.dup
|
58
|
-
|
59
|
-
assert_kind_of Enumerator, enum = sth.reject!
|
60
|
-
assert_equal [:foo, 8], enum.next
|
61
|
-
assert_equal [:bar, nil], enum.next
|
62
|
-
assert_equal [:hoge, 7], enum.next
|
63
|
-
assert_raises StopIteration do
|
64
|
-
enum.next
|
65
|
-
end
|
66
|
-
|
67
|
-
enum.rewind
|
68
|
-
assert_equal [:foo, 8], enum.next
|
69
|
-
|
70
|
-
assert_equal nil, sth2.reject!{|k, v|false}
|
71
|
-
assert_equal sth3, sth3.reject!{|k, v|k == :hoge && v == 7}
|
72
|
-
assert_equal nil, sth3.hoge
|
73
|
-
assert_equal false, sth3.assign?(:hoge)
|
74
|
-
assert_equal true, sth3.assign?(:foo)
|
75
|
-
assert_equal 8, sth3.foo
|
76
|
-
end
|
77
|
-
|
78
|
-
def test_delete_if
|
79
|
-
sth = Sth[hoge: 7, foo: 8]
|
80
|
-
sth2, sth3 = sth.dup, sth.dup
|
81
|
-
|
82
|
-
assert_kind_of Enumerator, enum = sth.delete_if
|
83
|
-
|
84
|
-
assert_equal [:foo, 8], enum.next
|
85
|
-
assert_equal [:bar, nil], enum.next
|
86
|
-
assert_equal [:hoge, 7], enum.next
|
87
|
-
assert_raises StopIteration do
|
88
|
-
enum.next
|
89
|
-
end
|
90
|
-
|
91
|
-
assert_equal sth2, sth2.delete_if{|k, v|false}
|
92
|
-
assert_equal sth3, sth3.delete_if{|k, v|k == :hoge && v == 7}
|
93
|
-
assert_equal nil, sth3.hoge
|
94
|
-
assert_equal false, sth3.assign?(:hoge)
|
95
|
-
assert_equal true, sth3.assign?(:foo)
|
96
|
-
assert_equal 8, sth3.foo
|
97
|
-
end
|
98
|
-
|
99
|
-
def test_assoc
|
100
|
-
sth = Sth[hoge: 7, foo: 8]
|
101
|
-
|
102
|
-
assert_equal [:foo, 8], sth.assoc(:foo)
|
103
|
-
assert_equal [:bar, nil], sth.assoc(:bar)
|
104
|
-
assert_equal [:hoge, 7], sth.assoc(:hoge)
|
105
|
-
|
106
|
-
assert_raises NameError do
|
107
|
-
sth.assoc(:dummy)
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
def test_rassoc
|
112
|
-
sth = Sth[hoge: 7, foo: 7]
|
113
|
-
|
114
|
-
assert_equal [:foo, 7], sth.rassoc(7)
|
115
|
-
assert_equal [:bar, nil], sth.rassoc(nil)
|
116
|
-
assert_equal nil, sth.rassoc(9)
|
117
|
-
end
|
118
|
-
|
119
|
-
def test_flatten
|
120
|
-
sth = Sth[hoge: 7, foo: 8, bar: [1, 2]]
|
121
|
-
assert_equal [:foo, 8, :bar, [1, 2], :hoge, 7], sth.flatten
|
122
|
-
assert_equal [:foo, 8, :bar, [1, 2], :hoge, 7], sth.flatten(1)
|
123
|
-
assert_equal [:foo, 8, :bar, 1, 2, :hoge, 7], sth.flatten(2)
|
124
|
-
assert_equal [:foo, 8, :bar, 1, 2, :hoge, 7], sth.flatten(3)
|
125
|
-
assert_equal [:foo, 8, :bar, 1, 2, :hoge, 7], sth.flatten(-1)
|
126
|
-
end
|
127
|
-
|
128
|
-
def test_select
|
129
|
-
sth = Sth[hoge: 7, foo: 8]
|
130
|
-
|
131
|
-
assert_kind_of Enumerator, sth.select
|
132
|
-
|
133
|
-
assert_equal Sth[hoge: 7], sth.select{|k, v|k == :hoge}
|
134
|
-
end
|
135
|
-
|
136
|
-
def test_reject
|
137
|
-
sth = Sth[hoge: 7, foo: 8]
|
138
|
-
|
139
|
-
assert_kind_of Enumerator, sth.reject
|
140
|
-
|
141
|
-
assert_equal Sth[foo: 8], sth.reject{|k, v|k == :hoge}
|
142
|
-
end
|
143
|
-
|
144
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
require_relative 'helper'
|
2
|
-
|
3
|
-
class Test_Striuct_Subclass_Instance_KeyVallidatable < Test::Unit::TestCase
|
4
|
-
|
5
|
-
Sth = Striuct.new :foo, :bar
|
6
|
-
|
7
|
-
def test_valid_keys?
|
8
|
-
sth = Sth.new
|
9
|
-
assert(sth.valid_keys?(must: [:foo, :bar]))
|
10
|
-
assert(sth.valid_keys?(let: [:foo, :bar]))
|
11
|
-
assert(sth.valid_keys?(must: [:foo], let: [:bar]))
|
12
|
-
assert(sth.valid_keys?(let: [:foo, :bar, :hoge]))
|
13
|
-
assert_same(false, sth.valid_keys?(must: [:foo, :bar, :hoge]))
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_valid_autonyms?
|
17
|
-
sth = Sth.new
|
18
|
-
assert(sth.valid_autonyms?(must: [:foo, :bar]))
|
19
|
-
assert(sth.valid_autonyms?(let: [:foo, :bar]))
|
20
|
-
assert(sth.valid_autonyms?(must: [:foo], let: [:bar]))
|
21
|
-
assert(sth.valid_autonyms?(let: [:foo, :bar, :hoge]))
|
22
|
-
assert_same(false, sth.valid_autonyms?(must: [:foo, :bar, :hoge]))
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
data/test/test_subc-i-lock.rb
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
require_relative 'helper'
|
2
|
-
|
3
|
-
class Test_Striuct_Subclass_Instance_Lock < Test::Unit::TestCase
|
4
|
-
|
5
|
-
Sth = Striuct.new :foo, :bar
|
6
|
-
Sth.__send__ :close
|
7
|
-
|
8
|
-
def test_lock
|
9
|
-
sth = Sth.new
|
10
|
-
assert_equal false, sth.locked?(:foo)
|
11
|
-
assert_equal false, sth.locked?(:bar)
|
12
|
-
assert_equal false, sth.secure?
|
13
|
-
sth.lock :bar
|
14
|
-
assert_equal true, sth.locked?(:bar)
|
15
|
-
assert_equal false, sth.secure?
|
16
|
-
|
17
|
-
assert_raises RuntimeError do
|
18
|
-
sth.bar = 1
|
19
|
-
end
|
20
|
-
|
21
|
-
sth.__send__ :unlock, :bar
|
22
|
-
|
23
|
-
assert_equal false, sth.locked?(:bar)
|
24
|
-
|
25
|
-
sth.bar = 1
|
26
|
-
assert_equal 1, sth.bar
|
27
|
-
|
28
|
-
sth.lock_all
|
29
|
-
assert_equal true, sth.locked?(:foo)
|
30
|
-
assert_equal true, sth.locked?(:bar)
|
31
|
-
assert_equal true, sth.all_locked?
|
32
|
-
|
33
|
-
assert_raises RuntimeError do
|
34
|
-
sth.foo = 1
|
35
|
-
end
|
36
|
-
|
37
|
-
assert_equal true, sth.secure?
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
data/test/test_subc-i-must.rb
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
require_relative 'helper'
|
2
|
-
|
3
|
-
class Test_Striuct_Subclass_Must < Test::Unit::TestCase
|
4
|
-
|
5
|
-
Sth = Striuct.define do
|
6
|
-
member :foo, ANYTHING?, must: false
|
7
|
-
member :bar, ANYTHING?, must: true
|
8
|
-
member :baz
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_must
|
12
|
-
assert_raises(Striuct::InvalidOperationError) do
|
13
|
-
Sth.new :foo
|
14
|
-
end
|
15
|
-
|
16
|
-
assert_raises(Striuct::InvalidOperationError) do
|
17
|
-
Sth.new foo: :foo, baz: :baz
|
18
|
-
end
|
19
|
-
|
20
|
-
sth = Sth.new :foo, :bar, :baz
|
21
|
-
assert_instance_of(Sth, sth)
|
22
|
-
|
23
|
-
assert_raises(Striuct::InvalidOperationError) do
|
24
|
-
sth.unassign :bar
|
25
|
-
end
|
26
|
-
|
27
|
-
assert_nil(sth.bar = nil)
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
require_relative 'helper'
|
2
|
-
|
3
|
-
class Test_Striuct_Subclass_Instance_to_s_Family < Test::Unit::TestCase
|
4
|
-
|
5
|
-
class User < Striuct.new
|
6
|
-
member :id, Integer, must: true
|
7
|
-
member :name, /\w+/
|
8
|
-
end
|
9
|
-
|
10
|
-
def setup
|
11
|
-
@sth = User.new 9999, 'taro'
|
12
|
-
@sth.lock(:name)
|
13
|
-
@sth.name.clear
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_to_s
|
17
|
-
assert_equal %q!#<struct' Test_Striuct_Subclass_Instance_to_s_Family::User id=9999, name="">!,
|
18
|
-
@sth.to_s
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_inspect
|
22
|
-
assert_equal %q!#<struct' Test_Striuct_Subclass_Instance_to_s_Family::User id=9999(must), name=""(invalid, locked)>!,
|
23
|
-
@sth.inspect
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
@@ -1,297 +0,0 @@
|
|
1
|
-
require_relative 'helper'
|
2
|
-
|
3
|
-
class Test_Striuct_Subclass_Instance_SpecificConditions < Test::Unit::TestCase
|
4
|
-
|
5
|
-
Sth = Striuct.define do
|
6
|
-
member :list_only_int, GENERICS(Integer)
|
7
|
-
member :true_or_false, BOOL?
|
8
|
-
member :like_str, STRINGABLE?
|
9
|
-
member :has_foo, CAN(:foo)
|
10
|
-
member :has_foo_and_bar, CAN(:foo, :bar)
|
11
|
-
member :one_of_member, MEMBER_OF([1, 3])
|
12
|
-
member :has_ignore, AND(1..5, 3..10)
|
13
|
-
member :nand, NAND(1..5, 3..10)
|
14
|
-
member :all_pass, OR(1..5, 3..10)
|
15
|
-
member :catch_error, CATCH(NoMethodError){|v|v.no_name!}
|
16
|
-
member :no_exception, QUIET(->v{v.class})
|
17
|
-
member :not_integer, NOT(Integer)
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_not
|
21
|
-
sth = Sth.new
|
22
|
-
|
23
|
-
obj = Object.new
|
24
|
-
|
25
|
-
sth.not_integer = obj
|
26
|
-
assert_same obj, sth.not_integer
|
27
|
-
|
28
|
-
assert_raises Validation::InvalidWritingError do
|
29
|
-
sth.not_integer = 1
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
|
34
|
-
def test_still
|
35
|
-
sth = Sth.new
|
36
|
-
|
37
|
-
obj = Object.new
|
38
|
-
|
39
|
-
sth.no_exception = obj
|
40
|
-
assert_same obj, sth.no_exception
|
41
|
-
sth.no_exception = false
|
42
|
-
|
43
|
-
obj.singleton_class.class_eval do
|
44
|
-
undef_method :class
|
45
|
-
end
|
46
|
-
|
47
|
-
assert_raises Validation::InvalidWritingError do
|
48
|
-
sth.no_exception = obj
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
def test_catch
|
53
|
-
sth = Sth.new
|
54
|
-
|
55
|
-
obj = Object.new
|
56
|
-
|
57
|
-
sth.catch_error = obj
|
58
|
-
assert_same obj, sth.catch_error
|
59
|
-
sth.catch_error = false
|
60
|
-
|
61
|
-
obj.singleton_class.class_eval do
|
62
|
-
def no_name!
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
assert_raises Validation::InvalidWritingError do
|
67
|
-
sth.catch_error = obj
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
def test_or
|
72
|
-
sth = Sth.new
|
73
|
-
|
74
|
-
assert_raises Validation::InvalidWritingError do
|
75
|
-
sth.all_pass = 11
|
76
|
-
end
|
77
|
-
|
78
|
-
sth.all_pass = 1
|
79
|
-
assert_equal 1, sth.all_pass
|
80
|
-
sth.all_pass = 4
|
81
|
-
assert_equal 4, sth.all_pass
|
82
|
-
assert_equal true, sth.valid?(:all_pass)
|
83
|
-
end
|
84
|
-
|
85
|
-
def test_and
|
86
|
-
sth = Sth.new
|
87
|
-
|
88
|
-
assert_raises Validation::InvalidWritingError do
|
89
|
-
sth.has_ignore = 1
|
90
|
-
end
|
91
|
-
|
92
|
-
assert_raises Validation::InvalidWritingError do
|
93
|
-
sth.has_ignore= 2
|
94
|
-
end
|
95
|
-
|
96
|
-
sth.has_ignore = 3
|
97
|
-
assert_equal 3, sth.has_ignore
|
98
|
-
assert_equal true, sth.valid?(:has_ignore)
|
99
|
-
|
100
|
-
assert_raises Validation::InvalidWritingError do
|
101
|
-
sth.has_ignore = []
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
def test_nand
|
106
|
-
sth = Sth.new
|
107
|
-
|
108
|
-
assert_raises Validation::InvalidWritingError do
|
109
|
-
sth.nand = 4
|
110
|
-
end
|
111
|
-
|
112
|
-
assert_raises Validation::InvalidWritingError do
|
113
|
-
sth.nand = 4.5
|
114
|
-
end
|
115
|
-
|
116
|
-
sth.nand = 2
|
117
|
-
assert_equal 2, sth.nand
|
118
|
-
assert_equal true, sth.valid?(:nand)
|
119
|
-
sth.nand = []
|
120
|
-
assert_equal [], sth.nand
|
121
|
-
end
|
122
|
-
|
123
|
-
def test_member_of
|
124
|
-
sth = Sth.new
|
125
|
-
|
126
|
-
assert_raises Validation::InvalidWritingError do
|
127
|
-
sth.one_of_member = 4
|
128
|
-
end
|
129
|
-
|
130
|
-
sth.one_of_member = 3
|
131
|
-
assert_equal 3, sth.one_of_member
|
132
|
-
assert_equal true, sth.valid?(:one_of_member)
|
133
|
-
end
|
134
|
-
|
135
|
-
def test_generics
|
136
|
-
sth = Sth.new
|
137
|
-
|
138
|
-
assert_raises Validation::InvalidWritingError do
|
139
|
-
sth.list_only_int = [1, '2']
|
140
|
-
end
|
141
|
-
|
142
|
-
sth.list_only_int = [1, 2]
|
143
|
-
assert_equal [1, 2], sth.list_only_int
|
144
|
-
assert_equal true, sth.valid?(:list_only_int)
|
145
|
-
sth.list_only_int = []
|
146
|
-
assert_equal [], sth.list_only_int
|
147
|
-
assert_equal true, sth.valid?(:list_only_int)
|
148
|
-
sth.list_only_int << '2'
|
149
|
-
assert_equal false, sth.valid?(:list_only_int)
|
150
|
-
end
|
151
|
-
|
152
|
-
def test_boolean
|
153
|
-
sth = Sth.new
|
154
|
-
|
155
|
-
assert_raises Validation::InvalidWritingError do
|
156
|
-
sth.true_or_false = nil
|
157
|
-
end
|
158
|
-
|
159
|
-
assert_equal false, sth.valid?(:true_or_false)
|
160
|
-
|
161
|
-
sth.true_or_false = true
|
162
|
-
assert_equal true, sth.true_or_false
|
163
|
-
assert_equal true, sth.valid?(:true_or_false)
|
164
|
-
sth.true_or_false = false
|
165
|
-
assert_equal false, sth.true_or_false
|
166
|
-
assert_equal true, sth.valid?(:true_or_false)
|
167
|
-
end
|
168
|
-
|
169
|
-
def test_stringable
|
170
|
-
sth = Sth.new
|
171
|
-
obj = Object.new
|
172
|
-
|
173
|
-
assert_raises Validation::InvalidWritingError do
|
174
|
-
sth.like_str = obj
|
175
|
-
end
|
176
|
-
|
177
|
-
sth.like_str = 'str'
|
178
|
-
assert_equal true, sth.valid?(:like_str)
|
179
|
-
sth.like_str = :sym
|
180
|
-
assert_equal true, sth.valid?(:like_str)
|
181
|
-
|
182
|
-
obj.singleton_class.class_eval do
|
183
|
-
def to_str
|
184
|
-
end
|
185
|
-
end
|
186
|
-
|
187
|
-
sth.like_str = obj
|
188
|
-
assert_equal true, sth.valid?(:like_str)
|
189
|
-
end
|
190
|
-
|
191
|
-
def test_responsible_arg1
|
192
|
-
sth = Sth.new
|
193
|
-
obj = Object.new
|
194
|
-
|
195
|
-
raise if obj.respond_to? :foo
|
196
|
-
|
197
|
-
assert_raises Validation::InvalidWritingError do
|
198
|
-
sth.has_foo = obj
|
199
|
-
end
|
200
|
-
|
201
|
-
obj.singleton_class.class_eval do
|
202
|
-
def foo
|
203
|
-
end
|
204
|
-
end
|
205
|
-
|
206
|
-
raise unless obj.respond_to? :foo
|
207
|
-
|
208
|
-
sth.has_foo = obj
|
209
|
-
assert_equal obj, sth.has_foo
|
210
|
-
assert_equal true, sth.valid?(:has_foo)
|
211
|
-
end
|
212
|
-
|
213
|
-
def test_responsible_arg2
|
214
|
-
sth = Sth.new
|
215
|
-
obj = Object.new
|
216
|
-
|
217
|
-
raise if obj.respond_to? :foo
|
218
|
-
raise if obj.respond_to? :bar
|
219
|
-
|
220
|
-
assert_raises Validation::InvalidWritingError do
|
221
|
-
sth.has_foo_and_bar = obj
|
222
|
-
end
|
223
|
-
|
224
|
-
obj.singleton_class.class_eval do
|
225
|
-
def foo
|
226
|
-
end
|
227
|
-
end
|
228
|
-
|
229
|
-
assert_raises Validation::InvalidWritingError do
|
230
|
-
sth.has_foo_and_bar = obj
|
231
|
-
end
|
232
|
-
|
233
|
-
raise unless obj.respond_to? :foo
|
234
|
-
|
235
|
-
obj.singleton_class.class_eval do
|
236
|
-
def bar
|
237
|
-
end
|
238
|
-
end
|
239
|
-
|
240
|
-
raise unless obj.respond_to? :bar
|
241
|
-
|
242
|
-
sth.has_foo_and_bar = obj
|
243
|
-
assert_equal obj, sth.has_foo_and_bar
|
244
|
-
assert_equal true, sth.valid?(:has_foo_and_bar)
|
245
|
-
end
|
246
|
-
|
247
|
-
end
|
248
|
-
|
249
|
-
class Test_Striuct_Subclass_Instance_SpecificConditions_FunctionalCondition < Test::Unit::TestCase
|
250
|
-
|
251
|
-
Sthlambda = Striuct.new do
|
252
|
-
member :lank, ->lank{lanks.include? lank}
|
253
|
-
member :lanks
|
254
|
-
default :lanks, 1..30
|
255
|
-
end
|
256
|
-
|
257
|
-
def test_lambda
|
258
|
-
sth = Sthlambda.new
|
259
|
-
sth.lank = 2
|
260
|
-
assert_equal 2, sth.lank
|
261
|
-
|
262
|
-
assert_raises Validation::InvalidWritingError do
|
263
|
-
sth.lank = 31
|
264
|
-
end
|
265
|
-
end
|
266
|
-
|
267
|
-
max = 9
|
268
|
-
|
269
|
-
SthProc = Striuct.new do
|
270
|
-
member :lank, Proc.new{|n|(3..max) === n}
|
271
|
-
end
|
272
|
-
|
273
|
-
def test_Proc
|
274
|
-
sth = SthProc.new
|
275
|
-
sth.lank = 8
|
276
|
-
assert_equal 8, sth.lank
|
277
|
-
|
278
|
-
assert_raises Validation::InvalidWritingError do
|
279
|
-
sth.lank = 2
|
280
|
-
end
|
281
|
-
end
|
282
|
-
|
283
|
-
SthMethod = Striuct.new do
|
284
|
-
member :lank, 7.method(:<)
|
285
|
-
end
|
286
|
-
|
287
|
-
def test_Method
|
288
|
-
sth = SthMethod.new
|
289
|
-
sth.lank = 8
|
290
|
-
assert_equal 8, sth.lank
|
291
|
-
|
292
|
-
assert_raises Validation::InvalidWritingError do
|
293
|
-
sth.lank = 6
|
294
|
-
end
|
295
|
-
end
|
296
|
-
|
297
|
-
end
|