-
Notifications
You must be signed in to change notification settings - Fork 91
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
Showing
5 changed files
with
186 additions
and
167 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
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,45 +1,55 @@ | ||
|
||
pub fn assert_eq[T : Debug + Eq](a : T, b : T) { | ||
fn debug_string[T : Debug](t : T) -> String { | ||
let buf = Buffer::make(50) | ||
t.debug_write(buf) | ||
buf.to_string() | ||
} | ||
|
||
pub fn assert_eq[T : Debug + Eq](a : T, b : T) -> Result[Unit,String] { | ||
if a == b { | ||
Ok(()) | ||
} else { | ||
let a = debug_string(a) | ||
let b = debug_string(b) | ||
Err("assertion failed for `\(a) == \(b)`") | ||
} | ||
} | ||
|
||
pub fn assert_ne[T : Debug + Eq](a : T, b : T) -> Result[Unit,String] { | ||
if a != b { | ||
let buf = Buffer::make(100) | ||
"assertion failed: ".debug_write(buf) | ||
a.debug_write(buf) | ||
" == ".debug_write(buf) | ||
b.debug_write(buf) | ||
"\n".debug_write(buf) | ||
abort(buf.to_string()) | ||
Ok(()) | ||
} else { | ||
let a = debug_string(a) | ||
let b = debug_string(b) | ||
Err("assertion failed for `\(a) == \(b)`") | ||
} | ||
} | ||
|
||
pub fn assert_false(b : Bool) { | ||
if b { | ||
let buf = Buffer::make(100) | ||
"assertion failed: ".debug_write(buf) | ||
b.debug_write(buf) | ||
" == false".debug_write(buf) | ||
"\n".debug_write(buf) | ||
abort(buf.to_string()) | ||
pub fn assert_false(x : Bool) -> Result[Unit,String] { | ||
if x == false { | ||
Ok(()) | ||
} else { | ||
Err("assert_false failed") | ||
} | ||
} | ||
|
||
pub fn assert_true(b : Bool) { | ||
if not (b) { | ||
let buf = Buffer::make(100) | ||
buf.write_string("assertion failed: ") | ||
b.debug_write(buf) | ||
buf.write_string(" == true\n") | ||
abort(buf.to_string()) | ||
pub fn assert_true(x : Bool) -> Result[Unit,String] { | ||
if x { | ||
Ok(()) | ||
} else { | ||
Err("assert_true failed") | ||
} | ||
} | ||
|
||
test "assert_true.true" { | ||
assert_true(true) | ||
assert_true(true)? | ||
} | ||
|
||
test "assert_false.false" { | ||
assert_false(false) | ||
assert_false(false)? | ||
} | ||
|
||
test "assert_eq.eq" { | ||
assert_eq(1, 1) | ||
assert_eq(1, 1)? | ||
assert_eq("123","123")? | ||
} |
Oops, something went wrong.