mockingbird icon indicating copy to clipboard operation
mockingbird copied to clipboard

Always getting "Use of protocol 'CaseIterableDefaultsLast' as a type must be written 'any CaseIterableDefaultsLast'" Compiler Error

Open nooglersoon opened this issue 7 months ago • 0 comments

New Issue Checklist

Overview

I created a new protocol:

/// An enum where the last case value is "unknown" and can be used as a default catch-all.
public protocol CaseIterableDefaultsLast: Decodable & CaseIterable & RawRepresentable {}

public extension CaseIterableDefaultsLast where RawValue: Decodable, AllCases: BidirectionalCollection {
    /// Initializes an enum such that if a known raw value is found, then it is decoded.
    /// Otherwise the last case ("unknown") is used.
    /// - Parameter decoder: A decoder.
    init(from decoder: Decoder) throws {
        if let value = try Self(rawValue: decoder.singleValueContainer().decode(RawValue.self)) {
            self = value
        } else if let lastValue = Self.allCases.last {
            self = lastValue
        } else {
            throw DecodingError.valueNotFound(
                Self.Type.self,
                .init(codingPath: decoder.codingPath, debugDescription: "CaseIterableDefaultsLast")
            )
        }
    }
}

But when run the test, the generated files got compiler error: Use of protocol 'CaseIterableDefaultsLast' as a type must be written 'any CaseIterableDefaultsLast' Compiler Error

Example

If the generator produces code that is malformed or does not compile, please provide:

  1. A minimal example of the original source
  2. The actual mocking code generated
@available(*, unavailable, message: "'CaseIterableDefaultsLast' inherits from the externally-defined type 'Decodable & CaseIterable & RawRepresentable' which needs to be declared in a supporting source file")
public func mock(_ type: CaseIterableDefaultsLast.Protocol, file: StaticString = #file, line: UInt = #line) -> CaseIterableDefaultsLastMock {
  fatalError()
}
Screenshot 2024-01-25 at 16 41 03

I always getting this error that turns my unit test cannot be tested. Please give me suggestion how to solve this problem? Or maybe need some enhancement from the Mockingbirds?

Expected Behavior

@available(*, unavailable, message: "'CaseIterableDefaultsLast' inherits from the externally-defined type 'Decodable & CaseIterable & RawRepresentable' which needs to be declared in a supporting source file")
public func mock(_ type: any CaseIterableDefaultsLast, file: StaticString = #file, line: UInt = #line) -> CaseIterableDefaultsLastMock {
  fatalError()
}

Environment

  • Mockingbird CLI version (mockingbird version)
  • Xcode and Swift version (swift --version)
  • Package manager (CocoaPods, Carthage, SPM project, SPM package)
  • Unit testing framework (XCTest, Quick/Nimble)
  • Custom configuration
    • [ ] Mockingbird ignore files
    • [ ] Supporting source files

nooglersoon avatar Jan 25 '24 09:01 nooglersoon