Skip to content

Commit eb37aaa

Browse files
committed
swift-testing
1 parent cc2eaa8 commit eb37aaa

File tree

6 files changed

+162
-116
lines changed

6 files changed

+162
-116
lines changed

SwiftDraw/Sources/SVG+Deprecated.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public import CoreGraphics
3434
public import Foundation
3535

3636
#if canImport(UIKit)
37-
import UIKit
37+
public import UIKit
3838
#endif
3939

4040
public extension SVG {

SwiftDraw/Sources/UIImage+SVG.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131

3232
import Foundation
3333
#if canImport(UIKit)
34-
import UIKit
34+
public import UIKit
3535
#if canImport(WatchKit)
36-
import WatchKit
36+
public import WatchKit
3737
#endif
3838

3939
public extension UIImage {

SwiftDraw/Tests/LayerTree/LayerTree.BuilderTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ final class LayerTreeBuilderTests: XCTestCase {
8484
var element = DOM.GraphicsElement()
8585
element.attributes.clipPath = URL(string: "#clip1")
8686

87-
var shapes = builder.createClipShapes(for: element)
8887
XCTAssertEqual(
8988
builder.createClipShapes(for: element),
9089
[.ellipse(within: LayerTree.Rect(x: 0, y: 0, width: 10, height: 10))]

SwiftDraw/Tests/NSImage+SVGTests.swift

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,53 +30,50 @@
3030
//
3131

3232
import SwiftDrawDOM
33-
import XCTest
33+
import Testing
34+
3435
@testable import SwiftDraw
3536
#if canImport(AppKit) && !targetEnvironment(macCatalyst)
37+
import AppKit
3638

37-
final class NSImageSVGTests: XCTestCase {
38-
39-
func testImageLoads() {
40-
let image = NSImage(svgNamed: "lines.svg", in: .test)
41-
XCTAssertNotNil(image)
42-
}
43-
44-
func testMissingImageDoesNotLoad() {
45-
let image = NSImage(svgNamed: "missing.svg", in: .test)
46-
XCTAssertNil(image)
47-
}
39+
struct NSImageSVGTests {
4840

49-
func testNSImageDraws() {
50-
let canvas = NSBitmapImageRep(pixelsWide: 2, pixelsHigh: 2)
41+
@Test
42+
func imageLoads() {
43+
let image = NSImage(svgNamed: "lines.svg", in: .test)
44+
#expect(image != nil)
45+
}
5146

52-
canvas.lockFocus()
53-
NSImage(svgNamed: "lines.svg", in: .test)?.draw(in: NSRect(x: 0, y: 0, width: 2, height: 2))
54-
canvas.unlockFocus()
55-
}
47+
@Test
48+
func missingImageDoesNotLoad() {
49+
let image = NSImage(svgNamed: "missing.svg", in: .test)
50+
#expect(image == nil)
51+
}
5652

57-
func testImageDraws() {
58-
let canvas = NSBitmapImageRep(pixelsWide: 2, pixelsHigh: 2)
59-
let image = SVG.makeQuad().rasterize(with: CGSize(width: 2, height: 2))
53+
@Test
54+
func imageDraws() {
55+
let canvas = NSBitmapImageRep(pixelsWide: 2, pixelsHigh: 2)
56+
let image = SVG.makeQuad().rasterize(with: CGSize(width: 2, height: 2))
6057

61-
canvas.lockFocus()
62-
image.draw(in: NSRect(x: 0, y: 0, width: 2, height: 2))
63-
canvas.unlockFocus()
58+
canvas.lockFocus()
59+
image.draw(in: NSRect(x: 0, y: 0, width: 2, height: 2))
60+
canvas.unlockFocus()
6461

65-
XCTAssertEqual(canvas.colorAt(x: 0, y: 0), NSColor(deviceRed: 1.0, green: 0, blue: 0, alpha: 1.0))
66-
XCTAssertEqual(canvas.colorAt(x: 1, y: 1), NSColor(deviceRed: 0.0, green: 0, blue: 1.0, alpha: 1.0))
67-
}
62+
#expect(canvas.colorAt(x: 0, y: 0) == NSColor(deviceRed: 1.0, green: 0, blue: 0, alpha: 1.0))
63+
#expect(canvas.colorAt(x: 1, y: 1) == NSColor(deviceRed: 0.0, green: 0, blue: 1.0, alpha: 1.0))
64+
}
6865
}
6966

7067
private extension SVG {
7168

72-
static func makeQuad() -> SVG {
73-
let svg = DOM.SVG(width: 2, height: 2)
74-
svg.childElements.append(DOM.Rect(x: 0, y: 0, width: 1, height: 1))
75-
svg.childElements.append(DOM.Rect(x: 1, y: 1, width: 1, height: 1))
76-
svg.childElements[0].attributes.fill = .color(DOM.Color.rgbi(255, 0, 0, 1.0))
77-
svg.childElements[1].attributes.fill = .color(DOM.Color.rgbi(0, 0, 255, 1.0))
78-
return SVG(dom: svg, options: .default)
79-
}
69+
static func makeQuad() -> SVG {
70+
let svg = DOM.SVG(width: 2, height: 2)
71+
svg.childElements.append(DOM.Rect(x: 0, y: 0, width: 1, height: 1))
72+
svg.childElements.append(DOM.Rect(x: 1, y: 1, width: 1, height: 1))
73+
svg.childElements[0].attributes.fill = .color(DOM.Color.rgbi(255, 0, 0, 1.0))
74+
svg.childElements[1].attributes.fill = .color(DOM.Color.rgbi(0, 0, 255, 1.0))
75+
return SVG(dom: svg, options: .default)
76+
}
8077
}
8178

8279
#endif

SwiftDraw/Tests/SVGTests.swift

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

32-
import XCTest
3332
import SwiftDrawDOM
3433
@testable import SwiftDraw
34+
import Testing
3535

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 {
3846

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)
4150
}
4251

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)
4555
}
4656

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)
4960
}
5061

51-
func testImageRasterizes() {
62+
@Test
63+
func imageRasterizes() {
5264
let image = SVG.makeLines()
5365
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+
}
5876
}
5977

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+
}
7490
}
7591

7692
#if canImport(UIKit)
77-
func testRasterize() {
93+
@Test
94+
func rasterize() {
7895
let svg = SVG(named: "gradient-apple.svg", in: .test)!
7996
.sized(CGSize(width: 100, height: 100))
8097
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)
83100

84101
let data = image.pngData()!
85102
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)
88105
}
89106
#endif
90107

91-
func testSize() {
108+
@Test
109+
func size() {
92110
let image = SVG.makeLines()
93111

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+
)
96118

97119
var copy = image
98120
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+
)
100124
}
101125

102-
func testScale() {
126+
@Test
127+
func scale() {
103128
let image = SVG.makeLines()
104129

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+
)
109142

110143
var copy = image
111144
copy.scale(5)
112-
XCTAssertEqual(copy.size, CGSize(width: 500, height: 500))
145+
#expect(
146+
copy.size == CGSize(width: 500, height: 500)
147+
)
113148
}
114149

115-
func testTranslate() {
150+
@Test
151+
func translate() {
116152
let image = SVG.makeLines()
117153

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+
)
120160

121161
var copy = image
122162
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+
)
124166
}
125167

126-
func testExpand() {
168+
@Test
169+
func expand() {
127170
let image = SVG.makeLines()
128171

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+
)
131178

132179
var copy = image
133180
copy.expand(-10)
134-
XCTAssertEqual(copy.size, CGSize(width: 80, height: 80))
181+
#expect(
182+
copy.size == CGSize(width: 80, height: 80)
183+
)
135184
}
136185

137-
func testHashable() {
186+
@Test
187+
func hashable() {
138188
var images = Set<SVG>()
139189
let lines = SVG.makeLines()
140190

141-
XCTAssertFalse(images.contains(lines))
191+
#expect(!images.contains(lines))
142192

143193
images.insert(SVG.makeLines())
144-
XCTAssertTrue(images.contains(lines))
194+
#expect(images.contains(lines))
145195

146196
let linesResized = lines.sized(CGSize(width: 10, height: 10))
147-
XCTAssertFalse(images.contains(linesResized))
197+
#expect(!images.contains(linesResized))
148198

149199
images.remove(lines)
150-
XCTAssertFalse(images.contains(SVG.makeLines()))
200+
#expect(!images.contains(SVG.makeLines()))
151201
}
152202

153-
func testDeepNestedSVG() async {
203+
@Test
204+
func deepNestedSVG() async {
154205
let circle = DOM.Circle(cx: 50, cy: 50, r: 10)
155206
let dom = DOM.SVG(width: 50, height: 50)
156207
dom.childElements.append(DOM.Group.make(child: circle, nestedLevels: 500))

0 commit comments

Comments
 (0)