mirror of
https://github.com/zhigang1992/RubyMotion.git
synced 2026-04-05 22:40:25 +08:00
[spec] Add coverage for the various types of C-blocks.
This commit is contained in:
@@ -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
|
||||
|
||||
5
test/test/vendor/code/code.h
vendored
5
test/test/vendor/code/code.h
vendored
@@ -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();
|
||||
|
||||
16
test/test/vendor/code/code.m
vendored
16
test/test/vendor/code/code.m
vendored
@@ -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; };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user