diff options
author | Frédéric Menou <frederic.menou@fretlink.com> | 2016-12-08 10:19:15 +0200 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@fretlink.com> | 2022-05-17 18:01:51 +0200 |
commit | a9d77a20008efe82862cc1adbfa7a6d4f09f8ff7 (patch) | |
tree | adf3186fdccaeef19151026cdfbd38a530cf9ecb /core/test/Parsing/PrimitivesTest.hs | |
download | edi-parser-a9d77a20008efe82862cc1adbfa7a6d4f09f8ff7.tar.gz edi-parser-a9d77a20008efe82862cc1adbfa7a6d4f09f8ff7.tar.zst edi-parser-a9d77a20008efe82862cc1adbfa7a6d4f09f8ff7.zip |
Diffstat (limited to 'core/test/Parsing/PrimitivesTest.hs')
-rw-r--r-- | core/test/Parsing/PrimitivesTest.hs | 105 |
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 | |||
3 | module Parsing.PrimitivesTest | ||
4 | ( suite | ||
5 | ) where | ||
6 | |||
7 | import Text.Edifact.Parsing | ||
8 | |||
9 | import Data.Text (Text, unpack) | ||
10 | import Text.Parsec (eof) | ||
11 | |||
12 | import Test.Framework | ||
13 | import Test.Framework.Providers.HUnit (testCase) | ||
14 | import Test.HUnit ((@?=)) | ||
15 | |||
16 | suite :: Test | ||
17 | suite = | ||
18 | testGroup "Primitives" | ||
19 | [ test_an | ||
20 | , test_an_3 | ||
21 | , test_an3 | ||
22 | , test_n | ||
23 | ] | ||
24 | |||
25 | test_an :: Test | ||
26 | test_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 | |||
36 | test_an_3 :: Test | ||
37 | test_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 | |||
52 | test_an3 :: Test | ||
53 | test_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 | |||
68 | test_n :: Test | ||
69 | test_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 | |||
95 | expectSuccess :: (Show a, Eq a) => Parser a -> Text -> a -> Test | ||
96 | expectSuccess p t = expectParse p t . pure | ||
97 | |||
98 | expectFailure :: (Show a, Eq a) => Parser a -> Text -> Test | ||
99 | expectFailure p t = expectParse p t Nothing | ||
100 | |||
101 | expectParse :: (Show a, Eq a) => Parser a -> Text -> Maybe a -> Test | ||
102 | expectParse p t e = | ||
103 | let either2Maybe = either (const Nothing) Just | ||
104 | title = "\"" <> unpack t <> "\"" | ||
105 | in testCase title (either2Maybe (parse (p <* eof) t) @?= e) | ||