Skip to content

Commit a7f218c

Browse files
committed
feat: add documentation / improve naming
1 parent 54cfbba commit a7f218c

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

Sources/SnapshotTesting/AssertSnapshot.swift

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ import ImageSerializationPlugin
77
@_implementationOnly import Testing
88
#endif
99

10+
/// Whether or not to change the default output image format to something else.
11+
@available(
12+
*,
13+
deprecated,
14+
message:
15+
"Use 'withSnapshotTesting' to customize the image output format. See the documentation for more information."
16+
)
1017
public var imageFormat: ImageSerializationFormat {
1118
get {
1219
_imageFormat

Sources/SnapshotTesting/Plugins/ImageSerializer.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class ImageSerializer {
1919
let plugins: [ImageSerialization]
2020

2121
public init() {
22-
self.plugins = PluginRegistry.shared.allPlugins()
22+
self.plugins = PluginRegistry.allPlugins()
2323
}
2424

2525
// TODO: async throws will be added later

Sources/SnapshotTesting/Plugins/PluginRegistry.swift

+36-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import SnapshotTestingPlugin
1111
/// and filter plugins that conform to the `ImageSerialization` protocol.
1212
public class PluginRegistry {
1313
/// The shared instance of the `PluginRegistry`, providing a single point of access.
14-
public static let shared = PluginRegistry()
14+
private static let shared = PluginRegistry()
1515

1616
/// A dictionary holding the registered plugins, keyed by their identifier.
1717
private var plugins: [String: AnyObject] = [:]
@@ -20,13 +20,42 @@ public class PluginRegistry {
2020
///
2121
/// Upon initialization, the registry automatically calls `registerAllPlugins()` to discover and register plugins.
2222
private init() {
23-
defer { registerAllPlugins() }
23+
defer { automaticPluginRegistration() }
2424
}
2525

2626
/// Registers a given plugin in the registry.
2727
///
2828
/// - Parameter plugin: An instance of a class conforming to `SnapshotTestingPlugin`.
29-
public func registerPlugin(_ plugin: SnapshotTestingPlugin) {
29+
public static func registerPlugin(_ plugin: SnapshotTestingPlugin) {
30+
PluginRegistry.shared.registerPlugin(plugin)
31+
}
32+
33+
/// Retrieves a plugin from the registry by its identifier and casts it to the specified type.
34+
///
35+
/// This method attempts to find a plugin in the registry that matches the given identifier and cast it to the specified generic type `Output`.
36+
/// If the plugin exists and can be cast to the specified type, it is returned; otherwise, `nil` is returned.
37+
///
38+
/// - Parameter identifier: A unique string identifier for the plugin.
39+
/// - Returns: The plugin instance cast to the specified type `Output` if found and castable, otherwise `nil`.
40+
public static func plugin<Output>(for identifier: String) -> Output? {
41+
PluginRegistry.shared.plugin(for: identifier)
42+
}
43+
44+
/// Returns all registered plugins that can be cast to the specified type.
45+
///
46+
/// This method retrieves all registered plugins and attempts to cast each one to the specified generic type `Output`.
47+
/// Only the plugins that can be successfully cast to `Output` are included in the returned array.
48+
///
49+
/// - Returns: An array of all registered plugins that can be cast to the specified type `Output`.
50+
public static func allPlugins<Output>() -> [Output] {
51+
PluginRegistry.shared.allPlugins()
52+
}
53+
54+
// MARK: - Internal Representation
55+
/// Registers a given plugin in the registry.
56+
///
57+
/// - Parameter plugin: An instance of a class conforming to `SnapshotTestingPlugin`.
58+
private func registerPlugin(_ plugin: SnapshotTestingPlugin) {
3059
plugins[type(of: plugin).identifier] = plugin
3160
}
3261

@@ -37,7 +66,7 @@ public class PluginRegistry {
3766
///
3867
/// - Parameter identifier: A unique string identifier for the plugin.
3968
/// - Returns: The plugin instance cast to the specified type `Output` if found and castable, otherwise `nil`.
40-
public func plugin<Output>(for identifier: String) -> Output? {
69+
private func plugin<Output>(for identifier: String) -> Output? {
4170
return plugins[identifier] as? Output
4271
}
4372

@@ -47,10 +76,10 @@ public class PluginRegistry {
4776
/// Only the plugins that can be successfully cast to `Output` are included in the returned array.
4877
///
4978
/// - Returns: An array of all registered plugins that can be cast to the specified type `Output`.
50-
public func allPlugins<Output>() -> [Output] {
79+
private func allPlugins<Output>() -> [Output] {
5180
return Array(plugins.values.compactMap { $0 as? Output })
5281
}
53-
82+
5483
/// Discovers and registers all classes that conform to the `SnapshotTestingPlugin` protocol.
5584
///
5685
/// This method iterates over all classes in the Objective-C runtime, identifies those that conform to the `SnapshotTestingPlugin`
@@ -62,7 +91,7 @@ public class PluginRegistry {
6291
/// 3. All class references are retrieved into the allocated memory.
6392
/// 4. Each class reference is checked for conformance to the `SnapshotTestingPlugin` protocol.
6493
/// 5. If a class conforms, it is instantiated and registered as a plugin using the `registerPlugin(_:)` method.
65-
func registerAllPlugins() {
94+
private func automaticPluginRegistration() {
6695
let classCount = objc_getClassList(nil, 0)
6796
guard classCount > 0 else { return }
6897

0 commit comments

Comments
 (0)