-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathChallengeTests.swift
104 lines (95 loc) · 3.68 KB
/
ChallengeTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import Foundation
import XCTest
@testable import VersionSort
final class ChallengeTests: XCTestCase {
static var allTests = [
("test", test)
]
func test() {
// runTest(compareVersions)
}
}
func runTest(_ method: (String, String) -> ComparisonResult) {
// Test cases from the README
runTest(method, "1.0.1", "1.1.0", .orderedAscending)
runTest(method, "1.9", "1.10", .orderedAscending)
runTest(method, "3.0", "2.1.8", .orderedDescending)
runTest(method, "4.7-alpha11", "4.7-alpha2", .orderedDescending)
runTest(method, "v2.22", "2.22", .orderedSame)
runTest(method, "2", "2.0.0", .orderedSame)
// Numeric-only test cases, ascending / descending
runTest(method, "0.1", "0.5", .orderedAscending)
runTest(method, "0.1", "0.6.0", .orderedAscending)
runTest(method, "0.1", "0.10", .orderedAscending)
runTest(method, "0.1", "1", .orderedAscending)
runTest(method, "0.1", "1.0", .orderedAscending)
runTest(method, "0.1", "1.0.1", .orderedAscending)
runTest(method, "0.1", "10", .orderedAscending)
runTest(method, "1.2.3", "3.2.1", .orderedAscending)
runTest(method, "1.2.3", "30.0", .orderedAscending)
// Numeric-only test cases, same
runTest(method, "1", "1", .orderedSame)
runTest(method, "2.10", "2.10", .orderedSame)
runTest(method, "3.23.12", "3.23.12", .orderedSame)
runTest(method, "4.55.17.3", "4.55.17.3", .orderedSame)
runTest(method, "172", "172", .orderedSame)
runTest(method, "2", "2.0", .orderedSame)
runTest(method, "2", "2.0.0", .orderedSame)
runTest(method, "2", "2.00.00", .orderedSame)
runTest(method, "2.0", "2.00.00", .orderedSame)
// v prefix test cases
runTest(method, "v9", "v20", .orderedAscending)
runTest(method, "v0.1", "v1.2.3", .orderedAscending)
runTest(method, "v3", "v3.0", .orderedSame)
runTest(method, "v1.2", "v1.2", .orderedSame)
runTest(method, "v3", "3.0.0", .orderedSame)
// Suffix test cases
runTest(method, "2.0-alpha1", "2.0-alpha2", .orderedAscending)
runTest(method, "2.0-alpha2", "2.0-alpha10", .orderedAscending)
runTest(method, "2.0-alpha10", "2.0-beta", .orderedAscending)
runTest(method, "2.0_beta", "2.0_final", .orderedAscending)
runTest(method, "2.0rc", "2.0rc", .orderedSame)
runTest(method, "2-final", "2.0.0-final", .orderedSame)
}
private let pattern = #"^v?\d+(?:\.\d+)*(?:[_-]?[a-zA-Z]*\d*)?$"#
private func runTest(
_ method: (String, String) -> ComparisonResult,
_ version1: String,
_ version2: String,
_ result: ComparisonResult,
file: StaticString = #file,
line: UInt = #line)
{
XCTAssertNotNil(
version1.range(of: pattern, options: .regularExpression),
"\(version1) does not match pattern", file: file, line: line)
XCTAssertNotNil(
version2.range(of: pattern, options: .regularExpression),
"\(version2) does not match pattern", file: file, line: line)
XCTAssertEqual(
method(version1, version2), result, file: file, line: line)
XCTAssertEqual(
method(version2, version1), result.inverted, file: file, line: line)
}
extension ComparisonResult: CustomStringConvertible {
public var description: String {
switch self {
case .orderedAscending:
return "orderedAscending"
case .orderedDescending:
return "orderedDescending"
case .orderedSame:
return "orderedSame"
}
}
public var inverted: ComparisonResult {
switch self {
case .orderedAscending:
return .orderedDescending
case .orderedDescending:
return .orderedAscending
case .orderedSame:
return .orderedSame
}
}
}