diff options
Diffstat (limited to 'spec/ruby/core/module/module_exec_spec.rb')
| -rw-r--r-- | spec/ruby/core/module/module_exec_spec.rb | 35 |
1 files changed, 33 insertions, 2 deletions
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 |
