|
29 | 29 | // 3. This notice may not be removed or altered from any source distribution. |
30 | 30 | // |
31 | 31 |
|
32 | | -import XCTest |
33 | 32 | import SwiftDrawDOM |
34 | 33 | @testable import SwiftDraw |
| 34 | +import Testing |
35 | 35 |
|
36 | | -#if canImport(CoreGraphics) |
37 | | -final class SVGTests: XCTestCase { |
| 36 | +#if canImport(AppKit) |
| 37 | +import AppKit |
| 38 | +#endif |
| 39 | + |
| 40 | +#if canImport(UIKit) |
| 41 | +import UIKit |
| 42 | +#endif |
| 43 | + |
| 44 | +#if canImport(AppKit) || canImport(UIKit) |
| 45 | +struct SVGTests { |
38 | 46 |
|
39 | | - func testValidSVGLoads() { |
40 | | - XCTAssertNotNil(SVG(named: "lines.svg", in: .test)) |
| 47 | + @Test |
| 48 | + func validSVGLoads() { |
| 49 | + #expect(SVG(named: "lines.svg", in: .test) != nil) |
41 | 50 | } |
42 | 51 |
|
43 | | - func testInvalidSVGReturnsNil() { |
44 | | - XCTAssertNil(SVG(named: "invalids.svg", in: .test)) |
| 52 | + @Test |
| 53 | + func invalidSVGReturnsNil() { |
| 54 | + #expect(SVG(named: "invalids.svg", in: .test) == nil) |
45 | 55 | } |
46 | 56 |
|
47 | | - func testMissingSVGReturnsNil() { |
48 | | - XCTAssertNil(SVG(named: "missing.svg", in: .test)) |
| 57 | + @Test |
| 58 | + func missingSVGReturnsNil() { |
| 59 | + #expect(SVG(named: "missing.svg", in: .test) == nil) |
49 | 60 | } |
50 | 61 |
|
51 | | - func testImageRasterizes() { |
| 62 | + @Test |
| 63 | + func imageRasterizes() { |
52 | 64 | let image = SVG.makeLines() |
53 | 65 | let rendered = image.rasterize(scale: 1) |
54 | | - XCTAssertEqual(rendered.size, image.size) |
55 | | - XCTAssertNoThrow(try image.pngData()) |
56 | | - XCTAssertNoThrow(try image.jpegData()) |
57 | | - XCTAssertNoThrow(try image.pdfData()) |
| 66 | + #expect(rendered.size == image.size) |
| 67 | + #expect(throws: Never.self) { |
| 68 | + try image.pngData() |
| 69 | + } |
| 70 | + #expect(throws: Never.self) { |
| 71 | + try image.jpegData() |
| 72 | + } |
| 73 | + #expect(throws: Never.self) { |
| 74 | + try image.pdfData() |
| 75 | + } |
58 | 76 | } |
59 | 77 |
|
60 | | -// func testImageRasterizeAndScales() { |
61 | | -// let image = SVG.makeLines() |
62 | | -// let doubleSize = CGSize(width: 200, height: 200) |
63 | | -// let rendered = image.rasterize(with: doubleSize, scale: 1) |
64 | | -// XCTAssertEqual(rendered.size, doubleSize) |
65 | | -// XCTAssertNoThrow(try image.pngData(size: doubleSize)) |
66 | | -// XCTAssertNoThrow(try image.jpegData(size: doubleSize)) |
67 | | -// } |
68 | | - |
69 | | - func testShapesImageRasterizes() throws { |
70 | | - let image = try XCTUnwrap(SVG(named: "shapes.svg", in: .test)) |
71 | | - XCTAssertNoThrow(try image.pngData()) |
72 | | - XCTAssertNoThrow(try image.jpegData()) |
73 | | - XCTAssertNoThrow(try image.pdfData()) |
| 78 | + @Test |
| 79 | + func shapesImageRasterizes() throws { |
| 80 | + let image = try #require(SVG(named: "shapes.svg", in: .test)) |
| 81 | + #expect(throws: Never.self) { |
| 82 | + try image.pngData() |
| 83 | + } |
| 84 | + #expect(throws: Never.self) { |
| 85 | + try image.jpegData() |
| 86 | + } |
| 87 | + #expect(throws: Never.self) { |
| 88 | + try image.pdfData() |
| 89 | + } |
74 | 90 | } |
75 | 91 |
|
76 | 92 | #if canImport(UIKit) |
77 | | - func testRasterize() { |
| 93 | + @Test |
| 94 | + func rasterize() { |
78 | 95 | let svg = SVG(named: "gradient-apple.svg", in: .test)! |
79 | 96 | .sized(CGSize(width: 100, height: 100)) |
80 | 97 | let image = svg.rasterize(scale: 3) |
81 | | - XCTAssertEqual(image.size, CGSize(width: 100, height: 100)) |
82 | | - XCTAssertEqual(image.scale, 3) |
| 98 | + #expect(image.size == CGSize(width: 100, height: 100)) |
| 99 | + #expect(image.scale == 3) |
83 | 100 |
|
84 | 101 | let data = image.pngData()! |
85 | 102 | let reloaded = UIImage(data: data)! |
86 | | - XCTAssertEqual(reloaded.size, CGSize(width: 300, height: 300)) |
87 | | - XCTAssertEqual(reloaded.scale, 1) |
| 103 | + #expect(reloaded.size == CGSize(width: 300, height: 300)) |
| 104 | + #expect(reloaded.scale == 1) |
88 | 105 | } |
89 | 106 | #endif |
90 | 107 |
|
91 | | - func testSize() { |
| 108 | + @Test |
| 109 | + func size() { |
92 | 110 | let image = SVG.makeLines() |
93 | 111 |
|
94 | | - XCTAssertEqual(image.size, CGSize(width: 100, height: 100)) |
95 | | - XCTAssertEqual(image.sized(CGSize(width: 200, height: 200)).size, CGSize(width: 200, height: 200)) |
| 112 | + #expect( |
| 113 | + image.size == CGSize(width: 100, height: 100) |
| 114 | + ) |
| 115 | + #expect( |
| 116 | + image.sized(CGSize(width: 200, height: 200)).size == CGSize(width: 200, height: 200) |
| 117 | + ) |
96 | 118 |
|
97 | 119 | var copy = image |
98 | 120 | copy.size(CGSize(width: 20, height: 20)) |
99 | | - XCTAssertEqual(copy.size, CGSize(width: 20, height: 20)) |
| 121 | + #expect( |
| 122 | + copy.size == CGSize(width: 20, height: 20) |
| 123 | + ) |
100 | 124 | } |
101 | 125 |
|
102 | | - func testScale() { |
| 126 | + @Test |
| 127 | + func scale() { |
103 | 128 | let image = SVG.makeLines() |
104 | 129 |
|
105 | | - XCTAssertEqual(image.size, CGSize(width: 100, height: 100)) |
106 | | - XCTAssertEqual(image.scaled(2).size, CGSize(width: 200, height: 200)) |
107 | | - XCTAssertEqual(image.scaled(0.5).size, CGSize(width: 50, height: 50)) |
108 | | - XCTAssertEqual(image.scaled(x: 2, y: 3).size, CGSize(width: 200, height: 300)) |
| 130 | + #expect( |
| 131 | + image.size == CGSize(width: 100, height: 100) |
| 132 | + ) |
| 133 | + #expect( |
| 134 | + image.scaled(2).size == CGSize(width: 200, height: 200) |
| 135 | + ) |
| 136 | + #expect( |
| 137 | + image.scaled(0.5).size == CGSize(width: 50, height: 50) |
| 138 | + ) |
| 139 | + #expect( |
| 140 | + image.scaled(x: 2, y: 3).size == CGSize(width: 200, height: 300) |
| 141 | + ) |
109 | 142 |
|
110 | 143 | var copy = image |
111 | 144 | copy.scale(5) |
112 | | - XCTAssertEqual(copy.size, CGSize(width: 500, height: 500)) |
| 145 | + #expect( |
| 146 | + copy.size == CGSize(width: 500, height: 500) |
| 147 | + ) |
113 | 148 | } |
114 | 149 |
|
115 | | - func testTranslate() { |
| 150 | + @Test |
| 151 | + func translate() { |
116 | 152 | let image = SVG.makeLines() |
117 | 153 |
|
118 | | - XCTAssertEqual(image.size, CGSize(width: 100, height: 100)) |
119 | | - XCTAssertEqual(image.translated(tx: 10, ty: 10).size, CGSize(width: 100, height: 100)) |
| 154 | + #expect( |
| 155 | + image.size == CGSize(width: 100, height: 100) |
| 156 | + ) |
| 157 | + #expect( |
| 158 | + image.translated(tx: 10, ty: 10).size == CGSize(width: 100, height: 100) |
| 159 | + ) |
120 | 160 |
|
121 | 161 | var copy = image |
122 | 162 | copy.translate(tx: 50, ty: 50) |
123 | | - XCTAssertEqual(copy.size, CGSize(width: 100, height: 100)) |
| 163 | + #expect( |
| 164 | + copy.size == CGSize(width: 100, height: 100) |
| 165 | + ) |
124 | 166 | } |
125 | 167 |
|
126 | | - func testExpand() { |
| 168 | + @Test |
| 169 | + func expand() { |
127 | 170 | let image = SVG.makeLines() |
128 | 171 |
|
129 | | - XCTAssertEqual(image.size, CGSize(width: 100, height: 100)) |
130 | | - XCTAssertEqual(image.expanded(top: 50, right: 30).size, CGSize(width: 130, height: 150)) |
| 172 | + #expect( |
| 173 | + image.size == CGSize(width: 100, height: 100) |
| 174 | + ) |
| 175 | + #expect( |
| 176 | + image.expanded(top: 50, right: 30).size == CGSize(width: 130, height: 150) |
| 177 | + ) |
131 | 178 |
|
132 | 179 | var copy = image |
133 | 180 | copy.expand(-10) |
134 | | - XCTAssertEqual(copy.size, CGSize(width: 80, height: 80)) |
| 181 | + #expect( |
| 182 | + copy.size == CGSize(width: 80, height: 80) |
| 183 | + ) |
135 | 184 | } |
136 | 185 |
|
137 | | - func testHashable() { |
| 186 | + @Test |
| 187 | + func hashable() { |
138 | 188 | var images = Set<SVG>() |
139 | 189 | let lines = SVG.makeLines() |
140 | 190 |
|
141 | | - XCTAssertFalse(images.contains(lines)) |
| 191 | + #expect(!images.contains(lines)) |
142 | 192 |
|
143 | 193 | images.insert(SVG.makeLines()) |
144 | | - XCTAssertTrue(images.contains(lines)) |
| 194 | + #expect(images.contains(lines)) |
145 | 195 |
|
146 | 196 | let linesResized = lines.sized(CGSize(width: 10, height: 10)) |
147 | | - XCTAssertFalse(images.contains(linesResized)) |
| 197 | + #expect(!images.contains(linesResized)) |
148 | 198 |
|
149 | 199 | images.remove(lines) |
150 | | - XCTAssertFalse(images.contains(SVG.makeLines())) |
| 200 | + #expect(!images.contains(SVG.makeLines())) |
151 | 201 | } |
152 | 202 |
|
153 | | - func testDeepNestedSVG() async { |
| 203 | + @Test |
| 204 | + func deepNestedSVG() async { |
154 | 205 | let circle = DOM.Circle(cx: 50, cy: 50, r: 10) |
155 | 206 | let dom = DOM.SVG(width: 50, height: 50) |
156 | 207 | dom.childElements.append(DOM.Group.make(child: circle, nestedLevels: 500)) |
|
0 commit comments