1
1
module Spec.Redis.Script (tests ) where
2
2
3
- import qualified Data.Attoparsec.Text as Attoparsec
4
3
import Data.Either (Either (.. ))
5
4
import qualified Expect
6
5
import Redis.Script
7
6
import qualified Test
7
+ import qualified Text.Megaparsec as P
8
8
9
9
tests :: Test. Test
10
10
tests =
@@ -16,19 +16,19 @@ tests =
16
16
parserTests :: List Test. Test
17
17
parserTests =
18
18
[ Test. test " 1 word" <| \ _ ->
19
- ( Attoparsec. parseOnly parser " Jabuticaba" )
19
+ P. runParser parser " " " Jabuticaba"
20
20
|> Expect. equal (Right [ScriptText " Jabuticaba" ]),
21
21
Test. test " 3 words" <| \ _ ->
22
- ( Attoparsec. parseOnly parser " Picolé de Jabuticaba" )
22
+ P. runParser parser " " " Picolé de Jabuticaba"
23
23
|> Expect. equal (Right [ScriptText " Picolé de Jabuticaba" ]),
24
24
Test. test " 1 value" <| \ _ ->
25
- ( Attoparsec. parseOnly parser " ${value}" )
25
+ P. runParser parser " " " ${value}"
26
26
|> Expect. equal (Right [ScriptVariable " value" ]),
27
27
Test. test " function application" <| \ _ ->
28
- ( Attoparsec. parseOnly parser " ${func arg1 arg2}" )
28
+ P. runParser parser " " " ${func arg1 arg2}"
29
29
|> Expect. equal (Right [ScriptVariable " func arg1 arg2" ]),
30
30
Test. test " text and variables" <| \ _ ->
31
- ( Attoparsec. parseOnly parser " some text ${value} some more text ${ anotherValue }" )
31
+ P. runParser parser " " " some text ${value} some more text ${ anotherValue }"
32
32
|> Expect. equal
33
33
( Right
34
34
[ ScriptText " some text " ,
@@ -37,13 +37,73 @@ parserTests =
37
37
ScriptVariable " anotherValue"
38
38
]
39
39
),
40
- Test. only <| Test. test " ERROR: nested ${}" <| \ _ -> do
41
- (Attoparsec. parseOnly parser " asdasd ${ ${ value } }" )
42
- |> Expect. equal (Left " Expected at least one > No '$', '{' or '}' allowed in interpolated expression. Note: I'm a simple parser and I don't support records inside ${}.: Failed reading: takeWhile1" ),
43
- Test. test " ERROR: misplaced ${ inside ${}" <| \ _ -> do
44
- (Attoparsec. parseOnly parser " ${ v$alue }" )
45
- |> Expect. equal (Left " Expected at least one > Expected '}' after: ${v > '}': Failed reading: satisfy" ),
40
+ Test. test " ERROR: empty" <| \ _ -> do
41
+ P. runParser parser " " " "
42
+ |> mapLeft P. errorBundlePretty
43
+ |> Expect. equal
44
+ ( Left
45
+ " 1:1:\n \
46
+ \ |\n \
47
+ \1 | <empty line>\n \
48
+ \ | ^\n \
49
+ \unexpected end of input\n \
50
+ \expecting \" ${\" or some plain text\n \
51
+ \"
52
+ ),
53
+ Test. test " ERROR: empty variable" <| \ _ -> do
54
+ P. runParser parser " " " ${}"
55
+ |> mapLeft P. errorBundlePretty
56
+ |> Expect. equal
57
+ ( Left
58
+ " 1:3:\n \
59
+ \ |\n \
60
+ \1 | ${}\n \
61
+ \ | ^\n \
62
+ \unexpected '}'\n \
63
+ \expecting anything but '$', '{' or '}' (no records, sorry) or white space\n \
64
+ \"
65
+ ),
66
+ Test. test " ERROR: nested ${}" <| \ _ -> do
67
+ P. runParser parser " " " asdasd ${ ${ value } }"
68
+ |> mapLeft P. errorBundlePretty
69
+ |> Expect. equal
70
+ ( Left
71
+ " 1:11:\n \
72
+ \ |\n \
73
+ \1 | asdasd ${ ${ value } }\n \
74
+ \ | ^\n \
75
+ \unexpected '$'\n \
76
+ \expecting anything but '$', '{' or '}' (no records, sorry) or white space\n \
77
+ \"
78
+ ),
46
79
Test. test " ERROR: misplaced ${ inside ${}" <| \ _ -> do
47
- (Attoparsec. parseOnly parser " ${ v{alue }" )
48
- |> Expect. equal (Left " Expected at least one > Expected '}' after: ${v > '}': Failed reading: satisfy" )
80
+ P. runParser parser " " " ${ v$alue }"
81
+ |> mapLeft P. errorBundlePretty
82
+ |> Expect. equal
83
+ ( Left
84
+ " 1:5:\n \
85
+ \ |\n \
86
+ \1 | ${ v$alue }\n \
87
+ \ | ^\n \
88
+ \unexpected '$'\n \
89
+ \expecting '}' or anything but '$', '{' or '}' (no records, sorry)\n \
90
+ \"
91
+ ),
92
+ Test. test " ERROR: misplaced { inside ${}" <| \ _ -> do
93
+ P. runParser parser " " " ${ v{alue }"
94
+ |> mapLeft P. errorBundlePretty
95
+ |> Expect. equal
96
+ ( Left
97
+ " 1:5:\n \
98
+ \ |\n \
99
+ \1 | ${ v{alue }\n \
100
+ \ | ^\n \
101
+ \unexpected '{'\n \
102
+ \expecting '}' or anything but '$', '{' or '}' (no records, sorry)\n \
103
+ \"
104
+ )
49
105
]
106
+
107
+ mapLeft :: (a -> c ) -> Either a b -> Either c b
108
+ mapLeft f (Left a) = Left (f a)
109
+ mapLeft _ (Right b) = Right b
0 commit comments