From 72ab9fc0b7aee60c5842f7dc92a014d2aae49571 Mon Sep 17 00:00:00 2001 From: Felix Mau Date: Fri, 4 Nov 2022 19:36:43 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20::=20Extract=20path=20crea?= =?UTF-8?q?tion=20into=20helper=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SnapshotTests/XCTest+AssertSnapshot.swift | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/GradientLoadingBar/Tests/SnapshotTests/XCTest+AssertSnapshot.swift b/GradientLoadingBar/Tests/SnapshotTests/XCTest+AssertSnapshot.swift index 8586ab1..b17e57f 100644 --- a/GradientLoadingBar/Tests/SnapshotTests/XCTest+AssertSnapshot.swift +++ b/GradientLoadingBar/Tests/SnapshotTests/XCTest+AssertSnapshot.swift @@ -83,27 +83,15 @@ extension XCTestCase { // MARK: - Private methods - // swiftlint:disable:next function_parameter_count function_body_length cyclomatic_complexity + // swiftlint:disable:next function_parameter_count cyclomatic_complexity private func assertSnapshot(matching image: UIImage, precision: Double, callFunction: StaticString, callFilePath: StaticString, file: StaticString, line: UInt) { - // E.g. "test_gradientActivityIndicatorView_shouldContainCorrectDefaultColors" - let snapshotImageFileName = String(callFunction).trimmingCharacters(in: .alphanumerics.inverted) - - let unitTestFileURL = URL(fileURLWithPath: String(callFilePath)) - let snapshotImageFileURL = unitTestFileURL - // Remove ".swift" - .deletingLastPathComponent() - // Append e.g. "__Snapshot__" directory - .appendingPathComponent(Config.snapshotDirectory, isDirectory: true) - // Append e.g. "test_gradientActivityIndicatorView_shouldContainCorrectDefaultColors" - .appendingPathComponent(snapshotImageFileName, isDirectory: false) - .appendingPathExtension("png") - - let snapshotFileManager = SnapshotFileManager(fileURL: snapshotImageFileURL) + let referenceImageFileURL = referenceImageFileURL(callFunction: callFunction, callFilePath: callFilePath) + let snapshotFileManager = SnapshotFileManager(fileURL: referenceImageFileURL) do { guard snapshotFileManager.hasReferenceImage() else { try snapshotFileManager.saveReferenceImage(image) @@ -158,6 +146,23 @@ extension XCTestCase { } } + private func referenceImageFileURL(callFunction: StaticString, callFilePath: StaticString) -> URL { + // E.g. "test_gradientActivityIndicatorView_shouldContainCorrectDefaultColors" + let referenceImageFileName = String(callFunction).trimmingCharacters(in: .alphanumerics.inverted) + + // E.g. "/Users/f.mau/iOS/GradientLoadingBar/GradientLoadingBar/Tests/SnapshotTests/GradientLoadingBar/GradientLoadingBarControllerTestCase.swift" + let unitTestFileURL = URL(fileURLWithPath: String(callFilePath)) + + return unitTestFileURL + // Remove "__FILENAME__.swift" + .deletingLastPathComponent() + // Append e.g. "__Snapshot__" directory + .appendingPathComponent(Config.snapshotDirectory, isDirectory: true) + // Append e.g. "test_gradientActivityIndicatorView_shouldContainCorrectDefaultColors" + .appendingPathComponent(referenceImageFileName, isDirectory: false) + .appendingPathExtension("png") + } + private func addAttachment(name: String, image: UIImage) { let imageAttachment = XCTAttachment(image: image) imageAttachment.name = name