From ba1fe9929c2ecb6fe58be4dbf938b5619b9e9165 Mon Sep 17 00:00:00 2001 From: rbasso Date: Wed, 27 Jul 2016 00:15:02 +0900 Subject: [PATCH] beer-song: Rewrite tests to use hspec with fail-fast. --- exercises/beer-song/package.yaml | 2 +- exercises/beer-song/test/Tests.hs | 62 ++++++++++++++----------------- 2 files changed, 28 insertions(+), 36 deletions(-) diff --git a/exercises/beer-song/package.yaml b/exercises/beer-song/package.yaml index 8e8331805..e37c8bcce 100644 --- a/exercises/beer-song/package.yaml +++ b/exercises/beer-song/package.yaml @@ -16,4 +16,4 @@ tests: source-dirs: test dependencies: - beer-song - - HUnit + - hspec diff --git a/exercises/beer-song/test/Tests.hs b/exercises/beer-song/test/Tests.hs index c4ee3bb5d..c0fcb3966 100644 --- a/exercises/beer-song/test/Tests.hs +++ b/exercises/beer-song/test/Tests.hs @@ -1,39 +1,31 @@ -import Test.HUnit (Assertion, (@=?), runTestTT, Test(..), Counts(..)) -import System.Exit (ExitCode(..), exitWith) -import Beer (sing, verse) +{-# 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 Test.Hspec (Spec, describe, it, shouldBe) +import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith) -testCase :: String -> Assertion -> Test -testCase label assertion = TestLabel label (TestCase assertion) +import Beer (sing, verse) main :: IO () -main = exitProperly (runTestTT (TestList [TestList verseTests, TestList singTests])) - -verse_8, verse_2, verse_1, verse_0 :: String -verse_8 = "8 bottles of beer on the wall, 8 bottles of beer.\nTake one down and pass it around, 7 bottles of beer on the wall.\n" -verse_2 = "2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n" -verse_1 = "1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n" -verse_0 = "No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n" - -song_8_6, song_3_0 :: String -song_8_6 = "8 bottles of beer on the wall, 8 bottles of beer.\nTake one down and pass it around, 7 bottles of beer on the wall.\n\n7 bottles of beer on the wall, 7 bottles of beer.\nTake one down and pass it around, 6 bottles of beer on the wall.\n\n6 bottles of beer on the wall, 6 bottles of beer.\nTake one down and pass it around, 5 bottles of beer on the wall.\n\n" -song_3_0 = "3 bottles of beer on the wall, 3 bottles of beer.\nTake one down and pass it around, 2 bottles of beer on the wall.\n\n2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n\n1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n\nNo more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n\n" - - -verseTests :: [Test] -verseTests = - [ testCase "verse 8" $ verse_8 @=? verse 8 - , testCase "verse 2" $ verse_2 @=? verse 2 - , testCase "verse 1" $ verse_1 @=? verse 1 - , testCase "verse 0" $ verse_0 @=? verse 0 - ] - -singTests :: [Test] -singTests = - [ testCase "song 8 6" $ song_8_6 @=? sing 8 6 - , testCase "song 3 0" $ song_3_0 @=? sing 3 0 - ] \ No newline at end of file +main = hspecWith defaultConfig {configFastFail = True} specs + +specs :: Spec +specs = describe "beer-song" $ do + + -- As of 2016-07-30, there was no reference file + -- for the test cases in `exercism/x-common`. + + describe "verse" $ do + it "verse 8" $ verse 8 `shouldBe` verse_8 + it "verse 2" $ verse 2 `shouldBe` verse_2 + it "verse 1" $ verse 1 `shouldBe` verse_1 + it "verse 0" $ verse 0 `shouldBe` verse_0 + describe "sing" $ do + it "song 8 6" $ sing 8 6 `shouldBe` song_8_6 + it "song 3 0" $ sing 3 0 `shouldBe` song_3_0 + where + verse_8 = "8 bottles of beer on the wall, 8 bottles of beer.\nTake one down and pass it around, 7 bottles of beer on the wall.\n" + verse_2 = "2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n" + verse_1 = "1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n" + verse_0 = "No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n" + song_8_6 = "8 bottles of beer on the wall, 8 bottles of beer.\nTake one down and pass it around, 7 bottles of beer on the wall.\n\n7 bottles of beer on the wall, 7 bottles of beer.\nTake one down and pass it around, 6 bottles of beer on the wall.\n\n6 bottles of beer on the wall, 6 bottles of beer.\nTake one down and pass it around, 5 bottles of beer on the wall.\n\n" + song_3_0 = "3 bottles of beer on the wall, 3 bottles of beer.\nTake one down and pass it around, 2 bottles of beer on the wall.\n\n2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n\n1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n\nNo more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n\n"