1
exercism fetch haskell isbn-verifier

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

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

import IsbnVerifier (isbn)

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

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

    test Case{..} = it description assertion
      where
        assertion = isbn input `shouldBe` expected

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

cases :: [Case]
cases = [ Case { description = "valid isbn number"
               , input       = "3-598-21508-8"
               , expected    = True
               }
        , Case { description = "invalid isbn check digit"
               , input       = "3-598-21508-9"
               , expected    = False
               }
        , Case { description = "valid isbn number with a check digit of 10"
               , input       = "3-598-21507-X"
               , expected    = True
               }
        , Case { description = "check digit is a character other than X"
               , input       = "3-598-21507-A"
               , expected    = False
               }
        , Case { description = "invalid character in isbn"
               , input       = "3-598-2K507-0"
               , expected    = False
               }
        , Case { description = "X is only valid as a check digit"
               , input       = "3-598-2X507-9"
               , expected    = False
               }
        , Case { description = "valid isbn without separating dashes"
               , input       = "3598215088"
               , expected    = True
               }
        , Case { description = "isbn without separating dashes and X as check digit"
               , input       = "359821507"
               , expected    = False
               }
        , Case { description = "too long isbn and no dashes"
               , input       = "3598215078X"
               , expected    = False
               }
        , Case { description = "isbn without check digit"
               , input       = "3-598-21507"
               , expected    = False
               }
        , Case { description = "too long isbn"
               , input       = "3-598-21507-XX"
               , expected    = False
               }
        , Case { description = "check digit of X should not be used for 0"
               , input       = "3-598-21515-X"
               , expected    = False
               }
        ]