Skip to content

Commit cf0b037

Browse files
committed
test: use Spectre to improve test readability/debuggability
Tricks: * (compact results) `env SPECTRE_REPORTER=dot swift test` * (test only file) `env SPECTRE_ADDOPTS=Tests/Cases/tests/draft2020-12/allOf.json swift test`
1 parent 7cf6205 commit cf0b037

14 files changed

+427
-483
lines changed

Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ let package = Package(
1212
],
1313
dependencies: [
1414
.package(url: "https://github.com/kylef/PathKit.git", .upToNextMajor(from: "1.0.0")),
15+
.package(url: "https://github.com/kylef/Spectre.git", .revision("d02129a9af77729de049d328dd61e530b6f2bb2b"))
1516
],
1617
targets: [
1718
.target(name: "JSONSchema", dependencies: [], path: "Sources"),
18-
.testTarget(name: "JSONSchemaTests", dependencies: ["JSONSchema", "PathKit"]),
19+
.testTarget(name: "JSONSchemaTests", dependencies: ["JSONSchema", "Spectre", "PathKit"]),
1920
]
2021
)
Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,75 @@
1-
import XCTest
1+
import Spectre
22
@testable import JSONSchema
33

44

5-
class DurationTests: XCTestCase {
6-
// dur-date
5+
public let testDurationFormat: ((ContextType) -> Void) = {
6+
$0.describe("dur-date") {
7+
$0.it("allows day duration") {
8+
try expect(isValidDuration("P3D")).to.beTrue()
9+
}
710

8-
func testDay() {
9-
XCTAssertTrue(isValidDuration("P3D"))
10-
}
11-
12-
func testMonth() {
13-
XCTAssertTrue(isValidDuration("P2M"))
14-
}
11+
$0.it("allows month duration") {
12+
try expect(isValidDuration("P2M")).to.beTrue()
13+
}
1514

16-
func testMonthDay() {
17-
XCTAssertTrue(isValidDuration("P2M1D"))
18-
}
15+
$0.it("allows month and day duration") {
16+
try expect(isValidDuration("P2M1D")).to.beTrue()
17+
}
1918

20-
func testYear() {
21-
XCTAssertTrue(isValidDuration("P5Y"))
22-
}
23-
24-
func testYearMonth() {
25-
XCTAssertTrue(isValidDuration("P5Y2M"))
26-
}
19+
$0.it("allows year duration") {
20+
try expect(isValidDuration("P5Y")).to.beTrue()
21+
}
2722

28-
func testYearMonthDay() {
29-
XCTAssertTrue(isValidDuration("P5Y2M1D"))
30-
}
23+
$0.it("allows year and month duration") {
24+
try expect(isValidDuration("P5Y2M")).to.beTrue()
25+
}
3126

32-
func testDateTime() {
33-
XCTAssertTrue(isValidDuration("P1DT5M"))
27+
$0.it("allows year, month and day duration") {
28+
try expect(isValidDuration("P5Y2M1D")).to.beTrue()
29+
}
3430
}
3531

36-
// dur-time
32+
$0.describe("dur-time") {
33+
$0.it("allows hour duration") {
34+
try expect(isValidDuration("PT1H")).to.beTrue()
35+
}
3736

38-
func testHour() {
39-
XCTAssertTrue(isValidDuration("PT1H"))
40-
}
37+
$0.it("allows hour duration with minutes") {
38+
try expect(isValidDuration("PT1H5M")).to.beTrue()
39+
}
4140

42-
func testHourMinute() {
43-
XCTAssertTrue(isValidDuration("PT1H5M"))
44-
}
41+
$0.it("allows hour duration with minutes and seconds") {
42+
try expect(isValidDuration("PT1H5M20S")).to.beTrue()
43+
}
4544

46-
func testHourMinuteSecond() {
47-
XCTAssertTrue(isValidDuration("PT1H5M20S"))
48-
}
45+
$0.it("allows minute duration") {
46+
try expect(isValidDuration("PT1M")).to.beTrue()
47+
}
4948

50-
func testMinute() {
51-
XCTAssertTrue(isValidDuration("PT1M"))
52-
}
49+
$0.it("allows minute duration with seconds") {
50+
try expect(isValidDuration("PT5M10S")).to.beTrue()
51+
}
5352

54-
func testMinuteSecond() {
55-
XCTAssertTrue(isValidDuration("PT5M10S"))
53+
$0.it("allows second duration") {
54+
try expect(isValidDuration("PT1S")).to.beTrue()
55+
}
5656
}
5757

58-
func testSecond() {
59-
XCTAssertTrue(isValidDuration("PT1S"))
58+
$0.describe("dur-week") {
59+
$0.it("allows weeb duration") {
60+
try expect(isValidDuration("P1W")).to.beTrue()
61+
}
6062
}
6163

62-
// dur-week
63-
64-
func testWeek() {
65-
XCTAssertTrue(isValidDuration("P1W"))
64+
$0.it("allows date and time duration") {
65+
try expect(isValidDuration("P1DT5M")).to.beTrue()
6666
}
6767

68-
// Negative
69-
70-
func testMissingDuration() {
71-
XCTAssertFalse(isValidDuration("P"))
68+
$0.it("fails validation without duration") {
69+
try expect(isValidDuration("P")).to.beFalse()
7270
}
7371

74-
func testMissingTime() {
75-
XCTAssertFalse(isValidDuration("PT"))
72+
$0.it("fails validation with empty time") {
73+
try expect(isValidDuration("PT")).to.beFalse()
7674
}
7775
}
Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import XCTest
1+
import Foundation
2+
import Spectre
23
@testable import JSONSchema
34

45

@@ -17,114 +18,116 @@ fileprivate let document: [String: Any] = [
1718
]
1819

1920

20-
class JSONPointerTests: XCTestCase {
21+
public let testJSONPointer: ((ContextType) -> Void) = {
2122
// Resolution (https://tools.ietf.org/html/rfc6901#section-5)
2223

23-
func testResolveWholeDocument() {
24+
$0.it("can resoleve entire document") {
2425
let pointer = JSONPointer(path: "")
2526

26-
XCTAssertEqual(pointer.resolve(document: document) as? NSDictionary, document as NSDictionary)
27+
try expect(pointer.resolve(document: document) as? NSDictionary) == document as NSDictionary
2728
}
2829

29-
func testResolveObjectValue() {
30+
$0.it("can resolve object value") {
3031
let pointer = JSONPointer(path: "/foo")
3132

32-
XCTAssertEqual(pointer.resolve(document: document) as? [String], ["bar", "baz"])
33+
try expect(pointer.resolve(document: document) as? [String]) == ["bar", "baz"]
3334
}
3435

35-
func testResolveArrayIndex() {
36+
$0.it("can resolve array index") {
3637
let pointer = JSONPointer(path: "/foo/0")
3738

38-
XCTAssertEqual(pointer.resolve(document: document) as? String, "bar")
39+
try expect(pointer.resolve(document: document) as? String) == "bar"
3940

4041
let pointer1 = JSONPointer(path: "/foo/1")
4142

42-
XCTAssertEqual(pointer1.resolve(document: document) as? String, "baz")
43+
try expect(pointer1.resolve(document: document) as? String) == "baz"
4344
}
4445

45-
func testResolveObjectValueKeyEmptyString() {
46+
$0.it("can resolve object value via empty key") {
4647
let pointer = JSONPointer(path: "/")
4748

48-
XCTAssertEqual(pointer.resolve(document: document) as? Int, 0)
49+
try expect(pointer.resolve(document: document) as? Int) == 0
4950
}
5051

51-
func testResolveObjectValueKeyEscapedForwardSlash() {
52+
$0.it("can resolve object value with escaped slash in key") {
5253
let pointer = JSONPointer(path: "/a~1b")
5354

54-
XCTAssertEqual(pointer.resolve(document: document) as? Int, 1)
55+
try expect(pointer.resolve(document: document) as? Int) == 1
5556
}
5657

57-
func testResolveObjectValueKeyPercent() {
58+
$0.it("can resolve object value with percent in key") {
5859
let pointer = JSONPointer(path: "/c%d")
5960

60-
XCTAssertEqual(pointer.resolve(document: document) as? Int, 2)
61+
try expect(pointer.resolve(document: document) as? Int) == 2
6162
}
6263

63-
func testResolveObjectValueKeyCarot() {
64+
$0.it("can resolve object value with carot in key") {
6465
let pointer = JSONPointer(path: "/e^f")
6566

66-
XCTAssertEqual(pointer.resolve(document: document) as? Int, 3)
67+
try expect(pointer.resolve(document: document) as? Int) == 3
6768
}
6869

69-
func testResolveObjectValueKeyPipe() {
70+
$0.it("can resolve object value with pipe in key") {
7071
let pointer = JSONPointer(path: "/g|h")
7172

72-
XCTAssertEqual(pointer.resolve(document: document) as? Int, 4)
73+
try expect(pointer.resolve(document: document) as? Int) == 4
7374
}
7475

75-
func testResolveObjectValueBackslace() {
76+
$0.it("can resolve object value with backslash in key") {
7677
let pointer = JSONPointer(path: "/i\\j")
7778

78-
XCTAssertEqual(pointer.resolve(document: document) as? Int, 5)
79+
try expect(pointer.resolve(document: document) as? Int) == 5
7980
}
8081

81-
func testResolveObjectValueDoubleQuote() {
82+
$0.it("can resolve object value with double quote in key") {
8283
let pointer = JSONPointer(path: "/k\"l")
8384

84-
XCTAssertEqual(pointer.resolve(document: document) as? Int, 6)
85+
try expect(pointer.resolve(document: document) as? Int) == 6
8586
}
8687

87-
func testResolveObjectValueSpace() {
88+
$0.it("can resolve object value with double space in key") {
8889
let pointer = JSONPointer(path: "/ ")
8990

90-
XCTAssertEqual(pointer.resolve(document: document) as? Int, 7)
91+
try expect(pointer.resolve(document: document) as? Int) == 7
9192
}
9293

93-
func testResolveObjectValueEscapedTilde() {
94+
$0.it("can resolve object value with escaped tilde in key") {
9495
let pointer = JSONPointer(path: "/m~0n")
9596

96-
XCTAssertEqual(pointer.resolve(document: document) as? Int, 8)
97+
try expect(pointer.resolve(document: document) as? Int) == 8
9798
}
9899

99100
// Resolve (negative cases)
100101

101-
func testResolveArrayIndexOutOfBounds() {
102+
$0.it("can resolve out of bounds array index") {
102103
let pointer = JSONPointer(path: "/foo/2")
103104

104-
XCTAssertNil(pointer.resolve(document: document))
105+
try expect(pointer.resolve(document: document)).to.beNil()
105106
}
106107

107-
func testResolveArrayIndexNegative() {
108+
$0.it("can resolve negative array index") {
108109
let pointer = JSONPointer(path: "/foo/-1")
109110

110-
XCTAssertNil(pointer.resolve(document: document))
111+
try expect(pointer.resolve(document: document)).to.beNil()
111112
}
112113

113-
func testResolveObjectMissingKey() {
114+
$0.it("can resolve missing key in object") {
114115
let pointer = JSONPointer(path: "/test")
115116

116-
XCTAssertNil(pointer.resolve(document: document))
117+
try expect(pointer.resolve(document: document)).to.beNil()
117118
}
118119

119120
// MARK: - #path
120121

121-
func testPath() {
122-
let pointer = JSONPointer(path: "/foo")
123-
XCTAssertEqual(pointer.path, "/foo")
124-
}
122+
$0.describe("#path") {
123+
$0.it("returns a string representation of path") {
124+
let pointer = JSONPointer(path: "/foo")
125+
try expect(pointer.path) == "/foo"
126+
}
125127

126-
func testPathWithSlash() {
127-
let pointer = JSONPointer(path: "/a~1b")
128-
XCTAssertEqual(pointer.path, "/a~1b")
128+
$0.it("returns a string representation of path escaping slashes") {
129+
let pointer = JSONPointer(path: "/a~1b")
130+
try expect(pointer.path) == "/a~1b"
131+
}
129132
}
130133
}

0 commit comments

Comments
 (0)