File: ReaderI.hs

package info (click to toggle)
haskell-hsini 0.5.2.2-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80 kB
  • sloc: haskell: 425; makefile: 3
file content (258 lines) | stat: -rw-r--r-- 6,833 bytes parent folder | download | duplicates (2)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TemplateHaskell #-}

-- Copyright : 2011-2014 Magnus Therning
-- License   : BSD3
module ReaderI (
    allTests,
) where

import Test.Tasty (TestTree)
import Test.Tasty.HUnit (Assertion, testCase, (@=?))
import Test.Tasty.TH (testGroupGenerator)
import Text.ParserCombinators.Parsec as P (Parser, parse)

import Data.Ini.Reader.Internals (
    IniFile (CommentL, OptionContL, OptionL, SectionL),
    iniParser,
    noiseParser,
    optContParser,
    optLineParser,
    secParser,
 )

-- Convenience function that translates a parser result to something that's
-- easier to check.
p2E :: Parser a -> String -> String -> Either String a
p2E p s t =
    let
        res = P.parse p s t
     in
        case res of
            Left _ -> Left "bad"
            Right e -> Right e

-- {{{1 secParser
case_secParserAllowedChars1 :: Assertion
case_secParserAllowedChars1 =
    let
        expected = Right $ SectionL "foo"
        actual = p2E secParser "sec" "[foo]\n"
     in
        expected @=? actual

case_secParserAllowedChars2 :: Assertion
case_secParserAllowedChars2 =
    let
        expected = Right $ SectionL "FooBar"
        actual = p2E secParser "sec" "[FooBar]\n"
     in
        expected @=? actual

case_secParserAllowedChars3 :: Assertion
case_secParserAllowedChars3 =
    let
        expected = Right $ SectionL "@Foo/Bar-"
        actual = p2E secParser "sec" "[@Foo/Bar-]\n"
     in
        expected @=? actual

case_secParserAllowedChars4 :: Assertion
case_secParserAllowedChars4 =
    let
        expected = Right $ SectionL "foo123"
        actual = p2E secParser "sec" "[foo123]\n"
     in
        expected @=? actual

case_secParserAllowedChars5 :: Assertion
case_secParserAllowedChars5 =
    let
        expected = Right $ SectionL "_foo"
        actual = p2E secParser "sec" "[_foo]\n"
     in
        expected @=? actual

case_secParserDropSpace :: Assertion
case_secParserDropSpace =
    let
        expected = Right $ SectionL "foo"
        actual = p2E secParser "sec" "[ \tfoo\t ]\n"
     in
        expected @=? actual

case_secParserDropTrailing :: Assertion
case_secParserDropTrailing =
    let
        expected = Right $ SectionL "foo"
        actual = p2E secParser "sec" "[foo]  \t foobar\n"
     in
        expected @=? actual

case_secParserAllowGit1 :: Assertion
case_secParserAllowGit1 =
    let
        expected = Right $ SectionL "branch \"master\""
        actual = p2E secParser "sec" "[branch \"master\"]\n"
     in
        expected @=? actual

case_secParserAllowGit2 :: Assertion
case_secParserAllowGit2 =
    let
        expected = Right $ SectionL "foo \"bar.baz\""
        actual = p2E secParser "sec" "[foo \"bar.baz\"]\n"
     in
        expected @=? actual

-- {{{1 optLineParser
case_optLineParserAllowedChars1 :: Assertion
case_optLineParserAllowedChars1 =
    let
        expected = Right $ OptionL "foo" "bar"
        actual = p2E optLineParser "optLine" "foo=bar\n"
     in
        expected @=? actual

case_optLineParserAllowedChars2 :: Assertion
case_optLineParserAllowedChars2 =
    let
        expected = Right $ OptionL "Foo" "bAr"
        actual = p2E optLineParser "optLine" "Foo=bAr\n"
     in
        expected @=? actual

case_optLineParserAllowedChars3 :: Assertion
case_optLineParserAllowedChars3 =
    let
        expected = Right $ OptionL "foo@/foo-" "bar"
        actual = p2E optLineParser "optLine" "foo@/foo-=bar\n"
     in
        expected @=? actual

case_optLineParserAllowedChars4 :: Assertion
case_optLineParserAllowedChars4 =
    let
        expected = Right $ OptionL "foo123" "bar"
        actual = p2E optLineParser "optLine" "foo123=bar\n"
     in
        expected @=? actual

case_optLineParserAllowedChars5 :: Assertion
case_optLineParserAllowedChars5 =
    let
        expected = Right $ OptionL "_foo" "bar"
        actual = p2E optLineParser "optLine" "_foo=bar\n"
     in
        expected @=? actual

case_optLineParserAllowedChars6 :: Assertion
case_optLineParserAllowedChars6 =
    let
        expected = Right $ OptionL "foo bar" "baz"
        actual = p2E optLineParser "optLine" "foo bar=baz\n"
     in
        expected @=? actual

case_optLineParserDisallowedChars1 :: Assertion
case_optLineParserDisallowedChars1 =
    let
        expected = Left "bad"
        actual = p2E optLineParser "optLine" "foo.bar=baz\n"
     in
        expected @=? actual

case_optLineParserDropSpace1 :: Assertion
case_optLineParserDropSpace1 =
    let
        expected = Right $ OptionL "foo" "bar"
        actual = p2E optLineParser "optLine" " foo =  bar\n"
     in
        expected @=? actual

case_optLineParserDropSpace2 :: Assertion
case_optLineParserDropSpace2 =
    let
        expected = Right $ OptionL "foo" "bar"
        actual = p2E optLineParser "optLine" " \tfoo\t \t=\t \t bar\n"
     in
        expected @=? actual

case_optLineParserKeepSpace :: Assertion
case_optLineParserKeepSpace =
    let
        expected = Right $ OptionL "foo" "bar \t \t"
        actual = p2E optLineParser "optLine" "foo\t \t=\t \t bar \t \t\n"
     in
        expected @=? actual

-- {{{1 optContParser
case_optContParserSpace :: Assertion
case_optContParserSpace =
    let
        expected = Right $ OptionContL "foo"
        actual = p2E optContParser "optCont" " foo\n"
     in
        expected @=? actual

case_optContParserTab :: Assertion
case_optContParserTab =
    let
        expected = Right $ OptionContL "foo"
        actual = p2E optContParser "optCont" "\tfoo\n"
     in
        expected @=? actual

case_optContParserKeepTrailing :: Assertion
case_optContParserKeepTrailing =
    let
        expected = Right $ OptionContL "foo  \t\t"
        actual = p2E optContParser "optCont" "\tfoo  \t\t\n"
     in
        expected @=? actual

-- {{{1 noiseParser
case_noiseParserEmptyLine :: Assertion
case_noiseParserEmptyLine =
    let
        expected = Right CommentL
        actual = p2E noiseParser "noise" "\n"
     in
        expected @=? actual

case_noiseParserComment1 :: Assertion
case_noiseParserComment1 =
    let
        expected = Right CommentL
        actual = p2E noiseParser "noise" "# a comment\n"
     in
        expected @=? actual

case_noiseParserComment2 :: Assertion
case_noiseParserComment2 =
    let
        expected = Right CommentL
        actual = p2E noiseParser "noise" "; another comment\n"
     in
        expected @=? actual

case_noiseParserNonEmpty :: Assertion
case_noiseParserNonEmpty =
    let
        expected = Left "bad"
        actual = p2E noiseParser "noise" " \n"
     in
        expected @=? actual

-- {{{1 iniParser
case_iniParserEmpty :: Assertion
case_iniParserEmpty = expected @=? actual
  where
    expected = Right []
    actual = p2E iniParser "parsing empty file" ""

-- {{{1 buildConfig
-- TBD

allTests :: TestTree
allTests = $(testGroupGenerator)