Swiftpack.co -  krzysztofzablocki/Difference as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
krzysztofzablocki/Difference
Simple way to identify what is different between 2 instances of any type. Must have for TDD.
.package(url: "https://github.com/krzysztofzablocki/Difference.git", from: "1.0.0")

Version License Platform

Difference

Better way to identify what's different between 2 instances.

Have you ever written tests? Usually they use equality asserts, e.g. XCTAssertEqual, what happens if the objects aren't equal? Xcode throws a wall of text at you:

This forces you to manually scan the text and try to figure out exactly whats wrong, what if instead you could just learn which property is different?

Installation

CocoaPods

Add pod 'Difference' to your Podfile.

Carthage

Add github "krzysztofzablocki/Difference" to your Cartfile.

SwiftPM

Add .package(name: "Difference", url: "https://github.com/krzysztofzablocki/Difference.git", .branch("master")), dependency in your Package manifest.

Using lldb

Write the following to see the difference between 2 instances:

po dumpDiff(expected, received)

Integrate with XCTest

Add this to your test target:

public func XCTAssertEqual<T: Equatable>(_ expected: @autoclosure () throws -> T, _ received: @autoclosure () throws -> T, file: StaticString = #filePath, line: UInt = #line) {
    do {
        let expected = try expected()
        let received = try received()
        XCTAssertTrue(expected == received, "Found difference for \n" + diff(expected, received).joined(separator: ", "), file: file, line: line)
    }
    catch {
        XCTFail("Caught error while testing: \(error)", file: file, line: line)
    }
}

Replace #filePath with #file if you're using Xcode 11 or earlier.

Integrate with Quick

Add this to your test target:

public func equalDiff<T: Equatable>(_ expectedValue: T?) -> Predicate<T> {
    return Predicate.define { actualExpression in
        let receivedValue = try actualExpression.evaluate()

        if receivedValue == nil {
            var message = ExpectationMessage.fail("")
            if let expectedValue = expectedValue {
                message = ExpectationMessage.expectedCustomValueTo("equal <\(expectedValue)>", "nil")
            }
            return PredicateResult(status: .fail, message: message)
        }
        if expectedValue == nil {
            return PredicateResult(status: .fail, message: ExpectationMessage.fail("").appendedBeNilHint())
        }

        return PredicateResult(bool: receivedValue == expectedValue, message: ExpectationMessage.fail("Found difference for " + diff(expectedValue, receivedValue).joined(separator: ", ")))
    }
}

Integrate with The Composable Architecture

If you are using The Composable Architecture nameLabels configuration to get a diff that's more appropiate for reducer instrumentation

diff(oldState, newState, indentationType: .pipe, nameLabels: .comparing)

You can use this function in your own variant of ReducerInstrumentation code based on this

That way your diffs will look more like this:

Received action:
  AppAction.home(.howTo(.setSelectedSlide))
State:
home:
|	selectedHowTo:
|	|	selectedSlide:
|	|	|	Current: 8mnkni91h4fe
|	|	|	Previous: exei4wpqsmdk

GitHub

link
Stars: 900
Last commit: 8 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

1.0.0
8 weeks ago
  • The library is stable so kicking off to 1.0.0
  • Adds ability to specify name labels so that the library can be used for different purposes, like TCA instrumentation

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