Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find a way to allow usage of Matcher class within custom matcher implementations. #10

Open
paweldudek opened this issue Mar 27, 2017 · 0 comments

Comments

@paweldudek
Copy link
Collaborator

paweldudek commented Mar 27, 2017

Right now custom matchers might require complicated logic as they cannot leverage internal components of Mimus.

For instance, consider following scenario. We're trying to write a custom matcher for URLRequest. In that matcher we need to compare URL? and [String: String]?.

That means we need to do the usual dance of unwrapping, checking for equality etc:

public func equalTo(other: MockEquatable?) -> Bool {
    if let otherURLRequest = other as? URLRequest {

        var urlsMatch = false
        var headersMatch = false

        if self.url == nil, otherURLRequest.url == nil {
            urlsMatch = true
        } else {
            if let actualURL = otherURLRequest.url, let expectedURL = self.url {
                urlsMatch = actualURL == expectedURL
            }
        }

        if self.allHTTPHeaderFields == nil, otherURLRequest.allHTTPHeaderFields == nil {
            headersMatch = true
        } else {
            if let actualHeaders = otherURLRequest.allHTTPHeaderFields, let expectedHeaders = self.allHTTPHeaderFields {
                headersMatch = actualHeaders == expectedHeaders
            }
        }

        //TODO: Move this to Mimus? Find a way we can leverage Mimus.Matcher
        return urlsMatch && headersMatch
    }

    return false
}

This could be greatly simplified by using Mimus Matcher class:

public func equalTo(other: MockEquatable?) -> Bool {
    if let otherURLRequest = other as? URLRequest {
        let matcher: Mimus.Matcher = // Get the matcher from somewhere?

        return matcher.match(expected: [self.url, self.allHTTPHeaderFields], actual: [otherURLRequest.url, otherURLRequest.allHTTPHeaderFields])
    }

    return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant