Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 of 4 tasks
nooglersoon opened this issue Jan 25, 2024 · 0 comments
Labels
generator bug Causes malformed generated code

Comments

@nooglersoon
Copy link

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 nooglersoon added the generator bug Causes malformed generated code label Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
generator bug Causes malformed generated code
Projects
None yet
Development

No branches or pull requests

1 participant