Last active
November 7, 2016 03:18
-
-
Save iosdevben/3bce3170925ff1b3e82d24e25d5876de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import PlaygroundSupport | |
import XCTest | |
struct Person { | |
let firstName: String | |
let lastName: String | |
let address: String | |
} | |
struct ViewModel { | |
var person: Person | |
var nameAndAddress: String { | |
return "\(person.firstName) \(person.lastName), \(person.address)" | |
} | |
init() { | |
person = Person(firstName: "John", | |
lastName: "Appleseed", | |
address: "1 Infinite Loop, Cupertino, CA") | |
} | |
} | |
let label = UILabel() | |
label.text = ViewModel().nameAndAddress | |
label.backgroundColor = UIColor.white | |
label.sizeToFit() | |
PlaygroundPage.current.liveView = label | |
class PersonTest: XCTestCase { | |
override func setUp() { | |
super.setUp() | |
} | |
override func tearDown() { | |
super.tearDown() | |
} | |
func testResultOfNameAndAddressFunc() { | |
XCTAssertEqual(ViewModel().nameAndAddress, | |
"John Appleseed, 1 Infinite Loop, Cupertino, CA") | |
} | |
} | |
class PlaygroundTestObserver : NSObject, XCTestObservation { | |
@objc func testCase(_ testCase: XCTestCase, didFailWithDescription description: String, inFile filePath: String?, atLine lineNumber: UInt) { | |
print("Test failed on line \(lineNumber): \(testCase.name!), \(description)") | |
} | |
} | |
let observer = PlaygroundTestObserver() | |
let center = XCTestObservationCenter.shared() | |
center.addTestObserver(observer) | |
struct TestRunner { | |
func runTests(testClass:AnyClass) { | |
print("Running test suite \(testClass)") | |
let tests = testClass as! XCTestCase.Type | |
let testSuite = tests.defaultTestSuite() | |
testSuite.run() | |
let run = testSuite.testRun as! XCTestSuiteRun | |
print("Ran \(run.executionCount) tests in \(run.testDuration)s with \(run.totalFailureCount) failures") | |
} | |
} | |
TestRunner().runTests(testClass: PersonTest.self) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment