Latest 0.0.6
Homepage https://github.com/motocodeltd/MCAssertReflectiveEqual
License MIT
Platforms ios 8.0
Frameworks XCTest
Authors

CI Status
Version
License
Platform

MCAssertReflectiveEqual is a function that can be used to write swift test assertions. It works very similarly to XCTest’s XCTAssertEqual
but doesn’t require Equatable items and uses reflection instead.
Therefore you don’t need to write
neither equals functions that your production code does not require, nor assert multiple
individual fields making tests a chore.

MCAssertReflectiveEquals works on primitives, structs, classes and enums. It gives you a nice error message
when items do not match. It deeply compares items and handles recursive loops (A -> B -> A,
where A & B are objects and -> is a references). It just makes tests easier.

The development of MCAssertReflectiveEqual is described here.

Example

import XCTest
import MCAssertReflectiveEqual

private class ClassWithVal {
    var val: Int

    init(_ val: Int) {
        self.val = val
    }
}

class ClassWithValTest : XCTestCase {

    func testAreEqual() {
        let expected = ClassWithVal(1)
        let actual = ClassWithVal(1)

        MCAssertReflectiveEqual(expected, actual) 
        MCAssertReflectiveEqual([expected], [actual])

        MCAssertReflectiveEqual(expected, ClassWithVal(5)) //fails
    }

}

Custom Matchers

Sometimes simple reflective matching is not good enough. Imagine a complex data structure that contains a geographical coordinate, a CLLocation. We may not be interested that our expected value is identical to the value produced by the system under test, only that they are close enough. Here’s how we go about that:

class FavouriteLocation {
    let user: String
    let location: CLLocation

    init(user: String, location: CLLocation) {
        self.user = user
        self.location = location
    }
}

let camdenTown = CLLocation(latitude: 51.5390, longitude: 0.1426)

let closeToCamdenTown = CLLocation(latitude: 51.5391, longitude: 0.1427)

let matcher = matcherFor(CLLocation.self, { (expected, actual) in
                    return expected.distance(from: actual) < 100 //close enough
              }) 

MCAssertReflectiveEqual(FavouriteLocation(user: "bob", location: closeToCamdenTown), 
                        FavouriteLocation(user: "bob", location: camdenTown), 
                        matchers: [matcher])        

Provided matchers

For convenience, a matcher for doubles with defined accuracy is provided. Instantiate it with

 let accuracy = 0.001

 let matcher = matchDoubles(withAccuracy: accuracy)

 let expected = 0.01
 let actual = 0.0101

 MCAssertReflectiveEqual(expected, actual) //fails

 MCAssertReflectiveEqual(expected, actual, matchers: [matcher]) //passes

More examples in MCAssertReflectiveEqualTest.swift

Installation

MCAssertReflectiveEqual is available through CocoaPods. To install
it, simply add the following line to your Podfile. It really only makes sense to reference it in your test target.

use_frameworks!

pod "MCAssertReflectiveEqual"

It’s implemented in a single file so if you do not want to use CocoaPods it should be easy to integrate with your project.

Author

Stefanos Zachariadis, motocode ltd, first name at last name dot net, https://moto.co.de

License

MCAssertReflectiveEqual is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "MCAssertReflectiveEqual",
    "version": "0.0.6",
    "summary": "An equality assertion utility using reflection for swift tests.",
    "description": "Tests are good. Writing production code just for the purpose of testing is not good if you can avoid it.nMCAssertReflectiveEqual works just like XCTest's XCAssertEqual but doesn't require Equatable items - it uses reflection to evaluate if two structs, classes, enums, primitives etc are equal or not. Don't write that equals function in your production code if you don't need it. Don't assert multiple fields in your testsn - let MCAssertReflectiveEqual do the job for you and make tests easier to read write. Works similarly to other languages' assert reflective equals (assertReflectiveEquals).n Allows use fof custom matchers when reflection is too coarse a tool.",
    "homepage": "https://github.com/motocodeltd/MCAssertReflectiveEqual",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Stefanos Zachariadis, motocode ltd": "first name at last name dot net"
    },
    "source": {
        "git": "https://github.com/motocodeltd/MCAssertReflectiveEqual.git",
        "tag": "0.0.6"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "MCAssertReflectiveEqual/Classes/**/*",
    "frameworks": "XCTest",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This