|
| 1 | +{-# LANGUAGE RecordWildCards #-} |
| 2 | + |
| 3 | +import Data.Foldable (for_) |
| 4 | +import Test.Hspec (Spec, describe, it, shouldBe) |
| 5 | +import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith) |
| 6 | +import Data.List (intercalate) |
| 7 | + |
| 8 | +import Proverb (recite) |
| 9 | + |
| 10 | +main :: IO () |
| 11 | +main = hspecWith defaultConfig {configFastFail = True} specs |
| 12 | + |
| 13 | +specs :: Spec |
| 14 | +specs = describe "recite" $ for_ cases test |
| 15 | + where |
| 16 | + |
| 17 | + test Case{..} = it description assertion |
| 18 | + where |
| 19 | + assertion = recite input `shouldBe` expected |
| 20 | + |
| 21 | + |
| 22 | +data Case = Case { description :: String |
| 23 | + , input :: [String] |
| 24 | + , expected :: String |
| 25 | + } |
| 26 | + |
| 27 | +joinLines :: [String] -> String |
| 28 | +joinLines = intercalate "\n" |
| 29 | + |
| 30 | +cases :: [Case] |
| 31 | +cases = [ Case { description = "zero pieces" |
| 32 | + , input = [] |
| 33 | + , expected = "" |
| 34 | + } |
| 35 | + , Case { description = "one piece" |
| 36 | + , input = ["nail"] |
| 37 | + , expected = "And all for the want of a nail." |
| 38 | + } |
| 39 | + , Case { description = "two pieces" |
| 40 | + , input = ["nail", "shoe"] |
| 41 | + , expected = |
| 42 | + joinLines [ "For want of a nail the shoe was lost." |
| 43 | + , "And all for the want of a nail." |
| 44 | + ] |
| 45 | + } |
| 46 | + , Case { description = "three pieces" |
| 47 | + , input = ["nail", "shoe", "horse"] |
| 48 | + , expected = |
| 49 | + joinLines [ "For want of a nail the shoe was lost." |
| 50 | + , "For want of a shoe the horse was lost." |
| 51 | + , "And all for the want of a nail." |
| 52 | + ] |
| 53 | + } |
| 54 | + , Case { description = "full proverb" |
| 55 | + , input = [ "nail" |
| 56 | + , "shoe" |
| 57 | + , "horse" |
| 58 | + , "rider" |
| 59 | + , "message" |
| 60 | + , "battle" |
| 61 | + , "kingdom" |
| 62 | + ] |
| 63 | + , expected = |
| 64 | + joinLines [ "For want of a nail the shoe was lost." |
| 65 | + , "For want of a shoe the horse was lost." |
| 66 | + , "For want of a horse the rider was lost." |
| 67 | + , "For want of a rider the message was lost." |
| 68 | + , "For want of a message the battle was lost." |
| 69 | + , "For want of a battle the kingdom was lost." |
| 70 | + , "And all for the want of a nail." |
| 71 | + ] |
| 72 | + } |
| 73 | + , Case { description = "four pieces modernized" |
| 74 | + , input = ["pin", "gun", "soldier", "battle"] |
| 75 | + , expected = |
| 76 | + joinLines [ "For want of a pin the gun was lost." |
| 77 | + , "For want of a gun the soldier was lost." |
| 78 | + , "For want of a soldier the battle was lost." |
| 79 | + , "And all for the want of a pin." |
| 80 | + ] |
| 81 | + } |
| 82 | + ] |
0 commit comments