Kolos65 / Mockable

A Swift macro driven auto-mocking library.
MIT License
199 stars 14 forks source link

Extend relaxed mode to custom types #36

Closed Kolos65 closed 3 months ago

Kolos65 commented 3 months ago

0.0.5 introduced relaxed mode for literal expressible types.

There are two issues with the current implementation though:

This PR replaces the literal expressible overloads of the mock function with a single overload that uses a custom Mockable protocol to infer auto-mockable types.

Mockable in combination with the relaxed Mockable option of MockerPolicycan be used to set an implicit return value for custom types:

struct Car {
    var name: String
    var seats: Int
}

extension Car: Mockable {
    static var mock: Car {
        Car(name: "Mock Car", seats: 4)
    }

    // Defaults to [mock] but we can
    // provide a custom array of cars:
    static var mocks: [Car] {
        [
            Car(name: "Mock Car 1", seats: 4),
            Car(name: "Mock Car 2", seats: 4)
        ]
    }
}

@Mockable
protocol CarService {
    func getCar() -> Car
    func getCars() -> [Car]
}

func testCarService() {
    func test() {
        let mock = MockCarService(policy: .relaxedMockable)
        // Implictly mocked without a given registration:
        let car = mock.getCar()
        let cars = mock.getCars()
    }
}