Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 44 lines (36 sloc) 1.111 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

module Roguestar.Lib.Tests
    (TestResult(..),
     TestCase,
     test,
     runAllTests,
     sampleTestCase)
    where

data TestResult = Passed String | Failed String deriving Show

type TestCase = IO TestResult

-- |
-- Sample test case that always passes.
--
sampleTestCase :: IO TestResult
sampleTestCase = do return (Passed "sampleTestCase")

-- |
-- True if the TestResult is Passed, False otherwise
--
testResultToBool :: TestResult -> Bool
testResultToBool (Passed _) = True
testResultToBool (Failed _) = False

-- |
-- Simple way to generate a TestResult based on a boolean test result.
--
test :: String -> Bool -> TestCase
test str True = return $ Passed str
test str False = return $ Failed str

-- |
-- Runs every specified test case, returning True iff all tests pass.
-- Results from the tests are printed.
--
runAllTests :: [TestCase] -> IO Bool
runAllTests [] = do return True
runAllTests (testCase:testCases) = do testResult <- testCase
putStrLn (show testResult)
testResults <- runAllTests testCases
return (testResults && testResultToBool testResult)
Something went wrong with that request. Please try again.