aboutsummaryrefslogtreecommitdiff
path: root/test/FunTests/Parser.hs
blob: 2339a8208ab400027c5d978a8e25fa2a611938f8 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
module FunTests.Parser
  ( parserTest
  ) where

import           Fun.Parser
import           Test.HUnit

isLeft :: Either a b -> Bool
isLeft (Left  _) = True
isLeft (Right _) = False

isRight :: Either a b -> Bool
isRight (Right _) = True
isRight (Left  _) = False

----

testIsDigit :: Test
testIsDigit = test
  [ isDigit '0' ~=? True
  , isDigit '9' ~=? True
  , isDigit 'A' ~=? False
  , isDigit 'z' ~=? False
  ]

testIsAlpha :: Test
testIsAlpha = test
  [ isAlpha 'A' ~=? True
  , isAlpha 'z' ~=? True
  , isAlpha '0' ~=? False
  , isAlpha '9' ~=? False
  ]

testIsLower :: Test
testIsLower = test
  [ isLower 'a' ~=? True
  , isLower 'z' ~=? True
  , isLower 'A' ~=? False
  , isLower '9' ~=? False
  ]

testIsUpper :: Test
testIsUpper = test
  [ isUpper 'A' ~=? True
  , isUpper 'Z' ~=? True
  , isUpper '9' ~=? False
  , isUpper 'a' ~=? False
  ]

testChar :: Test
testChar = test
  [ char "abc" ~=? Right ('a', "bc")
  , char "a" ~=? Right ('a', "")
  , isLeft (char "") ~=? True
  ]

testDigit :: Test
testDigit = test
  [ digit "123" ~=? Right ('1', "23")
  , digit "9abc" ~=? Right ('9', "abc")
  , isLeft (digit "a123") ~=? True
  ]

testDigits :: Test
testDigits = test
  [ digits "123" ~=? Right ("123", "")
  , digits "98abc" ~=? Right ("98", "abc")
  , isLeft (digits "a123") ~=? True
  ]

testNumber :: Test
testNumber = test
  [ number "-123" ~=? Right (-123, "")
  , number "98abc" ~=? Right (98, "abc")
  , isLeft (number "--123") ~=? True
  , isLeft (number "a123") ~=? True
  ]

testSpace :: Test
testSpace = test
  [ space " ab" ~=? Right (' ', "ab")
  , space "  a" ~=? Right (' ', " a")
  , isLeft (space "a b") ~=? True
  ]

testNotSpace :: Test
testNotSpace = test
  [ notSpace "~b" ~=? Right ('~', "b")
  , notSpace "8b" ~=? Right ('8', "b")
  , notSpace "ab" ~=? Right ('a', "b")
  , isLeft (notSpace " b") ~=? True
  ]

testNewline :: Test
testNewline = test
  [ newline "\nab" ~=? Right ('\n', "ab")
  , newline "\n\na" ~=? Right ('\n', "\na")
  --, newline "\r\na" ~=? Right ('\r', "\na") -- or sth TODO
  , isLeft (newline "a\nb") ~=? True
  ]

testLetter :: Test
testLetter = test
  [ letter "abc" ~=? Right ('a', "bc")
  , letter "a123" ~=? Right ('a', "123")
  , isLeft (letter "1abc") ~=? True
  ]

testLetters :: Test
testLetters = test
  [ letters "abc" ~=? Right ("abc", "")
  , letters "ab123" ~=? Right ("ab", "123")
  , isLeft (letters "1abc") ~=? True
  ]

testAlphanum :: Test
testAlphanum = test
  [ alphanum "abc" ~=? Right ('a', "bc")
  , alphanum "a123" ~=? Right ('a', "123")
  , alphanum "1234" ~=? Right ('1', "234")
  , isLeft (alphanum "_abc") ~=? True
  , isLeft (alphanum "*123") ~=? True
  ]

testSpecial :: Test
testSpecial = test
  [ special "*123" ~=? Right ('*', "123")
  , special "#abc" ~=? Right ('#', "abc")
  , isLeft (special "abc") ~=? True
  , isLeft (special "123") ~=? True
  ]

testOneOf :: Test
testOneOf = test
  [ oneOf "abcd" "d123" ~=? Right ('d', "123")
  , oneOf "#(!" "#abc" ~=? Right ('#', "abc")
  , isLeft (oneOf "abcde" "fu") ~=? True
  , isLeft (oneOf "123" "a123") ~=? True
  ]

testString :: Test
testString = test
  [ string "\"hallo\"naa" ~=? Right ("hallo", "naa")
  , string "\"\"ah" ~=? Right ("", "ah")
  , isLeft (string "\"hallo naa") ~=? True
  , isLeft (string "na\"hallo\"") ~=? True
  ]

testLiteral :: Test
testLiteral = test
  [ literal 'a' "abc" ~=? Right ('a', "bc")
  , literal '~' "~12" ~=? Right ('~', "12")
  , isLeft (literal 'a' "123abc") ~=? True
  , isLeft (literal '1' " 123") ~=? True
  ]

testResult :: Test
testResult = test
  [result 'a' "abc" ~=? Right ('a', "abc"), result ' ' "" ~=? Right (' ', "")]

testOneOrMore :: Test
testOneOrMore = test
  [ oneOrMore (literal 'a') "abc" ~=? Right ("a", "bc")
  , oneOrMore (literal '1') "111234" ~=? Right ("111", "234")
  , isLeft (oneOrMore (literal 'a') "1234") ~=? True
  ]

testIter :: Test
testIter = test
  [ iter (literal 'a') "abc" ~=? Right ("a", "bc")
  , iter (literal '1') "111234" ~=? Right ("111", "234")
  , iter (literal 'a') "1234" ~=? Right ("", "1234")
  ]

testIterFull :: Test
testIterFull = test
  [ iterFull (literal 'a') "aaa" ~=? Right ("aaa", "")
  , iterFull (literal '1') "1" ~=? Right ("1", "")
  , isLeft (iterFull (literal 'a') "abc") ~=? True
  , isLeft (iterFull (literal 'a') "1234") ~=? True
  ]

testToken :: Test
testToken = test
  [ token (literal 'a') "a b" ~=? Right ('a', "b")
  , token (literal ' ') "  a" ~=? Right (' ', "a")
  , isLeft (token (literal 'a') "ab") ~=? True
  , isLeft (token (literal 'a') "ba ") ~=? True
  ]

testAccept :: Test
testAccept = test
  [ accept "abc" "abc 123" ~=? Right ("abc", "123")
  , accept "1" "1 1 23" ~=? Right ("1", "1 23")
  , isLeft (accept " " "  a") ~=? True
  , isLeft (accept "abc" "aabc ") ~=? True
  ]

parserTest :: IO Counts
parserTest = runTestTT $ TestList
  [ testIsDigit
  , testIsAlpha
  , testIsLower
  , testIsUpper
  , testChar
  , testDigit
  , testDigits
  , testNumber
  , testSpace
  , testNotSpace
  , testNewline
  , testLetter
  , testLetters
  , testAlphanum
  , testSpecial
  , testOneOf
  , testString
  , testLiteral
  , testResult
  , testOneOrMore
  , testIter
  , testIterFull
  , testToken
  , testAccept
  ]