Skip to content

Commit

Permalink
Merge pull request #6835 from headius/ostruct_mess
Browse files Browse the repository at this point in the history
Revert ostruct tests/specs due to reverted gem
  • Loading branch information
headius committed Sep 16, 2021
2 parents 806f6cb + 9d6b339 commit e2b3533
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 107 deletions.
5 changes: 5 additions & 0 deletions spec/tags/ruby/library/openstruct/to_h_tags.txt
@@ -0,0 +1,5 @@
fails(reverted ostruct due to https://github.com/ruby/ostruct/issues/30):OpenStruct#to_h with block converts [key, value] pairs returned by the block to a hash
fails(reverted ostruct due to https://github.com/ruby/ostruct/issues/30):OpenStruct#to_h with block raises ArgumentError if block returns longer or shorter array
fails(reverted ostruct due to https://github.com/ruby/ostruct/issues/30):OpenStruct#to_h with block raises TypeError if block returns something other than Array
fails(reverted ostruct due to https://github.com/ruby/ostruct/issues/30):OpenStruct#to_h with block coerces returned pair to Array with #to_ary
fails(reverted ostruct due to https://github.com/ruby/ostruct/issues/30):OpenStruct#to_h with block does not coerce returned pair to Array with #to_a
1 change: 1 addition & 0 deletions test/mri/excludes/TC_OpenStruct.rb
@@ -1 +1,2 @@
exclude :test_method_missing, "depends on __callee__ which is broken"
exclude :test_to_h_with_block, "test is newer than the released ostruct 0.1.0 (https://github.com/ruby/ostruct/issues/31)"
113 changes: 6 additions & 107 deletions test/mri/ostruct/test_ostruct.rb
@@ -1,7 +1,6 @@
# frozen_string_literal: true
require 'test/unit'
require 'ostruct'
require 'yaml'

class TC_OpenStruct < Test::Unit::TestCase
def test_initialize
Expand Down Expand Up @@ -67,15 +66,16 @@ def test_frozen
o = OpenStruct.new(foo: 42)
o.a = 'a'
o.freeze
assert_raise(FrozenError) {o.b = 'b'}
expected_error = defined?(FrozenError) ? FrozenError : RuntimeError
assert_raise(expected_error) {o.b = 'b'}
assert_not_respond_to(o, :b)
assert_raise(FrozenError) {o.a = 'z'}
assert_raise(expected_error) {o.a = 'z'}
assert_equal('a', o.a)
assert_equal(42, o.foo)
o = OpenStruct.new :a => 42
def o.frozen?; nil end
o.freeze
assert_raise(FrozenError, '[ruby-core:22559]') {o.a = 1764}
assert_raise(expected_error, '[ruby-core:22559]') {o.a = 1764}
end

def test_delete_field
Expand Down Expand Up @@ -179,7 +179,8 @@ def test_method_missing

def test_accessor_defines_method
os = OpenStruct.new(foo: 42)
assert_respond_to(os, :foo)
assert os.respond_to? :foo
assert_equal([], os.singleton_methods)
assert_equal(42, os.foo)
assert_equal([:foo, :foo=], os.singleton_methods.sort)
end
Expand All @@ -202,15 +203,6 @@ def initialize(x,y={})super(y);end
assert_instance_of(c, os)
end

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

def test_private_method
os = OpenStruct.new
class << os
Expand All @@ -234,97 +226,4 @@ def foo
os.foo true, true
end
end

def test_access_undefined
os = OpenStruct.new
assert_nil os.foo
end

def test_overriden_private_methods
os = OpenStruct.new(puts: :foo, format: :bar)
assert_equal(:foo, os.puts)
assert_equal(:bar, os.format)
end

def test_overriden_public_methods
os = OpenStruct.new(method: :foo, class: :bar)
assert_equal(:foo, os.method)
assert_equal(:bar, os.class)
end

def test_access_original_methods
os = OpenStruct.new(method: :foo, hash: 42)
assert_equal(os.object_id, os.method!(:object_id).call)
assert_not_equal(42, os.hash!)
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)
end

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

def test_ractor
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
end if defined?(Ractor)

def test_legacy_yaml
s = "--- !ruby/object:OpenStruct\ntable:\n :foo: 42\n"
o = YAML.load(s)
assert_equal(42, o.foo)

o = OpenStruct.new(table: {foo: 42})
assert_equal({foo: 42}, YAML.load(YAML.dump(o)).table)
end

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)
os2 = YAML.load(os1.to_yaml)
assert_equal yaml, os1.to_yaml
assert_equal os1, os2
assert_equal true, os1.eql?(os2)
assert_equal 300.42, os2.pension
end
end

0 comments on commit e2b3533

Please sign in to comment.