DiUS / pact-consumer-swift

A Swift / ObjeciveC DSL for creating pacts.
MIT License
98 stars 43 forks source link

MockService's testFunction should `rethrows` #83

Closed huwr closed 4 years ago

huwr commented 4 years ago

Hi,

It'd be great if the testFunction from the MockService could be marked as rethrows.

This will allow us to call throwing functions inside testFunction, and for that error to be thrown in turn by the XCTest function. Errors thrown from XCTest funcs fail the test. It is likely that an exception inside the testFunction would be a good enough reason to fail the test. This means we won't have to catch the error if all we wanted to do was fail the test.

Example of usage would be:

    //Run the tests
    animalMockService.run { (testComplete) -> Void in
      let animals = try animalServiceClient.getAlligators()
      XCTAssertTrue(animals.count > 0)
      testComplete()
    }

Also means we don't need to use try!, which would crash the test.

The standard library uses rethrows inside sequence algorithms for things like flatMap: https://github.com/apple/swift/blob/master/stdlib/public/core/SequenceAlgorithms.swift#L756

More on rethrows: https://docs.swift.org/swift-book/ReferenceManual/Declarations.html and https://www.hackingwithswift.com/example-code/language/how-to-use-the-rethrows-keyword

andrewspinks commented 4 years ago

Sounds like a good idea @huwr . Although, I think that might break compatibility with swift 4.2, so we'd need to drop support for it in a new version with this change...?

huwr commented 4 years ago

I think rethrows has been around since before 4.2. The rethrows in flatMap has been there for 3 years: https://github.com/apple/swift/blame/master/stdlib/public/core/SequenceAlgorithms.swift#L756

I believe we can do it while maintaining compatibility.

andrewspinks commented 4 years ago

haha, right. I read "Swift version: 5.1" on https://www.hackingwithswift.com/example-code/language/how-to-use-the-rethrows-keyword and thought that was when it was introduced 😊