Skip to content

Commit 5124af6

Browse files
committed
Add platform builds to CI
1 parent 57ad3aa commit 5124af6

File tree

5 files changed

+77
-52
lines changed

5 files changed

+77
-52
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,12 @@ jobs:
1414
shell: bash
1515
run: |
1616
sudo xcode-select --switch /Applications/Xcode_14.2.app
17-
xcodebuild test -scheme LiveViewNative -sdk iphonesimulator16.2 -destination "OS=16.2,name=iPhone 14 Pro"
17+
xcodebuild test -scheme LiveViewNative -sdk iphonesimulator16.2 -destination "OS=16.2,name=iPhone 14 Pro"
18+
- name: Build for macOS
19+
shell: bash
20+
run: |
21+
xcodebuild -scheme LiveViewNative -sdk macosx13.1 -destination "platform=macOS"
22+
- name: Build for watchOS
23+
shell: bash
24+
run: |
25+
xcodebuild -scheme LiveViewNative -sdk watchsimulator9.1 -destination "OS=9.1,name=Apple Watch Series 8 (45mm)"

Package.resolved

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ let package = Package(
3434
]),
3535
.testTarget(
3636
name: "LiveViewNativeTests",
37-
dependencies: ["LiveViewNative"]),
37+
dependencies: ["LiveViewNative"]
38+
),
3839
.testTarget(
3940
name: "RenderingTests",
4041
dependencies: ["LiveViewNative"]

Tests/RenderingTests/ProgressViewTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ import SwiftUI
1212
@MainActor
1313
final class ProgressViewTests: XCTestCase {
1414
func testValue() throws {
15-
try assertMatch(#"<progressview value="0.5" />"#) {
15+
try assertMatch(#"<progress-view value="0.5" />"#, size: .init(width: 200, height: 200)) {
1616
ProgressView(value: 0.5)
1717
}
1818
}
1919

2020
func testTotal() throws {
21-
try assertMatch(#"<progressview value="2.5" total="5" />"#) {
22-
ProgressView(value: 0.5, total: 5)
21+
try assertMatch(#"<progress-view value="2.5" total="5" />"#, size: .init(width: 200, height: 200)) {
22+
ProgressView(value: 2.5, total: 5)
2323
}
2424
}
2525
}

Tests/RenderingTests/assertMatch.swift

Lines changed: 62 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,56 +11,71 @@ import Foundation
1111
@testable import LiveViewNative
1212
import LiveViewNativeCore
1313

14-
@MainActor
15-
func assertMatch(
16-
_ markup: String,
17-
_ file: String = #file,
18-
_ line: Int = #line,
19-
_ function: StaticString = #function,
20-
environment: @escaping (inout EnvironmentValues) -> () = { _ in },
21-
size: CGSize? = nil,
22-
@ViewBuilder _ view: () -> some View
23-
) throws {
24-
try assertMatch(name: "\(URL(filePath: file).lastPathComponent)-\(line)-\(function)", markup, environment: environment, size: size, view)
25-
}
26-
27-
@MainActor
28-
func assertMatch(
29-
name: String,
30-
_ markup: String,
31-
environment: @escaping (inout EnvironmentValues) -> () = { _ in },
32-
size: CGSize? = nil,
33-
@ViewBuilder _ view: () -> some View
34-
) throws {
35-
let session = LiveSessionCoordinator(URL(string: "http://localhost")!)
36-
let document = try LiveViewNativeCore.Document.parse(markup)
37-
let viewTree = session.rootCoordinator.builder.fromNodes(
38-
document[document.root()].children(),
39-
context: LiveContext(coordinator: session.rootCoordinator, url: session.url)
40-
).environment(\.coordinatorEnvironment, CoordinatorEnvironment(session.rootCoordinator, document: document))
41-
42-
let markupImage = snapshot(
43-
viewTree
44-
.transformEnvironment(\.self, transform: environment),
45-
size: size
46-
)?.pngData()
47-
let viewImage = snapshot(
48-
view()
49-
.transformEnvironment(\.self, transform: environment),
50-
size: size
51-
)?.pngData()
14+
extension XCTestCase {
15+
@MainActor
16+
func assertMatch(
17+
_ markup: String,
18+
_ file: String = #file,
19+
_ line: Int = #line,
20+
_ function: StaticString = #function,
21+
environment: @escaping (inout EnvironmentValues) -> () = { _ in },
22+
size: CGSize? = nil,
23+
@ViewBuilder _ view: () -> some View
24+
) throws {
25+
try assertMatch(name: "\(URL(filePath: file).lastPathComponent)-\(line)-\(function)", markup, environment: environment, size: size, view)
26+
}
5227

53-
if markupImage == viewImage {
54-
XCTAssert(true)
55-
} else {
56-
let markupURL = URL.temporaryDirectory.appendingPathComponent("\(name)_markup", conformingTo: .png)
57-
let viewURL = URL.temporaryDirectory.appendingPathComponent("\(name)_view", conformingTo: .png)
58-
try markupImage?.write(to: markupURL)
59-
try viewImage?.write(to: viewURL)
60-
XCTAssert(false, "Rendered views did not match. Outputs saved to \(markupURL.path()) and \(viewURL.path())")
28+
@MainActor
29+
func assertMatch(
30+
name: String,
31+
_ markup: String,
32+
environment: @escaping (inout EnvironmentValues) -> () = { _ in },
33+
size: CGSize? = nil,
34+
@ViewBuilder _ view: () -> some View
35+
) throws {
36+
#if !os(iOS)
37+
fatalError("Rendering tests not supported on platforms other than iOS at this time")
38+
#else
39+
let session = LiveSessionCoordinator(URL(string: "http://localhost")!)
40+
let document = try LiveViewNativeCore.Document.parse(markup)
41+
let viewTree = session.rootCoordinator.builder.fromNodes(
42+
document[document.root()].children(),
43+
context: LiveContext(coordinator: session.rootCoordinator, url: session.url)
44+
).environment(\.coordinatorEnvironment, CoordinatorEnvironment(session.rootCoordinator, document: document))
45+
46+
guard let markupImage = snapshot(
47+
viewTree
48+
.transformEnvironment(\.self, transform: environment),
49+
size: size
50+
)
51+
else {
52+
return XCTAssert(false, "Markup failed to render an image")
53+
}
54+
guard let viewImage = snapshot(
55+
view()
56+
.transformEnvironment(\.self, transform: environment),
57+
size: size
58+
)
59+
else {
60+
return XCTAssert(false, "View failed to render an image")
61+
}
62+
63+
self.add(XCTAttachment(image: markupImage))
64+
self.add(XCTAttachment(image: viewImage))
65+
66+
let markupData = markupImage.pngData()
67+
let viewData = viewImage.pngData()
68+
69+
if markupData == viewData {
70+
XCTAssert(true)
71+
} else {
72+
XCTAssert(false, "Rendered views did not match. Attachments can be viewed in the Report navigator.")
73+
}
74+
#endif
6175
}
6276
}
6377

78+
#if os(iOS)
6479
private class SnapshotWindow: UIWindow {
6580
override var safeAreaInsets: UIEdgeInsets {
6681
.zero
@@ -86,3 +101,4 @@ private func snapshot(_ view: some View, size: CGSize?) -> UIImage? {
86101
uiView.layer.render(in: context.cgContext)
87102
}
88103
}
104+
#endif

0 commit comments

Comments
 (0)