Project

General

Profile

Backport #8341 » testcase.rb

pythonesque (Joshua Yanovski), 04/28/2013 06:32 AM

 
# Note: this testcase requires RSpec but it is trivial to demonstrate the same behavior without RSpec.

gem "rspec"

RSpec.configure do |c|
c.alias_it_should_behave_like_to :it_has_behavior, 'has behavior:'
end

shared_examples "accepts a block" do
def test_block_given
return block_given?
end
describe "block_given?" do
context "with no block" do
it "returns false" do
call_block_given.should be_false
end
end
context "with a block" do
it "returns true" do
call_block_given { }.should be_true
end
end
context "with no block again" do
it "returns false" do
call_block_given.should be_false
end
end
context "with a block again" do
it "returns true" do
call_block_given { }.should be_true
end
end
end
end

describe "method()" do
it_has_behavior "accepts a block" do
alias_method :call_block_given, :test_block_given
end
end

describe "method().call()" do
it_has_behavior "accepts a block" do
before (:all) do
@block_given_method = method(:test_block_given)
end

def call_block_given &block
@block_given_method.call &block
end
end
end

describe "method().to_proc().call() (proc recreated for each call)" do
it_has_behavior "accepts a block" do
def call_block_given &block
method(:test_block_given).to_proc.call &block
end
end
end

describe "method().to_proc().call() (proc persisted across calls)" do
it_has_behavior "accepts a block" do
before (:all) do
@block_given_method = method(:test_block_given).to_proc
end

def call_block_given &block
@block_given_method.call &block
end
end
end
(1-1/2)