1 {-# LANGUAGE OverloadedStrings #-}
3 module Parsing.PrimitivesTest
7 import Text.Edifact.Parsing
9 import Data.Text (Text, unpack)
10 import Text.Parsec (eof)
13 import Test.Framework.Providers.HUnit (testCase)
14 import Test.HUnit ((@?=))
18 testGroup "Primitives"
27 let parser = alphaNumeric
29 [ expectSuccess parser "A" 'A'
30 , expectSuccess parser "?:" ':'
31 , expectSuccess parser "?+" '+'
32 , expectSuccess parser "??" '?'
33 , expectSuccess parser "?'" '\''
38 let parser = alphaNumeric `upTo` 3
41 [ expectSuccess parser "" ""
42 , expectSuccess parser "A" "A"
43 , expectSuccess parser "AB" "AB"
44 , expectSuccess parser "ABC" "ABC"
45 , expectSuccess parser "AB??" "AB?"
48 [ expectFailure parser "ABCD"
54 let parser = alphaNumeric `exactly` 3
57 [ expectSuccess parser "ABC" "ABC"
58 , expectSuccess parser "AB??" "AB?"
61 [ expectFailure parser ""
62 , expectFailure parser "A"
63 , expectFailure parser "AB"
64 , expectFailure parser "ABCD"
73 [ expectSuccess parser "0" '0'
74 , expectSuccess parser "1" '1'
75 , expectSuccess parser "2" '2'
76 , expectSuccess parser "3" '3'
77 , expectSuccess parser "4" '4'
78 , expectSuccess parser "5" '5'
79 , expectSuccess parser "6" '6'
80 , expectSuccess parser "7" '7'
81 , expectSuccess parser "8" '8'
82 , expectSuccess parser "9" '9'
83 , expectSuccess parser "-" '-'
84 , expectSuccess parser "." '.'
87 [ expectFailure parser "A"
88 , expectFailure parser "?:"
89 , expectFailure parser "?+"
90 , expectFailure parser "??"
91 , expectFailure parser "?'"
95 expectSuccess :: (Show a, Eq a) => Parser a -> Text -> a -> Test
96 expectSuccess p t = expectParse p t . pure
98 expectFailure :: (Show a, Eq a) => Parser a -> Text -> Test
99 expectFailure p t = expectParse p t Nothing
101 expectParse :: (Show a, Eq a) => Parser a -> Text -> Maybe a -> Test
103 let either2Maybe = either (const Nothing) Just
104 title = "\"" <> unpack t <> "\""
105 in testCase title (either2Maybe (parse (p <* eof) t) @?= e)