Skip to content

Instantly share code, notes, and snippets.

@lukeredpath
Last active August 29, 2015 14:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukeredpath/6af2f55f65faa52de74d to your computer and use it in GitHub Desktop.
Save lukeredpath/6af2f55f65faa52de74d to your computer and use it in GitHub Desktop.
My first experiments with Swift
// Playground - noun: a place where people can play
import Cocoa
protocol Matcher {
func matches(object: AnyObject) -> Bool
}
class EqualMatcher: Matcher {
let testObject: AnyObject
init(_ testObject: AnyObject) {
self.testObject = testObject
}
func matches(object: AnyObject) -> Bool {
return object.isEqualTo(testObject)
}
}
protocol ExpectationOperatorProxy {
func satisfiesMatchWith(matcher: Matcher) -> Bool
}
class Expectation {
let object: AnyObject
init(_ object: AnyObject) {
self.object = object
}
func to(matcher: Matcher) -> Bool {
return matcher.matches(object)
}
var to: ExpectationOperatorProxy {
get {
return ToOperatorProxy(self)
}
}
func notTo(matcher: Matcher) -> Bool {
return !to(matcher)
}
var notTo: ExpectationOperatorProxy {
get {
return NotToOperatorProxy(self)
}
}
var not: ExpectationOperatorProxy {
get {
return NotToOperatorProxy(self)
}
}
class AbstractExpectationOperatorProxy {
let expectation: Expectation
init(_ expectation: Expectation) {
self.expectation = expectation
}
}
class ToOperatorProxy: AbstractExpectationOperatorProxy, ExpectationOperatorProxy {
func satisfiesMatchWith(matcher: Matcher) -> Bool {
return expectation.to(matcher)
}
}
class NotToOperatorProxy: AbstractExpectationOperatorProxy, ExpectationOperatorProxy {
func satisfiesMatchWith(matcher: Matcher) -> Bool {
return expectation.notTo(matcher)
}
}
}
@infix func == (left: ExpectationOperatorProxy, right: AnyObject) -> Bool {
return left => EqualMatcher(right)
}
@infix func == (left: Expectation, right: AnyObject) -> Bool {
return left.to => EqualMatcher(right)
}
func expect(object: AnyObject) -> Expectation {
return Expectation(object)
}
func equal(object: AnyObject) -> Matcher {
return EqualMatcher(object)
}
operator infix => {}
@infix func => (left: ExpectationOperatorProxy, right: Matcher) -> Bool {
return left.satisfiesMatchWith(right)
}
@infix func => (left: Expectation, right: Matcher) -> Bool {
return left.to(right)
}
@infix func => (left: Expectation, right: Bool) -> Bool {
return left.to(EqualMatcher(right))
}
// simple style
expect(123).to(equal(123))
expect(123).notTo(equal(456))
// special operator style more readable without the wrapped function call?
expect(123).to => equal(123)
expect(456).notTo => equal(456)
// => is inspired by Ruby - perhaps it can be more concise for positives?
expect(123) => equal(123)
// we can wrap bools automatically
expect(true) => true
expect(true) => false
expect(false) => false
// use operator overloading for things like equality
expect(123).to == 123
expect(123).notTo == "foo"
// for positive operator matches we can omit the .to
expect(123) == 123
@lukeredpath
Copy link
Author

Originally used ~> as a custom operator but felt it was too close to the Swift -> returns operator. => is Ruby inspired.

@vfig
Copy link

vfig commented Jun 13, 2014

How about !=> as a the operator for negatives?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment