Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c7d882
commit 634495e
Showing
5 changed files
with
60 additions
and
82 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,38 @@ | ||
use std::result::Result; | ||
|
||
pub fn assert_that<T: Clone, U: Matcher<T> + SelfDescribing>(actual: &T, matcher: U) { | ||
if !matcher.matches(actual) { | ||
let mut desc = Description::new(); | ||
|
||
desc | ||
.append_text("\nExpected: ") | ||
.append_description_of(&matcher as &SelfDescribing) | ||
.append_text("\n but: "); | ||
pub type MatchResult = Result<(), ~str>; | ||
|
||
matcher.describe_mismatch(actual, &mut desc); | ||
|
||
fail!("{}", desc.to_str()); | ||
} | ||
pub fn success() -> MatchResult { | ||
Ok(()) | ||
} | ||
|
||
pub struct Description { | ||
msg: ~str | ||
} | ||
|
||
impl Description { | ||
pub fn new() -> Description { | ||
Description { msg: ~"" } | ||
pub fn expect(predicate: bool, msg: ~str) -> MatchResult { | ||
if predicate { | ||
success() | ||
} | ||
|
||
pub fn append_text<'r>(&'r mut self, item: &str) -> &'r mut Description { | ||
self.msg = self.msg + item; | ||
self | ||
} | ||
|
||
pub fn append_description_of<'r>(&'r mut self, item: &SelfDescribing) -> &'r mut Description { | ||
item.describe_to(self); | ||
self | ||
else { | ||
Err(msg) | ||
} | ||
} | ||
|
||
pub fn to_str<'r>(&'r self) -> &'r str { | ||
self.msg.as_slice() | ||
pub fn assert_that<T: Clone, U: Matcher<T> + SelfDescribing>(actual: &T, matcher: U) { | ||
match matcher.matches(actual) { | ||
Ok(_) => return, | ||
Err(mismatch) => { | ||
let expected = matcher.describe(); | ||
fail!("\nExpected: {}\n but: {}", expected, mismatch); | ||
} | ||
} | ||
} | ||
|
||
pub trait SelfDescribing { | ||
|
||
fn describe_to(&self, desc: &mut Description) { | ||
desc.append_text(format!("{:?}", self)); | ||
fn describe(&self) -> ~str { | ||
format!("{:?}", self) | ||
} | ||
|
||
} | ||
|
||
pub trait Matcher<T> : SelfDescribing + Clone { | ||
fn matches(&self, actual: &T) -> bool; | ||
|
||
fn describe_mismatch(&self, _: &T, desc: &mut Description) { | ||
desc.append_text(format!("was {:?}", self)); | ||
} | ||
fn matches(&self, actual: &T) -> MatchResult; | ||
} |
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
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
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
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