summaryrefslogtreecommitdiff
path: root/spec/ruby/core/module/shared/class_exec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/module/shared/class_exec.rb')
-rw-r--r--spec/ruby/core/module/shared/class_exec.rb35
1 files changed, 35 insertions, 0 deletions
diff --git a/spec/ruby/core/module/shared/class_exec.rb b/spec/ruby/core/module/shared/class_exec.rb
new file mode 100644
index 0000000000..e51af1966d
--- /dev/null
+++ b/spec/ruby/core/module/shared/class_exec.rb
@@ -0,0 +1,35 @@
+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