zoul zoul - 4 months ago 15
Objective-C Question

Execute multiple statemens in a single STAssert… macro?

I’d like to execute more than one statement in the

STAssertXX
family of macros, is that possible? Something like:

STAssertTrue([foo doSomething] && [barDoSomethingElse], @"Should…");


The code above works for methods that return booleans, I’d like something similar for
void
return types:

STAssertNoThrow({
[foo doSomething];
[bar doSomethingElse];
}, @"Should…");


Of course, here the preprocessor complains.

Answer

Since any valid C code is also valid Objective-C; you can use the , operator from C for a single argument, as long as you use it in parenthesis.

Like this:

NSAssertNoThrow(([foo doSomething], [bar doSomethingElse]), @"Whatever");

The , operator lets you composite several expressions as a single statement returning the value of the last expression.

I would advise strongly against using this in STAssert… statements since you will loose important information about which tests that failed.