1
exercism fetch haskell crypto-square

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

import Data.Char         (isSpace)
import Data.Foldable     (for_)
import Data.Function     (on)
import Test.Hspec        (Spec, describe, it, shouldBe, shouldMatchList)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import CryptoSquare (encode)

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

specs :: Spec
specs = describe "encode" $ for_ cases test
  where

    test Case{..} = describe description $ do

      let shouldMatchWords  = shouldBe        `on` words
          shouldMatchString = shouldBe        `on` filter (not . isSpace)
          shouldMatchChars  = shouldMatchList `on` filter (not . isSpace)

      it "normalizes the input"    $ encode input `shouldMatchChars`  expected
      it "reorders the characters" $ encode input `shouldMatchString` expected
      it "groups the output"       $ encode input `shouldMatchWords`  expected

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

cases :: [Case]
cases = [ Case { description = "empty plaintext results in an empty ciphertext"
               , input       = ""
               , expected    = ""
               }
        , Case { description = "Lowercase"
               , input       = "A"
               , expected    = "a"
               }
        , Case { description = "Remove spaces"
               , input       = "  b "
               , expected    = "b"
               }
        , Case { description = "Remove punctuation"
               , input       = "@1,%!"
               , expected    = "1"
               }
        , Case { description = "9 character plaintext results in 3 chunks of 3 characters"
               , input       = "This is fun!"
               , expected    = "tsf hiu isn"
               }
        , Case { description = "8 character plaintext results in 3 chunks, the last one with a trailing space"
               , input       = "Chill out."
               , expected    = "clu hlt io "
               }
        , Case { description = "54 character plaintext results in 7 chunks, the last two padded with spaces"
               , input       = "If man was meant to stay on the ground, god would have given us roots."
               , expected    = "imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn  sseoau "
               }
        ]