• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Detecting iOS and Mac OS X when running unit tests
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Detecting iOS and Mac OS X when running unit tests


  • Subject: Re: Detecting iOS and Mac OS X when running unit tests
  • From: Tito Ciuro <email@hidden>
  • Date: Sun, 08 Apr 2012 18:06:09 -0300

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

I found this here: http://developer.apple.com/library/mac/#documentation/developertools/conceptual/cross_development/Using/using.html, listing 3.3

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.");
    } else {
        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

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Detecting iOS and Mac OS X when running unit tests (From: Tito Ciuro <email@hidden>)
 >Re: Detecting iOS and Mac OS X when running unit tests (From: Jens Alfke <email@hidden>)

  • Prev by Date: Re: Detecting iOS and Mac OS X when running unit tests
  • Next by Date: How can I obtain the current SDK version specified in the target?
  • Previous by thread: Re: Detecting iOS and Mac OS X when running unit tests
  • Next by thread: How can I obtain the current SDK version specified in the target?
  • Index(es):
    • Date
    • Thread