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

Yet another introduction of Protocol extension-based matchers #480

Closed
wants to merge 5 commits into from

Conversation

mosamer
Copy link

@mosamer mosamer commented Nov 9, 2017

This PR is another attempt for #217, which is much less ambitious compared to #218

This PR introduces protocol extension-based matchers for Expectation. It only provides an infrastructure for the technique and would allow users to write their own custom matchers using it. Nimble matchers can be gradually migrated on later pull requests.

It allows the following code;

class NimblePlaygroundTests: XCTestCase {
    
    func testExample() {
        expect(1).to.equal(1)
        expect(1).notTo.equal(2)
        expect(1).toNot.equal(2)
        expect(1).toEventually.equal(1)
        expect(1).toNotEventually.equal(2)
        expect(1).toNotEventually.equal(2)
    }
}

extension ExtensibleExpecation where Type: Equatable {
    func equal(_ expectedValue: Type) {
        match {
            Predicate {actualExpression in
                let actualValue = try actualExpression.evaluate()
                return PredicateResult(
                    bool: actualValue == expectedValue,
                    message: .expectedCustomValueTo(
                        "equal \(stringify(expectedValue))",
                        stringify(actualValue)))
            }
        }
    }
}

Future Work:

  • Add test cases
  • Update readme
  • Migrate Nimble matchers to protocol extensions
  • Migrate Predicate helpers to ExtensibleExpectation
  • Better name for ExtensibleExpectation ¯_(ツ)_/¯


/// Expectation extension to test the actual value using a matcher to not match by checking continously at pollInterval until the timeout is reached.
public func toEventuallyNot(timeout: TimeInterval, pollInterval: TimeInterval) -> ExtensibleExpecation<T> {
return ExtensibleExpecation(base: self, style: .toNotMatch, isAsync: true, timeout: timeout, pollInterval: pollInterval)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line Length Violation: Line should be 120 characters or less: currently 128 characters (line_length)


/// Expectation extension to test the actual value using a matcher to match by checking continously at pollInterval until the timeout is reached.
public func toEventually(timeout: TimeInterval, pollInterval: TimeInterval) -> ExtensibleExpecation<T> {
return ExtensibleExpecation(base: self, style: .toMatch, isAsync: true, timeout: timeout, pollInterval: pollInterval)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line Length Violation: Line should be 120 characters or less: currently 125 characters (line_length)


public func match(description: String? = nil, _ predicateFactory: @escaping () -> Predicate<T>) {
if isAsync {
nimblePrecondition(base.expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line Length Violation: Line should be 120 characters or less: currently 126 characters (line_length)

}
}

//MARK: Async Expectation Extensions

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mark Violation: MARK comment should be in valid format. e.g. '// MARK: ...' or '// MARK: - ...' (mark)

}
}

//MARK: Expectation extensions

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mark Violation: MARK comment should be in valid format. e.g. '// MARK: ...' or '// MARK: - ...' (mark)

public var to: ExtensibleExpecation<T> {
return ExtensibleExpecation(base: self, style: .toMatch)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

timeout: timeout,
poll: pollInterval,
fnName: style == .toMatch ? "toEventually" : "toEventuallyNot")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

if isAsync {
nimblePrecondition(base.expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

self.timeout = timeout
self.pollInterval = pollInterval
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

) {
self.base = base
self.style = style

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

@mosamer
Copy link
Author

mosamer commented Jan 20, 2018

@jeffh any chance this could be reviewed anytime soon?

@jeffh
Copy link
Member

jeffh commented Feb 12, 2018

Hey @mosamer,

I haven’t had much free time to do any development for awhile (computer and apt issues).

Is there a reason to introduce a new extensible matched type over extending predicate (or even expectation)?

Also, could you please add tests, thanks!

@ikesyo ikesyo linked an issue Oct 8, 2020 that may be closed by this pull request
@ikesyo
Copy link
Member

ikesyo commented Oct 8, 2020

Closing as this is stale.

@ikesyo ikesyo closed this Oct 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Protocol Extension Discoverability
4 participants