Swiftpack.co - asharov/nimble-matchers as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by asharov.
asharov/nimble-matchers v0.6.0
Miscellaneous Nimble matchers
⭐️ 0
🕓 11 weeks ago
iOS macOS tvOS
.package(url: "https://github.com/asharov/nimble-matchers.git", from: "v0.6.0")

Miscellaneous Nimble Matchers

Build Status codecov License: MIT

I prefer to use Quick and Nimble for testing in my Swift projects. And sometimes I write my own matchers for Nimble when there is nothing suitable in the predefined matchers. This library collects some generally useful matchers I've written.

Codable Round-Trip Verification

When implementing a custom conformance to Codable, it's useful to verify that there are no mistakes in the implementation. A simple check that helps a lot is to verify that a decode-encode cycle and an encode-decode cycle produce output equivalent to the input. The two matchers roundTripFromJson and roundTripThroughJson perform these checks.

roundTripFromJson(throughType:) is used on Data values that represent an encoded JSON form. It will decode the Data into a value of the type given as a parameter, encode this value into JSON, and verify that the initial Data and the encoded result are equal as JSON.

roundTripThroughJson() is used on values of your own type. It will encode the value into JSON, decode the JSON into a value of the original type, and verify that the initial value and decoded value are equal. This requires the custom type to also conform to Equatable, so that the equality check can be performed.

As an example, the following code shows how to verify the Codable implementation for MyType.

struct MyType: Codable, Equatable { // Equatable conformance not needed for roundTripFromJson
	...
}

let value: MyType = ...
let encodedJson = "...".data(using: .utf8)!

expect(encodedJson).to(roundTripFromJson(throughType: MyType.self))
expect(value).to(roundTripThroughJson())

GitHub

link
Stars: 0
Last commit: 11 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Dependencies

Release Notes

3 years ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics