Ibland, Jens and Fritz,
My framework runs on Mac and iOS. The unit test in question checks whether SQLite has the FTS3 extension built-in. I see now that Mac OS X Lion 10.7 and iOS 5.1 includes a version of SQLite with FTS3. This wasn't the case before, so I need to check whether I'm running in these versions (or later). I found a solution which encompasses two steps:
1) Gather the OS version in setUp:
#ifdef __MAC_OS_X_VERSION_MAX_ALLOWED // code only compiled when targeting Mac OS X and not iOS // Obtain the system version SInt32 major, minor; Gestalt(gestaltSystemVersionMajor, &major); Gestalt(gestaltSystemVersionMinor, &minor); _systemVersion = major + (minor/10.0); #else // Round to the nearest since it's not always exact _systemVersion = floorf([[[UIDevice currentDevice]systemVersion]floatValue] * 10 + 0.5) / 10; #endif
2) Check the version string:
- (void)testSearchTestFTS3 { // Assume the testing code goes here...
if ((_systemVersion >= 10.7f) || (_systemVersion >= 5.1f)) { STAssertTrue ([results error] == nil, @"Wasn't expecting an error."); STAssertTrue ([results error] != nil, @"Was expecting an error."); }
In step 1 I found I had to round the value because floatValue was returning a value approximating 5.09, not 5.1. The comparison in step 2 would then fail, so rounding seems to get rid of the problem.
I can now run the unit test in both environments and seems to be working fine.
Thanks for the help,
-- Tito On Apr 8, 2012, at 5:25 PM, Jens Alfke wrote: On Apr 8, 2012, at 12:50 PM, Tito Ciuro wrote: I'm not sure whether I should post this question on the Cocoa or Xcode list. I'll try here since it has to do with unit tests: when I'm running a unit test, I need to detect whether I'm running on the Mac or on the simulator. […]At compile time I could use TARGET_IPHONE_SIMULATOR and TARGET_OS_MAC, but at runtime I'm not getting it right.
Why doesn't the compile-time check work? Any binary only runs on one of those three platforms, so there's nothing to check at runtime. - (void)testFoo { #if defined(TARGET_OS_IPHONE) && !defined(TARGET_OS_IPHONE_SIMULATOR) // Do something for iPhone... #else // Do something for Mac OS or simulator... #endif }
(In my experience you can't use TARGET_OS_MAC because it's true in all three environments; I think this is defined for the benefit of code that also compiles on Windows?)
—Jens
|