Skip to content

Commit 9f024c2

Browse files
committed
Share TestObject::ToStrCounter class
1 parent cb35cc6 commit 9f024c2

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

test/ruby/test_object.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,12 @@ def test_methods_prepend_singleton
280280
assert_equal([:foo], k.private_methods(false))
281281
end
282282

283+
class ToStrCounter
284+
def initialize(str = "@foo") @str = str; @count = 0; end
285+
def to_str; @count += 1; @str; end
286+
def count; @count; end
287+
end
288+
283289
def test_instance_variable_get
284290
o = Object.new
285291
o.instance_eval { @foo = :foo }
@@ -291,9 +297,7 @@ def test_instance_variable_get
291297
assert_raise(NameError) { o.instance_variable_get("bar") }
292298
assert_raise(TypeError) { o.instance_variable_get(1) }
293299

294-
n = Object.new
295-
def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "@foo"; end
296-
def n.count; @count; end
300+
n = ToStrCounter.new
297301
assert_equal(:foo, o.instance_variable_get(n))
298302
assert_equal(1, n.count)
299303
end
@@ -308,9 +312,7 @@ def test_instance_variable_set
308312
assert_raise(NameError) { o.instance_variable_set("bar", 1) }
309313
assert_raise(TypeError) { o.instance_variable_set(1, 1) }
310314

311-
n = Object.new
312-
def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "@foo"; end
313-
def n.count; @count; end
315+
n = ToStrCounter.new
314316
o.instance_variable_set(n, :bar)
315317
assert_equal(:bar, o.instance_eval { @foo })
316318
assert_equal(1, n.count)
@@ -327,9 +329,7 @@ def test_instance_variable_defined
327329
assert_raise(NameError) { o.instance_variable_defined?("bar") }
328330
assert_raise(TypeError) { o.instance_variable_defined?(1) }
329331

330-
n = Object.new
331-
def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "@foo"; end
332-
def n.count; @count; end
332+
n = ToStrCounter.new
333333
assert_equal(true, o.instance_variable_defined?(n))
334334
assert_equal(1, n.count)
335335
end

0 commit comments

Comments
 (0)