Last active
August 29, 2015 14:02
-
-
Save ianterrell/75df65794badba57628f to your computer and use it in GitHub Desktop.
Rspec style matchers for Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
protocol Matchable : Equatable, Comparable { } | |
class Matcher<T : Matchable> { | |
let subjectWrapper: T[] | |
var subject: T { | |
get { | |
return subjectWrapper[0] | |
} | |
set(newValue) { | |
subjectWrapper[0] = newValue | |
} | |
} | |
init(subject: T) { | |
subjectWrapper = [subject] | |
} | |
} | |
@infix func ==<T> (left: Matcher<T>, right: T) -> Bool { | |
XCTAssertEqual(right, left.subject, "expected \(left.subject) to equal \(right)") | |
return (left.subject == right) | |
} | |
@infix func !=<T> (left: Matcher<T>, right: T) -> Bool { | |
XCTAssertNotEqual(right, left.subject, "expected \(left.subject) not to equal \(right)") | |
return (left.subject != right) | |
} | |
@infix func <<T> (left: Matcher<T>, right: T) -> Bool { | |
XCTAssertLessThan(left.subject, right, "expected \(left.subject) to be less than \(right)") | |
return (left.subject < right) | |
} | |
@infix func <=<T> (left: Matcher<T>, right: T) -> Bool { | |
XCTAssertLessThanOrEqual(left.subject, right, "expected \(left.subject) to be less than or equal to \(right)") | |
return (left.subject < right) || (left.subject == right) | |
} | |
@infix func ><T> (left: Matcher<T>, right: T) -> Bool { | |
XCTAssertGreaterThan(left.subject, right, "expected \(left.subject) to be less than \(right)") | |
return (right < left.subject) | |
} | |
@infix func >=<T> (left: Matcher<T>, right: T) -> Bool { | |
XCTAssertGreaterThanOrEqual(left.subject, right, "expected \(left.subject) to be less than or equal to \(right)") | |
return (right < left.subject) || (left.subject == right) | |
} | |
extension Int : Matchable { | |
var should: Matcher<Int> { | |
return Matcher(subject: self) | |
} | |
} | |
extension UInt : Matchable { | |
var should: Matcher<UInt> { | |
return Matcher(subject: self) | |
} | |
} | |
extension Float : Matchable { | |
var should: Matcher<Float> { | |
return Matcher(subject: self) | |
} | |
} | |
extension Double : Matchable { | |
var should: Matcher<Double> { | |
return Matcher(subject: self) | |
} | |
} | |
extension String : Matchable { | |
var should: Matcher<String> { | |
return Matcher(subject: self) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
class IntMatcherTest : XCTestCase { | |
func testEqual() { | |
3.should == 3 | |
} | |
func testNotEqual() { | |
3.should != 5 | |
} | |
func testLessThan() { | |
3.should < 5 | |
} | |
func testLessThanOrEqualTo() { | |
3.should <= 3 | |
3.should <= 5 | |
} | |
func testGreaterThan() { | |
3.should > 2 | |
} | |
func testGreaterThanOrEqualTo() { | |
3.should >= 3 | |
3.should >= 2 | |
} | |
} | |
class UIntMatcherTest : XCTestCase { | |
func testEqual() { | |
UInt(3).should == UInt(3) | |
} | |
func testNotEqual() { | |
UInt(3).should != UInt(5) | |
} | |
func testLessThan() { | |
UInt(3).should < UInt(5) | |
} | |
func testLessThanOrEqualTo() { | |
UInt(3).should <= UInt(3) | |
UInt(3).should <= UInt(5) | |
} | |
func testGreaterThan() { | |
UInt(3).should > UInt(2) | |
} | |
func testGreaterThanOrEqualTo() { | |
UInt(3).should >= UInt(3) | |
UInt(3).should >= UInt(2) | |
} | |
} | |
class FloatMatcherTest : XCTestCase { | |
func testEqual() { | |
3.0.should == 3.0 | |
} | |
func testNotEqual() { | |
3.0.should != 5.0 | |
} | |
func testLessThan() { | |
3.0.should < 5.0 | |
} | |
func testLessThanOrEqualTo() { | |
3.0.should <= 3.0 | |
3.0.should <= 5.0 | |
} | |
func testGreaterThan() { | |
3.0.should > 2.0 | |
} | |
func testGreaterThanOrEqualTo() { | |
3.0.should >= 3.0 | |
3.0.should >= 2.0 | |
} | |
} | |
class DoubleMatcherTest : XCTestCase { | |
func testEqual() { | |
Double(3.0).should == Double(3.0) | |
} | |
func testNotEqual() { | |
Double(3.0).should != Double(5.0) | |
} | |
func testLessThan() { | |
Double(3.0).should < Double(5.0) | |
} | |
func testLessThanOrEqualTo() { | |
Double(3.0).should <= Double(3.0) | |
Double(3.0).should <= Double(5.0) | |
} | |
func testGreaterThan() { | |
Double(3.0).should > Double(2.0) | |
} | |
func testGreaterThanOrEqualTo() { | |
Double(3.0).should >= Double(3.0) | |
Double(3.0).should >= Double(2.0) | |
} | |
} | |
class StringMatcherTest : XCTestCase { | |
func testEqual() { | |
"3".should == "3" | |
} | |
func testNotEqual() { | |
"3".should != "5" | |
} | |
func testLessThan() { | |
"3".should < "5" | |
} | |
func testLessThanOrEqualTo() { | |
"3".should <= "3" | |
"3".should <= "5" | |
} | |
func testGreaterThan() { | |
"3".should > "2" | |
} | |
func testGreaterThanOrEqualTo() { | |
"3".should >= "3" | |
"3".should >= "2" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Thanks to @jspahrsummers for https://gist.github.com/jspahrsummers/a0fb1abbb71f598d712f to work around Swift's not yet supporting instance variables of generic type. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment