diff --git a/test/test/spec/block_spec.rb b/test/test/spec/block_spec.rb index 5dadd0be..b2da311b 100644 --- a/test/test/spec/block_spec.rb +++ b/test/test/spec/block_spec.rb @@ -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 diff --git a/test/test/vendor/code/code.h b/test/test/vendor/code/code.h index cdb40e86..ceb2b126 100644 --- a/test/test/vendor/code/code.h +++ b/test/test/vendor/code/code.h @@ -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(); diff --git a/test/test/vendor/code/code.m b/test/test/vendor/code/code.m index 6fd86ddd..500abda9 100644 --- a/test/test/vendor/code/code.m +++ b/test/test/vendor/code/code.m @@ -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; }; +}