Skip to content

Commit

Permalink
GRMustacheContext swallows all NSUndefinedExceptions, in order to let…
Browse files Browse the repository at this point in the history
… users implement proxy objects
  • Loading branch information
groue committed May 26, 2012
1 parent 77d79e4 commit 74aca2c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
34 changes: 20 additions & 14 deletions src/classes/GRMustacheContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,27 +95,33 @@ - (id)valueForKey:(NSString *)key scoped:(BOOL)scoped
{
id value = nil;

if (_object) {
@try {
if (preventingNSUndefinedKeyExceptionAttack) {
if (_object)
{
@try
{
if (preventingNSUndefinedKeyExceptionAttack)
{
value = [GRMustacheNSUndefinedKeyExceptionGuard valueForKey:key inObject:_object];
} else {
}
else
{
value = [_object valueForKey:key];
}
}
@catch (NSException *exception) {
#if !defined(NS_BLOCK_ASSERTIONS)
// For testing purpose
GRMustacheContextDidCatchNSUndefinedKeyException = YES;
#endif

if (![[exception name] isEqualToString:NSUndefinedKeyException] ||
[[exception userInfo] objectForKey:@"NSTargetObjectUserInfoKey"] != _object ||
![[[exception userInfo] objectForKey:@"NSUnknownUserInfoKey"] isEqualToString:key])
@catch (NSException *exception)
{
// swallow all NSUndefinedKeyException, reraise other exceptions
if (![[exception name] isEqualToString:NSUndefinedKeyException])
{
// that's some exception we are not related to
[exception raise];
}
#if !defined(NS_BLOCK_ASSERTIONS)
else
{
// For testing purpose
GRMustacheContextDidCatchNSUndefinedKeyException = YES;
}
#endif
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/tests/Private/GRMustacheContextTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ - (id)valueForKey:(NSString *)key
if ([key isEqualToString:@"NonNSUndefinedKeyException"]) {
NSAssert(NO, @"");
}
if ([key isEqualToString:@"OtherNSUndefinedKeyException"]) {
if ([key isEqualToString:@"NonSelfNSUndefinedKeyException"]) {
return [@"" valueForKey:@"foo"];
}
return [super valueForKey:key];
Expand Down Expand Up @@ -214,11 +214,12 @@ - (void)testContextRethrowsNonNSUndefinedKeyException
STAssertThrows([context contextForKey:@"NonNSUndefinedKeyException" scoped:NO], nil);
}

- (void)testContextRethrowsOtherNSUndefinedKeyException
- (void)testContextSwallowsNonSelfNSUndefinedKeyException
{
// This test makes sure users can implement proxy objects
ThrowingObject *throwingObject = [[[ThrowingObject alloc] init] autorelease];
GRMustacheContext *context = [GRMustacheContext contextWithObject:throwingObject];
STAssertThrows([context contextForKey:@"OtherNSUndefinedKeyException" scoped:NO], nil);
STAssertNoThrow([context contextForKey:@"NonSelfNSUndefinedKeyException" scoped:NO], nil);
}

- (void)testContextSwallowsSelfNSUndefinedKeyException
Expand Down

0 comments on commit 74aca2c

Please sign in to comment.