1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* test/ruby/test_refinement.rb: fix some tests to use neither

Module#using nor Module#module_eval.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38241 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
shugo 2012-12-06 15:12:36 +00:00
parent 2b0af28589
commit d87b56c411
2 changed files with 96 additions and 81 deletions

View file

@ -1,3 +1,8 @@
Fri Dec 7 00:11:44 2012 Shugo Maeda <shugo@ruby-lang.org>
* test/ruby/test_refinement.rb: fix some tests to use neither
Module#using nor Module#module_eval.
Thu Dec 6 23:27:50 2012 Shugo Maeda <shugo@ruby-lang.org> Thu Dec 6 23:27:50 2012 Shugo Maeda <shugo@ruby-lang.org>
* eval.c (ruby_Init_refinement): a new function to enable * eval.c (ruby_Init_refinement): a new function to enable

View file

@ -60,9 +60,10 @@ class TestRefinement < Test::Unit::TestCase
end end
end end
class FooExtClient eval <<-EOF, TOPLEVEL_BINDING
using FooExt using TestRefinement::FooExt
class TestRefinement::FooExtClient
def self.invoke_x_on(foo) def self.invoke_x_on(foo)
return foo.x return foo.x
end end
@ -87,15 +88,18 @@ class TestRefinement < Test::Unit::TestCase
return foo.call_x return foo.call_x
end end
end end
EOF
class FooExtClient2 eval <<-EOF, TOPLEVEL_BINDING
using FooExt using TestRefinement::FooExt
using FooExt2 using TestRefinement::FooExt2
class TestRefinement::FooExtClient2
def self.invoke_y_on(foo) def self.invoke_y_on(foo)
return foo.y return foo.y
end end
end end
EOF
def test_override def test_override
foo = Foo.new foo = Foo.new
@ -175,34 +179,36 @@ class TestRefinement < Test::Unit::TestCase
assert_equal("Foo#x", foo.x) assert_equal("Foo#x", foo.x)
end end
def test_instance_eval def test_instance_eval_without_refinement
foo = Foo.new foo = Foo.new
ext_client = FooExtClient.new ext_client = FooExtClient.new
assert_equal("Foo#x", foo.x) assert_equal("Foo#x", foo.x)
assert_equal("FooExt#x", ext_client.instance_eval { foo.x }) assert_equal("Foo#x", ext_client.instance_eval { foo.x })
assert_equal("Foo#x", foo.x) assert_equal("Foo#x", foo.x)
end end
def test_override_builtin_method module FixnumSlashExt
m = Module.new {
refine Fixnum do refine Fixnum do
def /(other) quo(other) end def /(other) quo(other) end
end end
} end
def test_override_builtin_method
assert_equal(0, 1 / 2) assert_equal(0, 1 / 2)
assert_equal(Rational(1, 2), m.module_eval { 1 / 2 }) assert_equal(Rational(1, 2), eval_using(FixnumSlashExt, "1 / 2"))
assert_equal(0, 1 / 2) assert_equal(0, 1 / 2)
end end
def test_override_builtin_method_with_method_added module FixnumPlusExt
m = Module.new {
refine Fixnum do refine Fixnum do
def self.method_added(*args); end def self.method_added(*args); end
def +(other) "overriden" end def +(other) "overriden" end
end end
} end
def test_override_builtin_method_with_method_added
assert_equal(3, 1 + 2) assert_equal(3, 1 + 2)
assert_equal("overriden", m.module_eval { 1 + 2 }) assert_equal("overriden", eval_using(FixnumPlusExt, "1 + 2"))
assert_equal(3, 1 + 2) assert_equal(3, 1 + 2)
end end
@ -217,25 +223,23 @@ class TestRefinement < Test::Unit::TestCase
assert_equal mod, result assert_equal mod, result
end end
def test_refine_same_class_twice module RefineSameClass
result1 = nil REFINEMENT1 = refine(Fixnum) {
result2 = nil
result3 = nil
m = Module.new {
result1 = refine(Fixnum) {
def foo; return "foo" end def foo; return "foo" end
} }
result2 = refine(Fixnum) { REFINEMENT2 = refine(Fixnum) {
def bar; return "bar" end def bar; return "bar" end
} }
result3 = refine(String) { REFINEMENT3 = refine(String) {
def baz; return "baz" end def baz; return "baz" end
} }
} end
assert_equal("foo", m.module_eval { 1.foo })
assert_equal("bar", m.module_eval { 1.bar }) def test_refine_same_class_twice
assert_equal(result1, result2) assert_equal("foo", eval_using(RefineSameClass, "1.foo"))
assert_not_equal(result1, result3) assert_equal("bar", eval_using(RefineSameClass, "1.bar"))
assert_equal(RefineSameClass::REFINEMENT1, RefineSameClass::REFINEMENT2)
assert_not_equal(RefineSameClass::REFINEMENT1, RefineSameClass::REFINEMENT3)
end end
def test_respond_to? def test_respond_to?
@ -249,15 +253,13 @@ class TestRefinement < Test::Unit::TestCase
assert_equal(false, 1.respond_to?(:foo)) assert_equal(false, 1.respond_to?(:foo))
end end
def test_builtin_method_no_local_rebinding module StringCmpExt
m = Module.new {
refine String do refine String do
def <=>(other) return 0 end def <=>(other) return 0 end
end end
} end
assert_equal(false, m.module_eval { "1" >= "2" })
m2 = Module.new { module ArrayEachExt
refine Array do refine Array do
def each def each
super do |i| super do |i|
@ -265,9 +267,11 @@ class TestRefinement < Test::Unit::TestCase
end end
end end
end end
} end
a = [1, 2, 3]
assert_equal(1, m2.module_eval { a.min }) def test_builtin_method_no_local_rebinding
assert_equal(false, eval_using(StringCmpExt, '"1" >= "2"'))
assert_equal(1, eval_using(ArrayEachExt, "[1, 2, 3].min"))
end end
def test_module_inclusion def test_module_inclusion
@ -859,4 +863,10 @@ class TestRefinement < Test::Unit::TestCase
c = RedifineRefinedMethod::C.new c = RedifineRefinedMethod::C.new
assert_equal("refined", RedifineRefinedMethod::M.module_eval { c.foo }) assert_equal("refined", RedifineRefinedMethod::M.module_eval { c.foo })
end end
private
def eval_using(mod, s)
eval("using #{mod}; #{s}", TOPLEVEL_BINDING)
end
end end