Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing #ifdef

What is the best way to unit test code that's behaviour changes based on an ifdef?

e.g.

+ (NSString*) someMethod:(NSString*)value {
    //Do some stuff ...
#ifdef DEBUG
    //Tell user about error
#else
    //Suppress error
#endif
}
like image 575
Sam Avatar asked Dec 06 '25 05:12

Sam


1 Answers

you should only need to test release build because this what the user see.

to actually answer your question: you can split the method to 3 method

+ (NSString*) someMethod:(NSString*)value {
#ifdef DEBUG
    return [self someMethod_debug:value];
#else
    return [self someMethod_release:value];
#endif
}

+ (void)someMethod_debug:(NSString *)value {
    //Do some stuff ...
    //Tell user about error
}

+ (void)someMethod_release:(NSString *)value {
    //Do some stuff ...
    //Suppress error
}

then you can test someMethod_debug and someMethod_release individually

like image 104
Bryan Chen Avatar answered Dec 08 '25 19:12

Bryan Chen