diff options
Diffstat (limited to 'test/ruby/marshaltestlib.rb')
| -rw-r--r-- | test/ruby/marshaltestlib.rb | 197 |
1 files changed, 68 insertions, 129 deletions
diff --git a/test/ruby/marshaltestlib.rb b/test/ruby/marshaltestlib.rb index ae60cd8479..7f100b7873 100644 --- a/test/ruby/marshaltestlib.rb +++ b/test/ruby/marshaltestlib.rb @@ -1,5 +1,7 @@ +# coding: utf-8 +# frozen_string_literal: false module MarshalTestLib - # include this module to a Test::Unit::TestCase and definde encode(o) and + # include this module to a Test::Unit::TestCase and define encode(o) and # decode(s) methods. e.g. # # def encode(o) @@ -17,7 +19,7 @@ module MarshalTestLib def marshaltest(o1) str = encode(o1) - print str, "\n" if $DEBUG + print str.dump, "\n" if $DEBUG o2 = decode(str) o2 end @@ -29,8 +31,8 @@ module MarshalTestLib iv1 = o1.instance_variables.sort iv2 = o2.instance_variables.sort assert_equal(iv1, iv2) - val1 = iv1.map {|var| o1.instance_eval {eval var}} - val2 = iv1.map {|var| o2.instance_eval {eval var}} + val1 = iv1.map {|var| o1.instance_eval {eval var.to_s}} + val2 = iv1.map {|var| o2.instance_eval {eval var.to_s}} assert_equal(val1, val2, msg) if block_given? assert_equal(yield(o1), yield(o2), msg) @@ -39,6 +41,14 @@ module MarshalTestLib end end + def marshal_equal_with_ancestry(o1, msg = nil) + marshal_equal(o1, msg) do |o| + ancestry = o.singleton_class.ancestors + ancestry[ancestry.index(o.singleton_class)] = :singleton_class + ancestry + end + end + class MyObject; def initialize(v) @v = v end; attr_reader :v; end def test_object o1 = Object.new @@ -53,24 +63,26 @@ module MarshalTestLib def test_object_extend o1 = Object.new o1.extend(Mod1) - marshal_equal(o1) { |o| - (class << self; self; end).ancestors - } + marshal_equal_with_ancestry(o1) o1.extend(Mod2) - marshal_equal(o1) { |o| - (class << self; self; end).ancestors - } + marshal_equal_with_ancestry(o1) end def test_object_subclass_extend o1 = MyObject.new(2) o1.extend(Mod1) - marshal_equal(o1) { |o| - (class << self; self; end).ancestors - } + marshal_equal_with_ancestry(o1) o1.extend(Mod2) - marshal_equal(o1) { |o| - (class << self; self; end).ancestors + marshal_equal_with_ancestry(o1) + end + + def test_object_prepend + bug8041 = '[ruby-core:53202] [Bug #8041]' + + o1 = MyObject.new(42) + o1.singleton_class.class_eval {prepend Mod1} + assert_nothing_raised(ArgumentError, bug8041) { + marshal_equal_with_ancestry(o1, bug8041) } end @@ -98,7 +110,9 @@ module MarshalTestLib class MyException < Exception; def initialize(v, *args) super(*args); @v = v; end; attr_reader :v; end def test_exception marshal_equal(Exception.new('foo')) {|o| o.message} - marshal_equal(assert_raise(NoMethodError) {no_such_method()}) {|o| o.message} + obj = Object.new + e = assert_raise(NoMethodError) {obj.no_such_method()} + marshal_equal(e) {|o| o.message.lines.first.chomp} end def test_exception_subclass @@ -128,7 +142,7 @@ module MarshalTestLib def test_hash_default_proc h = Hash.new {} - assert_raises(TypeError) { marshaltest(h) } + assert_raise(TypeError) { marshaltest(h) } end def test_hash_ivar @@ -140,25 +154,17 @@ module MarshalTestLib def test_hash_extend o1 = Hash.new o1.extend(Mod1) - marshal_equal(o1) { |o| - (class << self; self; end).ancestors - } + marshal_equal_with_ancestry(o1) o1.extend(Mod2) - marshal_equal(o1) { |o| - (class << self; self; end).ancestors - } + marshal_equal_with_ancestry(o1) end def test_hash_subclass_extend o1 = MyHash.new(2) o1.extend(Mod1) - marshal_equal(o1) { |o| - (class << self; self; end).ancestors - } + marshal_equal_with_ancestry(o1) o1.extend(Mod2) - marshal_equal(o1) { |o| - (class << self; self; end).ancestors - } + marshal_equal_with_ancestry(o1) end def test_bignum @@ -177,28 +183,6 @@ module MarshalTestLib marshal_equal(0x3fff_ffff) end - def test_fixnum_ivar - o1 = 1 - o1.instance_eval { @iv = 2 } - marshal_equal(o1) {|o| o.instance_eval { @iv }} - ensure - 1.instance_eval { remove_instance_variable("@iv") } - end - - def test_fixnum_ivar_self - o1 = 1 - o1.instance_eval { @iv = 1 } - marshal_equal(o1) {|o| o.instance_eval { @iv }} - ensure - 1.instance_eval { remove_instance_variable("@iv") } - end - - def test_fixnum_64bit - obj = [1220278665, 1220278662, 1220278661, 1220278661, 1220278656] - - marshal_equal(obj) - end - def test_float marshal_equal(-1.0) marshal_equal(0.0) @@ -212,30 +196,6 @@ module MarshalTestLib marshal_equal(NegativeZero) {|o| 1.0/o} end - def test_float_ivar - o1 = 1.23 - o1.instance_eval { @iv = 1 } - marshal_equal(o1) {|o| o.instance_eval { @iv }} - end - - def test_float_ivar_self - o1 = 5.5 - o1.instance_eval { @iv = o1 } - marshal_equal(o1) {|o| o.instance_eval { @iv }} - end - - def test_float_extend - o1 = 0.0/0.0 - o1.extend(Mod1) - marshal_equal(o1) { |o| - (class << self; self; end).ancestors - } - o1.extend(Mod2) - marshal_equal(o1) { |o| - (class << self; self; end).ancestors - } - end - class MyRange < Range; def initialize(v, *args) super(*args); @v = v; end end def test_range marshal_equal(1..2) @@ -251,6 +211,12 @@ module MarshalTestLib marshal_equal(/a/) marshal_equal(/A/i) marshal_equal(/A/mx) + marshal_equal(/a\u3042/) + marshal_equal(/ać/) + assert_equal(Regexp.new("ć".force_encoding("ASCII-8BIT")), + Marshal.load("\004\b/\b\343\201\202\000")) + assert_equal(/au3042/, Marshal.load("\004\b/\fa\\u3042\000")) + #assert_equal(/au3042/u, Marshal.load("\004\b/\fa\\u3042@")) # spec end def test_regexp_subclass @@ -276,7 +242,7 @@ module MarshalTestLib str = MyString.new(10, "b") str.instance_eval { @v = str } marshal_equal(str) { |o| - assert_equal(o.__id__, o.instance_eval { @v }.__id__) + assert_same(o, o.instance_eval { @v }) o.instance_eval { @v } } end @@ -285,37 +251,18 @@ module MarshalTestLib o = "abc" o.extend(Mod1) str = MyString.new(o, "c") - marshal_equal(str) { |o| - assert(o.instance_eval { @v }).kind_of?(Mod1) + marshal_equal(str) { |v| + assert_kind_of(Mod1, v.instance_eval { @v }) } end MyStruct = Struct.new("MyStruct", :a, :b) - if RUBY_VERSION < "1.8.0" - # Struct#== is not defined in ruby/1.6 - class MyStruct - def ==(rhs) - return true if __id__ == rhs.__id__ - return false unless rhs.is_a?(::Struct) - return false if self.class != rhs.class - members.each do |member| - return false if self.__send__(member) != rhs.__send__(member) - end - return true - end - end - end class MySubStruct < MyStruct; def initialize(v, *args) super(*args); @v = v; end end def test_struct marshal_equal(MyStruct.new(1,2)) end def test_struct_subclass - if RUBY_VERSION < "1.8.0" - # Substruct instance cannot be dumped in ruby/1.6 - # ::Marshal.dump(MySubStruct.new(10, 1, 2)) #=> uninitialized struct - return false - end marshal_equal(MySubStruct.new(10,1,2)) end @@ -328,13 +275,9 @@ module MarshalTestLib def test_struct_subclass_extend o1 = MyStruct.new o1.extend(Mod1) - marshal_equal(o1) { |o| - (class << self; self; end).ancestors - } + marshal_equal_with_ancestry(o1) o1.extend(Mod2) - marshal_equal(o1) { |o| - (class << self; self; end).ancestors - } + marshal_equal_with_ancestry(o1) end def test_symbol @@ -389,6 +332,11 @@ module MarshalTestLib marshal_equal(o1) {|o| o.instance_eval { @iv }} end + def test_time_in_array + t = Time.now + assert_equal([t,t], Marshal.load(Marshal.dump([t, t])), "[ruby-dev:34159]") + end + def test_true marshal_equal(true) end @@ -419,16 +367,21 @@ module MarshalTestLib def test_singleton o = Object.new def o.m() end - assert_raises(TypeError) { marshaltest(o) } + assert_raise(TypeError) { marshaltest(o) } + + bug8043 = '[ruby-core:53206] [Bug #8043]' + class << o; prepend Mod1; end + assert_raise(TypeError, bug8043) {marshaltest(o)} + o = Object.new c = class << o @v = 1 class C; self; end end - assert_raises(TypeError) { marshaltest(o) } - assert_raises(TypeError) { marshaltest(c) } - assert_raises(TypeError) { marshaltest(ARGF) } - assert_raises(TypeError) { marshaltest(ENV) } + assert_raise(TypeError) { marshaltest(o) } + assert_raise(TypeError) { marshaltest(c) } + assert_raise(TypeError) { marshaltest(ARGF) } + assert_raise(TypeError) { marshaltest(ENV) } end def test_extend @@ -438,10 +391,10 @@ module MarshalTestLib o = Object.new o.extend Mod1 o.extend Mod2 - marshal_equal(o) {|obj| class << obj; ancestors end} + marshal_equal_with_ancestry(o) o = Object.new o.extend Module.new - assert_raises(TypeError) { marshaltest(o) } + assert_raise(TypeError) { marshaltest(o) } end def test_extend_string @@ -451,19 +404,19 @@ module MarshalTestLib o = "" o.extend Mod1 o.extend Mod2 - marshal_equal(o) {|obj| class << obj; ancestors end} + marshal_equal_with_ancestry(o) o = "" o.extend Module.new - assert_raises(TypeError) { marshaltest(o) } + assert_raise(TypeError) { marshaltest(o) } end def test_anonymous c = Class.new - assert_raises(TypeError) { marshaltest(c) } + assert_raise(TypeError) { marshaltest(c) } o = c.new - assert_raises(TypeError) { marshaltest(o) } + assert_raise(TypeError) { marshaltest(o) } m = Module.new - assert_raises(TypeError) { marshaltest(m) } + assert_raise(TypeError) { marshaltest(m) } end def test_string_empty @@ -479,20 +432,6 @@ module MarshalTestLib end MyStruct2 = Struct.new(:a, :b) - if RUBY_VERSION < "1.8.0" - # Struct#== is not defined in ruby/1.6 - class MyStruct2 - def ==(rhs) - return true if __id__ == rhs.__id__ - return false unless rhs.is_a?(::Struct) - return false if self.class != rhs.class - members.each do |member| - return false if self.__send__(member) != rhs.__send__(member) - end - return true - end - end - end def test_struct_toplevel o = MyStruct2.new(1,2) marshal_equal(o) |
