aboutsummaryrefslogtreecommitdiffhomepage
path: root/core/test/Parsing/PrimitivesTest.hs
diff options
context:
space:
mode:
authorFrédéric Menou <frederic.menou@fretlink.com>2016-12-08 10:19:15 +0200
committerIsmaël Bouya <ismael.bouya@fretlink.com>2022-05-17 18:01:51 +0200
commita9d77a20008efe82862cc1adbfa7a6d4f09f8ff7 (patch)
treeadf3186fdccaeef19151026cdfbd38a530cf9ecb /core/test/Parsing/PrimitivesTest.hs
downloadedi-parser-a9d77a20008efe82862cc1adbfa7a6d4f09f8ff7.tar.gz
edi-parser-a9d77a20008efe82862cc1adbfa7a6d4f09f8ff7.tar.zst
edi-parser-a9d77a20008efe82862cc1adbfa7a6d4f09f8ff7.zip
Release code as open sourceHEADmaster
Diffstat (limited to 'core/test/Parsing/PrimitivesTest.hs')
-rw-r--r--core/test/Parsing/PrimitivesTest.hs105
1 files changed, 105 insertions, 0 deletions
diff --git a/core/test/Parsing/PrimitivesTest.hs b/core/test/Parsing/PrimitivesTest.hs
new file mode 100644
index 0000000..95bc8c8
--- /dev/null
+++ b/core/test/Parsing/PrimitivesTest.hs
@@ -0,0 +1,105 @@
1{-# LANGUAGE OverloadedStrings #-}
2
3module Parsing.PrimitivesTest
4 ( suite
5 ) where
6
7import Text.Edifact.Parsing
8
9import Data.Text (Text, unpack)
10import Text.Parsec (eof)
11
12import Test.Framework
13import Test.Framework.Providers.HUnit (testCase)
14import Test.HUnit ((@?=))
15
16suite :: Test
17suite =
18 testGroup "Primitives"
19 [ test_an
20 , test_an_3
21 , test_an3
22 , test_n
23 ]
24
25test_an :: Test
26test_an =
27 let parser = alphaNumeric
28 in testGroup "an"
29 [ expectSuccess parser "A" 'A'
30 , expectSuccess parser "?:" ':'
31 , expectSuccess parser "?+" '+'
32 , expectSuccess parser "??" '?'
33 , expectSuccess parser "?'" '\''
34 ]
35
36test_an_3 :: Test
37test_an_3 =
38 let parser = alphaNumeric `upTo` 3
39 in testGroup "an..3"
40 [ testGroup "valid"
41 [ expectSuccess parser "" ""
42 , expectSuccess parser "A" "A"
43 , expectSuccess parser "AB" "AB"
44 , expectSuccess parser "ABC" "ABC"
45 , expectSuccess parser "AB??" "AB?"
46 ]
47 , testGroup "invalid"
48 [ expectFailure parser "ABCD"
49 ]
50 ]
51
52test_an3 :: Test
53test_an3 =
54 let parser = alphaNumeric `exactly` 3
55 in testGroup "an3"
56 [ testGroup "valid"
57 [ expectSuccess parser "ABC" "ABC"
58 , expectSuccess parser "AB??" "AB?"
59 ]
60 , testGroup "invalid"
61 [ expectFailure parser ""
62 , expectFailure parser "A"
63 , expectFailure parser "AB"
64 , expectFailure parser "ABCD"
65 ]
66 ]
67
68test_n :: Test
69test_n =
70 let parser = numeric
71 in testGroup "n"
72 [ testGroup "valid"
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 "." '.'
85 ]
86 , testGroup "invalid"
87 [ expectFailure parser "A"
88 , expectFailure parser "?:"
89 , expectFailure parser "?+"
90 , expectFailure parser "??"
91 , expectFailure parser "?'"
92 ]
93 ]
94
95expectSuccess :: (Show a, Eq a) => Parser a -> Text -> a -> Test
96expectSuccess p t = expectParse p t . pure
97
98expectFailure :: (Show a, Eq a) => Parser a -> Text -> Test
99expectFailure p t = expectParse p t Nothing
100
101expectParse :: (Show a, Eq a) => Parser a -> Text -> Maybe a -> Test
102expectParse p t e =
103 let either2Maybe = either (const Nothing) Just
104 title = "\"" <> unpack t <> "\""
105 in testCase title (either2Maybe (parse (p <* eof) t) @?= e)