Skip to content

Instantly share code, notes, and snippets.

@royratcliffe
Last active August 29, 2015 14:14
Show Gist options
  • Save royratcliffe/1953ed1e150601168002 to your computer and use it in GitHub Desktop.
Save royratcliffe/1953ed1e150601168002 to your computer and use it in GitHub Desktop.
Bundle-based XCTestCase extensions
// XCTest XCTestCase+NSBundle.swift
//
// Copyright © 2015, Roy Ratcliffe, Pioneering Software, United Kingdom
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the “Software”), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS,” WITHOUT WARRANTY OF ANY KIND, EITHER
// EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
//------------------------------------------------------------------------------
import XCTest
extension XCTestCase {
/// Searches for a resource path given the name of a resource and
/// the resource type, its extension. Searches _all_ bundles
/// looking for the given resource. Answers `nil` if no resource
/// with the given name and type can be found.
///
/// This is very useful when testing. Test bundles execute within
/// the context of an application bundle. Hence, iOS fails when it
/// tries to find the resource in the main bundle. Use this
/// method. It searches all bundles which includes the test
/// bundle. Include test resources with the test bundle and this
/// method will find it.
func pathForResource(name: String, ofType type: String) -> String? {
for bundle in NSBundle.allBundles() {
if let path = bundle.pathForResource(name, ofType: type) {
return path
}
}
return nil
}
func dataForResource(name: String, ofType type: String) -> NSData? {
return NSData(contentsOfFile: self.pathForResource(name, ofType: type)!)
}
func stringForResource(name: String, ofType type: String, encoding: UInt) -> NSString? {
return NSString(data: self.dataForResource(name, ofType: type)!, encoding: encoding)
}
func UTF8StringForResource(name: String, ofType type: String) -> NSString? {
return self.stringForResource(name, ofType: type, encoding: NSUTF8StringEncoding)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment