Skip to content

Commit 860d7a1

Browse files
authored
Merge pull request #132 from liveviewnative/snapshot-testing
Snapshot Testing
2 parents a050038 + e0feb35 commit 860d7a1

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

Package.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,9 @@ let package = Package(
3737
.testTarget(
3838
name: "LiveViewNativeTests",
3939
dependencies: ["LiveViewNative"]),
40+
.testTarget(
41+
name: "RenderingTests",
42+
dependencies: ["LiveViewNative"]
43+
)
4044
]
4145
)

Tests/RenderingTests/TextTests.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// TextTests.swift
3+
//
4+
//
5+
// Created by Carson Katri on 1/10/23.
6+
//
7+
8+
import XCTest
9+
import SwiftUI
10+
@testable import LiveViewNative
11+
12+
@MainActor
13+
final class TextTests: XCTestCase {
14+
func testSimple() throws {
15+
try assertMatch("<text>Hello, world!</text>") {
16+
Text("Hello, world!")
17+
}
18+
}
19+
func testStyles() throws {
20+
for style in Font.TextStyle.allCases {
21+
try assertMatch(#"<text font="\#(style)">Hello, world!</text>"#) {
22+
Text("Hello, world!").font(.system(style, weight: .regular))
23+
}
24+
}
25+
}
26+
func testWeights() throws {
27+
let allWeights: [String:Font.Weight] = [
28+
"ultraLight": .ultraLight,
29+
"thin": .thin,
30+
"light": .light,
31+
"regular": .regular,
32+
"medium": .medium,
33+
"semibold": .semibold,
34+
"bold": .bold,
35+
"heavy": .heavy,
36+
"black": .black,
37+
]
38+
for (name, weight) in allWeights {
39+
try assertMatch(name: "weight-\(name)", #"<text font="body" font-weight="\#(name)">Hello, world!</text>"#) {
40+
Text("Hello, world!").font(.system(.body, weight: weight))
41+
}
42+
}
43+
}
44+
func testColor() throws {
45+
for color in [Color.primary, Color.red, Color.blue] {
46+
try assertMatch(#"<text color="system-\#(color)">Hello, world!</text>"#) {
47+
Text("Hello, world!").foregroundColor(color)
48+
}
49+
}
50+
}
51+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// XCTestManifests.swift
3+
//
4+
//
5+
// Created by Carson Katri on 1/10/23.
6+
//
7+
8+
import XCTest
9+
10+
#if !canImport(ObjectiveC)
11+
public func allTests() -> [XCTestCaseEntry] {
12+
return [
13+
]
14+
}
15+
#endif
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// assertMatch.swift
3+
//
4+
//
5+
// Created by Carson Katri on 1/10/23.
6+
//
7+
8+
import XCTest
9+
import SwiftUI
10+
import Foundation
11+
@testable import LiveViewNative
12+
import LiveViewNativeCore
13+
14+
@MainActor
15+
func assertMatch(
16+
_ markup: String,
17+
_ file: String = #file,
18+
_ line: Int = #line,
19+
_ function: StaticString = #function,
20+
@ViewBuilder _ view: () -> some View
21+
) throws {
22+
try assertMatch(name: "\(URL(filePath: file).lastPathComponent)-\(line)-\(function)", markup, view)
23+
}
24+
25+
@MainActor
26+
func assertMatch(
27+
name: String,
28+
_ markup: String,
29+
@ViewBuilder _ view: () -> some View
30+
) throws {
31+
let coordinator = LiveViewCoordinator(URL(string: "http://localhost")!)
32+
let document = try LiveViewNativeCore.Document.parse(markup)
33+
let viewTree = coordinator.builder.fromNodes(
34+
document[document.root()].children(),
35+
context: LiveContext(coordinator: coordinator, url: coordinator.currentURL)
36+
).environment(\.coordinatorEnvironment, CoordinatorEnvironment(coordinator, document: document))
37+
let markupImage = ImageRenderer(content: viewTree).uiImage?.pngData()
38+
let viewImage = ImageRenderer(content: view()).uiImage?.pngData()
39+
40+
if markupImage == viewImage {
41+
XCTAssert(true)
42+
} else {
43+
let markupURL = URL.temporaryDirectory.appendingPathComponent("\(name)_markup", conformingTo: .png)
44+
let viewURL = URL.temporaryDirectory.appendingPathComponent("\(name)_view", conformingTo: .png)
45+
try markupImage?.write(to: markupURL)
46+
try viewImage?.write(to: viewURL)
47+
XCTAssert(false, "Rendered views did not match. Outputs saved to \(markupURL.path()) and \(viewURL.path())")
48+
}
49+
}

0 commit comments

Comments
 (0)