aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/core_ext/object_and_class_ext_test.rb
blob: 16f4ab888e15ae7f6e888c79f3353dc10cfe2ef0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
require 'abstract_unit'

class ClassA; end
class ClassB < ClassA; end
class ClassC < ClassB; end
class ClassD < ClassA; end

class ClassI; end
class ClassJ < ClassI; end

class ClassK
end
module Nested
  class << self
    def on_const_missing(&callback)
      @on_const_missing = callback
    end
    private
      def const_missing(mod_id)
        @on_const_missing[mod_id] if @on_const_missing
        super
      end
  end
  class ClassL < ClassK
  end
end

module Bar
  def bar; end
end

module Baz
  def baz; end
end

class Foo
  include Bar
end

class ClassExtTest < Test::Unit::TestCase
  def test_methods
    assert defined?(ClassB)
    assert defined?(ClassC)
    assert defined?(ClassD)

    ClassA.remove_subclasses

    assert !defined?(ClassB)
    assert !defined?(ClassC)
    assert !defined?(ClassD)
  end

  def test_subclasses_of
    cj = ClassJ
    assert_equal [ClassJ], Object.subclasses_of(ClassI)
    ClassI.remove_subclasses
    assert_equal [], Object.subclasses_of(ClassI)
  ensure
    Object.const_set :ClassJ, cj
  end

  def test_subclasses_of_should_find_nested_classes
    assert Object.subclasses_of(ClassK).include?(Nested::ClassL)
  end

  def test_subclasses_of_should_not_return_removed_classes
    # First create the removed class
    old_class = Nested.class_eval { remove_const :ClassL }
    new_class = Class.new(ClassK)
    Nested.const_set :ClassL, new_class
    assert_equal "Nested::ClassL", new_class.name # Sanity check

    subclasses = Object.subclasses_of(ClassK)
    assert subclasses.include?(new_class)
    assert ! subclasses.include?(old_class)
  ensure
    Nested.const_set :ClassL, old_class unless defined?(Nested::ClassL)
  end
  
  def test_subclasses_of_should_not_trigger_const_missing
    const_missing = false
    Nested.on_const_missing { const_missing = true }
    
    subclasses = Object.subclasses_of ClassK
    assert !const_missing
    assert_equal [ Nested::ClassL ], subclasses
    
    removed = Nested.class_eval { remove_const :ClassL }  # keep it in memory
    subclasses = Object.subclasses_of ClassK
    assert !const_missing
    assert subclasses.empty?
  ensure
    Nested.const_set :ClassL, removed unless defined?(Nested::ClassL)
  end
  
  def test_subclasses_of_with_multiple_roots
    classes = Object.subclasses_of(ClassI, ClassK)
    assert_equal %w(ClassJ Nested::ClassL), classes.collect(&:to_s).sort
  end

  def test_subclasses_of_doesnt_find_anonymous_classes
    assert_equal [], Object.subclasses_of(Foo)
    bar = Class.new(Foo)
    assert_nothing_raised do
      assert_equal [bar], Object.subclasses_of(Foo)
    end
  end
end

class ObjectTests < Test::Unit::TestCase
  def test_send_bang_aliases_send_before_19
    assert_respond_to 'a', :send!
    assert_equal 1, 'a'.send!(:size)
  end

  def test_suppress_re_raises
    assert_raises(LoadError) { suppress(ArgumentError) {raise LoadError} }
  end
  def test_suppress_supresses
    suppress(ArgumentError) { raise ArgumentError }
    suppress(LoadError) { raise LoadError }
    suppress(LoadError, ArgumentError) { raise LoadError }
    suppress(LoadError, ArgumentError) { raise ArgumentError }
  end

  def test_extended_by
    foo = Foo.new
    assert foo.extended_by.include?(Bar)
    foo.extend(Baz)
    assert(([Bar, Baz] - foo.extended_by).empty?, "Expected Bar, Baz in #{foo.extended_by.inspect}")
  end

  def test_extend_with_included_modules_from
    foo, object = Foo.new, Object.new
    assert !object.respond_to?(:bar)
    assert !object.respond_to?(:baz)

    object.extend_with_included_modules_from(foo)
    assert object.respond_to?(:bar)
    assert !object.respond_to?(:baz)

    foo.extend(Baz)
    object.extend_with_included_modules_from(foo)
    assert object.respond_to?(:bar)
    assert object.respond_to?(:baz)
  end

  class DuckTime
    def acts_like_time?
      true
    end
  end

  def test_duck_typing
    object = Object.new
    time   = Time.now
    date   = Date.today
    dt     = DateTime.new
    duck   = DuckTime.new

    assert !object.acts_like?(:time)
    assert !object.acts_like?(:date)

    assert time.acts_like?(:time)
    assert !time.acts_like?(:date)

    assert !date.acts_like?(:time)
    assert date.acts_like?(:date)

    assert dt.acts_like?(:time)
    assert dt.acts_like?(:date)

    assert duck.acts_like?(:time)
    assert !duck.acts_like?(:date)
  end
end

class ObjectInstanceVariableTest < Test::Unit::TestCase
  def setup
    @source, @dest = Object.new, Object.new
    @source.instance_variable_set(:@bar, 'bar')
    @source.instance_variable_set(:@baz, 'baz')
  end

  def test_instance_variable_names
    assert_equal %w(@bar @baz), @source.instance_variable_names.sort
  end

  def test_instance_variable_defined
    assert @source.instance_variable_defined?('@bar')
    assert @source.instance_variable_defined?(:@bar)
    assert !@source.instance_variable_defined?(:@foo)
    assert !@source.instance_variable_defined?('@foo')
  end

  def test_copy_instance_variables_from_without_explicit_excludes
    assert_equal [], @dest.instance_variables
    @dest.copy_instance_variables_from(@source)

    assert_equal %w(@bar @baz), @dest.instance_variables.sort.map(&:to_s)
    %w(@bar @baz).each do |name|
      assert_equal @source.instance_variable_get(name).object_id,
                   @dest.instance_variable_get(name).object_id
    end
  end

  def test_copy_instance_variables_from_with_explicit_excludes
    @dest.copy_instance_variables_from(@source, ['@baz'])
    assert !@dest.instance_variable_defined?('@baz')
    assert_equal 'bar', @dest.instance_variable_get('@bar')
  end

  def test_copy_instance_variables_automatically_excludes_protected_instance_variables
    @source.instance_variable_set(:@quux, 'quux')
    class << @source
      def protected_instance_variables
        ['@bar', :@quux]
      end
    end

    @dest.copy_instance_variables_from(@source)
    assert !@dest.instance_variable_defined?('@bar')
    assert !@dest.instance_variable_defined?('@quux')
    assert_equal 'baz', @dest.instance_variable_get('@baz')
  end

  def test_instance_values
    object = Object.new
    object.instance_variable_set :@a, 1
    object.instance_variable_set :@b, 2
    assert_equal({'a' => 1, 'b' => 2}, object.instance_values)
  end

  def test_instance_exec_passes_arguments_to_block
    assert_equal %w(hello goodbye), 'hello'.instance_exec('goodbye') { |v| [self, v] }
  end

  def test_instance_exec_with_frozen_obj
    assert_equal %w(olleh goodbye), 'hello'.freeze.instance_exec('goodbye') { |v| [reverse, v] }
  end

  def test_instance_exec_nested
    assert_equal %w(goodbye olleh bar), 'hello'.instance_exec('goodbye') { |arg|
      [arg] + instance_exec('bar') { |v| [reverse, v] } }
  end
end