-
Notifications
You must be signed in to change notification settings - Fork 584
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 Image Serialization Plugin #906
base: main
Are you sure you want to change the base?
Conversation
21e1a8d
to
87ebba0
Compare
4e6843c
to
ddac472
Compare
I hope this better explains what I’m trying to achieve with this PR. The FilePlugin doesn’t exist yet, but it’s a potential future plugin (prototype implementation not finished). graph TD
Dev --> A(Strategies)
Dev -. "Third-Party" .-> B(Strategies)
Dev --> C(Strategies)
Dev -. "Third-Party" .-> D(Strategies)
A --> swift-snapshot-testing
B --> swift-snapshot-testing
C --> swift-snapshot-testing
D --> swift-snapshot-testing
subgraph swift-snapshot-testing
Plugin
Plugin --> Image
Plugin --> File
Plugin --> Diff
end
Image -- "Default Value" --> PNG
Image -. "Third-Party" .-> JXL
Image -. "Third-Party" .-> HEIC
File -- "Default Value" --> Local
File -. "Third-Party" .-> SFTP
File -. "Third-Party" .-> Database
Diff -- "Default Value" --> Pixel
Diff -. "Third-Party" .-> Hash+Pixel
Diff -. "Third-Party" .-> MSE
%% Style adjustments to highlight default values
style PNG stroke:#FF0000,stroke-width:3px
style Local stroke:#FF0000,stroke-width:3px
style Pixel stroke:#FF0000,stroke-width:3px
%% Style adjustments for optional nodes
classDef optionalNode stroke-dasharray: 5,stroke:#333,fill:#E0E0E0,color:#000000
linkStyle default stroke:#005f73
classDef teal fill:#006d77,stroke:#333,color:#FFFFFF
classDef lightTeal fill:#83c5be,stroke:#333,color:#000000
classDef blue fill:#028090,stroke:#333,color:#FFFFFF
classDef purple fill:#6a4c93,stroke:#333,color:#FFFFFF
classDef lightBlue fill:#b2f7ef,stroke:#333,color:#000000
classDef orange fill:#ff6700,stroke:#333,color:#000000
Dev:::teal
A:::lightTeal
B:::optionalNode
C:::lightTeal
D:::optionalNode
swift-snapshot-testing:::blue
Plugin:::purple
Image:::purple
File:::purple
Diff:::purple
PNG:::purple
JXL:::optionalNode
HEIC:::optionalNode
Local:::purple
Database:::optionalNode
SFTP:::optionalNode
Hash+Pixel:::optionalNode
MSE:::optionalNode
Pixel:::purple
|
What has changed regarding the public API of swift-snapshot-testing. Inside
The new
The new
That's it. |
A plugin could also manage the diff algorithm. Instead of pixel comparison, we can use file hashing since generated images typically yield identical results. If the file hashes differ, we can then fall back to pixel-based diffing or even apply more advanced methods, like those below.
|
This PR need to be merged after #905 that lays the groundwork for the usage of plugin inside
swift-snapshot-testing
.Overview
This PR introduces a plugin for image serialization that all existing strategies will be using by default, enhancing the flexibility and extensibility of image handling within the framework. This plugin allows for the integration of various image formats through plugins, which can be dynamically discovered and registered at runtime. The core components of this implementation include the
ImageSerializationPlugin
protocol and theImageSerializer
class, designed to manage and utilize these plugins.Key Features
ImageSerializationPlugin Protocol: Defines methods for encoding and decoding images, allowing conforming classes to support various image formats. It combines the functionalities of
ImageSerialization
andSnapshotTestingPlugin
for seamless integration with the existing plugin system.imageFormat
: A static property that specifies the format supported by the plugin.encodeImage(_:)
: Encodes an image into the specified format.decodeImage(_:)
: Decodes image data back into an image.ImageSerializer Class: Manages the encoding and decoding of images using available plugins. It:
PluginRegistry
.Dynamic Plugin Discovery: Plugins conforming to the
ImageSerializationPlugin
protocol are automatically discovered and registered using the Objective-C runtime, making it easy to extend supported formats without modifying core functionality.Benefits
This PR lays the groundwork for a more modular and flexible approach to image serialization within SnapshotTesting, setting the stage for future enhancements and customizations through dedicated plugins.
Examples of plugins that use the
ImageSerializationPlugin
Warning
These implementations are tests and prototypes, not production-ready. They are simply here to demonstrate how easy it is to use the
ImageSerializationPlugin
..heic
format, which reduces file sizes compared to PNG..webp
format, which offers better compression than PNG..jxl
format, which provides superior compression and quality compared to PNG.