GHUnit using non-required do-while loop?
I was looking through the GHUnit source code, and noticed that they wrap most/all of their testing functions in do-while
loops, but the while condition will always evaluate to false
. Why is this?
Here's their listing for #define GHAssertEqualStrings (a1, a2, description, ...)
do { \
@try {\
id a1value = (a1); \
id a2value = (a2); \
if (a1value == a2value) continue; \
if ([a1value isKindOfClass:[NSString class]] && \
[a2value isKindOfClass:[NSString class]] && \
[a1value compare:a2value options:0] == NSOrderedSame) continue; \
[self failWithException:[NSException ghu_failureInEqualityBetweenObject: a1value \
andObject: a2value \
inFile: [NSString stringWith开发者_StackOverflow中文版UTF8String:__FILE__] \
atLine: __LINE__ \
withDescription: GHComposeString(description, ##__VA_ARGS__)]]; \
}\
@catch (id anException) {\
[self failWithException:[NSException ghu_failureInRaise:[NSString stringWithFormat: @"(%s) == (%s)", #a1, #a2] \
exception:anException \
inFile:[NSString stringWithUTF8String:__FILE__] \
atLine:__LINE__ \
withDescription:GHComposeString(description, ##__VA_ARGS__)]]; \
}\
} while(0)
Upon further inspection, it's quite obvious that they are using the do while loop to their advantage. The author is using the continue
statement at various places in order to alter the normal order of execution. You could think of it as a simpler/less hacky goto
statement.
精彩评论