Skip to content

Commit de77dba

Browse files
committed
feat: add tests
1 parent db004d2 commit de77dba

File tree

4 files changed

+85
-9
lines changed

4 files changed

+85
-9
lines changed

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ let package = Package(
3232
name: "SnapshotTesting",
3333
dependencies: ["SnapshotTestingPlugin"]
3434
),
35+
.target(name: "SnapshotTestingPlugin"),
3536
.target(
3637
name: "InlineSnapshotTesting",
3738
dependencies: [

Sources/SnapshotTesting/Documentation.docc/Articles/Plugins.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,3 @@ The primary components of the plugin system include:
1717
- **Objective-C Runtime Integration**: Allows automatic discovery of plugins that conform to specific protocols.
1818

1919
The `PluginRegistry` is a singleton that registers plugins during its initialization. Plugins can be retrieved by their identifier or cast to specific types, allowing flexible interaction.
20-
21-
## ImageSerializer
22-
23-
The `ImageSerializer` is a plugin-based system that provides support for encoding and decoding images. It leverages the plugin architecture to extend its support for different image formats without needing to modify the core system.
24-
25-
Plugins that conform to the `ImageSerializationPlugin` protocol can be registered into the `PluginRegistry` and used to encode or decode images in different formats, such as PNG, JPEG, WebP, HEIC, and more.
26-
27-
When a plugin supporting a specific image format is available, the `ImageSerializer` can dynamically choose the correct plugin based on the image format required, ensuring modularity and scalability in image handling.

Sources/SnapshotTesting/Plugins/PluginRegistry.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#if canImport(SwiftUI) && canImport(ObjectiveC)
22
import Foundation
33
import ObjectiveC.runtime
4-
import ImageSerializationPlugin
54
import SnapshotTestingPlugin
65

76
/// A singleton class responsible for managing and registering plugins conforming to the `SnapshotTestingPlugin` protocol.
@@ -95,5 +94,16 @@ public class PluginRegistry {
9594
self.registerPlugin(pluginType.init())
9695
}
9796
}
97+
98+
// TEST-ONLY Reset Method
99+
#if DEBUG
100+
internal static func reset() {
101+
shared.plugins.removeAll()
102+
}
103+
104+
internal static func automaticPluginRegistration() {
105+
shared.automaticPluginRegistration()
106+
}
107+
#endif
98108
}
99109
#endif
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#if canImport(SwiftUI) && canImport(ObjectiveC)
2+
import XCTest
3+
import ObjectiveC
4+
@testable import SnapshotTesting
5+
import SnapshotTestingPlugin
6+
7+
class MockPlugin: NSObject, SnapshotTestingPlugin {
8+
static var identifier: String = "MockPlugin"
9+
10+
required override init() {
11+
super.init()
12+
}
13+
}
14+
15+
class AnotherMockPlugin: NSObject, SnapshotTestingPlugin {
16+
static var identifier: String = "AnotherMockPlugin"
17+
18+
required override init() {
19+
super.init()
20+
}
21+
}
22+
23+
final class PluginRegistryTests: XCTestCase {
24+
25+
override func setUp() {
26+
super.setUp()
27+
PluginRegistry.reset() // Reset state before each test
28+
}
29+
30+
override func tearDown() {
31+
PluginRegistry.reset() // Reset state after each test
32+
super.tearDown()
33+
}
34+
35+
func testRegisterPlugin() {
36+
// Register a mock plugin
37+
PluginRegistry.registerPlugin(MockPlugin())
38+
39+
// Retrieve the plugin by identifier
40+
let retrievedPlugin: MockPlugin? = PluginRegistry.plugin(for: MockPlugin.identifier)
41+
XCTAssertNotNil(retrievedPlugin)
42+
}
43+
44+
func testRetrieveNonExistentPlugin() {
45+
// Try to retrieve a non-existent plugin
46+
let nonExistentPlugin: MockPlugin? = PluginRegistry.plugin(for: "NonExistentPlugin")
47+
XCTAssertNil(nonExistentPlugin)
48+
}
49+
50+
func testAllPlugins() {
51+
// Register two mock plugins
52+
PluginRegistry.registerPlugin(MockPlugin())
53+
PluginRegistry.registerPlugin(AnotherMockPlugin())
54+
55+
// Retrieve all plugins
56+
let allPlugins: [SnapshotTestingPlugin] = PluginRegistry.allPlugins()
57+
58+
XCTAssertEqual(allPlugins.count, 2)
59+
XCTAssertTrue(allPlugins.contains { $0 is MockPlugin })
60+
XCTAssertTrue(allPlugins.contains { $0 is AnotherMockPlugin })
61+
}
62+
63+
func testAutomaticPluginRegistration() {
64+
// Automatically register plugins using the Objective-C runtime
65+
PluginRegistry.automaticPluginRegistration() // Reset state before each test
66+
67+
// Verify if the mock plugin was automatically registered
68+
let registeredPlugin: MockPlugin? = PluginRegistry.plugin(for: MockPlugin.identifier)
69+
XCTAssertNotNil(registeredPlugin)
70+
}
71+
}
72+
73+
#endif

0 commit comments

Comments
 (0)