Last active
November 2, 2018 00:07
-
-
Save ashleyng/dcecc67e14b3b7ccddf1120f0781f2c4 to your computer and use it in GitHub Desktop.
Running Unit Tests in a Playground
This file contains hidden or 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 XCTest | |
| class NSNumberTests: XCTestCase { | |
| func testNumberReturnIntValue() { | |
| let rawNumberFloat: Float = 8.74 | |
| let number = NSNumber(value: rawNumberFloat) | |
| let expectedReturnValue: Int = 8 | |
| let actualReturnValue = number.intValue | |
| XCTAssertEqual(expectedReturnValue, actualReturnValue, "NSNumber should return an Int value") | |
| } | |
| func testNumberReturnBoolValue() { | |
| let rawNumberInt: Int = 0 | |
| let number = NSNumber(value: rawNumberInt) | |
| let expectedReturnValue: Bool = false | |
| let actualReturnValue = number.boolValue | |
| XCTAssertEqual(expectedReturnValue, actualReturnValue, "NSNumber should return a boolean") | |
| } | |
| } | |
| class NumberFormatterTests: XCTestCase { | |
| func testRoundingTypeFloor() { | |
| let formatter = NumberFormatter() | |
| formatter.roundingMode = .floor | |
| let number = NSNumber(value: 8.8) | |
| let expectedRoundingValue = "8" | |
| let actualRoundingValue = formatter.string(from: number) | |
| XCTAssertEqual(expectedRoundingValue, actualRoundingValue, "NumberFormatter should round down") | |
| } | |
| func testDecimalTruncation() { | |
| let formatter = NumberFormatter() | |
| formatter.minimumFractionDigits = 2 | |
| formatter.maximumFractionDigits = 2 | |
| let number = NSNumber(value: 8.87345) | |
| let expectedValue = "8.87" | |
| let actualValue = formatter.string(from: number) | |
| XCTAssertEqual(expectedValue, actualValue, "NumberFormatter should truncate decimals") | |
| } | |
| func testDecimalAddition() { | |
| let formatter = NumberFormatter() | |
| formatter.minimumFractionDigits = 2 | |
| formatter.maximumFractionDigits = 2 | |
| let number = NSNumber(value: 8.3) | |
| let expectedValue = "8.30" | |
| let actualValue = formatter.string(from: number) | |
| XCTAssertEqual(expectedValue, actualValue, "NumberFormatter should add an extra 0") | |
| } | |
| } | |
| NSNumberTests.defaultTestSuite.run() | |
| NumberFormatterTests.defaultTestSuite.run() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Following some of the tips in this article, but using an iOS/Swift example. As well as writing unit tests that will run in a Playground (Swift by Sundell Blog Post)