Skip to content

Commit

Permalink
Test+Fix: section helpers enter context stack
Browse files Browse the repository at this point in the history
  • Loading branch information
groue committed Sep 25, 2012
1 parent 7344a18 commit 180fc59
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/classes/GRMustacheSectionElement.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ - (void)renderInBuffer:(NSMutableString *)buffer withRuntime:(GRMustacheRuntime
{
// Helper
if (!_inverted) {
GRMustacheSection *section = [GRMustacheSection sectionWithSectionElement:self runtime:sectionRuntime];
GRMustacheRuntime *helperRuntime = [sectionRuntime runtimeByAddingContextObject:value];
GRMustacheSection *section = [GRMustacheSection sectionWithSectionElement:self runtime:helperRuntime];
NSString *rendering = [(id<GRMustacheSectionHelper>)value renderSection:section];
if (rendering) {
[buffer appendString:rendering];
Expand Down
27 changes: 27 additions & 0 deletions src/tests/Public/v5.0/GRMustacheSectionHelperTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ - (NSString *)renderSection:(GRMustacheSection *)section
}
@end

@interface GRMustacheAttributedSectionHelper : NSObject<GRMustacheHelper> {
NSString *_attribute;
}
@property (nonatomic, copy) NSString *attribute;
@end

@implementation GRMustacheAttributedSectionHelper
@synthesize attribute=_attribute;
- (void)dealloc
{
self.attribute = nil;
[super dealloc];
}
- (NSString *)renderSection:(GRMustacheSection *)section
{
return [section renderTemplateString:@"{{attribute}}" error:NULL];
}
@end

@interface GRMustacheRecorderSectionHelper : NSObject<GRMustacheHelper> {
NSUInteger _invocationCount;
NSString *_lastInnerTemplateString;
Expand Down Expand Up @@ -279,6 +298,14 @@ - (void)testHelperCanRenderCurrentContextInDistinctTemplate
STAssertEqualObjects(result, @"---", @"");
}

- (void)testHelperEnterCurrentContext
{
GRMustacheAttributedSectionHelper *attributedHelper = [[[GRMustacheAttributedSectionHelper alloc] init] autorelease];
attributedHelper.attribute = @"---";
NSString *result = [GRMustacheTemplate renderObject:@{ @"helper": attributedHelper } fromString:@"{{#helper}}{{/helper}}" error:NULL];
STAssertEqualObjects(result, @"---", @"");
}

- (void)testHelperCanRenderCurrentContextInDistinctTemplateContainingPartial
{
id helper = [GRMustacheHelper helperWithBlock:^NSString *(GRMustacheSection *section) {
Expand Down

0 comments on commit 180fc59

Please sign in to comment.