striuct 0.3.0 → 0.3.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.
- 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
@@ -0,0 +1,153 @@
|
|
1
|
+
require_relative 'helper'
|
2
|
+
|
3
|
+
class TestStriuctHashLike < Test::Unit::TestCase
|
4
|
+
|
5
|
+
Sth = Striuct.new :foo, :bar, :hoge
|
6
|
+
|
7
|
+
def test_empty?
|
8
|
+
sth = Sth[hoge: 7, foo: 8]
|
9
|
+
assert_equal false, sth.empty?
|
10
|
+
sth.each_member{|name|sth[name] = nil}
|
11
|
+
assert_equal false, sth.empty?
|
12
|
+
sth.each_member{|name|sth.unassign name}
|
13
|
+
assert_equal true, sth.empty?
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_has_value?
|
17
|
+
sth = Sth[hoge: 7, foo: 8]
|
18
|
+
assert_equal true, sth.value?(7)
|
19
|
+
assert_equal true, sth.value?(8)
|
20
|
+
assert_equal false, sth.value?(9)
|
21
|
+
assert_equal false, sth.value?(nil)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_select!
|
25
|
+
sth = Sth[hoge: 7, foo: 8]
|
26
|
+
sth2, sth3 = sth.dup, sth.dup
|
27
|
+
|
28
|
+
assert_kind_of Enumerator, enum = sth.select!
|
29
|
+
assert_equal [:foo, 8], enum.next
|
30
|
+
assert_equal [:bar, nil], enum.next
|
31
|
+
assert_equal [:hoge, 7], enum.next
|
32
|
+
assert_raises StopIteration do
|
33
|
+
enum.next
|
34
|
+
end
|
35
|
+
|
36
|
+
assert_equal nil, sth2.select!{|k, v|true}
|
37
|
+
assert_equal sth3, sth3.select!{|k, v|k == :hoge && v == 7}
|
38
|
+
assert_equal nil, sth3.foo
|
39
|
+
assert_equal true, sth3.assign?(:hoge)
|
40
|
+
assert_equal false, sth3.assign?(:foo)
|
41
|
+
assert_equal 7, sth3.hoge
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_keep_if
|
45
|
+
sth = Sth[hoge: 7, foo: 8]
|
46
|
+
sth2, sth3 = sth.dup, sth.dup
|
47
|
+
|
48
|
+
assert_kind_of Enumerator, enum = sth.keep_if
|
49
|
+
assert_equal [:foo, 8], enum.next
|
50
|
+
assert_equal [:bar, nil], enum.next
|
51
|
+
assert_equal [:hoge, 7], enum.next
|
52
|
+
assert_raises StopIteration do
|
53
|
+
enum.next
|
54
|
+
end
|
55
|
+
|
56
|
+
assert_equal sth2, sth2.keep_if{|k, v|true}
|
57
|
+
assert_equal sth3, sth3.keep_if{|k, v|k == :hoge && v == 7}
|
58
|
+
assert_equal nil, sth3.foo
|
59
|
+
assert_equal true, sth3.assign?(:hoge)
|
60
|
+
assert_equal false, sth3.assign?(:foo)
|
61
|
+
assert_equal 7, sth3.hoge
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_reject!
|
65
|
+
sth = Sth[hoge: 7, foo: 8]
|
66
|
+
sth2, sth3 = sth.dup, sth.dup
|
67
|
+
|
68
|
+
assert_kind_of Enumerator, enum = sth.reject!
|
69
|
+
assert_equal [:foo, 8], enum.next
|
70
|
+
assert_equal [:bar, nil], enum.next
|
71
|
+
assert_equal [:hoge, 7], enum.next
|
72
|
+
assert_raises StopIteration do
|
73
|
+
enum.next
|
74
|
+
end
|
75
|
+
|
76
|
+
enum.rewind
|
77
|
+
assert_equal [:foo, 8], enum.next
|
78
|
+
|
79
|
+
assert_equal nil, sth2.reject!{|k, v|false}
|
80
|
+
assert_equal sth3, sth3.reject!{|k, v|k == :hoge && v == 7}
|
81
|
+
assert_equal nil, sth3.hoge
|
82
|
+
assert_equal false, sth3.assign?(:hoge)
|
83
|
+
assert_equal true, sth3.assign?(:foo)
|
84
|
+
assert_equal 8, sth3.foo
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_delete_if
|
88
|
+
sth = Sth[hoge: 7, foo: 8]
|
89
|
+
sth2, sth3 = sth.dup, sth.dup
|
90
|
+
|
91
|
+
assert_kind_of Enumerator, enum = sth.delete_if
|
92
|
+
|
93
|
+
assert_equal [:foo, 8], enum.next
|
94
|
+
assert_equal [:bar, nil], enum.next
|
95
|
+
assert_equal [:hoge, 7], enum.next
|
96
|
+
assert_raises StopIteration do
|
97
|
+
enum.next
|
98
|
+
end
|
99
|
+
|
100
|
+
assert_equal sth2, sth2.delete_if{|k, v|false}
|
101
|
+
assert_equal sth3, sth3.delete_if{|k, v|k == :hoge && v == 7}
|
102
|
+
assert_equal nil, sth3.hoge
|
103
|
+
assert_equal false, sth3.assign?(:hoge)
|
104
|
+
assert_equal true, sth3.assign?(:foo)
|
105
|
+
assert_equal 8, sth3.foo
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_assoc
|
109
|
+
sth = Sth[hoge: 7, foo: 8]
|
110
|
+
|
111
|
+
assert_equal [:foo, 8], sth.assoc(:foo)
|
112
|
+
assert_equal [:bar, nil], sth.assoc(:bar)
|
113
|
+
assert_equal [:hoge, 7], sth.assoc(:hoge)
|
114
|
+
|
115
|
+
assert_raises NameError do
|
116
|
+
sth.assoc(:dummy)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_rassoc
|
121
|
+
sth = Sth[hoge: 7, foo: 7]
|
122
|
+
|
123
|
+
assert_equal [:foo, 7], sth.rassoc(7)
|
124
|
+
assert_equal [:bar, nil], sth.rassoc(nil)
|
125
|
+
assert_equal nil, sth.rassoc(9)
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_flatten
|
129
|
+
sth = Sth[hoge: 7, foo: 8, bar: [1, 2]]
|
130
|
+
assert_equal [:foo, 8, :bar, [1, 2], :hoge, 7], sth.flatten
|
131
|
+
assert_equal [:foo, 8, :bar, [1, 2], :hoge, 7], sth.flatten(1)
|
132
|
+
assert_equal [:foo, 8, :bar, 1, 2, :hoge, 7], sth.flatten(2)
|
133
|
+
assert_equal [:foo, 8, :bar, 1, 2, :hoge, 7], sth.flatten(3)
|
134
|
+
assert_equal [:foo, 8, :bar, 1, 2, :hoge, 7], sth.flatten(-1)
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_select
|
138
|
+
sth = Sth[hoge: 7, foo: 8]
|
139
|
+
|
140
|
+
assert_kind_of Enumerator, sth.select
|
141
|
+
|
142
|
+
assert_equal Sth[hoge: 7], sth.select{|k, v|k == :hoge}
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_reject
|
146
|
+
sth = Sth[hoge: 7, foo: 8]
|
147
|
+
|
148
|
+
assert_kind_of Enumerator, sth.reject
|
149
|
+
|
150
|
+
assert_equal Sth[foo: 8], sth.reject{|k, v|k == :hoge}
|
151
|
+
end
|
152
|
+
|
153
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require_relative 'helper'
|
2
|
+
|
3
|
+
class TestStriuctInference < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_inference
|
6
|
+
klass = Striuct.define do
|
7
|
+
member :n, Numeric, inference: true
|
8
|
+
member :m, ANYTHING?, inference: true
|
9
|
+
end
|
10
|
+
|
11
|
+
sth, sth2 = klass.new, klass.new
|
12
|
+
|
13
|
+
assert_raises Validation::InvalidWritingError do
|
14
|
+
sth.n = '1'
|
15
|
+
end
|
16
|
+
|
17
|
+
sth.n = 1.1
|
18
|
+
|
19
|
+
assert_equal 1.1, sth.n
|
20
|
+
|
21
|
+
assert_raises Validation::InvalidWritingError do
|
22
|
+
sth.n = 1
|
23
|
+
end
|
24
|
+
|
25
|
+
assert_raises Validation::InvalidWritingError do
|
26
|
+
sth2.n = 1
|
27
|
+
end
|
28
|
+
|
29
|
+
sth.n = 2.1
|
30
|
+
|
31
|
+
assert_equal 2.1, sth.n
|
32
|
+
|
33
|
+
|
34
|
+
sth2.m = 1
|
35
|
+
|
36
|
+
assert_equal 1, sth2.m
|
37
|
+
|
38
|
+
assert_raises Validation::InvalidWritingError do
|
39
|
+
sth.m = 1.0
|
40
|
+
end
|
41
|
+
|
42
|
+
assert_raises Validation::InvalidWritingError do
|
43
|
+
sth2.m = 1.0
|
44
|
+
end
|
45
|
+
|
46
|
+
sth.m = 2
|
47
|
+
|
48
|
+
assert_equal 2, sth.m
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require_relative 'helper'
|
2
|
+
|
3
|
+
class TestStriuctInheritable < Test::Unit::TestCase
|
4
|
+
|
5
|
+
Sth = Striuct.define do
|
6
|
+
member :foo, String
|
7
|
+
member :bar, OR(nil, Fixnum)
|
8
|
+
end
|
9
|
+
|
10
|
+
class SubSth < Sth
|
11
|
+
member :hoge, OR(nil, 1..3)
|
12
|
+
end
|
13
|
+
|
14
|
+
class SubSubSth < SubSth
|
15
|
+
member :rest, AND(/\d/, Symbol)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_inherit
|
19
|
+
assert_equal [*Sth.members, :hoge], SubSth.members
|
20
|
+
sth = Sth.new
|
21
|
+
substh = SubSth.new
|
22
|
+
|
23
|
+
assert_raises Validation::InvalidWritingError do
|
24
|
+
substh.bar = 'str'
|
25
|
+
end
|
26
|
+
|
27
|
+
assert_raises Validation::InvalidWritingError do
|
28
|
+
substh.hoge = 4
|
29
|
+
end
|
30
|
+
|
31
|
+
assert_raises NoMethodError do
|
32
|
+
sth.hoge = 3
|
33
|
+
end
|
34
|
+
|
35
|
+
assert_raises NoMethodError do
|
36
|
+
substh.rest = :a4
|
37
|
+
end
|
38
|
+
|
39
|
+
subsubsth = SubSubSth.new
|
40
|
+
|
41
|
+
assert_raises Validation::InvalidWritingError do
|
42
|
+
subsubsth.rest = 4
|
43
|
+
end
|
44
|
+
|
45
|
+
subsubsth.rest = :a4
|
46
|
+
|
47
|
+
assert_equal :a4, subsubsth[:rest]
|
48
|
+
|
49
|
+
assert_equal true, Sth.__send__(:closed?)
|
50
|
+
assert_equal false, SubSth.__send__(:closed?)
|
51
|
+
SubSth.__send__(:close)
|
52
|
+
assert_equal true, SubSth.__send__(:closed?)
|
53
|
+
assert_equal false, SubSubSth.__send__(:closed?)
|
54
|
+
SubSubSth.__send__(:close)
|
55
|
+
assert_equal true, SubSubSth.__send__(:closed?)
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,289 @@
|
|
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
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require_relative 'helper'
|
2
|
+
|
3
|
+
class TestStriuctLock < 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.lock?(:foo)
|
11
|
+
assert_equal false, sth.lock?(:bar)
|
12
|
+
assert_equal false, sth.secure?
|
13
|
+
sth.lock :bar
|
14
|
+
assert_equal true, sth.lock?(: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.lock?(:bar)
|
24
|
+
|
25
|
+
sth.bar = 1
|
26
|
+
assert_equal 1, sth.bar
|
27
|
+
|
28
|
+
sth.lock
|
29
|
+
assert_equal true, sth.lock?(:foo)
|
30
|
+
assert_equal true, sth.lock?(:bar)
|
31
|
+
assert_equal true, sth.lock?
|
32
|
+
|
33
|
+
assert_raises RuntimeError do
|
34
|
+
sth.foo = 1
|
35
|
+
end
|
36
|
+
|
37
|
+
assert_equal true, sth.secure?
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require_relative 'helper'
|
2
|
+
|
3
|
+
class TestStriuctSafetyNaming < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_cname?
|
6
|
+
klass = Striuct.new
|
7
|
+
assert_same false, klass.cname?(Object)
|
8
|
+
assert_same false, klass.cname?('m?')
|
9
|
+
assert_same false, klass.cname?('__foo__')
|
10
|
+
assert_same false, klass.cname?('a b')
|
11
|
+
assert_same false, klass.cname?('object_id')
|
12
|
+
assert_same false, klass.cname?('to_ary')
|
13
|
+
assert_same true, klass.cname?('foo')
|
14
|
+
klass.__send__ :protect_level, :warning
|
15
|
+
assert_same false, klass.cname?('m?')
|
16
|
+
assert_same false, klass.cname?('__foo__')
|
17
|
+
assert_same false, klass.cname?('a b')
|
18
|
+
assert_same false, klass.cname?('object_id')
|
19
|
+
assert_same false, klass.cname?('to_ary')
|
20
|
+
assert_same true, klass.cname?('foo')
|
21
|
+
klass.__send__ :protect_level, :struct
|
22
|
+
assert_same true, klass.cname?('m?')
|
23
|
+
assert_same true, klass.cname?('__foo__')
|
24
|
+
assert_same true, klass.cname?('a b')
|
25
|
+
assert_same true, klass.cname?('object_id')
|
26
|
+
assert_same true, klass.cname?('to_ary')
|
27
|
+
assert_same true, klass.cname?('foo')
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_protect
|
31
|
+
klass = Striuct.new
|
32
|
+
assert_raises NameError do
|
33
|
+
klass.class_eval do
|
34
|
+
member ''
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
assert_raises NameError do
|
39
|
+
klass.class_eval do
|
40
|
+
member :'a b'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
assert_raises NameError do
|
45
|
+
klass.class_eval do
|
46
|
+
member :'__send__'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
assert_raises NameError do
|
51
|
+
klass.class_eval do
|
52
|
+
member :'__foo__'
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
assert_raises NameError do
|
57
|
+
klass.class_eval do
|
58
|
+
member :'m?'
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
assert_same false, klass.member?('m?')
|
63
|
+
|
64
|
+
klass.class_eval do
|
65
|
+
protect_level :struct
|
66
|
+
member :'m?'
|
67
|
+
end
|
68
|
+
|
69
|
+
assert_same true, klass.member?('m?')
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|