Permalink
Browse files

Add specs to block_given? for Kernel.block_given? and self.send(:bloc…

…k_given?)
  • Loading branch information...
1 parent 1267889 commit dbbd5f2ad63092b640b7b211a25dd913aee36422 Yehuda Katz committed Jun 6, 2009
Showing with 93 additions and 5 deletions.
  1. +39 −5 core/kernel/block_given_spec.rb
  2. +54 −0 core/kernel/fixtures/classes.rb
View
44 core/kernel/block_given_spec.rb
@@ -1,16 +1,50 @@
require File.dirname(__FILE__) + '/../../spec_helper'
require File.dirname(__FILE__) + '/fixtures/classes'
+describe :kernel_block_given, :shared => true do
+ it "returns true if and only if a block is supplied" do
+ @object.accept_block {}.should_not == false
+ @object.accept_block_as_argument {}.should_not == false
+ @object.accept_block.should_not == true
+ @object.accept_block_as_argument.should_not == true
+ end
+
+ # Clarify: Based on http://www.ruby-forum.com/topic/137822 it appears
+ # that Matz wanted this to be true in 1.9.
+ it "returns false when a method defined by define_method is called with a block" do
+ @object.defined_block {}.should == false
+ end
+end
+
describe "Kernel#block_given?" do
+ it_behaves_like :kernel_block_given, :block_given?, KernelSpecs::BlockGiven
+
+ it "returns false outside of a method" do
+ block_given?.should == false
+ end
+
it "is a private method" do
Kernel.should have_private_instance_method(:block_given?)
end
+end
+
+describe "Kernel.block_given?" do
+ it_behaves_like :kernel_block_given, :block_given?, KernelSpecs::KernelBlockGiven
+end
+
+describe "self.send(:block_given?)" do
+ ruby_version_is ""..."1.9" do
+ it "always returns false" do
+ KernelSpecs::SelfBlockGiven.accept_block {}.should == false
+ KernelSpecs::SelfBlockGiven.accept_block_as_argument {}.should == false
+ KernelSpecs::SelfBlockGiven.accept_block.should == false
+ KernelSpecs::SelfBlockGiven.accept_block_as_argument.should == false
+ KernelSpecs::SelfBlockGiven.defined_block {}.should == false
+ end
+ end
- it "returns true if and only if a block is supplied" do
- KernelSpecs::BlockGiven::accept_block {}.should_not == false
- KernelSpecs::BlockGiven::accept_block_as_argument {}.should_not == false
- KernelSpecs::BlockGiven::accept_block.should_not == true
- KernelSpecs::BlockGiven::accept_block_as_argument.should_not == true
+ ruby_version_is "1.9" do
+ it_behaves_like :kernel_block_given, :block_given?, KernelSpecs::SelfBlockGiven
end
end
View
54 core/kernel/fixtures/classes.rb
@@ -138,6 +138,60 @@ def self.accept_block
def self.accept_block_as_argument(&block)
block_given?
end
+
+ class << self
+ define_method(:defined_block) do
+ block_given?
+ end
+ end
+ end
+
+ module KernelBlockGiven
+ def self.accept_block
+ Kernel.block_given?
+ end
+
+ def self.accept_block_as_argument(&block)
+ Kernel.block_given?
+ end
+
+ class << self
+ define_method(:defined_block) do
+ Kernel.block_given?
+ end
+ end
+ end
+
+ module SelfBlockGiven
+ def self.accept_block
+ self.send(:block_given?)
+ end
+
+ def self.accept_block_as_argument(&block)
+ self.send(:block_given?)
+ end
+
+ class << self
+ define_method(:defined_block) do
+ self.send(:block_given?)
+ end
+ end
+ end
+
+ module KernelBlockGiven
+ def self.accept_block
+ Kernel.block_given?
+ end
+
+ def self.accept_block_as_argument(&block)
+ Kernel.block_given?
+ end
+
+ class << self
+ define_method(:defined_block) do
+ Kernel.block_given?
+ end
+ end
end
def self.before_and_after

0 comments on commit dbbd5f2

Please sign in to comment.