4 assert('Struct', '15.2.18') do
5 assert_equal Class, Struct.class
8 assert('Struct.new', '15.2.18.3.1') do
9 c = Struct.new(:m1, :m2)
10 assert_equal Struct, c.superclass
11 assert_equal [:m1, :m2], c.members
14 assert('Struct#==', '15.2.18.4.1') do
15 c = Struct.new(:m1, :m2)
18 assert_true cc1 == cc2
20 Struct.new(:m1, :m2) { def foo; end }
21 assert_raise(NoMethodError) { Struct.new(:m1).new.foo }
24 assert('Struct#[]', '15.2.18.4.2') do
25 c = Struct.new(:m1, :m2)
27 assert_equal 1, cc[:m1]
28 assert_equal 2, cc["m2"]
30 assert_equal 2, cc[-1]
31 assert_raise(TypeError) { cc[[]] }
32 assert_raise(IndexError) { cc[2] }
33 assert_raise(NameError) { cc['tama'] }
36 assert('Struct#[]=', '15.2.18.4.3') do
37 c = Struct.new(:m1, :m2)
40 assert_equal 3, cc[:m1]
42 assert_equal 3, cc["m2"]
46 assert_equal 5, cc[-1]
47 assert_raise(TypeError) { cc[[]] = 3 }
48 assert_raise(IndexError) { cc[2] = 7 }
49 assert_raise(NameError) { cc['pochi'] = 8 }
52 assert('Struct#each', '15.2.18.4.4') do
53 c = Struct.new(:m1, :m2)
59 assert_equal [1, 2], a
62 assert('Struct#each_pair', '15.2.18.4.5') do
63 c = Struct.new(:m1, :m2)
69 assert_equal [[:m1, 1], [:m2, 2]], a
72 assert('Struct#members', '15.2.18.4.6') do
73 c = Struct.new(:m1, :m2)
74 assert_equal [:m1, :m2], c.new(1,2).members
77 assert('Struct#select', '15.2.18.4.7') do
78 c = Struct.new(:m1, :m2)
79 assert_equal([2]) { c.new(1,2).select{|v| v % 2 == 0} }
82 assert('large struct') do
83 c = Struct.new(:m1, :m2, :m3, :m4, :m5, :m6, :m7, :m8, :m9, :m10, :m11, :m12, :m13)
84 cc = c.new(1,2,3,4,5,6,7,8,9,10,11,12,13)
94 assert_equal 10, cc.m10
95 assert_equal 13, cc.m13
98 assert_equal 'test', cc.m13
100 assert_raise(NoMethodError) { cc.m14 }
103 assert('wrong struct arg count') do
105 assert_raise ArgumentError do
110 assert('struct dup') do
111 c = Struct.new(:m1, :m2, :m3, :m4, :m5)
112 cc = c.new(1,2,3,4,5)
113 assert_nothing_raised {
114 assert_equal(cc, cc.dup)
118 assert('struct inspect') do
119 c = Struct.new(:m1, :m2, :m3, :m4, :m5, :recur)
120 cc = c.new(1,2,3,4,5,nil)
122 assert_equal "#<struct m1=1, m2=2, m3=3, m4=4, m5=5, recur=#<struct #{cc.class}:...>>", cc.inspect
125 assert('Struct#length, Struct#size') do
126 s = Struct.new(:f1, :f2).new(0, 1)
127 assert_equal 2, s.size
128 assert_equal 2, s.length
131 assert('Struct#to_a, Struct#values') do
132 s = Struct.new(:mem1, :mem2).new('a', 'b')
133 assert_equal ['a', 'b'], s.to_a
134 assert_equal ['a', 'b'], s.values
137 assert('Struct#to_h') do
138 s = Struct.new(:white, :red, :green).new('ruuko', 'yuzuki', 'hitoe')
139 assert_equal(:white => 'ruuko', :red => 'yuzuki', :green => 'hitoe') { s.to_h }
142 assert('Struct#values_at') do
143 a = Struct.new(:blue, :purple).new('aki', 'io')
144 assert_equal ['aki'], a.values_at(0)
145 assert_equal ['io', 'aki'], a.values_at(1, 0)
146 assert_raise(IndexError) { a.values_at 2 }
149 assert("Struct#dig") do
150 a = Struct.new(:blue, :purple).new('aki', Struct.new(:red).new(1))
151 assert_equal 'aki', a.dig(:blue)
152 assert_equal 1, a.dig(:purple, :red)
153 assert_equal 1, a.dig(1, 0)
156 # TODO: suppress redefining Struct warning during test
157 # assert("Struct.new removes existing constant") do
159 # assert_not_equal Struct.new("Test", :a), Struct.new("Test", :a, :b)
161 # Struct.remove_const :Test
165 assert("Struct#initialize_copy requires struct to be the same type") do
167 Struct.new("Test", :a)
168 a = Struct::Test.new("a")
169 Struct.remove_const :Test
170 Struct.new("Test", :a, :b)
171 assert_raise(TypeError) do
172 a.initialize_copy(Struct::Test.new("a", "b"))
175 Struct.remove_const :Test
179 assert("Struct.new does not allow array") do
180 assert_raise(TypeError) do
181 Struct.new("Test", [:a])
185 assert("Struct.new does not allow invalid class name") do
186 assert_raise(NameError) { Struct.new("Test-", :a) }
189 assert("Struct.new generates subclass of Struct") do
191 original_struct = Struct
193 assert_equal original_struct, original_struct.new(:foo).superclass
195 Struct = original_struct
199 assert 'Struct#freeze' do
204 assert_equal :test, o.m
207 assert_raise(FrozenError) { o.m = :modify }
208 assert_raise(FrozenError) { o[:m] = :modify }
209 assert_equal :test, o.m