Skip to content

Instantly share code, notes, and snippets.

@sebastianbenz
Created May 2, 2013 10:31
Show Gist options
  • Save sebastianbenz/5501388 to your computer and use it in GitHub Desktop.
Save sebastianbenz/5501388 to your computer and use it in GitHub Desktop.
package matchers
describe "Matchers"{
val personOfAgeFour = new Person("A", 4)
val personOfAgeFive = new Person("B", 5)
context "Hamcrest"{
fact "Example:"{
personOfAgeFour should be underAge
personOfAgeFour should be olderThan(personOfAgeFive)
/* Fails with:
java.lang.AssertionError:
Expected personOfAgeFour should be olderThan(personOfAgeFive) but
personOfAgeFour is <A(4)>
olderThan(personOfAgeFive) is <older than>
personOfAgeFive is <B(5)>
*/
}
val underAge = matches("underage")[Person p | p.age < 18]
def olderThan(Person p){
matches("older than")[Person other | other.age > p.age]
}
val olderThan = matches("olderThan")[Person p | p.age < 18]
}
context "Lambda Expression"{
fact "Example"{
personOfAgeFour should be underAge
personOfAgeFour should be olderThan(personOfAgeFive)
/* Fails with:
java.lang.AssertionError:
Expected personOfAgeFour should be olderThan(personOfAgeFive) but
personOfAgeFour is <A(4)>
personOfAgeFive is <B(5)>
*/
}
def olderThan(Person p){
[Person other | other.age > p.age]
}
def underAge() {
[Person p | p.age < 18]
}
def <T> should_be(T obj, Functions$Function1<T, Boolean> func){
func.apply(obj)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment