Last active
August 29, 2015 14:13
-
-
Save biinari/441c9f4cd630e9fb574a to your computer and use it in GitHub Desktop.
Tests for RNA transcription exercise from exercism.io, added error cases
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
module AssertException (assertException, assertErrorCall) where | |
import Control.Exception (ErrorCall(..), Exception, handleJust, SomeException(..)) | |
import Control.Monad (guard) | |
import Test.HUnit (assertFailure) | |
import System.IO (hPutStrLn, stderr) | |
assertException :: (Exception e, Eq e) => e -> IO a -> IO () | |
assertException ex action = | |
handleJust isWanted (const $ return ()) $ do | |
action | |
assertFailure $ "Expected exception: " ++ show ex | |
where | |
isWanted = guard . (== ex) | |
assertErrorCall :: String -> IO a -> IO () | |
assertErrorCall msg action = assertException (ErrorCall msg) action |
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
import Test.HUnit (Assertion, (@=?), runTestTT, Test(..), Counts(..)) | |
import System.Exit (ExitCode(..), exitWith) | |
import DNA (toRNA) | |
import AssertException (assertErrorCall) | |
import Control.Exception (evaluate) | |
exitProperly :: IO Counts -> IO () | |
exitProperly m = do | |
counts <- m | |
exitWith $ if failures counts /= 0 || errors counts /= 0 then ExitFailure 1 else ExitSuccess | |
testCase :: String -> Assertion -> Test | |
testCase label assertion = TestLabel label (TestCase assertion) | |
toRNATests :: [Test] | |
toRNATests = | |
[ testCase "transcribes cytosine to guanine" $ | |
"G" @=? toRNA "C" | |
, testCase "transcribes guanine to cytosine" $ | |
"C" @=? toRNA "G" | |
, testCase "transcribes adenine to uracil" $ | |
"U" @=? toRNA "A" | |
, testCase "transcribes thymine to adenine" $ | |
"A" @=? toRNA "T" | |
, testCase "transcribes all ACGT to UGCA" $ | |
"UGCACCAGAAUU" @=? toRNA "ACGTGGTCTTAA" | |
, testCase "raises error for invalid nucleotide" $ | |
assertErrorCall "Invalid DNA nucleotide: 'B'" (evaluate $ toRNA "B") | |
, testCase "raises error for invalid string" $ | |
assertErrorCall "Invalid DNA nucleotide: 'g'" (evaluate $ toRNA "CAgTa") | |
] | |
main :: IO () | |
main = exitProperly (runTestTT (TestList toRNATests)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment