Skip to content

Instantly share code, notes, and snippets.

@ramirez7
Created September 21, 2016 04:35
Show Gist options
  • Select an option

  • Save ramirez7/e659acd04271bbc98100bac54eabd48c to your computer and use it in GitHub Desktop.

Select an option

Save ramirez7/e659acd04271bbc98100bac54eabd48c to your computer and use it in GitHub Desktop.
type UnitTest a = S.StateT Bool IO a
test :: (Show a, Eq a) => String -> a -> a -> UnitTest ()
test name found expected
| found == expected = passTest
| otherwise = failTest
where
failTest = (S.put True) >> S.liftIO (testStrLn (printf "[FAILED] \"%s\"; Found { %s } ; Expected { %s }" name (show found) (show expected)))
passTest = S.liftIO (testStrLn (printf "[PASSED] \"%s\"" name))
runTests :: [UnitTest a] -> IO ()
runTests tests = do
(_, failed) <- S.runStateT (F.sequenceA_ tests) False
if failed then testStrLn "Some tests FAILED" else testStrLn "All tests PASSED"
-- example:
-- https://www.hackerrank.com/challenges/time-conversion
tests = [test "Midnight" (to24hr (Time 12 0 0 AM)) "00:00:00",
test "Noon" (to24hr (Time 12 0 0 PM)) "12:00:00",
test "Normal AM" (to24hr (Time 5 24 23 AM)) "05:24:23",
test "Normal PM" (to24hr (Time 7 22 08 PM)) "19:22:08"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment