Skip to content

Commit

Permalink
fix: add missing documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mackoj committed Sep 15, 2024
1 parent 247849b commit 21e1a8d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,46 @@ The **Image Serialization Plugin** relies on the PluginAPI that is a combination
- When an image needs to be serialized, the `ImageSerializer` checks the available plugins for one that supports the requested format.
- If no plugin is found, it defaults to the built-in PNG encoding/decoding methods.

#### Extensibility:
#### Extensibility

The plugin architecture allows developers to introduce new image formats without modifying the core SnapshotTesting library. By creating a new plugin that conforms to `ImageSerialization` and `SnapshotTestingPlugin`, other formats.
The plugin architecture allows developers to introduce new image formats without modifying the core SnapshotTesting library. By creating a new plugin that conforms to `ImageSerializationPlugin`, you can easily add support for additional image formats.

Here are a few example plugins demonstrating how to extend the library with new image formats:

- **[Image Serialization Plugin - HEIC](https://github.com/mackoj/swift-snapshot-testing-plugin-heic)**: Enables storing images in the `.heic` format, which reduces file sizes compared to PNG.
- **[Image Serialization Plugin - WEBP](https://github.com/mackoj/swift-snapshot-testing-plugin-webp)**: Allows storing images in the `.webp` format, which offers better compression than PNG.
- **[Image Serialization Plugin - JXL](https://github.com/mackoj/swift-snapshot-testing-plugin-jxl)**: Facilitates storing images in the `.jxl` format, which provides superior compression and quality compared to PNG.

## Usage

For example, if you want to use JPEG XL as a new image format for your snapshots, you can follow these steps. This approach applies to any image format as long as you have a plugin that conforms to `ImageSerializationPlugin`.

1. **Add the Dependency**: Include the appropriate image serialization plugin as a dependency in your `Package.swift` file. For JPEG XL, it would look like this:

```swift
.package(url: "https://github.com/mackoj/swift-snapshot-testing-plugin-jxl.git", revision: "0.0.1"),
```

2. **Link to Your Test Target**: Add the image serialization plugin to your test target's dependencies:

```swift
.product(name: "JXLImageSerializer", package: "swift-snapshot-testing-plugin-jxl"),
```

3. **Import and Set Up**: In your test file, import the serializer and configure the image format in the `setUp()` method:

```swift
import JXLImageSerializer

override class func setUp() {
SnapshotTesting.imageFormat = JXLImageSerializer.imageFormat
}
```

Alternatively, you can specify the image format for individual assertions:

```swift
assertSnapshot(of: label, as: .image(precision: 0.9, format: JXLImageSerializer.imageFormat))
```

This setup demonstrates how to integrate a specific image format plugin. Replace `JXLImageSerializer` with the appropriate plugin and format for other image formats.
4 changes: 3 additions & 1 deletion Sources/SnapshotTesting/SnapshotTestingConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import ImageSerializationPlugin
/// - Parameters:
/// - record: The record mode to use while asserting snapshots.
/// - diffTool: The diff tool to use while asserting snapshots.
/// - imageFormat: The image format used while encoding/decoding images(default: .png).
/// - operation: The operation to perform.
public func withSnapshotTesting<R>(
record: SnapshotTestingConfiguration.Record? = nil,
Expand All @@ -44,7 +45,7 @@ public func withSnapshotTesting<R>(

/// Customizes `assertSnapshot` for the duration of an asynchronous operation.
///
/// See ``withSnapshotTesting(record:diffTool:operation:)-2kuyr`` for more information.
/// See ``withSnapshotTesting(record:diffTool:imageFormat:operation:)-2kuyr`` for more information.
public func withSnapshotTesting<R>(
record: SnapshotTestingConfiguration.Record? = nil,
diffTool: SnapshotTestingConfiguration.DiffTool? = nil,
Expand Down Expand Up @@ -77,6 +78,7 @@ public struct SnapshotTestingConfiguration: Sendable {
/// See ``Record-swift.struct`` for more information.
public var record: Record?

/// The image format to use while encoding/decoding snapshot tests.
public var imageFormat: ImageSerializationFormat?

public init(
Expand Down

0 comments on commit 21e1a8d

Please sign in to comment.