Skip to content

Instantly share code, notes, and snippets.

@wendorf
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wendorf/4598cf529804b18d90e4 to your computer and use it in GitHub Desktop.
Save wendorf/4598cf529804b18d90e4 to your computer and use it in GitHub Desktop.
fn main() {
it("works", ||{
expect("hello").to(eq("hello"));
expect("hello").to(eq("Hello"));
expect(1).to(eq(2));
expect(1).to(eq(1));
});
}
struct Expectation<T> {
actual: T
}
impl<T: Eq + std::fmt::Show> Expectation<T> {
fn to(&self, expected: EqMatcher<T>) {
let success = expected.test(&self.actual);
if !success {
println!("Error: expected {} to equal {}", self.actual, expected.expected);
}
}
}
trait Matcher<T: Eq + std::fmt::Show> {
fn test(&self, actual: &T) -> bool;
}
struct EqMatcher<T> {
expected: T
}
fn eq<T>(expected: T) -> EqMatcher<T> {
EqMatcher { expected: expected }
}
impl<T: Eq + std::fmt::Show> Matcher<T> for EqMatcher<T> {
fn test(&self, actual: &T) -> bool {
self.expected == *actual
}
}
fn expect<T>(actual: T) -> Expectation<T> {
Expectation { actual: actual }
}
fn it(description: &str, body: ||) {
let _ = description;
body();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment