1
exercism fetch haskell rail-fence-cipher

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
{-# LANGUAGE RecordWildCards #-}

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

import RailFenceCipher (encode, decode)

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

specs :: Spec
specs = do
          describe "encode" $ for_ encodeCases testE
          describe "decode" $ for_ decodeCases testD
  where
    testE Case{..} = it description $ encode key text `shouldBe` expected
    testD Case{..} = it description $ decode key text `shouldBe` expected

data Case = Case { description :: String
                 , key         :: Int
                 , text        :: String
                 , expected    :: String
                 }

encodeCases :: [Case]
encodeCases = [ Case { description = "encode with two rails"
                     , key         = 2
                     , text        = "XOXOXOXOXOXOXOXOXO"
                     , expected    = "XXXXXXXXXOOOOOOOOO"
                     }
              , Case { description = "encode with three rails"
                     , key         = 3
                     , text        = "WEAREDISCOVEREDFLEEATONCE"
                     , expected    = "WECRLTEERDSOEEFEAOCAIVDEN"
                     }
              , Case { description = "encode with ending in the middle"
                     , key         = 4
                     , text        = "EXERCISES"
                     , expected    = "ESXIEECSR"
                     }
              ]

decodeCases :: [Case]
decodeCases = [ Case { description = "decode with three rails"
                     , key         = 3
                     , text        = "TEITELHDVLSNHDTISEIIEA"
                     , expected    = "THEDEVILISINTHEDETAILS"
                     }
              , Case { description = "decode with five rails"
                     , key         = 5
                     , text        = "EIEXMSMESAORIWSCE"
                     , expected    = "EXERCISMISAWESOME"
                     }
              , Case { description = "decode with six rails"
                     , key         = 6
                     , text        = "133714114238148966225439541018335470986172518171757571896261"
                     , expected    = "112358132134558914423337761098715972584418167651094617711286"
                     }
              ]