Skip to content

Instantly share code, notes, and snippets.

@ashleyng
Last active November 2, 2018 00:07
Show Gist options
  • Select an option

  • Save ashleyng/dcecc67e14b3b7ccddf1120f0781f2c4 to your computer and use it in GitHub Desktop.

Select an option

Save ashleyng/dcecc67e14b3b7ccddf1120f0781f2c4 to your computer and use it in GitHub Desktop.
Running Unit Tests in a Playground
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()
@ashleyng
Copy link
Author

ashleyng commented Nov 2, 2018

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment