-
-
Notifications
You must be signed in to change notification settings - Fork 195
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
rbasso
merged 1 commit into
exercism:hspec-fail-fast
from
rbasso:hspec-nucleotide-count
Jul 29, 2016
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,4 @@ tests: | |
source-dirs: test | ||
dependencies: | ||
- nucleotide-count | ||
- HUnit | ||
- hspec |
This file contains hidden or 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
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" $ | ||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the dashes?
There was a problem hiding this comment.
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.