Match - A lightweight matching library for XCTest Framework

Match - A lightweight matching library for XCTest Framework

I am a big fan of unit tests. I enjoy writing them, but there is one thing I don't like - assertions. I don't know why, but they feel primitive, unintuitive, and in general, hard to work with. I know that there are libraries, like Quick and Nimble, that allow writing tests in more human-readable form (BDD). Those are great libraries, but on the other hand, they are big, and adding them to the small Swift Project doesn't feel right.
Being frustrated by this, I started asking myself: how hard it would be to write my own matching library. It turned out it's not, and after a couple of afternoons, I created Match.

GitHub - mtynior/match: A simple and lightweight matching library for XCTest framework.
A simple and lightweight matching library for XCTest framework. - GitHub - mtynior/match: A simple and lightweight matching library for XCTest framework.

Match allows writing intuitive expectations like in other unit testing libraries. At the moment, it has a basic set of matchers, but they allow to verify most of the cases. From simple comparisons:

func testEquality() {
    let actual = 2
    expect(actual).toBeEqual(5) // Fails
    expect(actual).toBeEqual(2) // Passes
}

To more complex scenarios:

struct RefreshOptions: OptionSet {
    let rawValue: Int8

    static let hourly = RefreshOptions(rawValue: 1 << 0)
    static let daily = RefreshOptions(rawValue: 1 << 1)
    static let weekly = RefreshOptions(rawValue: 1 << 2)
    static let monthly = RefreshOptions(rawValue: 1 << 3)
    static let yearly = RefreshOptions(rawValue: 1 << 4)
}

func testRefreshOptions() {
    let set: RefreshOptions = [.hourly, .daily, .weekly, .monthly]

    expect(set).toContain(.daily) // Passes
    expect(set).toContain([.hourly, .daily]) // Passes
    expect(set).toContain(.yearly) // Fails
    expect(set).toContain([.daily, .yearly]) // Fails
}

I had a lot of fun writing it, and I intend to use it in my future projects. I also hope it will be helpful to other developers.


Comments

Anything interesting to share? Write a comment.