Skip to content

Instantly share code, notes, and snippets.

@lzell
Created August 11, 2016 20:01
Show Gist options
  • Select an option

  • Save lzell/3a4f43d00657a347363343c035e5db4c to your computer and use it in GitHub Desktop.

Select an option

Save lzell/3a4f43d00657a347363343c035e5db4c to your computer and use it in GitHub Desktop.

Revisions

  1. lzell created this gist Aug 11, 2016.
    60 changes: 60 additions & 0 deletions xctesting_in_repl_or_script.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    // Start repl with:
    // $ xcrun swift -F /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks

    // Or run as script:
    // $ xcrun swift -F /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks %


    import Foundation

    if dlopen("/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Versions/A/XCTest", RTLD_NOW) == nil {
    perror(dlerror())
    }
    if dlopen("/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftXCTest.dylib", RTLD_NOW) == nil {
    perror(dlerror())
    }

    import XCTest

    class ATest : XCTestCase {
    func testIt() {
    XCTAssertTrue(false, "Now I see this!")
    }
    func testThat() {
    XCTAssertNil(nil, "Passes!")
    }
    }

    class BTest : XCTestCase {
    func testIt() {
    XCTAssertFalse(false, "whatever")
    }
    func testThat() {
    XCTAssertEqual(1, 0, "I fail too")
    }
    }

    // MARK: -
    func selectors<T: XCTestCase>(forType type: T.Type) -> [Selector] {
    var selectors = [Selector]()
    var count : UInt32 = 0
    let methods = class_copyMethodList(type, &count)!
    for i in 0..<count {
    let method = methods.advanced(by: Int(i)).pointee!
    selectors.append(method_getName(method)!)
    }
    return selectors
    }

    func runTests(_ types: XCTestCase.Type...) {
    let suite = XCTestSuite(name: "Required")
    for t in types {
    let tests = selectors(forType: t).filter() { String($0).hasPrefix("test") }
    tests.map(t.init)
    .forEach(suite.addTest)
    }
    suite.run()
    }

    runTests(ATest.self, BTest.self)