Skip to content

Commit

Permalink
Merge pull request #1 from AirHelp/additional-matcher-types
Browse files Browse the repository at this point in the history
Additional matcher types
  • Loading branch information
paweldudek committed Mar 23, 2017
2 parents ba09b83 + 5a6bba0 commit 27a4b94
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 25 deletions.
29 changes: 29 additions & 0 deletions Mimus/Source/Matchers/FoundationMatchers.swift
Expand Up @@ -27,3 +27,32 @@ extension NSNumber: MockEquatable {
return false
}
}

extension NSArray: MockEquatable {

public func equalTo(other: MockEquatable?) -> Bool {
if let array = self as? Array<Any> {
return array.equalTo(other: other)
} else {
return false
}
}
}

extension NSDictionary: MockEquatable {

public func equalTo(other: MockEquatable?) -> Bool {
if let dictionary = self as? Dictionary<AnyHashable, Any> {
return dictionary.equalTo(other: other)
} else {
return false
}
}
}

extension NSNull: MockEquatable {

public func equalTo(other: MockEquatable?) -> Bool {
return other == nil || other is NSNull
}
}
26 changes: 21 additions & 5 deletions Mimus/Source/Matchers/SwiftMatchers.swift
Expand Up @@ -72,10 +72,9 @@ extension Array: MockEquatable {

public func equalTo(other: MockEquatable?) -> Bool {
// Small hack to go around type system
let anyArray = self as Any
guard let selfArray = anyArray as? [MockEquatable?],
let otherArray = other as? [MockEquatable?],
selfArray.count == otherArray.count else {
let selfAny = self as Any?
guard let selfArray = selfAny as? [MockEquatable?],
let otherArray = other as? [MockEquatable?] else {
let expectedMirror = Mirror(reflecting: self as Any)
let location = TestLocation.currentTestLocation()
XCTFail("Attempted to compare unsupported array types. Expected type \(expectedMirror.subjectType)",
Expand All @@ -84,10 +83,19 @@ extension Array: MockEquatable {
return false
}

if selfArray.count != otherArray.count {
return false
}

var equal = true

for (index, item) in selfArray.enumerated() {
let otherItem = otherArray[index]

if areNullEquivalents(item: item, other: otherItem) {
continue
}

guard let expected = item, let actual = otherItem else {
return false
}
Expand All @@ -96,16 +104,24 @@ extension Array: MockEquatable {
}
return equal
}

private func areNullEquivalents(item: MockEquatable?, other: MockEquatable?) -> Bool {
if item == nil || item is NSNull {
return other == nil || other is NSNull
}
return false
}
}

extension Dictionary: MockEquatable {

public func equalTo(other: MockEquatable?) -> Bool {
// Small hack to go around type system
let anySelf: [AnyHashable: Any] = self
let anyOther = other as? [AnyHashable: Any]

guard let expected = anySelf as? [AnyHashable: MockEquatable],
let actual = other as? [AnyHashable: MockEquatable] else {
let actual = anyOther as? [AnyHashable: MockEquatable] else {
let location = TestLocation.currentTestLocation()
XCTFail("Attempted to compare unsupported dictionary types. Values should conform to \(MockEquatable.self)",
file: location.file,
Expand Down
131 changes: 125 additions & 6 deletions MimusTests/FoundationMatcherTests.swift
Expand Up @@ -34,7 +34,7 @@ class MatcherTests: XCTestCase {
let actual: NSString = "Another Fixture String"

let result = matcher.match(expected: [expected], actual: [actual])
XCTAssertFalse(result, "Expected strings to not match")
XCTAssertFalse(result, "Expected strings not to match")
}

func testStaticStringWithNSStringPassingInvocation() {
Expand All @@ -50,7 +50,7 @@ class MatcherTests: XCTestCase {
let actual: NSString = "Another Fixture String"

let result = matcher.match(expected: [expected], actual: [actual])
XCTAssertFalse(result, "Expected strings to not match")
XCTAssertFalse(result, "Expected strings not to match")
}

func testStringWithNSStringPassingInvocation() {
Expand All @@ -66,7 +66,7 @@ class MatcherTests: XCTestCase {
let actual: NSString = "Another Fixture String"

let result = matcher.match(expected: [expected], actual: [actual])
XCTAssertFalse(result, "Expected strings to not match")
XCTAssertFalse(result, "Expected strings not to match")
}

// MARK: NSNumber
Expand All @@ -84,7 +84,7 @@ class MatcherTests: XCTestCase {
let actual: NSNumber = 43

let result = matcher.match(expected: [expected], actual: [actual])
XCTAssertFalse(result, "Expected NSNumbers to not match")
XCTAssertFalse(result, "Expected NSNumbers not to match")
}

// MARK: NSError
Expand All @@ -102,7 +102,126 @@ class MatcherTests: XCTestCase {
let actual = NSError(domain: "Fixture Domain", code: 43)

let result = matcher.match(expected: [expected], actual: [actual])
XCTAssertFalse(result, "Expected NSErrors to not match")
XCTAssertFalse(result, "Expected NSErrors not to match")
}

// MARK: NSArray

func testNSArrayPassingInvocation() {
let expected = NSArray(objects: NSString(string: "Fixture String"), NSNumber(floatLiteral: 0.5))
let actual = NSArray(objects: NSString(string: "Fixture String"), NSNumber(floatLiteral: 0.5))

let result = matcher.match(expected: [expected], actual: [actual])
XCTAssertTrue(result, "Expected arrays to match")
}

func testNSArrayFailingInvocation() {
let expected = NSArray(objects: NSNumber(floatLiteral: 0.5), NSString(string: "Fixture String"))
let actual = NSArray(objects: NSString(string: "Fixture String"), NSNumber(floatLiteral: 0.5))

let result = matcher.match(expected: [expected], actual: [actual])
XCTAssertFalse(result, "Expected arrays not to match")
}

func testIncompatibleSizesNSArrayFailingInvocation() {
let expected = NSArray(objects: NSNumber(floatLiteral: 0.5))
let actual = NSArray(objects: NSString(string: "Fixture String"), NSNumber(floatLiteral: 0.5))

let result = matcher.match(expected: [expected], actual: [actual])
XCTAssertFalse(result, "Expected arrays not to match")
}

func testNestedNSArrayPassingInvocation() {
let nestedExpectedArray = NSArray(object: NSNumber(floatLiteral: 0.5))
let nestedActualArray = NSArray(object: NSNumber(floatLiteral: 0.5))
let expected = NSArray(objects: NSNumber(floatLiteral: 0.5), nestedExpectedArray)
let actual = NSArray(objects: NSNumber(floatLiteral: 0.5), nestedActualArray)

let result = matcher.match(expected: [expected], actual: [actual])
XCTAssertTrue(result, "Expected arrays to match")
}

func testNestedNSArrayFailingInvocation() {
let nestedExpectedArray = NSArray(objects: NSNumber(floatLiteral: 0.5), NSArray(objects: NSNumber(floatLiteral: 0.5)))
let nestedActualArray = NSArray(object: NSNumber(floatLiteral: 0.5))
let expected = NSArray(objects: NSNumber(floatLiteral: 0.5), nestedExpectedArray)
let actual = NSArray(objects: NSNumber(floatLiteral: 0.5), nestedActualArray)

let result = matcher.match(expected: [expected], actual: [actual])
XCTAssertFalse(result, "Expected arrays not to match")
}

// MARK: NSDictionary

func testNSDictionaryPassingInvocation() {
let expected = NSDictionary(dictionaryLiteral: (NSString(string: "Fixture Key 1"), NSString(string: "Fixture Value")), (NSString(string: "Fixture Key 2"), NSNumber(floatLiteral: 0.5)))
let actual = NSDictionary(dictionaryLiteral: (NSString(string: "Fixture Key 1"), NSString(string: "Fixture Value")), (NSString(string: "Fixture Key 2"), NSNumber(floatLiteral: 0.5)))

let result = matcher.match(expected: [expected], actual: [actual])
XCTAssertTrue(result, "Expected dictionaries to match")
}

func testNSDictionaryFailingInvocation() {
let expected = NSDictionary(dictionaryLiteral: (NSString(string: "Fixture Key 1"), NSString(string: "Fixture Value")), (NSString(string: "Fixture Key 2"), NSNumber(floatLiteral: 0.5)))
let actual = NSDictionary(dictionaryLiteral: (NSString(string: "Fixture Key 2"), NSString(string: "Fixture Value")), (NSString(string: "Fixture Key 1"), NSNumber(floatLiteral: 0.5)))

let result = matcher.match(expected: [expected], actual: [actual])
XCTAssertFalse(result, "Expected dictionaries not to match")
}

func testIncompatibleSizesNSDictionaryFailingInvocation() {
let expected = NSDictionary(dictionaryLiteral: (NSString(string: "Fixture Key 1"), NSString(string: "Fixture Value")))
let actual = NSDictionary(dictionaryLiteral: (NSString(string: "Fixture Key 2"), NSString(string: "Fixture Value")), (NSString(string: "Fixture Key 1"), NSNumber(floatLiteral: 0.5)))

let result = matcher.match(expected: [expected], actual: [actual])
XCTAssertFalse(result, "Expected dictionaries not to match")
}

func testNestedNSDictionaryPassingInvocation() {
let nestedExpectedDictionary = NSDictionary(dictionaryLiteral: (NSString(string: "Fixture Key 1"), NSString(string: "Fixture Value")), (NSString(string: "Fixture Key 2"), NSNumber(floatLiteral: 0.5)))
let nestedActualDictionary = NSDictionary(dictionaryLiteral: (NSString(string: "Fixture Key 1"), NSString(string: "Fixture Value")), (NSString(string: "Fixture Key 2"), NSNumber(floatLiteral: 0.5)))
let expected = NSDictionary(dictionaryLiteral: (NSString(string: "Fixture Key 1"), nestedExpectedDictionary))
let actual = NSDictionary(dictionaryLiteral: (NSString(string: "Fixture Key 1"), nestedActualDictionary))

let result = matcher.match(expected: [expected], actual: [actual])
XCTAssertTrue(result, "Expected dictionaries to match")
}

func testNestedNSDictionaryFailingInvocation() {
let nestedExpectedDictionary = NSDictionary(dictionaryLiteral: (NSString(string: "Fixture Key 1"), NSString(string: "Fixture Value")), (NSString(string: "Fixture Key 2"), NSNumber(floatLiteral: 0.5)))
let nestedActualDictionary = NSDictionary(dictionaryLiteral: (NSString(string: "Fixture Key 1"), NSString(string: "Fixture Value")), (NSString(string: "Fixture Key 2"), NSNumber(floatLiteral: 0.5)))
let expected = NSDictionary(dictionaryLiteral: (NSString(string: "Fixture Key 1"), nestedExpectedDictionary))
let actual = NSDictionary(dictionaryLiteral: (NSString(string: "Fixture Key 1"), nestedActualDictionary), (NSString(string: "Fixture Key 2"), nestedExpectedDictionary))

let result = matcher.match(expected: [expected], actual: [actual])
XCTAssertTrue(result, "Expected dictionaries not to match")
}

// MARK: More Complicated Scenarios

func testPassingInvocation() {
let nestedDictionary = NSDictionary(dictionaryLiteral: (NSString(string: "Fixture Key 1"), NSString(string: "Fixture Value")), (NSString(string: "Fixture Key 2"), NSNull()))
let nestedArray = NSArray(object: NSNumber(floatLiteral: 0.5))
let expected = NSArray(objects: NSString(string: "Fixture String"), NSNull(), NSNumber(integerLiteral: 42), NSNumber(floatLiteral: 1.0), nestedArray, nestedDictionary)
let actual = NSArray(objects: NSString(string: "Fixture String"), NSNull(), NSNumber(integerLiteral: 42), NSNumber(floatLiteral: 1.0), nestedArray, nestedDictionary)
let result = matcher.match(
expected: [expected],
actual: [actual]
)

XCTAssertTrue(result, "Expected elements to match")
}

func testFailingInvocation() {
let nestedDictionary = NSDictionary(dictionaryLiteral: (NSString(string: "Fixture Key 1"), NSString(string: "Fixture Value")), (NSString(string: "Fixture Key 2"), NSNumber(floatLiteral: 0.5)))
let nestedArray = NSArray(object: NSNumber(floatLiteral: 0.5))
let expected = NSArray(objects: NSString(string: "Fixture String"), NSNull(), NSNumber(integerLiteral: 42), NSNumber(floatLiteral: 1.0), nestedArray, nestedDictionary)
let actual = NSArray(objects: NSString(string: "Fixture String"), NSNull(), nestedArray, nestedDictionary)
let result = matcher.match(
expected: [expected],
actual: [actual]
)

XCTAssertFalse(result, "Expected elements to match")
}

}

0 comments on commit 27a4b94

Please sign in to comment.