|
29 | 29 | // 3. This notice may not be removed or altered from any source distribution. |
30 | 30 | // |
31 | 31 |
|
| 32 | +import Foundation |
32 | 33 | import SwiftDrawDOM |
33 | | -import XCTest |
| 34 | +import Testing |
| 35 | + |
34 | 36 | @testable import SwiftDrawDOM |
35 | 37 |
|
36 | 38 | private typealias Coordinate = DOM.Coordinate |
37 | 39 | private typealias Segment = DOM.Path.Segment |
38 | 40 | private typealias CoordinateSpace = DOM.Path.Segment.CoordinateSpace |
39 | 41 |
|
40 | | -final class ParserXMLPathTests: XCTestCase { |
| 42 | +@Suite("Parser XML Path Tests") |
| 43 | +struct ParserXMLPathTests { |
41 | 44 |
|
42 | | - func testScanBool() { |
| 45 | + @Test |
| 46 | + func scanBool() throws { |
43 | 47 | let scanner = XMLParser.PathScanner(string: "true FALSE 0 1") |
44 | 48 |
|
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() } |
50 | 54 | } |
51 | 55 |
|
52 | | - func testScanCoordinate() { |
| 56 | + @Test |
| 57 | + func scanCoordinate() throws { |
53 | 58 | let scanner = XMLParser.PathScanner(string: "10 20.0") |
54 | 59 |
|
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() } |
58 | 63 | } |
59 | 64 |
|
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)) |
63 | 68 |
|
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)) |
66 | 70 |
|
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)) |
69 | 72 | } |
70 | 73 |
|
71 | | - func testEmpty() throws { |
| 74 | + @Test |
| 75 | + func empty() throws { |
72 | 76 | let parser = SwiftDrawDOM.XMLParser() |
73 | | - XCTAssertEqual(try parser.parsePathSegments(""), []) |
74 | | - XCTAssertEqual(try parser.parsePathSegments(" "), []) |
| 77 | + #expect(try parser.parsePathSegments("") == []) |
| 78 | + #expect(try parser.parsePathSegments(" ") == []) |
75 | 79 | } |
76 | 80 |
|
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)) |
84 | 89 |
|
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)]) |
87 | 92 |
|
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)]) |
90 | 95 | } |
91 | 96 |
|
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)]) |
102 | 108 | } |
103 | 109 |
|
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)) |
110 | 117 |
|
111 | | - AssertSegmentsEquals("h10 5", [horizontal(10, .relative), |
112 | | - horizontal(5, .relative)]) |
| 118 | + #expect(parseSegments("h10 5") == [horizontal(10, .relative), |
| 119 | + horizontal(5, .relative)]) |
113 | 120 | } |
114 | 121 |
|
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)) |
121 | 129 | } |
122 | 130 |
|
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)) |
137 | 138 | } |
138 | 139 |
|
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)) |
145 | 147 | } |
146 | 148 |
|
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)) |
153 | 156 | } |
154 | 157 |
|
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)) |
162 | 161 | } |
163 | 162 |
|
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) |
168 | 168 | } |
169 | 169 |
|
170 | | - func testPath() { |
| 170 | + @Test |
| 171 | + func path() throws { |
171 | 172 | let node = ["d": "M 10 10 h 10 v 10 h -10 v -10"] |
172 | 173 | let parser = DOMXMLParser() |
173 | 174 |
|
174 | | - let path = try! parser.parsePath(node) |
| 175 | + let path = try parser.parsePath(node) |
175 | 176 |
|
176 | | - XCTAssertEqual(path.segments.count, 5) |
| 177 | + #expect(path.segments.count == 5) |
177 | 178 |
|
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)) |
183 | 184 | } |
184 | 185 |
|
185 | | - func testPathLineBreak() { |
| 186 | + @Test |
| 187 | + func pathLineBreak() throws { |
186 | 188 | let node = ["d": "M230 520\n \t\t A 45 45, 0, 1, 0, 275 565 \n \t\t L 275 520 Z"] |
187 | 189 | let parser = DOMXMLParser() |
188 | 190 |
|
189 | 191 | let path = try? parser.parsePath(node) |
190 | 192 |
|
191 | | - XCTAssertEqual(path?.segments.count, 4) |
| 193 | + #expect(path?.segments.count == 4) |
192 | 194 | } |
193 | 195 |
|
194 | | - func testPathLong() throws { |
| 196 | + @Test |
| 197 | + func pathLong() throws { |
195 | 198 |
|
196 | 199 | let node = ["d": "m10,2h-30v-40zm50,60"] |
197 | 200 | let parser = DOMXMLParser() |
198 | 201 |
|
199 | | - let path = try! parser.parsePath(node) |
| 202 | + let path = try parser.parsePath(node) |
200 | 203 |
|
201 | | - XCTAssertEqual(path.segments.count, 5) |
| 204 | + #expect(path.segments.count == 5) |
202 | 205 |
|
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)) |
208 | 211 | } |
209 | 212 | } |
210 | 213 |
|
211 | | -private func AssertSegmentEquals(_ text: String, _ expected: Segment, file: StaticString = #filePath, line: UInt = #line) { |
| 214 | +private func parseSegment(_ text: String) -> Segment? { |
212 | 215 | let parsed = try? XMLParser().parsePathSegments(text) |
213 | | - XCTAssertEqual(parsed?.count, 1) |
214 | | - XCTAssertEqual(parsed![0], expected, file: file, line: line) |
| 216 | + return parsed?[0] |
215 | 217 | } |
216 | 218 |
|
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 ?? [] |
223 | 222 | } |
224 | 223 |
|
225 | | - |
226 | 224 | // helpers to create Segments without labels |
227 | 225 | // splatting of tuple is no longer supported |
228 | 226 | private func move(_ x: Coordinate, _ y: Coordinate, _ space: CoordinateSpace) -> Segment { |
|
0 commit comments