Skip to content

Instantly share code, notes, and snippets.

@MP0w
Created November 16, 2016 11:52
Show Gist options
  • Save MP0w/7e8c981ed1e35b3d2fa8a51b49b10d71 to your computer and use it in GitHub Desktop.
Save MP0w/7e8c981ed1e35b3d2fa8a51b49b10d71 to your computer and use it in GitHub Desktop.
ScrollMatcher
import Foundation
import UIKit
import Nimble
func scroll(_ axis: UILayoutConstraintAxis) -> ScrollMatcher {
return ScrollMatcher(axis: axis)
}
struct ScrollMatcher: Matcher {
typealias ValueType = UIScrollView
let axis: UILayoutConstraintAxis
private func composeFailureMessage(_ message: FailureMessage, negated: Bool) {
message.expected = "Expected scrollView"
let negation = negated ? "not" : ""
let direction = axis == .vertical ? "vertically" : "horizontally"
message.to = "to \(negation) scroll \(direction)"
message.postfixMessage = ""
message.actualValue = nil
}
private func canScroll(_ actualExpression: Expression<UIScrollView>) throws -> Bool {
guard let scrollView = try actualExpression.evaluate() else {
return false
}
if axis == .vertical {
return scrollView.contentSize.height > scrollView.bounds.height
} else {
return scrollView.contentSize.width > scrollView.bounds.width
}
}
func matches(_ actualExpression: Expression<UIScrollView>,
failureMessage: FailureMessage) throws -> Bool {
composeFailureMessage(failureMessage, negated: false)
return try canScroll(actualExpression)
}
func doesNotMatch(_ actualExpression: Expression<UIScrollView>,
failureMessage: FailureMessage) throws -> Bool {
composeFailureMessage(failureMessage, negated: true)
return try !canScroll(actualExpression)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment