Skip to content

Commit 0873211

Browse files
committed
Swift Testing
1 parent ebea6f3 commit 0873211

8 files changed

+419
-391
lines changed

DOM/Tests/Parser.XML.PathTests.swift

Lines changed: 120 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -29,200 +29,198 @@
2929
// 3. This notice may not be removed or altered from any source distribution.
3030
//
3131

32+
import Foundation
3233
import SwiftDrawDOM
33-
import XCTest
34+
import Testing
35+
3436
@testable import SwiftDrawDOM
3537

3638
private typealias Coordinate = DOM.Coordinate
3739
private typealias Segment = DOM.Path.Segment
3840
private typealias CoordinateSpace = DOM.Path.Segment.CoordinateSpace
3941

40-
final class ParserXMLPathTests: XCTestCase {
42+
@Suite("Parser XML Path Tests")
43+
struct ParserXMLPathTests {
4144

42-
func testScanBool() {
45+
@Test
46+
func scanBool() throws {
4347
let scanner = XMLParser.PathScanner(string: "true FALSE 0 1")
4448

45-
XCTAssertTrue(try scanner.scanBool())
46-
XCTAssertFalse(try scanner.scanBool())
47-
XCTAssertFalse(try scanner.scanBool())
48-
XCTAssertTrue(try scanner.scanBool())
49-
XCTAssertThrowsError(try scanner.scanBool())
49+
#expect(try scanner.scanBool() == true)
50+
#expect(try scanner.scanBool() == false)
51+
#expect(try scanner.scanBool() == false)
52+
#expect(try scanner.scanBool() == true)
53+
#expect(throws: (any Error).self) { _ = try scanner.scanBool() }
5054
}
5155

52-
func testScanCoordinate() {
56+
@Test
57+
func scanCoordinate() throws {
5358
let scanner = XMLParser.PathScanner(string: "10 20.0")
5459

55-
XCTAssertEqual(try scanner.scanCoordinate(), 10.0)
56-
XCTAssertEqual(try scanner.scanCoordinate(), 20.0)
57-
XCTAssertThrowsError(try scanner.scanCoordinate())
60+
#expect(try scanner.scanCoordinate() == 10.0)
61+
#expect(try scanner.scanCoordinate() == 20.0)
62+
#expect(throws: (any Error).self) { _ = try scanner.scanCoordinate() }
5863
}
5964

60-
func testEquality() {
61-
XCTAssertEqual(Segment.move(x: 10, y: 20, space: .relative),
62-
move(10, 20, .relative))
65+
@Test
66+
func equality() {
67+
#expect(Segment.move(x: 10, y: 20, space: .relative) == move(10, 20, .relative))
6368

64-
XCTAssertNotEqual(Segment.move(x: 20, y: 20, space: .absolute),
65-
move(10, 20, .absolute))
69+
#expect(Segment.move(x: 20, y: 20, space: .absolute) != move(10, 20, .absolute))
6670

67-
XCTAssertNotEqual(Segment.move(x: 10, y: 20, space: .relative),
68-
move(10, 20, .absolute))
71+
#expect(Segment.move(x: 10, y: 20, space: .relative) != move(10, 20, .absolute))
6972
}
7073

71-
func testEmpty() throws {
74+
@Test
75+
func empty() throws {
7276
let parser = SwiftDrawDOM.XMLParser()
73-
XCTAssertEqual(try parser.parsePathSegments(""), [])
74-
XCTAssertEqual(try parser.parsePathSegments(" "), [])
77+
#expect(try parser.parsePathSegments("") == [])
78+
#expect(try parser.parsePathSegments(" ") == [])
7579
}
7680

77-
func testMove() {
78-
AssertSegmentEquals("M 10 20", move(10, 20, .absolute))
79-
AssertSegmentEquals("m 10 20", move(10, 20, .relative))
80-
AssertSegmentEquals("M10,20", move(10, 20, .absolute))
81-
AssertSegmentEquals("M10;20", move(10, 20, .absolute))
82-
AssertSegmentEquals("M 10; 20 ", move(10, 20, .absolute))
83-
AssertSegmentEquals("M10-20", move(10, -20, .absolute))
81+
@Test
82+
func moveSegments() {
83+
#expect(parseSegment("M 10 20") == move(10, 20, .absolute))
84+
#expect(parseSegment("m 10 20") == move(10, 20, .relative))
85+
#expect(parseSegment("M10,20") == move(10, 20, .absolute))
86+
#expect(parseSegment("M10;20") == move(10, 20, .absolute))
87+
#expect(parseSegment("M 10; 20 ") == move(10, 20, .absolute))
88+
#expect(parseSegment("M10-20") == move(10, -20, .absolute))
8489

85-
AssertSegmentsEquals("M10-20 5 1", [move(10, -20, .absolute),
86-
line(5, 1, .absolute)])
90+
#expect(parseSegments("M10-20 5 1") == [move(10, -20, .absolute),
91+
line(5, 1, .absolute)])
8792

88-
AssertSegmentsEquals("m10-20 5 1", [move(10, -20, .relative),
89-
line(5, 1, .relative)])
93+
#expect(parseSegments("m10-20 5 1") == [move(10, -20, .relative),
94+
line(5, 1, .relative)])
9095
}
9196

92-
func testLine() {
93-
AssertSegmentEquals("L 10 20", line(10, 20, .absolute))
94-
AssertSegmentEquals("l 10 20", line(10, 20, .relative))
95-
AssertSegmentEquals("L10,20", line(10, 20, .absolute))
96-
AssertSegmentEquals("L10;20", line(10, 20, .absolute))
97-
AssertSegmentEquals(" L 10;20 ", line(10, 20, .absolute))
98-
AssertSegmentEquals("L10-20 ", line(10, -20, .absolute))
99-
100-
AssertSegmentsEquals("L10-20 5 1", [line(10, -20, .absolute),
101-
line(5, 1, .absolute)])
97+
@Test
98+
func lineSegments() {
99+
#expect(parseSegment("L 10 20") == line(10, 20, .absolute))
100+
#expect(parseSegment("l 10 20") == line(10, 20, .relative))
101+
#expect(parseSegment("L10,20") == line(10, 20, .absolute))
102+
#expect(parseSegment("L10;20") == line(10, 20, .absolute))
103+
#expect(parseSegment(" L 10;20 ") == line(10, 20, .absolute))
104+
#expect(parseSegment("L10-20 ") == line(10, -20, .absolute))
105+
106+
#expect(parseSegments("L10-20 5 1") == [line(10, -20, .absolute),
107+
line(5, 1, .absolute)])
102108
}
103109

104-
func testHorizontal() {
105-
AssertSegmentEquals("H 10", horizontal(10, .absolute))
106-
AssertSegmentEquals("h 10", horizontal(10, .relative))
107-
AssertSegmentEquals("H10", horizontal(10, .absolute))
108-
AssertSegmentEquals("H10;", horizontal(10, .absolute))
109-
AssertSegmentEquals(" H10 ", horizontal(10, .absolute))
110+
@Test
111+
func horizontalSegments() {
112+
#expect(parseSegment("H 10") == horizontal(10, .absolute))
113+
#expect(parseSegment("h 10") == horizontal(10, .relative))
114+
#expect(parseSegment("H10") == horizontal(10, .absolute))
115+
#expect(parseSegment("H10;") == horizontal(10, .absolute))
116+
#expect(parseSegment(" H10 ") == horizontal(10, .absolute))
110117

111-
AssertSegmentsEquals("h10 5", [horizontal(10, .relative),
112-
horizontal(5, .relative)])
118+
#expect(parseSegments("h10 5") == [horizontal(10, .relative),
119+
horizontal(5, .relative)])
113120
}
114121

115-
func testVerical() {
116-
AssertSegmentEquals("V 10", vertical(10, .absolute))
117-
AssertSegmentEquals("v 10", vertical(10, .relative))
118-
AssertSegmentEquals("V10", vertical(10, .absolute))
119-
AssertSegmentEquals("V10;", vertical(10, .absolute))
120-
AssertSegmentEquals(" V10 ", vertical(10, .absolute))
122+
@Test
123+
func vericalSegments() {
124+
#expect(parseSegment("V 10") == vertical(10, .absolute))
125+
#expect(parseSegment("v 10") == vertical(10, .relative))
126+
#expect(parseSegment("V10") == vertical(10, .absolute))
127+
#expect(parseSegment("V10;") == vertical(10, .absolute))
128+
#expect(parseSegment(" V10 ") == vertical(10, .absolute))
121129
}
122130

123-
// func testCubic() {
124-
// AssertSegmentEquals("C 10 20 30 40 50 60", cubic(10, 20, 30, 40, 50, 60, .absolute))
125-
// AssertSegmentEquals("c 10 20 30 40 50 60", cubic(10, 20, 30, 40, 50, 60, .relative))
126-
// AssertSegmentEquals("C10,20,30,40,50,60", cubic(10, 20, 30, 40, 50, 60, .absolute))
127-
// AssertSegmentEquals("C10;20;30;40;50;60", cubic(10, 20, 30, 40, 50, 60, .absolute))
128-
// AssertSegmentEquals(" C10; 20; 30 40; 50; 60", cubic(10, 20, 30, 40, 50, 60, .absolute))
129-
// }
130-
131-
func testCubicSmooth() {
132-
AssertSegmentEquals("S 10 20 50 60", cubicSmooth(10, 20, 50, 60, .absolute))
133-
AssertSegmentEquals("s 10 20 50 60", cubicSmooth(10, 20, 50, 60, .relative))
134-
AssertSegmentEquals("S10,20,50,60", cubicSmooth(10, 20, 50, 60, .absolute))
135-
AssertSegmentEquals("S10;20;50;60", cubicSmooth(10, 20, 50, 60, .absolute))
136-
AssertSegmentEquals(" S10; 20; 50; 60", cubicSmooth(10, 20, 50, 60, .absolute))
131+
@Test
132+
func cubicSmoothSegments() {
133+
#expect(parseSegment("S 10 20 50 60") == cubicSmooth(10, 20, 50, 60, .absolute))
134+
#expect(parseSegment("s 10 20 50 60") == cubicSmooth(10, 20, 50, 60, .relative))
135+
#expect(parseSegment("S10,20,50,60") == cubicSmooth(10, 20, 50, 60, .absolute))
136+
#expect(parseSegment("S10;20;50;60") == cubicSmooth(10, 20, 50, 60, .absolute))
137+
#expect(parseSegment(" S10; 20; 50; 60") == cubicSmooth(10, 20, 50, 60, .absolute))
137138
}
138139

139-
func testQuadratic() {
140-
AssertSegmentEquals("Q 10 20 50 60", quadratic(10, 20, 50, 60, .absolute))
141-
AssertSegmentEquals("q 10 20 50 60", quadratic(10, 20, 50, 60, .relative))
142-
AssertSegmentEquals("Q10,20,50,60", quadratic(10, 20, 50, 60, .absolute))
143-
AssertSegmentEquals("Q10;20;50;60", quadratic(10, 20, 50, 60, .absolute))
144-
AssertSegmentEquals(" Q10; 20; 50; 60", quadratic(10, 20, 50, 60, .absolute))
140+
@Test
141+
func quadraticSegments() {
142+
#expect(parseSegment("Q 10 20 50 60") == quadratic(10, 20, 50, 60, .absolute))
143+
#expect(parseSegment("q 10 20 50 60") == quadratic(10, 20, 50, 60, .relative))
144+
#expect(parseSegment("Q10,20,50,60") == quadratic(10, 20, 50, 60, .absolute))
145+
#expect(parseSegment("Q10;20;50;60") == quadratic(10, 20, 50, 60, .absolute))
146+
#expect(parseSegment(" Q10; 20; 50; 60") == quadratic(10, 20, 50, 60, .absolute))
145147
}
146148

147-
func testQuadraticSmooth() {
148-
AssertSegmentEquals("T 10 20", quadraticSmooth(10, 20, .absolute))
149-
AssertSegmentEquals("t 10 20", quadraticSmooth(10, 20, .relative))
150-
AssertSegmentEquals("T10,20", quadraticSmooth(10, 20, .absolute))
151-
AssertSegmentEquals("T10;20", quadraticSmooth(10, 20, .absolute))
152-
AssertSegmentEquals(" T10; 20;", quadraticSmooth(10, 20, .absolute))
149+
@Test
150+
func quadraticSmoothSegments() {
151+
#expect(parseSegment("T 10 20") == quadraticSmooth(10, 20, .absolute))
152+
#expect(parseSegment("t 10 20") == quadraticSmooth(10, 20, .relative))
153+
#expect(parseSegment("T10,20") == quadraticSmooth(10, 20, .absolute))
154+
#expect(parseSegment("T10;20") == quadraticSmooth(10, 20, .absolute))
155+
#expect(parseSegment(" T10; 20;") == quadraticSmooth(10, 20, .absolute))
153156
}
154157

155-
func testArc() {
156-
//
157-
// AssertSegmentEquals("A 10 20 30 1 0 40 50", arc(10, 20, 30, true, false, 40, 50, .absolute))
158-
// AssertSegmentEquals("a 10 20 30 1 0 40 50", arc(10, 20, 30, true, false, 40, 50, .relative))
159-
// AssertSegmentEquals("A10,20,30,1,0,40,50", arc(10, 20, 30, true, false, 40, 50, .absolute))
160-
// AssertSegmentEquals("A10;20;30;1;0;40;50", arc(10, 20, 30, true, false, 40, 50, .absolute))
161-
AssertSegmentEquals(" A10; 20; 30; 1 0;40 50", arc(10, 20, 30, true, false, 40, 50, .absolute))
158+
@Test
159+
func arcSegments() {
160+
#expect(parseSegment(" A10; 20; 30; 1 0;40 50") == arc(10, 20, 30, true, false, 40, 50, .absolute))
162161
}
163162

164-
func testClose() {
165-
AssertSegmentEquals("Z", .close)
166-
AssertSegmentEquals("z", .close)
167-
AssertSegmentEquals(" z ", .close)
163+
@Test
164+
func closeSegments() {
165+
#expect(parseSegment("Z") == .close)
166+
#expect(parseSegment("z") == .close)
167+
#expect(parseSegment(" z ") == .close)
168168
}
169169

170-
func testPath() {
170+
@Test
171+
func path() throws {
171172
let node = ["d": "M 10 10 h 10 v 10 h -10 v -10"]
172173
let parser = DOMXMLParser()
173174

174-
let path = try! parser.parsePath(node)
175+
let path = try parser.parsePath(node)
175176

176-
XCTAssertEqual(path.segments.count, 5)
177+
#expect(path.segments.count == 5)
177178

178-
XCTAssertEqual(path.segments[0], .move(x: 10, y: 10, space: .absolute))
179-
XCTAssertEqual(path.segments[1], .horizontal(x: 10, space: .relative))
180-
XCTAssertEqual(path.segments[2], .vertical(y: 10, space: .relative))
181-
XCTAssertEqual(path.segments[3], .horizontal(x: -10, space: .relative))
182-
XCTAssertEqual(path.segments[4], .vertical(y: -10, space: .relative))
179+
#expect(path.segments[0] == .move(x: 10, y: 10, space: .absolute))
180+
#expect(path.segments[1] == .horizontal(x: 10, space: .relative))
181+
#expect(path.segments[2] == .vertical(y: 10, space: .relative))
182+
#expect(path.segments[3] == .horizontal(x: -10, space: .relative))
183+
#expect(path.segments[4] == .vertical(y: -10, space: .relative))
183184
}
184185

185-
func testPathLineBreak() {
186+
@Test
187+
func pathLineBreak() throws {
186188
let node = ["d": "M230 520\n \t\t A 45 45, 0, 1, 0, 275 565 \n \t\t L 275 520 Z"]
187189
let parser = DOMXMLParser()
188190

189191
let path = try? parser.parsePath(node)
190192

191-
XCTAssertEqual(path?.segments.count, 4)
193+
#expect(path?.segments.count == 4)
192194
}
193195

194-
func testPathLong() throws {
196+
@Test
197+
func pathLong() throws {
195198

196199
let node = ["d": "m10,2h-30v-40zm50,60"]
197200
let parser = DOMXMLParser()
198201

199-
let path = try! parser.parsePath(node)
202+
let path = try parser.parsePath(node)
200203

201-
XCTAssertEqual(path.segments.count, 5)
204+
#expect(path.segments.count == 5)
202205

203-
XCTAssertEqual(path.segments[0], .move(x: 10, y: 2.0, space: .relative))
204-
XCTAssertEqual(path.segments[1], .horizontal(x: -30, space: .relative))
205-
XCTAssertEqual(path.segments[2], .vertical(y: -40, space: .relative))
206-
XCTAssertEqual(path.segments[3], .close)
207-
XCTAssertEqual(path.segments[4], .move(x: 50, y: 60, space: .relative))
206+
#expect(path.segments[0] == .move(x: 10, y: 2.0, space: .relative))
207+
#expect(path.segments[1] == .horizontal(x: -30, space: .relative))
208+
#expect(path.segments[2] == .vertical(y: -40, space: .relative))
209+
#expect(path.segments[3] == .close)
210+
#expect(path.segments[4] == .move(x: 50, y: 60, space: .relative))
208211
}
209212
}
210213

211-
private func AssertSegmentEquals(_ text: String, _ expected: Segment, file: StaticString = #filePath, line: UInt = #line) {
214+
private func parseSegment(_ text: String) -> Segment? {
212215
let parsed = try? XMLParser().parsePathSegments(text)
213-
XCTAssertEqual(parsed?.count, 1)
214-
XCTAssertEqual(parsed![0], expected, file: file, line: line)
216+
return parsed?[0]
215217
}
216218

217-
private func AssertSegmentsEquals(_ text: String, _ expected: [Segment], file: StaticString = #filePath, line: UInt = #line) {
218-
guard let parsed = try? XMLParser().parsePathSegments(text) else {
219-
XCTFail("could not parse segments", file: file, line: line)
220-
return
221-
}
222-
XCTAssertEqual(parsed, expected, file: file, line: line)
219+
private func parseSegments(_ text: String) -> [Segment] {
220+
let parsed = try? XMLParser().parsePathSegments(text)
221+
return parsed ?? []
223222
}
224223

225-
226224
// helpers to create Segments without labels
227225
// splatting of tuple is no longer supported
228226
private func move(_ x: Coordinate, _ y: Coordinate, _ space: CoordinateSpace) -> Segment {

0 commit comments

Comments
 (0)