Skip to content

Instantly share code, notes, and snippets.

@ZevEisenberg ZevEisenberg/License
Last active Mar 2, 2019

Embed
What would you like to do?
Test lots of values without lots of boilerplate
Copyright 2019 Zev Eisenberg
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
// Gist version of an article posted on the Rightpoint developer blog
import XCTest
// We need some struct to test with. Let's model a software version number,
// since comparing version numbers can get tricky.
struct Version: CustomStringConvertible {
let major: Int
let minor: Int
let bugfix: Int
/// Convenience initializer that takes a string.
///
/// Version("1") // 1.0.0
/// Version("1.2") // 1.2.0
/// Version("1.2.3") // 1.2.3
///
init(_ string: String) {
// Naïve implementation that assumes that input is
// 1, 2, or 3 numbers separated by periods. Please
// do better input validation in your own code.
let segments = string
.split(separator: ".") // Split into segments by "."
.map(String.init) // Convert Substring to String
.compactMap(Int.init) // Convert String to Int. Using compactMap because init?(string:) is failable.
self.major = segments.count >= 1 ? segments[0] : 0
self.minor = segments.count >= 2 ? segments[1] : 0
self.bugfix = segments.count >= 3 ? segments[2] : 0
}
// Make debug and error output easier to read
var description: String {
return "\(major).\(minor).\(bugfix)"
}
}
extension Version: Comparable {
// Comparison function inspired by the one from Apple's Foundation.OperatingSystemVersion:
// https://github.com/apple/swift-corelibs-foundation/blob/291550f937f812f0d0dbea3d640a7636cd147c0e/Foundation/ProcessInfo.swift#L120
static func <(lhs: Version, rhs: Version) -> Bool {
if lhs.major < rhs.major { return true }
if lhs.major > rhs.major { return false }
if lhs.minor > rhs.minor { return true } // BUG! ">" should be "<"
if lhs.minor > rhs.minor { return false }
if lhs.bugfix < rhs.bugfix { return true }
return false
}
}
class LineByLineTests: XCTestCase {
// Test the hard way: one test per line, everything written out longhand.
// The important parts are the strings, but they get lost in the noise.
func testTheHardWay() {
XCTAssertLessThan(Version("1.2"), Version("1.2.3"))
XCTAssertLessThan(Version("1.0"), Version("1.0.1"))
XCTAssertLessThan(Version("1.0.0"), Version("1.0.1"))
XCTAssertLessThan(Version("1"), Version("2"))
XCTAssertLessThan(Version("1.1"), Version("1.2"))
XCTAssertLessThan(Version("1.2.3"), Version("1.2.4"))
}
// Test with abstraction
func testWithAbstraction() {
// shorten type name to make tests easier to read
typealias V = Version
let pairs: [(left: Version, right: Version)] = [
(V("1.2"), V("1.2.3")),
(V("1.0"), V("1.0.1")),
(V("1.0.0"), V("1.0.1")),
(V("1"), V("2")),
(V("1.1"), V("1.2")),
(V("1.2.3"), V("1.2.4")),
]
for pair in pairs {
let (left, right) = pair
// Unfortunately, all test failures show up on the following line,
// no matter which test failed. Makes it harder to see where the
// problem is, as well as how many cases are failing.
XCTAssertLessThan(left, right)
}
}
// Test with abstraction and line numbers
func testWithAbstractionAndLineNumbers() {
typealias V = Version
// Capture the line where the array is declared. We will use it later
// to figure out which line to show the test failure on.
let pairsLine = #line; let pairs: [(left: Version, right: Version)] = [
(V("1.2"), V("1.2.3")),
(V("1.0"), V("1.0.1")),
(V("1.0.0"), V("1.0.1")),
(V("1"), V("2")),
(V("1.1"), V("1.2")),
(V("1.2.3"), V("1.2.4")),
]
for (offset, pair) in pairs.enumerated() {
// "offset" is the index we're currently at in the array.
// To get the line number for this test, add "offset" to the
// line where the array is declared, then add 1 because
// the iteration counts from 0.
let line = pairsLine + offset + 1
let (left, right) = pair
XCTAssertLessThan(left, right, line: UInt(line))
}
}
func testWithHelper() {
typealias V = Version
assertLessThan(testCases: [
Pair(V("1.2"), V("1.2.3")),
// safe to comment out or add extra lines without throwing off the counting
// Pair(V("1.0"), V("1.0.1")),
Pair(V("1.0.0"), V("1.0.1")),
Pair(V("1"), V("2")),
Pair(V("1.1"), V("1.2")),
Pair(V("1.2.3"), V("1.2.4")),
])
}
// No failures here, because we're testing equality,
// and the auto-generated Equatable conformance
// is never wrong!
func testWithCustomHelper() {
typealias V = Version
assertCustom(testCases: [
Pair((V("1.2"), V("1.2.3")), false),
Pair((V("1"), V("1.0")), true),
Pair((V("1"), V("1.0.0")), true),
Pair((V("1"), V("2")), false),
Pair((V("1.0.1"), V("1.0.2")), false),
Pair((V("1.0.2"), V("1.0.2")), true),
Pair((V("1.2.1"), V("1.3.1")), false),
], tests: { (pair, file, line) in
if pair.right {
XCTAssertEqual(pair.left.0, pair.left.1, file: file, line: line)
} else {
XCTAssertNotEqual(pair.left.0, pair.left.1, file: file, line: line)
}
})
}
}
import XCTest
extension XCTestCase {
struct Pair<T, U> {
let left: T
let right: U
// Extra properties to capture the file and line
// where the pair was created
let line: UInt
let file: StaticString
init(_ left: T, _ right: U, file: StaticString = #file, line: UInt = #line) {
self.left = left
self.right = right
self.file = file
self.line = line
}
}
func assertBoolean(testCases: [Pair<Bool, Bool>]) {
for testCase in testCases {
if testCase.right {
XCTAssertTrue(testCase.left, file: testCase.file, line: testCase.line)
} else {
XCTAssertFalse(testCase.left, file: testCase.file, line: testCase.line)
}
}
}
func assertLessThan<T: Comparable>(testCases: [Pair<T, T>]) {
for testCase in testCases {
XCTAssertLessThan(testCase.left, testCase.right, file: testCase.file, line: testCase.line)
}
}
func assertGreaterThan<T: Comparable>(testCases: [Pair<T, T>]) {
for testCase in testCases {
XCTAssertGreaterThan(testCase.left, testCase.right, file: testCase.file, line: testCase.line)
}
}
func assertLessThanOrEqual<T: Comparable>(testCases: [Pair<T, T>]) {
for testCase in testCases {
XCTAssertLessThanOrEqual(testCase.left, testCase.right, file: testCase.file, line: testCase.line)
}
}
func assertGreaterThanOrEqual<T: Comparable>(testCases: [Pair<T, T>]) {
for testCase in testCases {
XCTAssertGreaterThanOrEqual(testCase.left, testCase.right, file: testCase.file, line: testCase.line)
}
}
func assertEqual<T: Equatable>(testCases: [Pair<T, T>]) {
for testCase in testCases {
XCTAssertEqual(testCase.left, testCase.right, file: testCase.file, line: testCase.line)
}
}
func assertEqual<T: FloatingPoint>(testCases: [Pair<T, T>], accuracy: T) {
for testCase in testCases {
XCTAssertEqual(testCase.left, testCase.right, accuracy: accuracy, file: testCase.file, line: testCase.line)
}
}
func assertNotEqual<T: Equatable>(testCases: [Pair<T, T>]) {
for testCase in testCases {
XCTAssertNotEqual(testCase.left, testCase.right, file: testCase.file, line: testCase.line)
}
}
func assertNotEqual<T: FloatingPoint>(testCases: [Pair<T, T>], accuracy: T) {
for testCase in testCases {
XCTAssertNotEqual(testCase.left, testCase.right, accuracy: accuracy, file: testCase.file, line: testCase.line)
}
}
func assertCustom<T, U>(
testCases: [Pair<T, U>],
tests: (Pair<T, U>, _ file: StaticString, _ line: UInt) -> Void
) {
for testCase in testCases {
tests(testCase, testCase.file, testCase.line)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.