Stop pinning shape edges
[ruby.git] / test / ruby / test_dup.rb
blob75c4fc0339e34d28c4f2952a89ca270fe7182782
1 # frozen_string_literal: false
2 require 'test/unit'
4 class TestDup < Test::Unit::TestCase
5   module M001; end
6   module M002; end
7   module M003; include M002; end
8   module M002; include M001; end
9   module M003; include M002; end
11   def test_dup
12     foo = Object.new
13     def foo.test
14       "test"
15     end
16     bar = foo.dup
17     def bar.test2
18       "test2"
19     end
21     assert_equal("test2", bar.test2)
22     assert_raise(NoMethodError) { bar.test }
23     assert_equal("test", foo.test)
25     assert_raise(NoMethodError) {foo.test2}
27     assert_equal([M003, M002, M001], M003.ancestors)
28   end
30   def test_frozen_properties_not_retained_on_dup
31     obj = Object.new.freeze
32     duped_obj = obj.dup
34     assert_predicate(obj, :frozen?)
35     refute_predicate(duped_obj, :frozen?)
36   end
38   def test_ivar_retained_on_dup
39     obj = Object.new
40     obj.instance_variable_set(:@a, 1)
41     duped_obj = obj.dup
43     assert_equal(obj.instance_variable_get(:@a), 1)
44     assert_equal(duped_obj.instance_variable_get(:@a), 1)
45   end
47   def test_ivars_retained_on_extended_obj_dup
48     ivars = { :@a => 1, :@b => 2, :@c => 3, :@d => 4 }
49     obj = Object.new
50     ivars.each do |ivar_name, val|
51       obj.instance_variable_set(ivar_name, val)
52     end
54     duped_obj = obj.dup
56     ivars.each do |ivar_name, val|
57       assert_equal(obj.instance_variable_get(ivar_name), val)
58       assert_equal(duped_obj.instance_variable_get(ivar_name), val)
59     end
60   end
62   def test_frozen_properties_not_retained_on_dup_with_ivar
63     obj = Object.new
64     obj.instance_variable_set(:@a, 1)
65     obj.freeze
67     duped_obj = obj.dup
69     assert_predicate(obj, :frozen?)
70     assert_equal(obj.instance_variable_get(:@a), 1)
72     refute_predicate(duped_obj, :frozen?)
73     assert_equal(duped_obj.instance_variable_get(:@a), 1)
74   end
76   def test_user_flags
77     assert_separately([], <<-EOS)
78       #
79       class Array
80         undef initialize_copy
81         def initialize_copy(*); end
82       end
83       x = [1, 2, 3].dup
84       assert_equal [], x, '[Bug #14847]'
85     EOS
87     assert_separately([], <<-EOS)
88       #
89       class Array
90         undef initialize_copy
91         def initialize_copy(*); end
92       end
93       x = [1,2,3,4,5,6,7][1..-2].dup
94       x.push(1,1,1,1,1)
95       assert_equal [1, 1, 1, 1, 1], x, '[Bug #14847]'
96     EOS
98     assert_separately([], <<-EOS)
99       #
100       class Hash
101         undef initialize_copy
102         def initialize_copy(*); end
103       end
104       h = {}
105       h.default_proc = proc { raise }
106       h = h.dup
107       assert_equal nil, h[:not_exist], '[Bug #14847]'
108     EOS
109   end