Skip to content

Commit

Permalink
Handle register/unregister of custom type matcher and add its unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sabirvirtuoso authored and sabirvirtuoso committed Jun 1, 2016
1 parent e179a23 commit dd1855e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
47 changes: 43 additions & 4 deletions Example/Tests/MockMatcherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class AnotherDifferentClassForMatching {
}


// MARK:- Custom Type Matcher
// MARK:- Custom Type Matchers


public class CustomMatcher: TypeMatcher {
Expand All @@ -60,6 +60,21 @@ public class CustomMatcher: TypeMatcher {

}

public class AnotherCustomMatcher: TypeMatcher {

public func match(argument arg: Any, withArgument withArg: Any) -> Bool {
switch (arg, withArg) {
case ( _ as AnotherDifferentClassForMatching, _ as AnotherDifferentClassForMatching):
return true
case ( _ as DifferentClassForMatching, _ as DifferentClassForMatching):
return false
default:
return false
}
}

}


// MARK:- Test cases for `MockMatcher`

Expand Down Expand Up @@ -105,7 +120,20 @@ class MockMatcherTests: XCTestCase {
XCTAssertFalse(result)
}

func testCustomMatcherRegistration() {
func testCustomMatcherRegistrationFailsIfCustomTypeAlreadyExists() {
// Given
let sut = mockMatcher

sut.register(CustomMatcher.self, typeMatcher: CustomMatcher())

// When
let registered = sut.register(CustomMatcher.self, typeMatcher: CustomMatcher())

// Then
XCTAssertFalse(registered)
}

func testSuccessfulCustomMatcherRegistration() {
// Given
let sut = mockMatcher

Expand All @@ -119,13 +147,24 @@ class MockMatcherTests: XCTestCase {
XCTAssertTrue(result)
}

func testCustomMatcherUnregistration() {
func testCustomMatcherUnRegistrationFailsIfCustomTypeDoesNotExist() {
// Given
let sut = mockMatcher

// When
let unregistered = sut.unregister(AnotherCustomMatcher.self)

// Then
XCTAssertFalse(unregistered)
}

func testSuccessfulCustomMatcherUnregistration() {
// Given
let sut = mockMatcher

sut.unregister(CustomMatcher.self)

let classToMatch = DifferentClassForMatching()
let classToMatch = AnotherDifferentClassForMatching()

// When
let result = sut.match(arguments: classToMatch, withArguments: classToMatch)
Expand Down
12 changes: 7 additions & 5 deletions Mockit/Classes/MockMatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,26 @@ public class MockMatcher {
}
}

public func register(type: Any, typeMatcher: TypeMatcher) {
public func register(type: Any, typeMatcher: TypeMatcher) -> Bool {
let typeKey = String(type)

guard !typeExists(forKey: typeKey) else {
fatalError("Failed to register \(typeKey). It already exists and is registered.")
return false
}

typeMatchers[typeKey] = typeMatcher

return true
}

public func unregister(type: Any) {
public func unregister(type: Any) -> Bool {
let typeKey = String(type)

guard typeExists(forKey: typeKey) else {
fatalError("Failed to unregister \(typeKey). It does not exist or is not registered yet.")
return false
}

typeMatchers.removeValueForKey(typeKey)
return true
}

private func typeExists(forKey typeKey: String) -> Bool {
Expand Down

0 comments on commit dd1855e

Please sign in to comment.