Skip to content

Commit

Permalink
Merge pull request #17 from AirHelp/feature/result-refactor
Browse files Browse the repository at this point in the history
Added more advanced info about comparison result
  • Loading branch information
pkozielecki authored Oct 5, 2017
2 parents a73a5f1 + 4c71126 commit a5a5cc9
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 100 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: Swift
xcode_project: Mimus.xcodeproj
xcode_scheme: MimusTests
xcode_sdk: [iphonesimulator10.2]
osx_image: xcode8.2
xcode_sdk: [iphonesimulator11.0]
osx_image: xcode9.0

script:
- ./run_tests.sh
50 changes: 42 additions & 8 deletions Mimus/Source/Matcher.swift
Original file line number Diff line number Diff line change
@@ -1,35 +1,69 @@
internal struct MatchResult {

struct MismatchedComparison {

let expected: MockEquatable?

let actual: MockEquatable?
}

let matching: Bool

let mismatchedComparisons: [MismatchedComparison]

init(matching: Bool) {
self.matching = matching
self.mismatchedComparisons = []
}

init(matching: Bool, mismatchedComparisons: [MismatchedComparison]) {
self.matching = matching
self.mismatchedComparisons = mismatchedComparisons
}
}

internal class Matcher {

func match(expected: [MockEquatable?]?, actual: [MockEquatable?]?) -> Bool {
func match(expected: [MockEquatable?]?, actual: [MockEquatable?]?) -> MatchResult {
if expected == nil && actual == nil {
return true
return MatchResult(matching: true)
}

guard let expectedArguments = expected, let actualArguments = actual else {
return false
return MatchResult(matching: false)
}

if expectedArguments.count != actualArguments.count {
return false
return MatchResult(matching: false)
}

return match(expectedArguments: expectedArguments, actualArguments: actualArguments)
}

func match(expectedArguments: [MockEquatable?], actualArguments: [MockEquatable?]) -> Bool {
func match(expectedArguments: [MockEquatable?], actualArguments: [MockEquatable?]) -> MatchResult {
// At this point we're sure both arrays have the same count

var equal = true

var mismatchedComparisons: [MatchResult.MismatchedComparison] = []

for (index, item) in expectedArguments.enumerated() {
let internalEqual: Bool

let other = actualArguments[index]
if let unwrappedItem = item {
equal = (unwrappedItem.equalTo(other: other)) && equal
internalEqual = (unwrappedItem.equalTo(other: other))
} else {
equal = other == nil
internalEqual = other == nil
}

if !internalEqual {
mismatchedComparisons.append(MatchResult.MismatchedComparison(expected: item, actual: other))
}

equal = internalEqual && equal
}

return equal
return MatchResult(matching: equal, mismatchedComparisons: mismatchedComparisons)
}
}
19 changes: 7 additions & 12 deletions Mimus/Source/Mock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,15 @@ public extension Mock {
let callCandidates = storage.filter {
$0.identifier == callIdentifier
}
var matchCount = 0
var differentArgumentsMatchCount = 0

for candidate in callCandidates {
if mockMatcher.match(expected: arguments, actual: candidate.arguments) {
matchCount += 1
} else {
differentArgumentsMatchCount += 1
}
}

let matchResults = callCandidates.map({ mockMatcher.match(expected: arguments, actual: $0.arguments) })

let matchedResults = matchResults.filter({ $0.matching })
let mismatchedResults = matchResults.filter({ !$0.matching })

VerificationHandler.shared.verifyCall(callIdentifier: callIdentifier,
matchCount: matchCount,
differentArgumentsMatchCount: differentArgumentsMatchCount,
matchedResults: matchedResults,
mismatchedArgumentsResults: mismatchedResults,
mode: mode,
testLocation: testLocation)

Expand Down
14 changes: 7 additions & 7 deletions Mimus/Source/Verification/VerificationHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ internal class VerificationHandler {

static var shared: VerificationHandler = VerificationHandler()

func verifyCall(callIdentifier: String, matchCount: Int, differentArgumentsMatchCount: Int, mode: VerificationMode, testLocation: TestLocation) {
func verifyCall(callIdentifier: String, matchedResults: [MatchResult], mismatchedArgumentsResults: [MatchResult], mode: VerificationMode, testLocation: TestLocation) {
switch mode {
case .never:
assertNever(callIdentifier: callIdentifier,
matchCount: matchCount,
differentArgumentsMatchCount: differentArgumentsMatchCount,
matchCount: matchedResults.count,
differentArgumentsMatchCount: mismatchedArgumentsResults.count,
testLocation: testLocation)
case .atLeast(let count):
assertAtLeast(callIdentifier: callIdentifier,
times: count,
matchCount: matchCount,
differentArgumentsMatchCount: differentArgumentsMatchCount,
matchCount: matchedResults.count,
differentArgumentsMatchCount: mismatchedArgumentsResults.count,
testLocation: testLocation)
case .times(let count):
assert(callIdentifier: callIdentifier,
times: count,
matchCount: matchCount,
differentArgumentsMatchCount: differentArgumentsMatchCount,
matchCount: matchedResults.count,
differentArgumentsMatchCount: mismatchedArgumentsResults.count,
testLocation: testLocation)
}
}
Expand Down
Loading

0 comments on commit a5a5cc9

Please sign in to comment.