2017-10-25 19:45:33 -04:00
|
|
|
# frozen_string_literal: true
|
2003-09-24 20:03:11 -04:00
|
|
|
require 'test/unit'
|
|
|
|
require 'ostruct'
|
2020-09-26 01:27:23 -04:00
|
|
|
require 'yaml'
|
2003-09-24 20:03:11 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-10-14 12:57:46 -04:00
|
|
|
def test_respond_to
|
|
|
|
o = OpenStruct.new
|
|
|
|
o.a = 1
|
|
|
|
assert_respond_to(o, :a)
|
|
|
|
assert_respond_to(o, :a=)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_respond_to_with_lazy_getter
|
|
|
|
o = OpenStruct.new a: 1
|
|
|
|
assert_respond_to(o, :a)
|
|
|
|
assert_respond_to(o, :a=)
|
|
|
|
end
|
|
|
|
|
2015-12-28 22:48:36 -05:00
|
|
|
def test_respond_to_allocated
|
|
|
|
assert_not_respond_to(OpenStruct.allocate, :a)
|
|
|
|
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)
|
2017-10-24 14:08:15 -04:00
|
|
|
assert_equal(false, foo.inspect.frozen?)
|
2009-01-13 07:52:23 -05:00
|
|
|
|
|
|
|
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)
|
2017-10-24 14:08:15 -04:00
|
|
|
assert_equal(false, foo.inspect.frozen?)
|
2009-01-13 07:52:23 -05:00
|
|
|
end
|
2009-02-15 07:43:46 -05:00
|
|
|
|
|
|
|
def test_frozen
|
2016-01-01 12:27:38 -05:00
|
|
|
o = OpenStruct.new(foo: 42)
|
2009-02-15 07:43:46 -05:00
|
|
|
o.a = 'a'
|
|
|
|
o.freeze
|
2020-09-02 15:34:33 -04:00
|
|
|
assert_raise(FrozenError) {o.b = 'b'}
|
2009-02-15 07:43:46 -05:00
|
|
|
assert_not_respond_to(o, :b)
|
2020-09-02 15:34:33 -04:00
|
|
|
assert_raise(FrozenError) {o.a = 'z'}
|
2009-02-17 04:57:12 -05:00
|
|
|
assert_equal('a', o.a)
|
2016-01-01 12:27:38 -05:00
|
|
|
assert_equal(42, o.foo)
|
2009-02-27 00:23:10 -05:00
|
|
|
o = OpenStruct.new :a => 42
|
|
|
|
def o.frozen?; nil end
|
|
|
|
o.freeze
|
2020-09-02 15:34:33 -04:00
|
|
|
assert_raise(FrozenError, '[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)
|
2021-06-14 09:53:20 -04:00
|
|
|
assert_equal('a', a)
|
2013-10-23 11:13:38 -04:00
|
|
|
s = Object.new
|
|
|
|
def s.to_sym
|
|
|
|
:foo
|
|
|
|
end
|
|
|
|
o[s] = true
|
|
|
|
assert_respond_to(o, :foo)
|
|
|
|
assert_respond_to(o, :foo=)
|
|
|
|
o.delete_field s
|
|
|
|
assert_not_respond_to(o, :foo)
|
|
|
|
assert_not_respond_to(o, :foo=)
|
2021-06-14 09:53:20 -04:00
|
|
|
|
|
|
|
assert_raise(NameError) { o.delete_field(s) }
|
|
|
|
assert_equal(:bar, o.delete_field(s) { :bar })
|
|
|
|
|
|
|
|
o[s] = :foobar
|
|
|
|
assert_respond_to(o, :foo)
|
|
|
|
assert_respond_to(o, :foo=)
|
|
|
|
assert_equal(:foobar, o.delete_field(s) { :baz })
|
|
|
|
|
|
|
|
assert_equal(42, OpenStruct.new(foo: 42).delete_field(:foo) { :bug })
|
2021-06-14 10:07:51 -04:00
|
|
|
|
|
|
|
o = OpenStruct.new(block_given?: 42)
|
|
|
|
assert_raise(NameError) { o.delete_field(:foo) }
|
2010-11-03 01:17:25 -04:00
|
|
|
end
|
2011-05-27 11:59:02 -04:00
|
|
|
|
2012-10-28 17:20:10 -04:00
|
|
|
def test_setter
|
|
|
|
os = OpenStruct.new
|
|
|
|
os[:foo] = :bar
|
|
|
|
assert_equal :bar, os.foo
|
|
|
|
os['foo'] = :baz
|
|
|
|
assert_equal :baz, os.foo
|
2011-05-27 11:59:02 -04:00
|
|
|
end
|
|
|
|
|
2012-10-28 17:20:10 -04:00
|
|
|
def test_getter
|
|
|
|
os = OpenStruct.new
|
|
|
|
os.foo = :bar
|
|
|
|
assert_equal :bar, os[:foo]
|
|
|
|
assert_equal :bar, os['foo']
|
2011-05-27 11:59:02 -04:00
|
|
|
end
|
|
|
|
|
2015-11-17 00:36:03 -05:00
|
|
|
def test_dig
|
|
|
|
os1 = OpenStruct.new
|
|
|
|
os2 = OpenStruct.new
|
|
|
|
os1.child = os2
|
|
|
|
os2.foo = :bar
|
|
|
|
os2.child = [42]
|
|
|
|
assert_equal :bar, os1.dig("child", :foo)
|
|
|
|
assert_nil os1.dig("parent", :foo)
|
2015-12-12 16:40:50 -05:00
|
|
|
assert_raise(TypeError) { os1.dig("child", 0) }
|
2015-11-17 00:36:03 -05:00
|
|
|
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
|
|
|
|
2018-12-22 12:05:03 -05:00
|
|
|
def test_to_h_with_block
|
|
|
|
os = OpenStruct.new("country" => "Australia", :capital => "Canberra")
|
|
|
|
assert_equal({"country" => "AUSTRALIA", "capital" => "CANBERRA" },
|
|
|
|
os.to_h {|name, value| [name.to_s, value.upcase]})
|
|
|
|
assert_equal("Australia", os.country)
|
|
|
|
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)
|
2017-02-03 13:25:28 -05:00
|
|
|
assert_same os, os.each_pair{ }
|
2012-10-28 17:18:53 -04:00
|
|
|
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
|
2013-10-23 11:13:57 -04:00
|
|
|
assert_equal 3, os.each_pair.size
|
2012-10-28 17:18:53 -04:00
|
|
|
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
|
2013-10-23 11:14:17 -04:00
|
|
|
|
|
|
|
def test_method_missing
|
|
|
|
os = OpenStruct.new
|
|
|
|
e = assert_raise(NoMethodError) { os.foo true }
|
|
|
|
assert_equal :foo, e.name
|
|
|
|
assert_equal [true], e.args
|
2013-11-06 01:25:18 -05:00
|
|
|
assert_match(/#{__callee__}/, e.backtrace[0])
|
2013-10-23 11:14:17 -04:00
|
|
|
e = assert_raise(ArgumentError) { os.send :foo=, true, true }
|
2013-11-06 01:25:18 -05:00
|
|
|
assert_match(/#{__callee__}/, e.backtrace[0])
|
2013-10-23 11:14:17 -04:00
|
|
|
end
|
2015-12-31 00:37:21 -05:00
|
|
|
|
|
|
|
def test_accessor_defines_method
|
|
|
|
os = OpenStruct.new(foo: 42)
|
2020-02-25 03:47:17 -05:00
|
|
|
assert_respond_to(os, :foo)
|
2015-12-31 00:37:21 -05:00
|
|
|
assert_equal(42, os.foo)
|
2016-01-04 02:47:44 -05:00
|
|
|
assert_equal([:foo, :foo=], os.singleton_methods.sort)
|
2015-12-31 00:37:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_does_not_redefine
|
2020-09-24 06:30:22 -04:00
|
|
|
$VERBOSE, verbose_bak = nil, $VERBOSE
|
2015-12-31 00:37:21 -05:00
|
|
|
os = OpenStruct.new(foo: 42)
|
|
|
|
def os.foo
|
|
|
|
43
|
|
|
|
end
|
|
|
|
os.foo = 44
|
|
|
|
assert_equal(43, os.foo)
|
2020-09-24 06:30:22 -04:00
|
|
|
ensure
|
|
|
|
$VERBOSE = verbose_bak
|
2015-12-31 00:37:21 -05:00
|
|
|
end
|
2017-03-24 08:16:54 -04:00
|
|
|
|
|
|
|
def test_allocate_subclass
|
|
|
|
bug = '[ruby-core:80292] [Bug #13358] allocate should not call initialize'
|
|
|
|
c = Class.new(OpenStruct) {
|
|
|
|
def initialize(x,y={})super(y);end
|
|
|
|
}
|
|
|
|
os = assert_nothing_raised(ArgumentError, bug) {c.allocate}
|
|
|
|
assert_instance_of(c, os)
|
|
|
|
end
|
2017-03-28 05:21:58 -04:00
|
|
|
|
2020-09-25 20:55:38 -04:00
|
|
|
def test_initialize_subclass
|
|
|
|
c = Class.new(OpenStruct) {
|
|
|
|
def initialize(x,y={})super(y);end
|
|
|
|
}
|
|
|
|
o = c.new(1, {a: 42})
|
|
|
|
assert_equal(42, o.dup.a)
|
|
|
|
assert_equal(42, o.clone.a)
|
|
|
|
end
|
|
|
|
|
2017-03-28 05:21:58 -04:00
|
|
|
def test_private_method
|
|
|
|
os = OpenStruct.new
|
|
|
|
class << os
|
|
|
|
private
|
|
|
|
def foo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_raise_with_message(NoMethodError, /private method/) do
|
|
|
|
os.foo true, true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_protected_method
|
|
|
|
os = OpenStruct.new
|
|
|
|
class << os
|
|
|
|
protected
|
|
|
|
def foo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_raise_with_message(NoMethodError, /protected method/) do
|
|
|
|
os.foo true, true
|
|
|
|
end
|
|
|
|
end
|
2020-09-08 15:08:50 -04:00
|
|
|
|
2020-09-14 14:03:56 -04:00
|
|
|
def test_access_undefined
|
|
|
|
os = OpenStruct.new
|
|
|
|
assert_nil os.foo
|
|
|
|
end
|
|
|
|
|
2020-09-17 07:34:59 -04:00
|
|
|
def test_overridden_private_methods
|
2020-09-08 15:08:50 -04:00
|
|
|
os = OpenStruct.new(puts: :foo, format: :bar)
|
|
|
|
assert_equal(:foo, os.puts)
|
|
|
|
assert_equal(:bar, os.format)
|
|
|
|
end
|
2020-09-14 14:03:56 -04:00
|
|
|
|
2021-01-05 18:34:45 -05:00
|
|
|
def test_super
|
|
|
|
c = Class.new(OpenStruct) {
|
|
|
|
def foo; super; end
|
|
|
|
}
|
|
|
|
os = c.new(foo: :bar)
|
|
|
|
assert_equal(:bar, os.foo)
|
|
|
|
end
|
2021-01-06 03:34:18 -05:00
|
|
|
|
2020-09-17 07:34:59 -04:00
|
|
|
def test_overridden_public_methods
|
2020-09-14 14:03:56 -04:00
|
|
|
os = OpenStruct.new(method: :foo, class: :bar)
|
|
|
|
assert_equal(:foo, os.method)
|
|
|
|
assert_equal(:bar, os.class)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_access_original_methods
|
2020-09-25 20:58:48 -04:00
|
|
|
os = OpenStruct.new(method: :foo, hash: 42)
|
2020-09-14 14:03:56 -04:00
|
|
|
assert_equal(os.object_id, os.method!(:object_id).call)
|
2020-09-25 20:58:48 -04:00
|
|
|
assert_not_equal(42, os.hash!)
|
2021-12-06 23:05:53 -05:00
|
|
|
refute os.methods.include?(:"!~!")
|
2020-09-25 20:58:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_override_subclass
|
|
|
|
c = Class.new(OpenStruct) {
|
|
|
|
def foo; :protect_me; end
|
|
|
|
private def bar; :protect_me; end
|
|
|
|
def inspect; 'protect me'; end
|
|
|
|
}
|
|
|
|
o = c.new(
|
|
|
|
foo: 1, bar: 2, inspect: '3', # in subclass: protected
|
|
|
|
table!: 4, # bang method: protected
|
|
|
|
each_pair: 5, to_s: 'hello', # others: not protected
|
|
|
|
)
|
|
|
|
# protected:
|
|
|
|
assert_equal(:protect_me, o.foo)
|
|
|
|
assert_equal(:protect_me, o.send(:bar))
|
|
|
|
assert_equal('protect me', o.inspect)
|
|
|
|
assert_not_equal(4, o.send(:table!))
|
|
|
|
# not protected:
|
|
|
|
assert_equal(5, o.each_pair)
|
|
|
|
assert_equal('hello', o.to_s)
|
2020-09-14 14:03:56 -04:00
|
|
|
end
|
2020-09-14 13:48:29 -04:00
|
|
|
|
2021-01-12 17:20:50 -05:00
|
|
|
def test_override_submodule
|
|
|
|
m = Module.new {
|
|
|
|
def foo; :protect_me; end
|
|
|
|
private def bar; :protect_me; end
|
|
|
|
def inspect; 'protect me'; end
|
|
|
|
}
|
|
|
|
m2 = Module.new {
|
|
|
|
def added_to_all_open_struct; :override_me; end
|
|
|
|
}
|
|
|
|
OpenStruct.class_eval do
|
|
|
|
include m2
|
|
|
|
# prepend case tbd
|
|
|
|
def added_to_all_open_struct_2; :override_me; end
|
|
|
|
end
|
|
|
|
c = Class.new(OpenStruct) { include m }
|
|
|
|
o = c.new(
|
|
|
|
foo: 1, bar: 2, inspect: '3', # in subclass: protected
|
|
|
|
table!: 4, # bang method: protected
|
|
|
|
each_pair: 5, to_s: 'hello', # others: not protected
|
|
|
|
# including those added by the user:
|
|
|
|
added_to_all_open_struct: 6, added_to_all_open_struct_2: 7,
|
|
|
|
)
|
|
|
|
# protected:
|
|
|
|
assert_equal(:protect_me, o.foo)
|
|
|
|
assert_equal(:protect_me, o.send(:bar))
|
|
|
|
assert_equal('protect me', o.inspect)
|
|
|
|
assert_not_equal(4, o.send(:table!))
|
|
|
|
# not protected:
|
|
|
|
assert_equal(5, o.each_pair)
|
|
|
|
assert_equal('hello', o.to_s)
|
|
|
|
assert_equal(6, o.added_to_all_open_struct)
|
|
|
|
assert_equal(7, o.added_to_all_open_struct_2)
|
|
|
|
end
|
|
|
|
|
2020-09-14 13:48:29 -04:00
|
|
|
def test_mistaken_subclass
|
|
|
|
sub = Class.new(OpenStruct) do
|
|
|
|
def [](k)
|
|
|
|
__send__(k)
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def []=(k, v)
|
|
|
|
@item_set = true
|
|
|
|
__send__("#{k}=", v)
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
o = sub.new
|
|
|
|
o.foo = 42
|
|
|
|
assert_equal 42, o.foo
|
|
|
|
end
|
2020-09-25 21:17:06 -04:00
|
|
|
|
|
|
|
def test_ractor
|
2020-12-19 12:19:06 -05:00
|
|
|
assert_ractor(<<~RUBY, require: 'ostruct')
|
|
|
|
obj1 = OpenStruct.new(a: 42, b: 42)
|
|
|
|
obj1.c = 42
|
|
|
|
obj1.freeze
|
|
|
|
|
|
|
|
obj2 = Ractor.new obj1 do |obj|
|
|
|
|
obj
|
|
|
|
end.take
|
|
|
|
assert obj1.object_id == obj2.object_id
|
|
|
|
RUBY
|
2021-04-28 04:58:04 -04:00
|
|
|
end if defined?(Ractor)
|
2020-09-26 01:27:23 -04:00
|
|
|
|
2021-08-20 08:12:28 -04:00
|
|
|
def test_access_methods_from_different_ractor
|
|
|
|
assert_ractor(<<~RUBY, require: 'ostruct')
|
|
|
|
os = OpenStruct.new
|
|
|
|
os.value = 100
|
|
|
|
r = Ractor.new(os) do |x|
|
|
|
|
v = x.value
|
|
|
|
Ractor.yield v
|
|
|
|
end
|
|
|
|
assert 100 == r.take
|
|
|
|
RUBY
|
|
|
|
end if defined?(Ractor)
|
|
|
|
|
2020-09-26 01:27:23 -04:00
|
|
|
def test_legacy_yaml
|
|
|
|
s = "--- !ruby/object:OpenStruct\ntable:\n :foo: 42\n"
|
2021-06-14 10:15:11 -04:00
|
|
|
o = YAML.safe_load(s, permitted_classes: [Symbol, OpenStruct])
|
2020-09-26 01:27:23 -04:00
|
|
|
assert_equal(42, o.foo)
|
|
|
|
|
|
|
|
o = OpenStruct.new(table: {foo: 42})
|
2021-06-14 10:15:11 -04:00
|
|
|
assert_equal({foo: 42}, YAML.safe_load(YAML.dump(o), permitted_classes: [Symbol, OpenStruct]).table)
|
2021-07-29 02:52:32 -04:00
|
|
|
end if RUBY_VERSION >= '2.6'
|
2020-09-26 01:27:23 -04:00
|
|
|
|
|
|
|
def test_yaml
|
|
|
|
h = {name: "John Smith", age: 70, pension: 300.42}
|
|
|
|
yaml = "--- !ruby/object:OpenStruct\nname: John Smith\nage: 70\npension: 300.42\n"
|
|
|
|
os1 = OpenStruct.new(h)
|
2021-06-14 10:15:11 -04:00
|
|
|
os2 = YAML.safe_load(os1.to_yaml, permitted_classes: [Symbol, OpenStruct])
|
2020-09-26 01:27:23 -04:00
|
|
|
assert_equal yaml, os1.to_yaml
|
|
|
|
assert_equal os1, os2
|
|
|
|
assert_equal true, os1.eql?(os2)
|
|
|
|
assert_equal 300.42, os2.pension
|
2021-07-29 02:52:32 -04:00
|
|
|
end if RUBY_VERSION >= '2.6'
|
2020-10-06 17:10:46 -04:00
|
|
|
|
|
|
|
def test_marshal
|
|
|
|
o = OpenStruct.new(name: "John Smith", age: 70, pension: 300.42)
|
|
|
|
o2 = Marshal.load(Marshal.dump(o))
|
|
|
|
assert_equal o, o2
|
|
|
|
end
|
2022-01-18 08:54:52 -05:00
|
|
|
|
|
|
|
def test_class
|
|
|
|
os = OpenStruct.new(class: 'my-class', method: 'post')
|
|
|
|
assert_equal('my-class', os.class)
|
|
|
|
assert_equal(OpenStruct, os.class!)
|
|
|
|
end
|
2003-09-24 20:03:11 -04:00
|
|
|
end
|