Skip to content

Commit

Permalink
fix: reduce ObjectiveC need
Browse files Browse the repository at this point in the history
  • Loading branch information
mackoj committed Sep 16, 2024
1 parent a793448 commit 2269248
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
29 changes: 16 additions & 13 deletions Sources/SnapshotTesting/Plugins/PluginRegistry.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#if canImport(SwiftUI) && canImport(ObjectiveC)
import Foundation
import ObjectiveC.runtime
import SnapshotTestingPlugin

#if canImport(ObjectiveC)
import ObjectiveC.runtime
#endif

/// A singleton class responsible for managing and registering plugins conforming to the `SnapshotTestingPlugin` protocol.
///
/// The `PluginRegistry` automatically discovers and registers classes conforming to the `SnapshotTestingPlugin` protocol
/// within the Objective-C runtime. It allows retrieval of specific plugins by identifier, access to all registered plugins,
/// and filtering of plugins that conform to the `ImageSerialization` protocol.
public class PluginRegistry {
class PluginRegistry {

/// Shared singleton instance of `PluginRegistry`.
private static let shared = PluginRegistry()
Expand All @@ -23,27 +25,27 @@ public class PluginRegistry {
defer { automaticPluginRegistration() }
}

// MARK: - Public Methods
// MARK: - Internal Methods

/// Registers a plugin.
///
/// - Parameter plugin: An instance conforming to `SnapshotTestingPlugin`.
public static func registerPlugin(_ plugin: SnapshotTestingPlugin) {
static func registerPlugin(_ plugin: SnapshotTestingPlugin) {
PluginRegistry.shared.registerPlugin(plugin)
}

/// Retrieves a plugin by its identifier, casting it to the specified type.
///
/// - Parameter identifier: The unique identifier for the plugin.
/// - Returns: The plugin instance cast to `Output` if found and castable, otherwise `nil`.
public static func plugin<Output>(for identifier: String) -> Output? {
static func plugin<Output>(for identifier: String) -> Output? {
PluginRegistry.shared.plugin(for: identifier)
}

/// Returns all registered plugins cast to the specified type.
///
/// - Returns: An array of all registered plugins that can be cast to `Output`.
public static func allPlugins<Output>() -> [Output] {
static func allPlugins<Output>() -> [Output] {
PluginRegistry.shared.allPlugins()
}

Expand Down Expand Up @@ -71,6 +73,7 @@ public class PluginRegistry {
return Array(plugins.values.compactMap { $0 as? Output })
}

#if canImport(ObjectiveC)
/// Discovers and registers all classes conforming to the `SnapshotTestingPlugin` protocol.
///
/// This method iterates over all Objective-C runtime classes, identifying those that conform to `SnapshotTestingPlugin`,
Expand All @@ -94,16 +97,16 @@ public class PluginRegistry {
self.registerPlugin(pluginType.init())
}
}
#endif

// TEST-ONLY Reset Method
#if DEBUG
#if DEBUG
internal static func reset() {
shared.plugins.removeAll()
shared.plugins.removeAll()
}

internal static func automaticPluginRegistration() {
shared.automaticPluginRegistration()
shared.automaticPluginRegistration()
}
#endif
}
#endif
}
3 changes: 2 additions & 1 deletion Sources/SnapshotTestingPlugin/SnapshotTestingPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import Foundation
/// environments where both Foundation and Objective-C are available, making it compatible with Objective-C runtime features.
///
/// Conforming classes must be marked with `@objc` to ensure compatibility with Objective-C runtime mechanisms.
@objc public protocol SnapshotTestingPlugin {
@objc
public protocol SnapshotTestingPlugin: AnyObject {

/// A unique string identifier for the plugin.
///
Expand Down
30 changes: 13 additions & 17 deletions Tests/SnapshotTestingTests/PluginRegistryTests.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
#if canImport(SwiftUI) && canImport(ObjectiveC)
#if canImport(SwiftUI)
import XCTest
import ObjectiveC
@testable import SnapshotTesting
import SnapshotTestingPlugin

class MockPlugin: NSObject, SnapshotTestingPlugin {
static var identifier: String = "MockPlugin"
final class PluginRegistryTests: XCTestCase {

required override init() {
super.init()
class MockPlugin: SnapshotTestingPlugin {
static var identifier: String = "MockPlugin"

required init() {}
}
}

class AnotherMockPlugin: NSObject, SnapshotTestingPlugin {
static var identifier: String = "AnotherMockPlugin"

required override init() {
super.init()
class AnotherMockPlugin: SnapshotTestingPlugin {
static var identifier: String = "AnotherMockPlugin"

required init() {}
}
}

final class PluginRegistryTests: XCTestCase {

override func setUp() {
super.setUp()
Expand Down Expand Up @@ -60,14 +55,15 @@ final class PluginRegistryTests: XCTestCase {
XCTAssertTrue(allPlugins.contains { $0 is AnotherMockPlugin })
}

#if canImport(ObjectiveC)
func testAutomaticPluginRegistration() {
// Automatically register plugins using the Objective-C runtime
PluginRegistry.automaticPluginRegistration() // Reset state before each test
PluginRegistry.automaticPluginRegistration()

// Verify if the mock plugin was automatically registered
let registeredPlugin: MockPlugin? = PluginRegistry.plugin(for: MockPlugin.identifier)
XCTAssertNotNil(registeredPlugin)
}
#endif
}

#endif

0 comments on commit 2269248

Please sign in to comment.