summaryrefslogtreecommitdiff
path: root/spec/ruby/core/module
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/module')
-rw-r--r--spec/ruby/core/module/class_eval_spec.rb6
-rw-r--r--spec/ruby/core/module/class_exec_spec.rb6
-rw-r--r--spec/ruby/core/module/inspect_spec.rb7
-rw-r--r--spec/ruby/core/module/module_eval_spec.rb172
-rw-r--r--spec/ruby/core/module/module_exec_spec.rb35
-rw-r--r--spec/ruby/core/module/shared/class_eval.rb172
-rw-r--r--spec/ruby/core/module/shared/class_exec.rb35
7 files changed, 216 insertions, 217 deletions
diff --git a/spec/ruby/core/module/class_eval_spec.rb b/spec/ruby/core/module/class_eval_spec.rb
index c6665d5aff..0c190ceaff 100644
--- a/spec/ruby/core/module/class_eval_spec.rb
+++ b/spec/ruby/core/module/class_eval_spec.rb
@@ -1,7 +1,7 @@
require_relative '../../spec_helper'
-require_relative 'fixtures/classes'
-require_relative 'shared/class_eval'
describe "Module#class_eval" do
- it_behaves_like :module_class_eval, :class_eval
+ it "is an alias of Module#module_eval" do
+ Module.instance_method(:class_eval).should == Module.instance_method(:module_eval)
+ end
end
diff --git a/spec/ruby/core/module/class_exec_spec.rb b/spec/ruby/core/module/class_exec_spec.rb
index 4acd0169ad..d47a6ba982 100644
--- a/spec/ruby/core/module/class_exec_spec.rb
+++ b/spec/ruby/core/module/class_exec_spec.rb
@@ -1,7 +1,7 @@
require_relative '../../spec_helper'
-require_relative 'fixtures/classes'
-require_relative 'shared/class_exec'
describe "Module#class_exec" do
- it_behaves_like :module_class_exec, :class_exec
+ it "is an alias of Module#module_exec" do
+ Module.instance_method(:class_exec).should == Module.instance_method(:module_exec)
+ end
end
diff --git a/spec/ruby/core/module/inspect_spec.rb b/spec/ruby/core/module/inspect_spec.rb
new file mode 100644
index 0000000000..68c8494c96
--- /dev/null
+++ b/spec/ruby/core/module/inspect_spec.rb
@@ -0,0 +1,7 @@
+require_relative '../../spec_helper'
+
+describe "Module#inspect" do
+ it "is an alias of Module#to_s" do
+ Module.instance_method(:inspect).should == Module.instance_method(:to_s)
+ end
+end
diff --git a/spec/ruby/core/module/module_eval_spec.rb b/spec/ruby/core/module/module_eval_spec.rb
index e9e9fda28d..bcd51ca19d 100644
--- a/spec/ruby/core/module/module_eval_spec.rb
+++ b/spec/ruby/core/module/module_eval_spec.rb
@@ -1,7 +1,175 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
-require_relative 'shared/class_eval'
describe "Module#module_eval" do
- it_behaves_like :module_class_eval, :module_eval
+ # TODO: This should probably be replaced with a "should behave like" that uses
+ # the many scoping/binding specs from kernel/eval_spec, since most of those
+ # behaviors are the same for instance_eval. See also module_eval/class_eval.
+
+ it "evaluates a given string in the context of self" do
+ ModuleSpecs.module_eval("self").should == ModuleSpecs
+ ModuleSpecs.module_eval("1 + 1").should == 2
+ end
+
+ it "does not add defined methods to other classes" do
+ FalseClass.module_eval do
+ def foo
+ 'foo'
+ end
+ end
+ -> {42.foo}.should.raise(NoMethodError)
+ end
+
+ it "resolves constants in the caller scope" do
+ ModuleSpecs::ClassEvalTest.get_constant_from_scope.should == ModuleSpecs::Lookup
+ end
+
+ it "resolves constants in the caller scope ignoring send" do
+ ModuleSpecs::ClassEvalTest.get_constant_from_scope_with_send(:module_eval).should == ModuleSpecs::Lookup
+ end
+
+ it "resolves constants in the receiver's scope" do
+ ModuleSpecs.module_eval("Lookup").should == ModuleSpecs::Lookup
+ ModuleSpecs.module_eval("Lookup::LOOKIE").should == ModuleSpecs::Lookup::LOOKIE
+ end
+
+ it "defines constants in the receiver's scope" do
+ ModuleSpecs.module_eval("module NewEvaluatedModule;end")
+ ModuleSpecs.const_defined?(:NewEvaluatedModule, false).should == true
+ end
+
+ it "evaluates a given block in the context of self" do
+ ModuleSpecs.module_eval { self }.should == ModuleSpecs
+ ModuleSpecs.module_eval { 1 + 1 }.should == 2
+ end
+
+ it "passes the module as the first argument of the block" do
+ given = nil
+ ModuleSpecs.module_eval do |block_parameter|
+ given = block_parameter
+ end
+ given.should.equal? ModuleSpecs
+ end
+
+ it "uses the optional filename and lineno parameters for error messages" do
+ ModuleSpecs.module_eval("[__FILE__, __LINE__]", "test", 102).should == ["test", 102]
+ end
+
+ it "uses the caller location as default filename" do
+ ModuleSpecs.module_eval("[__FILE__, __LINE__]").should == ["(eval at #{__FILE__}:#{__LINE__})", 1]
+ end
+
+ it "converts a non-string filename to a string using to_str" do
+ (file = mock(__FILE__)).should_receive(:to_str).and_return(__FILE__)
+ ModuleSpecs.module_eval("1+1", file)
+
+ (file = mock(__FILE__)).should_receive(:to_str).and_return(__FILE__)
+ ModuleSpecs.module_eval("1+1", file, 15)
+ end
+
+ it "raises a TypeError when the given filename can't be converted to string using to_str" do
+ (file = mock('123')).should_receive(:to_str).and_return(123)
+ -> { ModuleSpecs.module_eval("1+1", file) }.should raise_consistent_error(TypeError, /can't convert MockObject into String/)
+ end
+
+ it "converts non string eval-string to string using to_str" do
+ (o = mock('1 + 1')).should_receive(:to_str).and_return("1 + 1")
+ ModuleSpecs.module_eval(o).should == 2
+
+ (o = mock('1 + 1')).should_receive(:to_str).and_return("1 + 1")
+ ModuleSpecs.module_eval(o, "file.rb").should == 2
+
+ (o = mock('1 + 1')).should_receive(:to_str).and_return("1 + 1")
+ ModuleSpecs.module_eval(o, "file.rb", 15).should == 2
+ end
+
+ it "raises a TypeError when the given eval-string can't be converted to string using to_str" do
+ o = mock('x')
+ -> { ModuleSpecs.module_eval(o) }.should.raise(TypeError, "no implicit conversion of MockObject into String")
+
+ (o = mock('123')).should_receive(:to_str).and_return(123)
+ -> { ModuleSpecs.module_eval(o) }.should raise_consistent_error(TypeError, /can't convert MockObject into String/)
+ end
+
+ it "raises an ArgumentError when no arguments and no block are given" do
+ -> { ModuleSpecs.module_eval }.should.raise(ArgumentError, "wrong number of arguments (given 0, expected 1..3)")
+ end
+
+ it "raises an ArgumentError when more than 3 arguments are given" do
+ -> {
+ ModuleSpecs.module_eval("1 + 1", "some file", 0, "bogus")
+ }.should.raise(ArgumentError, "wrong number of arguments (given 4, expected 1..3)")
+ end
+
+ it "raises an ArgumentError when a block and normal arguments are given" do
+ -> {
+ ModuleSpecs.module_eval("1 + 1") { 1 + 1 }
+ }.should.raise(ArgumentError, "wrong number of arguments (given 1, expected 0)")
+ end
+
+ # This case was found because Rubinius was caching the compiled
+ # version of the string and not duping the methods within the
+ # eval, causing the method addition to change the static scope
+ # of the shared CompiledCode.
+ it "adds methods respecting the lexical constant scope" do
+ code = "def self.attribute; C; end"
+
+ a = Class.new do
+ self::C = "A"
+ end
+
+ b = Class.new do
+ self::C = "B"
+ end
+
+ a.module_eval(code)
+ b.module_eval(code)
+
+ a.attribute.should == "A"
+ b.attribute.should == "B"
+ end
+
+ it "activates refinements from the eval scope" do
+ refinery = Module.new do
+ refine ModuleSpecs::NamedClass do
+ def foo
+ "bar"
+ end
+ end
+ end
+
+ mid = :module_eval
+ result = nil
+
+ Class.new do
+ using refinery
+
+ result = send(mid, "ModuleSpecs::NamedClass.new.foo")
+ end
+
+ result.should == "bar"
+ end
+
+ it "activates refinements from the eval scope with block" do
+ refinery = Module.new do
+ refine ModuleSpecs::NamedClass do
+ def foo
+ "bar"
+ end
+ end
+ end
+
+ mid = :module_eval
+ result = nil
+
+ Class.new do
+ using refinery
+
+ result = send(mid) do
+ ModuleSpecs::NamedClass.new.foo
+ end
+ end
+
+ result.should == "bar"
+ end
end
diff --git a/spec/ruby/core/module/module_exec_spec.rb b/spec/ruby/core/module/module_exec_spec.rb
index 47cdf7ef52..6b36e8a02f 100644
--- a/spec/ruby/core/module/module_exec_spec.rb
+++ b/spec/ruby/core/module/module_exec_spec.rb
@@ -1,7 +1,38 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
-require_relative 'shared/class_exec'
describe "Module#module_exec" do
- it_behaves_like :module_class_exec, :module_exec
+ it "does not add defined methods to other classes" do
+ FalseClass.module_exec do
+ def foo
+ 'foo'
+ end
+ end
+ -> {42.foo}.should.raise(NoMethodError)
+ end
+
+ it "defines method in the receiver's scope" do
+ ModuleSpecs::Subclass.module_exec { def foo; end }
+ ModuleSpecs::Subclass.new.respond_to?(:foo).should == true
+ end
+
+ it "evaluates a given block in the context of self" do
+ ModuleSpecs::Subclass.module_exec { self }.should == ModuleSpecs::Subclass
+ ModuleSpecs::Subclass.new.module_exec { 1 + 1 }.should == 2
+ end
+
+ it "raises a LocalJumpError when no block is given" do
+ -> { ModuleSpecs::Subclass.module_exec }.should.raise(LocalJumpError)
+ end
+
+ it "passes arguments to the block" do
+ a = ModuleSpecs::Subclass
+ a.module_exec(1) { |b| b }.should.equal?(1)
+ end
+
+ describe "with optional argument" do
+ it "does not destructure a single array argument" do
+ ModuleSpecs::Subclass.module_exec([1, 2, 3]) { |a = 99| a }.should == [1, 2, 3]
+ end
+ end
end
diff --git a/spec/ruby/core/module/shared/class_eval.rb b/spec/ruby/core/module/shared/class_eval.rb
deleted file mode 100644
index ee2860449a..0000000000
--- a/spec/ruby/core/module/shared/class_eval.rb
+++ /dev/null
@@ -1,172 +0,0 @@
-describe :module_class_eval, shared: true do
- # TODO: This should probably be replaced with a "should behave like" that uses
- # the many scoping/binding specs from kernel/eval_spec, since most of those
- # behaviors are the same for instance_eval. See also module_eval/class_eval.
-
- it "evaluates a given string in the context of self" do
- ModuleSpecs.send(@method, "self").should == ModuleSpecs
- ModuleSpecs.send(@method, "1 + 1").should == 2
- end
-
- it "does not add defined methods to other classes" do
- FalseClass.send(@method) do
- def foo
- 'foo'
- end
- end
- -> {42.foo}.should.raise(NoMethodError)
- end
-
- it "resolves constants in the caller scope" do
- ModuleSpecs::ClassEvalTest.get_constant_from_scope.should == ModuleSpecs::Lookup
- end
-
- it "resolves constants in the caller scope ignoring send" do
- ModuleSpecs::ClassEvalTest.get_constant_from_scope_with_send(@method).should == ModuleSpecs::Lookup
- end
-
- it "resolves constants in the receiver's scope" do
- ModuleSpecs.send(@method, "Lookup").should == ModuleSpecs::Lookup
- ModuleSpecs.send(@method, "Lookup::LOOKIE").should == ModuleSpecs::Lookup::LOOKIE
- end
-
- it "defines constants in the receiver's scope" do
- ModuleSpecs.send(@method, "module NewEvaluatedModule;end")
- ModuleSpecs.const_defined?(:NewEvaluatedModule, false).should == true
- end
-
- it "evaluates a given block in the context of self" do
- ModuleSpecs.send(@method) { self }.should == ModuleSpecs
- ModuleSpecs.send(@method) { 1 + 1 }.should == 2
- end
-
- it "passes the module as the first argument of the block" do
- given = nil
- ModuleSpecs.send(@method) do |block_parameter|
- given = block_parameter
- end
- given.should.equal? ModuleSpecs
- end
-
- it "uses the optional filename and lineno parameters for error messages" do
- ModuleSpecs.send(@method, "[__FILE__, __LINE__]", "test", 102).should == ["test", 102]
- end
-
- it "uses the caller location as default filename" do
- ModuleSpecs.send(@method, "[__FILE__, __LINE__]").should == ["(eval at #{__FILE__}:#{__LINE__})", 1]
- end
-
- it "converts a non-string filename to a string using to_str" do
- (file = mock(__FILE__)).should_receive(:to_str).and_return(__FILE__)
- ModuleSpecs.send(@method, "1+1", file)
-
- (file = mock(__FILE__)).should_receive(:to_str).and_return(__FILE__)
- ModuleSpecs.send(@method, "1+1", file, 15)
- end
-
- it "raises a TypeError when the given filename can't be converted to string using to_str" do
- (file = mock('123')).should_receive(:to_str).and_return(123)
- -> { ModuleSpecs.send(@method, "1+1", file) }.should raise_consistent_error(TypeError, /can't convert MockObject into String/)
- end
-
- it "converts non string eval-string to string using to_str" do
- (o = mock('1 + 1')).should_receive(:to_str).and_return("1 + 1")
- ModuleSpecs.send(@method, o).should == 2
-
- (o = mock('1 + 1')).should_receive(:to_str).and_return("1 + 1")
- ModuleSpecs.send(@method, o, "file.rb").should == 2
-
- (o = mock('1 + 1')).should_receive(:to_str).and_return("1 + 1")
- ModuleSpecs.send(@method, o, "file.rb", 15).should == 2
- end
-
- it "raises a TypeError when the given eval-string can't be converted to string using to_str" do
- o = mock('x')
- -> { ModuleSpecs.send(@method, o) }.should.raise(TypeError, "no implicit conversion of MockObject into String")
-
- (o = mock('123')).should_receive(:to_str).and_return(123)
- -> { ModuleSpecs.send(@method, o) }.should raise_consistent_error(TypeError, /can't convert MockObject into String/)
- end
-
- it "raises an ArgumentError when no arguments and no block are given" do
- -> { ModuleSpecs.send(@method) }.should.raise(ArgumentError, "wrong number of arguments (given 0, expected 1..3)")
- end
-
- it "raises an ArgumentError when more than 3 arguments are given" do
- -> {
- ModuleSpecs.send(@method, "1 + 1", "some file", 0, "bogus")
- }.should.raise(ArgumentError, "wrong number of arguments (given 4, expected 1..3)")
- end
-
- it "raises an ArgumentError when a block and normal arguments are given" do
- -> {
- ModuleSpecs.send(@method, "1 + 1") { 1 + 1 }
- }.should.raise(ArgumentError, "wrong number of arguments (given 1, expected 0)")
- end
-
- # This case was found because Rubinius was caching the compiled
- # version of the string and not duping the methods within the
- # eval, causing the method addition to change the static scope
- # of the shared CompiledCode.
- it "adds methods respecting the lexical constant scope" do
- code = "def self.attribute; C; end"
-
- a = Class.new do
- self::C = "A"
- end
-
- b = Class.new do
- self::C = "B"
- end
-
- a.send @method, code
- b.send @method, code
-
- a.attribute.should == "A"
- b.attribute.should == "B"
- end
-
- it "activates refinements from the eval scope" do
- refinery = Module.new do
- refine ModuleSpecs::NamedClass do
- def foo
- "bar"
- end
- end
- end
-
- mid = @method
- result = nil
-
- Class.new do
- using refinery
-
- result = send(mid, "ModuleSpecs::NamedClass.new.foo")
- end
-
- result.should == "bar"
- end
-
- it "activates refinements from the eval scope with block" do
- refinery = Module.new do
- refine ModuleSpecs::NamedClass do
- def foo
- "bar"
- end
- end
- end
-
- mid = @method
- result = nil
-
- Class.new do
- using refinery
-
- result = send(mid) do
- ModuleSpecs::NamedClass.new.foo
- end
- end
-
- result.should == "bar"
- end
-end
diff --git a/spec/ruby/core/module/shared/class_exec.rb b/spec/ruby/core/module/shared/class_exec.rb
deleted file mode 100644
index e51af1966d..0000000000
--- a/spec/ruby/core/module/shared/class_exec.rb
+++ /dev/null
@@ -1,35 +0,0 @@
-describe :module_class_exec, shared: true do
- it "does not add defined methods to other classes" do
- FalseClass.send(@method) do
- def foo
- 'foo'
- end
- end
- -> {42.foo}.should.raise(NoMethodError)
- end
-
- it "defines method in the receiver's scope" do
- ModuleSpecs::Subclass.send(@method) { def foo; end }
- ModuleSpecs::Subclass.new.respond_to?(:foo).should == true
- end
-
- it "evaluates a given block in the context of self" do
- ModuleSpecs::Subclass.send(@method) { self }.should == ModuleSpecs::Subclass
- ModuleSpecs::Subclass.new.send(@method) { 1 + 1 }.should == 2
- end
-
- it "raises a LocalJumpError when no block is given" do
- -> { ModuleSpecs::Subclass.send(@method) }.should.raise(LocalJumpError)
- end
-
- it "passes arguments to the block" do
- a = ModuleSpecs::Subclass
- a.send(@method, 1) { |b| b }.should.equal?(1)
- end
-
- describe "with optional argument" do
- it "does not destructure a single array argument" do
- ModuleSpecs::Subclass.send(@method, [1, 2, 3]) { |a = 99| a }.should == [1, 2, 3]
- end
- end
-end