I have developed a Swift macro called @CodableInit in the SwiftCodableMacro module, and I’m able to use it successfully in my main project. Here’s an example usage:
import SwiftCodableMacro
@CodableInit // This is for Codable macros
public class ErrorMonitoringWebPlugin {
public var identifier: UUID = UUID()
// MARK: - Codable
required public init(from decoder:Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
identifier = try values.decode(UUID.self, forKey: .identifier)
}
}
However, when I try to write a unit test for the ErrorMonitoringWebPlugin class, I encounter an issue. Here's the test case:
func testCodableSubjectIdentifierShouldEqualDecodedSubjectIdentifier() {
self.measure {
let encoder = JSONEncoder()
let data = try? encoder.encode(subject)
//Here I am getting this error
Class 'JSONEncoder' requires that 'ErrorMonitoringWebPlugin' conform to 'Encodable'
let decoder = JSONDecoder()
let decodedSubject = try? decoder.decode(ErrorMonitoringWebPlugin.self, from: data!)
XCTAssertEqual(subject.identifier, decodedSubject?.identifier)
}
}
The compiler throws an error saying: Class 'JSONEncoder' requires that 'ErrorMonitoringWebPlugin' conform to 'Encodable' Even though the @CodableInit macro is supposed to generate conformance, it seems that this macro-generated code is not visible or active inside the test target.
How can I ensure that the @CodableInit macro (from SwiftCodableMacro) is correctly applied and recognized within the XCTest target of my main project?