Coder Social home page Coder Social logo

ytyubox / difference Goto Github PK

View Code? Open in Web Editor NEW

This project forked from krzysztofzablocki/difference

0.0 1.0 0.0 2.67 MB

Simple way to identify what is different between 2 instances of any type. Must have for TDD.

Home Page: http://merowing.info

License: MIT License

Ruby 3.07% Swift 96.93%

difference's Introduction

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: ", ")))
    }
}

difference's People

Contributors

krzysztofzablocki avatar mr-v avatar jeehut avatar ben-henshall avatar gregorymaks avatar arangato avatar rag0n avatar tahirmt avatar thepsguy avatar zeveisenberg avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.