1
- {-# LANGUAGE TypeApplications #-}
2
1
{-# OPTIONS_GHC -Wno-orphans #-}
3
2
4
3
module Text.Megaparsec.UtilsSpec
@@ -25,8 +24,10 @@ import Text.Megaparsec (Parsec, eof, parseMaybe,
25
24
import Text.Megaparsec.Char (alphaNumChar , char , digitChar ,
26
25
string )
27
26
import Text.Megaparsec.Utils (boundedEnumShowParser ,
28
- commaSeparated , occurrence ,
29
- occurrences )
27
+ commaSeparated , numParser ,
28
+ occurrence , occurrences ,
29
+ posDecNumParser , posNumParser )
30
+ import Text.Printf (printf )
30
31
31
32
newtype SomeData = SomeData Int
32
33
deriving Eq
@@ -112,6 +113,21 @@ spec = do
112
113
it " SomeADT" . property $ \ v ->
113
114
parseMaybe someADTParser (show (v :: SomeADT )) `shouldBe` Just v
114
115
116
+ context " posDecNumParser" $ do
117
+ it " no decimals" . property $ \ v ->
118
+ parseMaybe posDecNumParser (show (abs (v :: Int ))) `shouldBe`
119
+ Just (fromIntegral (abs v))
120
+
121
+ it " decimals" . property $ \ v ->
122
+ parseMaybe posDecNumParser (printf " %f" (abs (v :: Double ))) `shouldBe`
123
+ Just (abs v)
124
+
125
+ it " posNumParser" . property $ \ v ->
126
+ parseMaybe posNumParser (show (abs (v :: Int ))) `shouldBe` Just (abs v)
127
+
128
+ it " numParser" . property $ \ v ->
129
+ parseMaybe numParser (show (v :: Int )) `shouldBe` Just v
130
+
115
131
describe " boundedEnumShowParser" $ do
116
132
context " lowercase" . exhaustive $ \ v ->
117
133
parseMaybe (boundedEnumShowParser <* eof) (show v) `shouldBe` Just (v :: SomeEnum )
@@ -154,8 +170,6 @@ spec = do
154
170
runParser (occurrences someADTParser) " test" s `shouldBe` Right [v]
155
171
156
172
describe " comma-separated" $ do
157
- let numParser = read @ Int <$> some digitChar
158
-
159
173
context " valid" $ do
160
174
it " single" . property $ \ x -> do
161
175
let y = abs x
0 commit comments