Skip to content

Commit

Permalink
Merge pull request #233 from rbasso/hspec-etl
Browse files Browse the repository at this point in the history
etl: Rewrite tests to use hspec with fail-fast.
  • Loading branch information
rbasso authored Jul 29, 2016
2 parents 983c5a3 + 170a44a commit 38d6112
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 46 deletions.
2 changes: 1 addition & 1 deletion exercises/etl/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ tests:
source-dirs: test
dependencies:
- etl
- HUnit
- hspec
91 changes: 46 additions & 45 deletions exercises/etl/test/Tests.hs
Original file line number Diff line number Diff line change
@@ -1,51 +1,52 @@
{-# OPTIONS_GHC -fno-warn-type-defaults #-}

import Test.HUnit (Assertion, (@=?), runTestTT, Test(..), Counts(..))
import System.Exit (ExitCode(..), exitWith)
import Data.Map (fromList)
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import ETL (transform)
import qualified Data.Map as M

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 "etl" $

main :: IO ()
main = exitProperly $ runTestTT $ TestList
[ TestList transformTests ]

transformTests :: [Test]
transformTests =
[ testCase "transform one value" $
M.fromList [('a', 1)] @=? transform (M.fromList [(1, "A")])
, testCase "transform multiple keys from one value" $
M.fromList [('a', 1), ('e', 1)] @=? transform (M.fromList [(1, "AE")])
, testCase "transform multiple keys from multiple values" $
M.fromList [('a', 1), ('b', 4)] @=?
transform (M.fromList [(1, "A"), (4, "B")])
, testCase "full dataset" $
M.fromList fullOut @=? transform (M.fromList fullIn)
]

fullOut :: [(Char, Int)]
fullOut =
[ ('a', 1), ('b', 3), ('c', 3), ('d', 2), ('e', 1)
, ('f', 4), ('g', 2), ('h', 4), ('i', 1), ('j', 8)
, ('k', 5), ('l', 1), ('m', 3), ('n', 1), ('o', 1)
, ('p', 3), ('q', 10), ('r', 1), ('s', 1), ('t', 1)
, ('u', 1), ('v', 4), ('w', 4), ('x', 8), ('y', 4)
, ('z', 10) ]

fullIn :: [(Int, String)]
fullIn =
[ (1, "AEIOULNRST")
, (2, "DG")
, (3, "BCMP")
, (4, "FHVWY")
, (5, "K")
, (8, "JX")
, (10,"QZ")
]
-- As of 2016-07-27, there was no reference file
-- for the test cases in `exercism/x-common`.

describe "transform" $ do

it "transform one value" $
transform (fromList [(1, "A")])
`shouldBe` fromList [('a', 1)]

it "transform multiple keys from one value" $
transform (fromList [(1, "AE")])
`shouldBe` fromList [('a', 1), ('e', 1)]

it "transform multiple keys from multiple values" $
transform (fromList [(1, "A"), (4, "B")])
`shouldBe` fromList [('a', 1), ('b', 4)]

it "full dataset" $
transform (fromList fullInput)
`shouldBe` fromList fullOutput

where

fullInput = [ ( 1, "AEIOULNRST")
, ( 2, "DG" )
, ( 3, "BCMP" )
, ( 4, "FHVWY" )
, ( 5, "K" )
, ( 8, "JX" )
, (10, "QZ" ) ]

fullOutput = [ ('a', 1) , ('b', 3) , ('c', 3) , ('d', 2)
, ('e', 1) , ('f', 4) , ('g', 2) , ('h', 4)
, ('i', 1) , ('j', 8) , ('k', 5) , ('l', 1)
, ('m', 3) , ('n', 1) , ('o', 1) , ('p', 3)
, ('q', 10) , ('r', 1) , ('s', 1) , ('t', 1)
, ('u', 1) , ('v', 4) , ('w', 4) , ('x', 8)
, ('y', 4) , ('z', 10) ]

0 comments on commit 38d6112

Please sign in to comment.