Skip to content

nucleotide-count: Rewrite to use hspec with fail-fast. #226

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/nucleotide-count/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ tests:
source-dirs: test
dependencies:
- nucleotide-count
- HUnit
- hspec
100 changes: 57 additions & 43 deletions exercises/nucleotide-count/test/Tests.hs
Original file line number Diff line number Diff line change
@@ -1,47 +1,61 @@
import Test.HUnit (Assertion, (@=?), runTestTT, Test(..), Counts(..))
import System.Exit (ExitCode(..), exitWith)
import DNA (count, nucleotideCounts)
import Data.Map (fromList)
{-# OPTIONS_GHC -fno-warn-type-defaults #-}

exitProperly :: IO Counts -> IO ()
exitProperly m = do
counts <- m
exitWith $ if failures counts /= 0 || errors counts /= 0 then ExitFailure 1 else ExitSuccess
import Data.Either (isLeft)
import Data.Map (fromList)
import Test.Hspec (Spec, describe, it, shouldBe, shouldSatisfy)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

testCase :: String -> Assertion -> Test
testCase label assertion = TestLabel label (TestCase assertion)
import DNA (count, nucleotideCounts)

main :: IO ()
main = exitProperly $ runTestTT $ TestList
[ TestList countTests
, TestList nucleotideCountTests]

countTests :: [Test]
countTests =
[ testCase "empty dna strand has no adenosine" $
Right 0 @=? count 'A' ""
, testCase "repetitive cytidine gets counted" $
Right 5 @=? count 'C' "CCCCC"
, testCase "counts only thymidine" $
Right 1 @=? count 'T' "GGGGGTAACCCGG"
, testCase "validates nucleotides" $
Left "invalid nucleotide 'X'" @=? count 'X' "GACT"
, testCase "validates strand" $
Left "invalid nucleotide 'Y'" @=? count 'G' "GACYT"
]

nucleotideCountTests :: [Test]
nucleotideCountTests =
[ testCase "empty dna strand has no nucleotides" $
Right (fromList [('A', 0), ('T', 0), ('C', 0), ('G', 0)]) @=?
nucleotideCounts ""
, testCase "repetitive-sequence-has-only-guanosine" $
Right (fromList [('A', 0), ('T', 0), ('C', 0), ('G', 8)]) @=?
nucleotideCounts "GGGGGGGG"
, testCase "counts all nucleotides" $
Right (fromList [('A', 20), ('T', 21), ('C', 12), ('G', 17)]) @=?
nucleotideCounts ("AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAA" ++
"GAGTGTCTGATAGCAGC")
, testCase "validates strand" $
Left "invalid nucleotide 'P'" @=? nucleotideCounts "GPAC"
]
main = hspecWith defaultConfig {configFastFail = True} specs

specs :: Spec
specs = describe "nucleotide-count" $ do

-- As of 2016-07-27, there was no reference file
-- for the test cases in `exercism/x-common`.

let x `matches` y = x `shouldBe` Right y
let x `matchesMap` y = x `shouldBe` (Right . fromList) y

describe "count" $ do

it "empty dna strand has no adenosine" $
count 'A' "" `matches` 0

it "repetitive cytidine gets counted" $
count 'C' "CCCCC" `matches` 5

it "counts only thymidine" $
count 'T' "GGGGGTAACCCGG" `matches` 1

it "validates nucleotides" $
count 'X' "GACT" `shouldSatisfy` isLeft

it "validates strand" $
count 'G' "GACYT" `shouldSatisfy` isLeft

describe "nucleotideCounts" $ do

it "empty dna strand has no nucleotides" $
nucleotideCounts "" `matchesMap` [ ('A', 0)
, ('C', 0)
, ('G', 0)
, ('T', 0) ]

it "repetitive-sequence-has-only-guanosine" $
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the dashes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea! This came from the original implementation from 2013-08-19. I just adapted the test cases to use hspec here, without really looking at them.

nucleotideCounts "GGGGGGGG" `matchesMap` [ ('A', 0)
, ('C', 0)
, ('G', 8)
, ('T', 0) ]

it "counts all nucleotides" $
nucleotideCounts "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC"
`matchesMap` [ ('A', 20)
, ('C', 12)
, ('G', 17)
, ('T', 21) ]

it "validates strand" $
nucleotideCounts "GPAC" `shouldSatisfy` isLeft