aboutsummaryrefslogtreecommitdiffhomepage
path: root/core/test/Parsing/PrimitivesTest.hs
blob: 95bc8c8e6f19ab20eb6877985f783bf1447cb13a (plain) (blame)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
{-# LANGUAGE OverloadedStrings #-}

module Parsing.PrimitivesTest
  ( suite
  ) where

import           Text.Edifact.Parsing

import           Data.Text                      (Text, unpack)
import           Text.Parsec                    (eof)

import           Test.Framework
import           Test.Framework.Providers.HUnit (testCase)
import           Test.HUnit                     ((@?=))

suite :: Test
suite =
  testGroup "Primitives"
    [ test_an
    , test_an_3
    , test_an3
    , test_n
    ]

test_an :: Test
test_an =
  let parser = alphaNumeric
  in testGroup "an"
       [ expectSuccess parser "A"  'A'
       , expectSuccess parser "?:" ':'
       , expectSuccess parser "?+" '+'
       , expectSuccess parser "??" '?'
       , expectSuccess parser "?'" '\''
       ]

test_an_3 :: Test
test_an_3 =
  let parser = alphaNumeric `upTo` 3
  in testGroup "an..3"
       [ testGroup "valid"
         [ expectSuccess parser ""     ""
         , expectSuccess parser "A"    "A"
         , expectSuccess parser "AB"   "AB"
         , expectSuccess parser "ABC"  "ABC"
         , expectSuccess parser "AB??" "AB?"
         ]
       , testGroup "invalid"
         [ expectFailure parser "ABCD"
         ]
       ]

test_an3 :: Test
test_an3 =
  let parser = alphaNumeric `exactly` 3
  in testGroup "an3"
       [ testGroup "valid"
         [ expectSuccess parser "ABC"  "ABC"
         , expectSuccess parser "AB??" "AB?"
         ]
       , testGroup "invalid"
        [ expectFailure parser ""
        , expectFailure parser "A"
        , expectFailure parser "AB"
        , expectFailure parser "ABCD"
        ]
      ]

test_n :: Test
test_n =
  let parser = numeric
  in testGroup "n"
       [ testGroup "valid"
         [ expectSuccess parser "0" '0'
         , expectSuccess parser "1" '1'
         , expectSuccess parser "2" '2'
         , expectSuccess parser "3" '3'
         , expectSuccess parser "4" '4'
         , expectSuccess parser "5" '5'
         , expectSuccess parser "6" '6'
         , expectSuccess parser "7" '7'
         , expectSuccess parser "8" '8'
         , expectSuccess parser "9" '9'
         , expectSuccess parser "-" '-'
         , expectSuccess parser "." '.'
         ]
       , testGroup "invalid"
         [ expectFailure parser "A"
         , expectFailure parser "?:"
         , expectFailure parser "?+"
         , expectFailure parser "??"
         , expectFailure parser "?'"
         ]
       ]

expectSuccess :: (Show a, Eq a) => Parser a -> Text -> a -> Test
expectSuccess p t = expectParse p t . pure

expectFailure :: (Show a, Eq a) => Parser a -> Text -> Test
expectFailure p t = expectParse p t Nothing

expectParse :: (Show a, Eq a) => Parser a -> Text -> Maybe a -> Test
expectParse p t e =
  let either2Maybe = either (const Nothing) Just
      title = "\"" <> unpack t <> "\""
  in testCase title (either2Maybe (parse (p <* eof) t) @?= e)