tags:

views:

82

answers:

2

This test will fail:

#import "GTMSenTestCase.h"

@interface Person : NSObject
@property (readonly) NSString *name;
@end
@implementation Person
- (NSString *)name { return @"Nick"; }
@end

@interface TemplateUnitTest : GTMTestCase @end

@implementation TemplateUnitTest

static BOOL called = NO;
- (Person *)get {
  if (called) { STFail(nil); }
  called = YES;
  return [[Person new] autorelease];
}

- (void)testPropertyMakesThingGetSentTwice {
  NSString *s = [[self get].name stringByAppendingString:@"foo"];
  STAssertEqualObjects(@"Nickfoo", s, nil);
}

@end

If I replace the [self get].name with [[self get] name], it passes. ie, Using dot-syntax, the LHS of the '.' is evaluated twice. How does this happen?

+2  A: 

Admitting in public that you use the dot syntax in Objective-C is likely to get you burned at the stake by the purists ;-)

It looks like it's a bug in this particular scenario, as the thread says, it's probably some pre-processing magic that's expanding it wrongly.

Steven Robbins
Ah, thanks :). Consider me a convert to purity now. In my case, the selector was calling a web-service - nasty.I've logged a bug with apple (#6729855), if you're interested.
Nick Partridge
+2  A: 

This was a compiler bug, and should be fixed in gcc-4.2.