Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Share snapshot callback and improve its output #1581

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
CB408CED2AC6549F00E1BA00 /* KotlinHostingXCTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB408CEC2AC6549F00E1BA00 /* KotlinHostingXCTestCase.swift */; };
CB408CF72AC657EF00E1BA00 /* KotlinHostingXCTestCaseHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = CB408CF62AC657EF00E1BA00 /* KotlinHostingXCTestCaseHelper.m */; };
CB8A21EF2AC6647F00C104C2 /* SnapshotTesting in Frameworks */ = {isa = PBXBuildFile; productRef = CB8A21EE2AC6647F00C104C2 /* SnapshotTesting */; };
CB9729D32AD82D0C00804E94 /* SnapshotTestingCallback.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB9729D22AD82D0C00804E94 /* SnapshotTestingCallback.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -23,6 +24,7 @@
CB408CF52AC657EF00E1BA00 /* RedwoodLayoutUIViewTests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "RedwoodLayoutUIViewTests-Bridging-Header.h"; sourceTree = "<group>"; };
CB408CF62AC657EF00E1BA00 /* KotlinHostingXCTestCaseHelper.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KotlinHostingXCTestCaseHelper.m; sourceTree = "<group>"; };
CB408CF82AC6581100E1BA00 /* KotlinHostingXCTestCaseHelper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KotlinHostingXCTestCaseHelper.h; sourceTree = "<group>"; };
CB9729D22AD82D0C00804E94 /* SnapshotTestingCallback.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SnapshotTestingCallback.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -67,6 +69,7 @@
children = (
102FEEF12AD5D09F00246158 /* UIViewFlexContainerTestHost.swift */,
CB408CE72AC64CEF00E1BA00 /* UIViewSpacerTestHost.swift */,
CB9729D22AD82D0C00804E94 /* SnapshotTestingCallback.swift */,
CB408CEC2AC6549F00E1BA00 /* KotlinHostingXCTestCase.swift */,
CB408CF62AC657EF00E1BA00 /* KotlinHostingXCTestCaseHelper.m */,
CB408CF52AC657EF00E1BA00 /* RedwoodLayoutUIViewTests-Bridging-Header.h */,
Expand Down Expand Up @@ -174,6 +177,7 @@
CB408CED2AC6549F00E1BA00 /* KotlinHostingXCTestCase.swift in Sources */,
CB408CF72AC657EF00E1BA00 /* KotlinHostingXCTestCaseHelper.m in Sources */,
CB408CE82AC64CEF00E1BA00 /* UIViewSpacerTestHost.swift in Sources */,
CB9729D32AD82D0C00804E94 /* SnapshotTestingCallback.swift in Sources */,
102FEEF22AD5D09F00246158 /* UIViewFlexContainerTestHost.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import RedwoodLayoutUIViewTestKt
import SnapshotTesting
import UIKit

final class SnapshotTestingCallback : UIViewSnapshotCallback {
private let testName: String
private let fileName: StaticString

init(named testName: String, _ fileName: StaticString = #file) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand how it works, but this default is evaluated in the context of the caller which is very different from how Kotlin does it. As a result, we get to capture the name of the calling file automatically.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.testName = testName
self.fileName = fileName
}

func verifySnapshot(view: UIView, name: String?) {
assertSnapshot(of: view, as: .image, named: name, file: fileName, testName: testName)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,6 @@ import UIKit

final class UIViewFlexContainerTestHost: KotlinHostingXCTestCase<UIViewFlexContainerTest> {
override class func initTest(name: String) -> UIViewFlexContainerTest {
return UIViewFlexContainerTest(callback: Callback(named: name))
}
}

private class Callback : UIViewFlexContainerTestCallback {
private let testName: String

init(named testName: String) {
self.testName = testName
}

func verifySnapshot(view: UIView, name: String?) {
var snapshotName = testName
if (name != nil) {
snapshotName = "\(testName)-\(name!)"
}
assertSnapshot(of: view, as: .image, named: snapshotName)
return UIViewFlexContainerTest(callback: SnapshotTestingCallback(named: name))
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
import SnapshotTesting
import RedwoodLayoutUIViewTestKt
import UIKit

final class UIViewSpacerTestHost: KotlinHostingXCTestCase<UIViewSpacerTest> {
override class func initTest(name: String) -> UIViewSpacerTest {
return UIViewSpacerTest(callback: Callback(named: name))
}
}

private class Callback : UIViewSpacerTestCallback {
private let name: String

init(named name: String) {
self.name = name
}

func verifySnapshot(view: UIView) {
assertSnapshot(of: view, as: .image, named: name)
return UIViewSpacerTest(callback: SnapshotTestingCallback(named: name))
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If no name is provided a counter is implicitly used.

File renamed without changes
Diff not rendered.
Diff not rendered.
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,8 @@ import platform.UIKit.UIColor
import platform.UIKit.UILabel
import platform.UIKit.UIView

interface UIViewFlexContainerTestCallback {
fun verifySnapshot(view: UIView, name: String?)
}

class UIViewFlexContainerTest(
private val callback: UIViewFlexContainerTestCallback,
private val callback: UIViewSnapshotCallback,
) : AbstractFlexContainerTest<UIView>() {
override fun flexContainer(direction: FlexDirection): TestFlexContainer<UIView> {
return UIViewTestFlexContainer(UIViewFlexContainer(direction))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (C) 2023 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.redwood.layout.uiview

import platform.UIKit.UIView

interface UIViewSnapshotCallback {
fun verifySnapshot(view: UIView, name: String?)
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@ import platform.UIKit.UILayoutFittingCompressedSize
import platform.UIKit.UIStackView
import platform.UIKit.UIView

interface UIViewSpacerTestCallback {
fun verifySnapshot(view: UIView)
}

class UIViewSpacerTest(
private val callback: UIViewSpacerTestCallback,
private val callback: UIViewSnapshotCallback,
) : AbstractSpacerTest<UIView>() {
private val factory = UIViewRedwoodLayoutWidgetFactory()

Expand All @@ -56,6 +52,6 @@ class UIViewSpacerTest(
}

override fun verifySnapshot(value: UIView) {
callback.verifySnapshot(value)
callback.verifySnapshot(value, null)
}
}