Skip to content
/ match Public

A simple and lightweight matching library for XCTest framework.

License

Notifications You must be signed in to change notification settings

mtynior/match

Repository files navigation

Match

A simple and lightweight matching library for XCTest framework.

Getting started

Swift Package Manager

You can add Match to your project by adding it as a dependency in your Package.swift file:

// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "MyProject",
    products: [
        .library(name: "MyProject", targets: ["MyProject"])
    ],
    dependencies: [
         .package(url: "https://github.com/mtynior/Match.git", .upToNextMajor(from: "0.5.0")),
    ],
    targets: [
        .target(name: "MyProject", dependencies: []),
        .testTarget(name: "MyProject", dependencies: ["Match"])
    ]
)

Xcode

Open your project in Xcode, then:

  1. Click File -> Add Packages,
  2. In the search bar type: https://github.com/mtynior/match.git and press Enter,
  3. Once Xcode finds the library, set Dependency rule to Up to next major version,
  4. Click Add Package,
  5. Select the Test Target (If you have multiple test targets, you can add the dependency manually from Xcode )
  6. Confirm selection by clicking on Add Package.

Writing tests

Now, when we have library integrated with our project, let's write some tests.

Imagine that we have a function that adds two numbers:

func sum(_ a: Int, _ b: Int) -> Int {
    a + b
}

Not we can test our function using XCTest Framework and the Match:

import XCTest
import Match
@testable MyProject

final class SumTests: XCTestCase {
    func testAddingTwoNumbers() {
        // given
        let expectedValue = 5
        
        // when
        let actualValue = sum(2, 3)

        // then
        expect(actualValue).toBeEqual(expectedValue) // Passes: "Expected: 5 is equal to received: 5"
    }
}

If we implemented the sum function correctly the test above will pass.

But let's see what will happen when we make a mistake in the sum implementation:

func sum(_ a: Int, _ b: Int) -> Int {
    a * b
}

Now, our test should fail:

import XCTest
import Match
@testable MyProject

final class SumTests: XCTestCase {
    func testAddingTwoNumbers() {
        // given
        let expectedValue = 5 
        
        // when
        let actualValue = sum(2, 3)

        // then
        expect(actualValue).toBeEqual(expectedValue) // Fails: "Expected: 5 to be equal to received: 6"
    }
}

That's it. Now, check how to use the rest of the Matchers and start testig.

Credits

Icon is made by Freepik and was available at FlatIcon.

License

Match is released under the MIT license. See LICENSE for details.