[spec] Add coverage for the various types of C-blocks.

This commit is contained in:
Eloy Durán
2013-11-14 12:38:07 +01:00
parent afce21ffad
commit 723e34bb36
3 changed files with 44 additions and 0 deletions

View File

@@ -238,6 +238,29 @@ describe "C-level blocks" do
end
$test_dealloc.should == true
end
it "wraps C-blocks as Procs" do
block = KreateMallocBlock(21)
block.should.be.instance_of Proc
block.call.should == 42
block = KreateGlobalBlock()
block.should.be.instance_of Proc
block.call.should == 42
end
it "yields C-blocks as Procs" do
return_value = nil
KreateStackBlock(lambda { |block| return_value = block.call })
return_value.should == 42
end
it "copies C 'stack' blocks onto the heap, making it safe outside of the yielded block" do
yielded_block = nil
KreateStackBlock(lambda { |block| yielded_block = block })
yielded_block.should.be.instance_of Proc
yielded_block.call.should == 42
end
end
describe "self" do

View File

@@ -73,3 +73,8 @@ extern int lowerCaseConstant;
#define TestStringConstant "foo"
#define TestNSStringConstant @"foo"
typedef int (^ReturnsIntBlock)();
void KreateStackBlock(void (^inputBlock)(ReturnsIntBlock));
ReturnsIntBlock KreateMallocBlock(int input);
ReturnsIntBlock KreateGlobalBlock();

View File

@@ -124,3 +124,19 @@ int lowerCaseConstant = 42;
@implementation lowerCaseClass
@end
void KreateStackBlock(void (^inputBlock)(ReturnsIntBlock))
{
int x = 42;
inputBlock(^{ return x; });
}
ReturnsIntBlock KreateMallocBlock(int input)
{
return Block_copy(^{ return input * 2; });
}
ReturnsIntBlock KreateGlobalBlock()
{
return ^{ return 42; };
}