1
exercism fetch haskell run-length-encoding

test/Tests.hs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
{-# LANGUAGE RecordWildCards #-}

import Data.Foldable     (for_)
import Test.Hspec        (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import RunLength (encode, decode)

main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs

specs :: Spec
specs = do
          describe "decode" $ for_ decodeCases $ test decode
          describe "encode" $ for_ encodeCases $ test encode
          describe "both"   $ for_ bothCases   $ test (decode . encode)
  where
    test f Case{..} = it description $ f input `shouldBe` expected

data Case = Case { description :: String
                 , input       :: String
                 , expected    :: String
                 }

encodeCases :: [Case]
encodeCases =
    [ Case { description = "encode empty string"
           , input       = ""
           , expected    = ""
           }
    , Case { description = "encode single characters only"
           , input       = "XYZ"
           , expected    = "XYZ"
           }
    , Case { description = "encode simple"
           , input       = "AABBBCCCC"
           , expected    = "2A3B4C"
           }
    , Case { description = "encode with single values"
           , input       = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"
           , expected    = "12WB12W3B24WB"
           }
    , Case { description = "encode whitespace"
           , input       = "  hsqq qww  "
           , expected    = "2 hs2q q2w2 "
           }
    , Case { description = "encode lowercase"
           , input       = "aabbbcccc"
           , expected    = "2a3b4c"
           }
    ]

decodeCases :: [Case]
decodeCases =
    [ Case { description = "decode empty string"
           , input       = ""
           , expected    = ""
           }
    , Case { description = "decode single characters only"
           , input       = "XYZ"
           , expected    = "XYZ"
           }
    , Case { description = "decode simple"
           , input       = "2A3B4C"
           , expected    = "AABBBCCCC"
           }
    , Case { description = "decode with single values"
           , input       = "12WB12W3B24WB"
           , expected    = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"
           }
    , Case { description = "decode whitespace"
           , input       = "2 hs2q q2w2 "
           , expected    = "  hsqq qww  "
           }
    , Case { description = "decode lowercase"
           , input       = "2a3b4c"
           , expected    = "aabbbcccc"
           }
    ]

bothCases :: [Case]
bothCases =
    [ Case { description = "decode . encode combination"
           , input       = "zzz ZZ  zZ"
           , expected    = "zzz ZZ  zZ"
           }
    ]