summaryrefslogtreecommitdiff
path: root/spec/ruby/core/module/using_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/module/using_spec.rb')
-rw-r--r--spec/ruby/core/module/using_spec.rb100
1 files changed, 95 insertions, 5 deletions
diff --git a/spec/ruby/core/module/using_spec.rb b/spec/ruby/core/module/using_spec.rb
index 9b6b38f622..a908363c96 100644
--- a/spec/ruby/core/module/using_spec.rb
+++ b/spec/ruby/core/module/using_spec.rb
@@ -24,7 +24,7 @@ describe "Module#using" do
end
end
- -> () {
+ -> {
Module.new do
using refinement
end
@@ -34,7 +34,7 @@ describe "Module#using" do
it "accepts module without refinements" do
mod = Module.new
- -> () {
+ -> {
Module.new do
using mod
end
@@ -44,7 +44,7 @@ describe "Module#using" do
it "does not accept class" do
klass = Class.new
- -> () {
+ -> {
Module.new do
using klass
end
@@ -52,7 +52,7 @@ describe "Module#using" do
end
it "raises TypeError if passed something other than module" do
- -> () {
+ -> {
Module.new do
using "foo"
end
@@ -93,7 +93,7 @@ describe "Module#using" do
end
end
- -> () {
+ -> {
mod.foo
}.should raise_error(RuntimeError, /Module#using is not permitted in methods/)
end
@@ -243,6 +243,96 @@ describe "Module#using" do
mod.call_foo(c).should == "foo from refinement"
end
+ it "is active for module defined via Module.new {}" do
+ refinement = Module.new do
+ refine Integer do
+ def foo; "foo from refinement"; end
+ end
+ end
+
+ result = nil
+
+ Module.new do
+ using refinement
+
+ Module.new do
+ result = 1.foo
+ end
+ end
+
+ result.should == "foo from refinement"
+ end
+
+ it "is active for class defined via Class.new {}" do
+ refinement = Module.new do
+ refine Integer do
+ def foo; "foo from refinement"; end
+ end
+ end
+
+ result = nil
+
+ Module.new do
+ using refinement
+
+ Class.new do
+ result = 1.foo
+ end
+ end
+
+ result.should == "foo from refinement"
+ end
+
+ it "is active for block called via instance_exec" do
+ refinement = Module.new do
+ refine Integer do
+ def foo; "foo from refinement"; end
+ end
+ end
+
+ c = Class.new do
+ using refinement
+
+ def abc
+ block = -> {
+ 1.foo
+ }
+
+ self.instance_exec(&block)
+ end
+ end
+
+ c.new.abc.should == "foo from refinement"
+ end
+
+ it "is active for block called via instance_eval" do
+ refinement = Module.new do
+ refine String do
+ def foo; "foo from refinement"; end
+ end
+ end
+
+ c = Class.new do
+ using refinement
+
+ def initialize
+ @a = +"1703"
+
+ @a.instance_eval do
+ def abc
+ "#{self}: #{self.foo}"
+ end
+ end
+ end
+
+ def abc
+ @a.abc
+ end
+ end
+
+ c.new.abc.should == "1703: foo from refinement"
+ end
+
it "is not active if `using` call is not evaluated" do
result = nil