@@ -11,56 +11,71 @@ import Foundation
11
11
@testable import LiveViewNative
12
12
import LiveViewNativeCore
13
13
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
+ }
52
27
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
61
75
}
62
76
}
63
77
78
+ #if os(iOS)
64
79
private class SnapshotWindow : UIWindow {
65
80
override var safeAreaInsets : UIEdgeInsets {
66
81
. zero
@@ -86,3 +101,4 @@ private func snapshot(_ view: some View, size: CGSize?) -> UIImage? {
86
101
uiView. layer. render ( in: context. cgContext)
87
102
}
88
103
}
104
+ #endif
0 commit comments