1 require 'rubicon_testcase'
3 class TestStruct < RubiconTestCase
5 # TODO: why is this a class variable, and why in setup?
7 @@struct ||= Struct.new 'TestStruct', :alpha, :bravo
11 for taint in [ false, true ]
12 for frozen in [ false, true ]
20 assert(a.__id__ != b.__id__)
21 assert_equal(a.frozen?, b.frozen?)
22 assert_equal(a.tainted?, b.tainted?)
23 assert_equal(a.alpha, b.alpha)
29 # TODO: raise NotImplementedError, 'Need to write test_each_pair'
33 assert_raises LocalJumpError do
38 @@struct.new('a', 'b').each { |x| a << x }
39 assert_equal ['a', 'b'], a
43 # TODO: raise NotImplementedError, 'Need to write test_eql_eh'
47 ts1 = @@struct.new 64, 42
48 ts2 = @@struct.new 64, 42
54 assert_not_equal ts1, ts3
56 os1 = Struct.new('OtherStruct', :alpha, :bravo).new 64, 42
58 assert_not_equal os1, ts1
60 os2 = Struct.new('OtherStruct2', :alpha, :bravo, :charlie).new 64, 42
62 assert_not_equal os2, ts1
66 # TODO: raise NotImplementedError, 'Need to write test_hash'
71 assert_nothing_raised do
73 assert_equal t.alpha, 64
76 assert_equal t.bravo, 112
79 assert_equal t.alpha, 65
82 assert_equal t.bravo, 113
85 assert_equal t.alpha, 66
88 assert_raises NameError do
91 assert_raise IndexError do
97 t = @@struct.new 64, 112
99 assert_equal 64, t['alpha']
100 assert_equal 64, t[:alpha]
102 assert_equal 64, t[0]
103 assert_equal 112, t[1]
104 assert_equal 112, t[-1]
106 assert_equal 112, t[1.5]
108 assert_raises NameError do
112 assert_raises IndexError do
118 assert_instance_of Class, @@struct
119 assert_equal Struct::TestStruct, @@struct
120 assert_instance_of Struct::TestStruct, @@struct.new(5)
123 assert_equal nil, t1.alpha
124 assert_equal nil, t1.bravo
127 assert_equal 1, t2.alpha
128 assert_equal nil, t2.bravo
130 t3 = @@struct.new 2, 3
131 assert_equal 2, t3.alpha
132 assert_equal 3, t3.bravo
134 assert_raises ArgumentError do
140 expected = "#<struct Struct::TestStruct alpha=\"a\", bravo=\"b\">"
141 assert_equal expected, @@struct.new('a', 'b').to_s
145 s = @@struct.new('a', 'b')
146 expected = "#<struct Struct::TestStruct alpha=\"a\", bravo=\"b\">"
147 assert_equal expected, s.inspect
152 assert_equal(2,t.length)
156 assert_equal ["alpha", "bravo"], @@struct.members
160 assert_equal ["alpha", "bravo"], @@struct.new.members
164 struct = @@struct.new 'a', 'b'
165 assert_equal ['a'], struct.select { |item| item == 'a' }
171 assert_equal(2, t.size)
175 t = @@struct.new 'a', 'b'
176 assert_equal ['a', 'b'], t.to_a
180 struct = @@struct.new 'a', 'b'
181 assert_equal ['b'], struct.values_at(-1)
182 assert_equal ['a'], struct.values_at(0)
183 assert_equal ['b'], struct.values_at(1)
184 assert_equal ['a', 'b'], struct.values_at(0..1)
188 t = @@struct.new 'a', 'b'
189 assert_equal ['a', 'b'], t.values