Created
January 28, 2013 09:13
-
-
Save mastoj/4654105 to your computer and use it in GitHub Desktop.
sml test setup
This file contains hidden or 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
val remove_card_tests = start_string | |
val should_thrown_exception_if_not_in_list = | |
assert_raise ((fn y => remove_card ([(Spades, Jack), (Hearts, Num 5)], (Diamonds, Num 4), CardNotFound)), CardNotFound) | |
val removes_first_entry_if_in_list = | |
assert_equal([(Diamonds, Num 8), (Spades, Jack), (Hearts, Ace)], | |
remove_card ([(Hearts, Ace), (Diamonds, Num 8), (Spades, Jack), (Hearts, Ace)], (Hearts, Ace), CardNotFound), | |
card_list_formatter) |
This file contains hidden or 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
fun assert_equal (expected, actual, formatter) = | |
if expected = actual | |
then " -- PASS" | |
else "Expected: " ^ formatter (expected) ^ ", but was: " ^ formatter (actual) ^ " -- FAIL" | |
fun assert_bool(expected, actual) = | |
if expected = actual then " -- PASS" | |
else "Expected: " ^ Bool.toString (expected) ^ ", but was: " ^ Bool.toString (actual) ^ " -- FAIL" | |
fun assert_true (actual) = assert_bool(true, actual) | |
fun assert_false (actual) = assert_bool(false, actual) | |
fun assert_raise (f, e) = | |
( | |
f(); | |
"Expected: " ^ exnName (e) ^ " to be thrown, but it wasn't -- FAIL" | |
) | |
handle e' => | |
if exnName e = exnName e' then " -- PASS" | |
else "Expected: " ^ exnName (e) ^ " to be thrown but " ^ exnName (e') ^ " was thrown -- FAIL" | |
fun some_list_formatter some_list = | |
case some_list of | |
NONE => "NONE" | |
| SOME [] => "" | |
| SOME (x::[]) => x | |
| SOME (x::rest) => x ^ ", " ^ some_list_formatter (SOME rest) | |
fun string_list_formatter list = | |
case list of | |
[] => "EMPTY" | |
| x::[] => x | |
| x::rest => x ^ ", " ^ string_list_formatter (rest) | |
fun full_name_formatter {first, middle, last} = | |
"First: " ^ first ^ ", Middle: " ^ middle ^ ", Last: " ^ last | |
fun full_name_list_formatter list = | |
case list of | |
[] => "" | |
| x::rest => (full_name_formatter (x)) ^ "\n" ^ (full_name_list_formatter (rest)) | |
fun color_formatter c = | |
case c of | |
Red => "Red" | |
| Black => "Black" | |
exception CardNotFound | |
fun exception_formatter cnf = | |
case cnf of | |
CardNotFound => "CardNotFound" | |
| _ => "Unknown exception" | |
fun rank_formatter rank = | |
case rank of | |
Jack => "Jack" | |
| Queen => "Queen" | |
| King => "King" | |
| Ace => "Ace" | |
| Num x => Int.toString x | |
fun suit_formatter suit = | |
case suit of | |
Clubs => "Clubs" | |
| Hearts => "Hearts" | |
| Spades => "Spades" | |
| Diamonds => "Diamonds" | |
fun card_formatter (suit, rank) = | |
(rank_formatter (rank)) ^ " of " ^ (suit_formatter (suit)) | |
fun card_list_formatter cl = | |
case cl of | |
[] => "" | |
| c::[] => card_formatter (c) | |
| c::rest => (card_formatter (c)) ^ ", " ^ (card_list_formatter (rest)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment