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

Add jpeg support #6

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions Sources/SnapshotTestCase/Extensions/UIImageExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ extension UIImage {
let referencePixelsData = UnsafeMutablePointer<Pixel>
.allocate(capacity: referenceCGImage.width * referenceCGImage.height)
let bitmapInfo = CGBitmapInfo(
rawValue: CGImageAlphaInfo.premultipliedLast
.rawValue & CGBitmapInfo.alphaInfoMask.rawValue
rawValue: CGImageAlphaInfo.premultipliedLast.rawValue
& CGBitmapInfo.alphaInfoMask.rawValue
)

guard let colorSpace = cgImage.colorSpace,
Expand Down
27 changes: 11 additions & 16 deletions Sources/SnapshotTestCase/Snapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@ import UIKit
public class Snapshot {
static var renderOffsetY: CGFloat = LaunchEnvironment.renderOffsetY

enum Constants {
static let imageExt: String = "png"
}

struct TestCase {
let filePath: URL
let name: String
let imageType: SnapshotImageType
let renderDelay: TimeInterval
let viewControllerBuilder: @MainActor () -> UIViewController
}

struct ExecutedTestCase {
let filePath: URL
let name: String
let testCase: TestCase
let config: SnapshotConfig.Config
let snapshot: UIImage
}
Expand Down Expand Up @@ -84,8 +80,8 @@ public class Snapshot {

private extension Snapshot {
func saveSnapshot(to path: String, executed testCase: ExecutedTestCase) async throws {
guard let data = testCase.snapshot.pngData() else {
throw SnapshotError.pngRepresentation
guard let data = testCase.snapshot.data(testCase.testCase.imageType) else {
throw SnapshotError.imageData
}
try await createFolderIfNeeded(at: path, executed: testCase)
let imageUrl = imageUrl(path, executed: testCase)
Expand Down Expand Up @@ -146,9 +142,9 @@ private extension Snapshot {
}

private func imagePath(_ path: String, executed testCase: ExecutedTestCase) -> URL {
testCase.filePath
testCase.testCase.filePath
.appendingPathComponent(path, isDirectory: true)
.appendingFolderIfNeeded(testCase.filePath.lastPathComponent)
.appendingFolderIfNeeded(testCase.testCase.filePath.lastPathComponent)
}

private func imageUrl(
Expand All @@ -158,7 +154,7 @@ private extension Snapshot {
) -> URL {
imagePath(path, executed: testCase)
.appendingPathComponent(testCase.filename + suffix)
.appendingPathExtension(Constants.imageExt)
.appendingPathExtension(testCase.testCase.imageType.pathExtension)
}
}

Expand All @@ -175,8 +171,7 @@ private extension Snapshot.TestCase {
@MainActor
func execute(with config: SnapshotConfig.Config) async throws -> Snapshot.ExecutedTestCase {
Snapshot.ExecutedTestCase(
filePath: filePath,
name: name,
testCase: self,
config: config,
snapshot: try await takeSnapshot(with: config)
)
Expand Down Expand Up @@ -246,16 +241,16 @@ private extension Snapshot.TestCase {
private extension Snapshot.ExecutedTestCase {
var filename: String {
var filename = ""
if name != "" {
filename += name
if testCase.name != "" {
filename += testCase.name
}
filename += "_\(config.id)"
return filename
}

func compare(with reference: UIImage, tolerance: Double) async throws {
guard let diff = snapshot.compare(with: reference, tolerance: 1000000) else {
throw SnapshotError.pngRepresentation
throw SnapshotError.imageData
}
guard diff <= tolerance else {
throw SnapshotError.referenceImageNotEqual(diff)
Expand Down
2 changes: 1 addition & 1 deletion Sources/SnapshotTestCase/SnapshotError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public enum SnapshotError: Error {
case saveSnapshot(Error)
case copySnapshot(Error)
case deleteSnapshot(Error)
case pngRepresentation
case imageData
case referenceImageDoesNotExist
case createFolder(Error)
case createView
Expand Down
27 changes: 27 additions & 0 deletions Sources/SnapshotTestCase/SnapshotImageType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Foundation
import UIKit

public enum SnapshotImageType {
case png
case jpeg(quality: CGFloat)

var pathExtension: String {
switch self {
case .png: "png"
case .jpeg: "jpeg"
}
}
}

public extension SnapshotImageType {
static var `default`: SnapshotImageType = .png
}

extension UIImage {
func data(_ imageType: SnapshotImageType) -> Data? {
switch imageType {
case .png: self.pngData()
case .jpeg(let quality): self.jpegData(compressionQuality: quality)
}
}
}
4 changes: 4 additions & 0 deletions Sources/SnapshotTestCase/SnapshotTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public extension SnapshotTestCase where Self: XCTestCase {
func verifySnapshot(
name: String? = nil,
config: SnapshotConfig = .default,
imageType: SnapshotImageType = .default,
renderDelay: TimeInterval = .snapshotRenderDelay,
file: String = #file,
function: String = #function,
Expand All @@ -20,6 +21,7 @@ public extension SnapshotTestCase where Self: XCTestCase {
try await verifySnapshot(
name: name,
config: config,
imageType: imageType,
renderDelay: renderDelay,
file: file,
function: function,
Expand All @@ -31,6 +33,7 @@ public extension SnapshotTestCase where Self: XCTestCase {
func verifySnapshot(
name: String? = nil,
config: SnapshotConfig = .default,
imageType: SnapshotImageType = .default,
renderDelay: TimeInterval = .snapshotRenderDelay,
file: String = #file,
function _: String = #function,
Expand All @@ -40,6 +43,7 @@ public extension SnapshotTestCase where Self: XCTestCase {
let testCase = Snapshot.TestCase(
filePath: getFilePath(file: file),
name: name ?? getTestCaseName() ?? "Test",
imageType: imageType,
renderDelay: renderDelay,
viewControllerBuilder: viewControllerBuilder
)
Expand Down
4 changes: 2 additions & 2 deletions Tests/SnapshotTestCaseTests/SnapshotTestCaseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import XCTest

class SnapshotTestCaseTests: XCTestCase, SnapshotTestCase {
func test() async throws {
try await verifySnapshot {
try await verifySnapshot(imageType: .png) {
TestView()
}
}

func test2() async throws {
try await verifySnapshot {
try await verifySnapshot(imageType: .png) {
let viewController = UIHostingController(rootView: TestView())
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
let subRootView = Rectangle()
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.