GHUnit使用非必需的do-while循环?

时间:2011-03-10 14:33:58

标签: objective-c unit-testing

我正在查看GHUnit源代码,并注意到它们将大部分/全部测试函数包含在do-while循环中,但while条件将始终计算为false。这是为什么?

以下是#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 stringWithUTF8String:__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)

1 个答案:

答案 0 :(得分:0)

经过进一步检查,很明显他们正在使用do while循环。作者在各个地方使用continue语句以改变正常的执行顺序。您可以将其视为更简单/更少hacky goto声明。