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>
* eval.c (ruby_Init_refinement): a new function to enable

View file

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