2003-09-24 20:03:11 -04:00
|
|
|
require 'test/unit'
|
|
|
|
require 'ostruct'
|
|
|
|
|
|
|
|
class TC_OpenStruct < Test::Unit::TestCase
|
2012-10-28 17:19:50 -04:00
|
|
|
def test_initialize
|
|
|
|
h = {name: "John Smith", age: 70, pension: 300}
|
|
|
|
assert_equal h, OpenStruct.new(h).to_h
|
|
|
|
assert_equal h, OpenStruct.new(OpenStruct.new(h)).to_h
|
|
|
|
assert_equal h, OpenStruct.new(Struct.new(*h.keys).new(*h.values)).to_h
|
|
|
|
end
|
|
|
|
|
2003-09-24 20:03:11 -04:00
|
|
|
def test_equality
|
|
|
|
o1 = OpenStruct.new
|
|
|
|
o2 = OpenStruct.new
|
|
|
|
assert_equal(o1, o2)
|
|
|
|
|
|
|
|
o1.a = 'a'
|
|
|
|
assert_not_equal(o1, o2)
|
|
|
|
|
|
|
|
o2.a = 'a'
|
|
|
|
assert_equal(o1, o2)
|
|
|
|
|
|
|
|
o1.a = 'b'
|
|
|
|
assert_not_equal(o1, o2)
|
|
|
|
|
|
|
|
o2 = Object.new
|
|
|
|
o2.instance_eval{@table = {:a => 'b'}}
|
|
|
|
assert_not_equal(o1, o2)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2009-01-13 07:52:23 -05:00
|
|
|
def test_inspect
|
|
|
|
foo = OpenStruct.new
|
|
|
|
assert_equal("#<OpenStruct>", foo.inspect)
|
|
|
|
foo.bar = 1
|
|
|
|
foo.baz = 2
|
|
|
|
assert_equal("#<OpenStruct bar=1, baz=2>", foo.inspect)
|
|
|
|
|
|
|
|
foo = OpenStruct.new
|
|
|
|
foo.bar = OpenStruct.new
|
|
|
|
assert_equal('#<OpenStruct bar=#<OpenStruct>>', foo.inspect)
|
|
|
|
foo.bar.foo = foo
|
|
|
|
assert_equal('#<OpenStruct bar=#<OpenStruct foo=#<OpenStruct ...>>>', foo.inspect)
|
|
|
|
end
|
2009-02-15 07:43:46 -05:00
|
|
|
|
|
|
|
def test_frozen
|
|
|
|
o = OpenStruct.new
|
|
|
|
o.a = 'a'
|
|
|
|
o.freeze
|
|
|
|
assert_raise(TypeError) {o.b = 'b'}
|
|
|
|
assert_not_respond_to(o, :b)
|
2009-02-17 04:57:12 -05:00
|
|
|
assert_raise(TypeError) {o.a = 'z'}
|
|
|
|
assert_equal('a', o.a)
|
2009-02-27 00:23:10 -05:00
|
|
|
o = OpenStruct.new :a => 42
|
|
|
|
def o.frozen?; nil end
|
|
|
|
o.freeze
|
|
|
|
assert_raise(TypeError, '[ruby-core:22559]') {o.a = 1764}
|
2009-02-15 07:43:46 -05:00
|
|
|
end
|
2010-11-03 01:17:25 -04:00
|
|
|
|
|
|
|
def test_delete_field
|
|
|
|
bug = '[ruby-core:33010]'
|
|
|
|
o = OpenStruct.new
|
|
|
|
assert_not_respond_to(o, :a)
|
|
|
|
assert_not_respond_to(o, :a=)
|
|
|
|
o.a = 'a'
|
|
|
|
assert_respond_to(o, :a)
|
|
|
|
assert_respond_to(o, :a=)
|
2012-02-22 13:59:03 -05:00
|
|
|
a = o.delete_field :a
|
2010-11-03 01:17:25 -04:00
|
|
|
assert_not_respond_to(o, :a, bug)
|
|
|
|
assert_not_respond_to(o, :a=, bug)
|
2012-02-22 13:59:03 -05:00
|
|
|
assert_equal(a, 'a')
|
2010-11-03 01:17:25 -04:00
|
|
|
end
|
2011-05-27 11:59:02 -04:00
|
|
|
|
|
|
|
def test_method_missing_handles_square_bracket_equals
|
|
|
|
o = OpenStruct.new
|
|
|
|
assert_raise(NoMethodError) { o[:foo] = :bar }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_method_missing_handles_square_brackets
|
|
|
|
o = OpenStruct.new
|
|
|
|
assert_raise(NoMethodError) { o[:foo] }
|
|
|
|
end
|
|
|
|
|
2012-04-23 23:46:55 -04:00
|
|
|
def test_to_h
|
|
|
|
h = {name: "John Smith", age: 70, pension: 300}
|
|
|
|
os = OpenStruct.new(h)
|
|
|
|
to_h = os.to_h
|
|
|
|
assert_equal(h, to_h)
|
|
|
|
|
|
|
|
to_h[:age] = 71
|
|
|
|
assert_equal(70, os.age)
|
|
|
|
assert_equal(70, h[:age])
|
|
|
|
|
|
|
|
assert_equal(h, OpenStruct.new("name" => "John Smith", "age" => 70, pension: 300).to_h)
|
|
|
|
end
|
2012-10-28 17:18:53 -04:00
|
|
|
|
|
|
|
def test_each_pair
|
|
|
|
h = {name: "John Smith", age: 70, pension: 300}
|
|
|
|
os = OpenStruct.new(h)
|
|
|
|
assert_equal '#<Enumerator: #<OpenStruct name="John Smith", age=70, pension=300>:each_pair>', os.each_pair.inspect
|
|
|
|
assert_equal [[:name, "John Smith"], [:age, 70], [:pension, 300]], os.each_pair.to_a
|
|
|
|
end
|
2012-10-28 17:19:15 -04:00
|
|
|
|
|
|
|
def test_eql_and_hash
|
|
|
|
os1 = OpenStruct.new age: 70
|
|
|
|
os2 = OpenStruct.new age: 70.0
|
|
|
|
assert_equal os1, os2
|
|
|
|
assert_equal false, os1.eql?(os2)
|
|
|
|
assert_not_equal os1.hash, os2.hash
|
|
|
|
assert_equal true, os1.eql?(os1.dup)
|
|
|
|
assert_equal os1.hash, os1.dup.hash
|
|
|
|
end
|
2003-09-24 20:03:11 -04:00
|
|
|
end
|