Skip to content

Instantly share code, notes, and snippets.

@garohussenjian
Last active August 27, 2016 04:34
Show Gist options
  • Save garohussenjian/e651db64ef3330e53131 to your computer and use it in GitHub Desktop.
Save garohussenjian/e651db64ef3330e53131 to your computer and use it in GitHub Desktop.
Main.swift implementation to fix leaky coverage in Unit Tests (with Unit Tests)
//
// main.swift
//
// Created by Garo Hussenjian on 8/20/16.
// Copyright © 2016 Xapnet, Inc. All rights reserved.
//
import UIKit
extension ProcessInfo {
static func isUnitTesting() -> Bool {
return NSClassFromString("XCTestCase") != nil
}
}
extension UIApplicationDelegate {
static func classNameIfRunning(_ isUnitTesting: (() -> Bool) = ProcessInfo.isUnitTesting) -> String? {
return isUnitTesting() ? nil : NSStringFromClass(self)
}
}
extension UIApplication {
@discardableResult static func main(with delegate: UIApplicationDelegate.Type) -> Int32 {
return CommandLine.unsafeArgv.withMemoryRebound(to: UnsafeMutablePointer<Int8>.self,
capacity: Int(CommandLine.argc)) { argv in
UIApplicationMain(CommandLine.argc, argv, nil, delegate.classNameIfRunning())
}
}
}
UIApplication.main(with: AppDelegate.self)
/*-------------------------------------------------------------------------------------------*/
//
// MainTests.swift
//
// Created by Garo Hussenjian on 8/21/16.
// Copyright © 2016 Xapnet, Inc. All rights reserved.
//
import XCTest
@testable import CoreListApp
class MainTests: XCTestCase {
func testProcessInfo_WhenRunningTests_TestingReturnsTrue() {
XCTAssertTrue(ProcessInfo.isUnitTesting(), "isUnitTesting() should return true when testing")
}
func testAppDelegate_WhenRunningTests_ClassNameReturnsNil() {
let testingClassName = AppDelegate.classNameIfRunning() { return true }
XCTAssertNil(testingClassName, "className() should return nil when running tests")
}
func testAppDelegate_WhenRunningApp_ClassNameReturnsCorrectName() {
let runningClassName = AppDelegate.classNameIfRunning() { return false }
XCTAssertEqual(runningClassName, NSStringFromClass(AppDelegate.self),
"className() should be correct value when running app")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment