Skip to content

Commit 7cdeab9

Browse files
committed
tests: PrettyParseTests: refactor
1 parent f6c7c54 commit 7cdeab9

File tree

1 file changed

+48
-46
lines changed

1 file changed

+48
-46
lines changed

tests/PrettyParseTests.hs

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ genPos = mkPos <$> Gen.int (Range.linear 1 256)
3737

3838
genSourcePos :: Gen SourcePos
3939
genSourcePos =
40-
liftA3
41-
SourcePos
40+
liftA3 SourcePos
4241
asciiString
4342
genPos
4443
genPos
@@ -53,13 +52,11 @@ genAntiquoted gen =
5352

5453
genBinding :: Gen (Binding NExpr)
5554
genBinding = Gen.choice
56-
[ liftA3
57-
NamedVar
55+
[ liftA3 NamedVar
5856
genAttrPath
5957
genExpr
6058
genSourcePos
61-
, liftA3
62-
Inherit
59+
, liftA3 Inherit
6360
(Gen.maybe genExpr)
6461
(Gen.list (Range.linear 0 5) genKeyName)
6562
genSourcePos
@@ -68,8 +65,7 @@ genBinding = Gen.choice
6865
genString :: Gen (NString NExpr)
6966
genString = Gen.choice
7067
[ DoubleQuoted <$> Gen.list (Range.linear 0 5) (genAntiquoted asciiText)
71-
, liftA2
72-
Indented
68+
, liftA2 Indented
7369
(Gen.int (Range.linear 0 10))
7470
(Gen.list
7571
(Range.linear 0 5)
@@ -79,25 +75,23 @@ genString = Gen.choice
7975

8076
genAttrPath :: Gen (NAttrPath NExpr)
8177
genAttrPath =
82-
liftA2
83-
(:|)
78+
liftA2 (:|)
8479
genKeyName
8580
$ Gen.list (Range.linear 0 4) genKeyName
8681

8782
genParams :: Gen (Params NExpr)
8883
genParams = Gen.choice
8984
[ Param <$> asciiText
90-
, liftA3
91-
ParamSet
85+
, liftA3 ParamSet
9286
(Gen.list (Range.linear 0 10) (liftA2 (,) asciiText $ Gen.maybe genExpr))
9387
Gen.bool
9488
(Gen.choice [stub, pure <$> asciiText])
9589
]
9690

9791
genAtom :: Gen NAtom
9892
genAtom = Gen.choice
99-
[ NInt <$> Gen.integral (Range.linear 0 1000)
100-
, NFloat <$> Gen.float (Range.linearFrac 0.0 1000.0)
93+
[ NInt <$> Gen.integral (Range.linear 0 1000 )
94+
, NFloat <$> Gen.float (Range.linearFrac 0.0 1000.0)
10195
, NBool <$> Gen.bool
10296
, pure NNull
10397
]
@@ -106,24 +100,27 @@ genAtom = Gen.choice
106100
-- list Arbitrary instance which makes the generator terminate. The
107101
-- distribution is not scientifically chosen.
108102
genExpr :: Gen NExpr
109-
genExpr = Gen.sized $ \(Size n) -> Fix <$> if n < 2
110-
then Gen.choice [genConstant, genStr, genSym, genLiteralPath, genEnvPath]
111-
else Gen.frequency
112-
[ (1 , genConstant)
113-
, (1 , genSym)
114-
, (4 , Gen.resize (Size (n `div` 3)) genIf)
115-
, (10, genRecSet)
116-
, (20, genSet)
117-
, (5 , genList)
118-
, (2 , genUnary)
119-
, (2, Gen.resize (Size (n `div` 3)) genBinary)
120-
, (3, Gen.resize (Size (n `div` 3)) genSelect)
121-
, (20, Gen.resize (Size (n `div` 2)) genAbs)
122-
, (2, Gen.resize (Size (n `div` 2)) genHasAttr)
123-
, (10, Gen.resize (Size (n `div` 2)) genLet)
124-
, (10, Gen.resize (Size (n `div` 2)) genWith)
125-
, (1, Gen.resize (Size (n `div` 2)) genAssert)
126-
]
103+
genExpr =
104+
Gen.sized $
105+
\(Size n) -> Fix <$>
106+
if n < 2
107+
then Gen.choice [genConstant, genStr, genSym, genLiteralPath, genEnvPath]
108+
else Gen.frequency
109+
[ (1 , genConstant)
110+
, (1 , genSym)
111+
, (4 , Gen.resize (Size (n `div` 3)) genIf)
112+
, (10, genRecSet)
113+
, (20, genSet)
114+
, (5 , genList)
115+
, (2 , genUnary)
116+
, (2 , Gen.resize (Size (n `div` 3)) genBinary)
117+
, (3 , Gen.resize (Size (n `div` 3)) genSelect)
118+
, (20, Gen.resize (Size (n `div` 2)) genAbs)
119+
, (2 , Gen.resize (Size (n `div` 2)) genHasAttr)
120+
, (10, Gen.resize (Size (n `div` 2)) genLet)
121+
, (10, Gen.resize (Size (n `div` 2)) genWith)
122+
, (1 , Gen.resize (Size (n `div` 2)) genAssert)
123+
]
127124
where
128125
genConstant = NConstant <$> genAtom
129126
genStr = NStr <$> genString
@@ -155,18 +152,21 @@ equivUpToNormalization :: NExpr -> NExpr -> Bool
155152
equivUpToNormalization x y = normalize x == normalize y
156153

157154
normalize :: NExpr -> NExpr
158-
normalize = foldFix $ Fix . \case
155+
normalize = foldFix $ \case
159156
NConstant (NInt n) | n < 0 ->
160-
NUnary NNeg $ Fix $ NConstant $ NInt $ negate n
157+
mkNeg $ mkInt $ negate n
161158
NConstant (NFloat n) | n < 0 ->
162-
NUnary NNeg $ Fix $ NConstant $ NFloat $ negate n
159+
mkNeg $ mkFloat $ negate n
163160

164-
NSet recur binds -> NSet recur $ normBinding <$> binds
165-
NLet binds r -> NLet (normBinding <$> binds) r
161+
NSet recur binds ->
162+
mkSet recur $ normBinding <$> binds
163+
NLet binds r ->
164+
mkLets (normBinding <$> binds) r
166165

167-
NAbs params r -> NAbs (normParams params) r
166+
NAbs params r ->
167+
mkFunction (normParams params) r
168168

169-
r -> r
169+
r -> Fix r
170170

171171
where
172172
normBinding (NamedVar path r pos) = NamedVar (normKey <$> path) r pos
@@ -176,17 +176,19 @@ normalize = foldFix $ Fix . \case
176176
normKey (StaticKey name ) = StaticKey name
177177

178178
normAntiquotedString
179-
:: Antiquoted (NString NExpr) NExpr -> Antiquoted (NString NExpr) NExpr
179+
:: Antiquoted (NString NExpr) NExpr
180+
-> Antiquoted (NString NExpr) NExpr
180181
normAntiquotedString (Plain (DoubleQuoted [EscapedNewline])) = EscapedNewline
181182
normAntiquotedString (Plain (DoubleQuoted strs)) =
182-
let strs' = normAntiquotedText <$> strs
183-
in
184-
if strs == strs'
185-
then Plain $ DoubleQuoted strs
186-
else normAntiquotedString $ Plain $ DoubleQuoted strs'
183+
bool normAntiquotedString id (strs == strs')
184+
(Plain $ DoubleQuoted strs')
185+
where
186+
strs' = normAntiquotedText <$> strs
187187
normAntiquotedString r = r
188188

189-
normAntiquotedText :: Antiquoted Text NExpr -> Antiquoted Text NExpr
189+
normAntiquotedText
190+
:: Antiquoted Text NExpr
191+
-> Antiquoted Text NExpr
190192
normAntiquotedText (Plain "\n" ) = EscapedNewline
191193
normAntiquotedText (Plain "''\n") = EscapedNewline
192194
normAntiquotedText r = r

0 commit comments

Comments
 (0)