Skip to content

rna-transcription: Rewrite tests to use hspec with fail-fast. #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion exercises/rna-transcription/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ tests:
source-dirs: test
dependencies:
- rna-transcription
- HUnit
- hspec
82 changes: 53 additions & 29 deletions exercises/rna-transcription/test/Tests.hs
Original file line number Diff line number Diff line change
@@ -1,34 +1,58 @@
import Test.HUnit (Assertion, (@=?), runTestTT, Test(..), Counts(..))
import System.Exit (ExitCode(..), exitWith)
{-# LANGUAGE RecordWildCards #-}

import Data.Foldable (for_)
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import DNA (toRNA)

exitProperly :: IO Counts -> IO ()
exitProperly m = do
counts <- m
exitWith $ if failures counts /= 0 || errors counts /= 0 then ExitFailure 1 else ExitSuccess
main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs

testCase :: String -> Assertion -> Test
testCase label assertion = TestLabel label (TestCase assertion)
specs :: Spec
specs = describe "rna-transcription" $
describe "toRNA" $ for_ cases test
where
test Case{..} = it description $ toRNA dna `shouldBe` expected

toRNATests :: [Test]
toRNATests =
[ testCase "transcribes cytosine to guanine" $
Just "G" @=? toRNA "C"
, testCase "transcribes guanine to cytosine" $
Just "C" @=? toRNA "G"
, testCase "transcribes adenine to uracil" $
Just "U" @=? toRNA "A"
, testCase "transcribes thymine to adenine" $
Just "A" @=? toRNA "T"
, testCase "transcribes all ACGT to UGCA" $
Just "UGCACCAGAAUU" @=? toRNA "ACGTGGTCTTAA"
, testCase "transcribes RNA only nucleotide uracil to Nothing" $
Nothing @=? toRNA "U"
, testCase "transcribes completely invalid DNA to Nothing" $
Nothing @=? toRNA "XXX"
, testCase "transcribes partially invalid DNA to Nothing" $
Nothing @=? toRNA "ACGTXXXCTTAA"
]
-- Test cases adapted from `exercism/x-common/rna-transcription.json` on 2016-07-24.

main :: IO ()
main = exitProperly (runTestTT (TestList toRNATests))
data Case = Case { description :: String
, dna :: String
, expected :: Maybe String
}

cases :: [Case]
cases = [ Case { description = "rna complement of cytosine is guanine"
, dna = "C"
, expected = Just "G"
}
, Case { description = "rna complement of guanine is cytosine"
, dna = "G"
, expected = Just "C"
}
, Case { description = "rna complement of thymine is adenine"
, dna = "T"
, expected = Just "A"
}
, Case { description = "rna complement of adenine is uracil"
, dna = "A"
, expected = Just "U"
}
, Case { description = "rna complement"
, dna = "ACGTGGTCTTAA"
, expected = Just "UGCACCAGAAUU"
}
, Case { description = "dna correctly handles invalid input"
, dna = "U"
, expected = Nothing
}
, Case { description = "dna correctly handles completely invalid input"
, dna = "XXX"
, expected = Nothing
}
, Case { description = "dna correctly handles partially invalid input"
, dna = "ACGTXXXCTTAA"
, expected = Nothing
}
]