Skip to content

Commit 17ca4bd

Browse files
committed
test: add tests for gpd accessors
test: check equality on each field test: improve import list test: use @? operator test: use a tuple to store all gpd fields test: define ToExpr tuple instance manually test: update expected
1 parent 8365cec commit 17ca4bd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+689
-694
lines changed

Cabal-tests/tests/ParserTests.hs

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,22 @@ import Test.Tasty
1010
import Test.Tasty.Golden.Advanced (goldenTest)
1111
import Test.Tasty.HUnit
1212

13-
import Control.Monad (unless, void)
13+
import Control.Monad (void)
1414
import Data.Algorithm.Diff (PolyDiff (..), getGroupedDiff)
1515
import Data.Maybe (isNothing)
1616
import Distribution.Fields (pwarning)
17-
import Distribution.PackageDescription (GenericPackageDescription)
17+
import Distribution.PackageDescription
18+
( GenericPackageDescription
19+
, packageDescription
20+
, gpdScannedVersion
21+
, genPackageFlags
22+
, condLibrary
23+
, condSubLibraries
24+
, condForeignLibs
25+
, condExecutables
26+
, condTestSuites
27+
, condBenchmarks
28+
)
1829
import Distribution.PackageDescription.Parsec (parseGenericPackageDescription)
1930
import Distribution.PackageDescription.PrettyPrint (showGenericPackageDescription)
2031
import Distribution.Parsec (PWarnType (..), PWarning (..), showPErrorWithSource, showPWarningWithSource)
@@ -233,12 +244,22 @@ formatGoldenTest fp = cabalGoldenTest "format" correct $ do
233244
#ifdef MIN_VERSION_tree_diff
234245
treeDiffGoldenTest :: FilePath -> TestTree
235246
treeDiffGoldenTest fp = ediffGolden goldenTest "expr" exprFile $ do
236-
contents <- BS.readFile input
237-
let res = withSource (PCabalFile (fp, contents)) $ parseGenericPackageDescription contents
238-
let (_, x) = runParseResult res
239-
case x of
240-
Right gpd -> pure (toExpr gpd)
241-
Left (_, errs) -> fail $ unlines $ "ERROR" : map (showPErrorWithSource . fmap renderCabalFileSource) (NE.toList errs)
247+
contents <- BS.readFile input
248+
let res = withSource (PCabalFile (fp, contents)) $ parseGenericPackageDescription contents
249+
let (_, x) = runParseResult res
250+
case x of
251+
Right gpd -> pure $ toExpr
252+
( packageDescription gpd
253+
, gpdScannedVersion gpd
254+
, genPackageFlags gpd
255+
, condLibrary gpd
256+
, condSubLibraries gpd
257+
, condForeignLibs gpd
258+
, condExecutables gpd
259+
, condTestSuites gpd
260+
, condBenchmarks gpd
261+
)
262+
Left (_, errs) -> fail $ unlines $ "ERROR" : map (showPErrorWithSource . fmap renderCabalFileSource) (NE.toList errs)
242263
where
243264
input = "tests" </> "ParserTests" </> "regressions" </> fp
244265
exprFile = replaceExtension input "expr"
@@ -250,24 +271,38 @@ formatRoundTripTest fp = testCase "roundtrip" $ do
250271
x <- parse contents
251272
let contents' = showGenericPackageDescription x
252273
y <- parse (toUTF8BS contents')
253-
-- previously we mangled licenses a bit
254-
let y' = y
274+
275+
let checkField field =
276+
field x == field y @?
255277
{- FOURMOLU_DISABLE -}
256-
unless (x == y') $
257278
#ifdef MIN_VERSION_tree_diff
258-
assertFailure $ unlines
259-
[ "re-parsed doesn't match"
260-
, show $ ansiWlEditExpr $ ediff x y
261-
]
279+
unlines
280+
[ "re-parsed doesn't match"
281+
, show $ ansiWlEditExpr $ ediff x y
282+
]
262283
#else
263-
assertFailure $ unlines
264-
[ "re-parsed doesn't match"
265-
, "expected"
266-
, show x
267-
, "actual"
268-
, show y
269-
]
284+
unlines
285+
[ "re-parsed doesn't match"
286+
, "expected"
287+
, show x
288+
, "actual"
289+
, show y
290+
]
270291
#endif
292+
-- Due to the imports being merged, the structural comparison will fail
293+
-- Instead, we check the equality after merging
294+
sequence_
295+
[ checkField packageDescription
296+
, checkField gpdScannedVersion
297+
, checkField genPackageFlags
298+
, checkField condLibrary
299+
, checkField condSubLibraries
300+
, checkField condForeignLibs
301+
, checkField condExecutables
302+
, checkField condTestSuites
303+
, checkField condBenchmarks
304+
]
305+
271306
where
272307
parse :: BS.ByteString -> IO GenericPackageDescription
273308
parse c = do

Cabal-tests/tests/ParserTests/regressions/Octree-0.5.expr

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
GenericPackageDescription {
2-
packageDescription =
1+
_×_×_×_×_×_×_×_×_
32
PackageDescription {
43
specVersion = CabalSpecV1_8,
54
package = PackageIdentifier {
@@ -67,10 +66,10 @@ GenericPackageDescription {
6766
extraSrcFiles = [],
6867
extraTmpFiles = [],
6968
extraDocFiles = [],
70-
extraFiles = []},
71-
gpdScannedVersion = Nothing,
72-
genPackageFlags = [],
73-
condLibrary = Just
69+
extraFiles = []}
70+
Nothing
71+
[]
72+
(Just
7473
CondNode {
7574
condTreeData = Library {
7675
libName = LMainLibName,
@@ -86,11 +85,11 @@ GenericPackageDescription {
8685
buildTools = [],
8786
buildToolDepends = [],
8887
cppOptions = [],
89-
jsppOptions = [],
9088
asmOptions = [],
9189
cmmOptions = [],
9290
ccOptions = [],
9391
cxxOptions = [],
92+
jsppOptions = [],
9493
ldOptions = [],
9594
hsc2hsOptions = [],
9695
pkgconfigDepends = [],
@@ -178,11 +177,11 @@ GenericPackageDescription {
178177
(OrLaterVersion
179178
(mkVersion [2, 4, 0]))
180179
mainLibSet],
181-
condTreeComponents = []},
182-
condSubLibraries = [],
183-
condForeignLibs = [],
184-
condExecutables = [],
185-
condTestSuites = [
180+
condTreeComponents = []})
181+
[]
182+
[]
183+
[]
184+
[
186185
_×_
187186
(UnqualComponentName
188187
"test_Octree")
@@ -199,11 +198,11 @@ GenericPackageDescription {
199198
buildTools = [],
200199
buildToolDepends = [],
201200
cppOptions = [],
202-
jsppOptions = [],
203201
asmOptions = [],
204202
cmmOptions = [],
205203
ccOptions = [],
206204
cxxOptions = [],
205+
jsppOptions = [],
207206
ldOptions = [],
208207
hsc2hsOptions = [],
209208
pkgconfigDepends = [],
@@ -303,11 +302,11 @@ GenericPackageDescription {
303302
buildTools = [],
304303
buildToolDepends = [],
305304
cppOptions = [],
306-
jsppOptions = [],
307305
asmOptions = [],
308306
cmmOptions = [],
309307
ccOptions = [],
310308
cxxOptions = [],
309+
jsppOptions = [],
311310
ldOptions = [],
312311
hsc2hsOptions = [],
313312
pkgconfigDepends = [],
@@ -400,5 +399,5 @@ GenericPackageDescription {
400399
(PackageName "markdown-unlit")
401400
(OrLaterVersion (mkVersion [0]))
402401
mainLibSet],
403-
condTreeComponents = []}],
404-
condBenchmarks = []}
402+
condTreeComponents = []}]
403+
[]

Cabal-tests/tests/ParserTests/regressions/anynone.expr

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
GenericPackageDescription {
2-
packageDescription =
1+
_×_×_×_×_×_×_×_×_
32
PackageDescription {
43
specVersion = CabalSpecV3_0,
54
package = PackageIdentifier {
@@ -33,10 +32,10 @@ GenericPackageDescription {
3332
extraSrcFiles = [],
3433
extraTmpFiles = [],
3534
extraDocFiles = [],
36-
extraFiles = []},
37-
gpdScannedVersion = Nothing,
38-
genPackageFlags = [],
39-
condLibrary = Just
35+
extraFiles = []}
36+
Nothing
37+
[]
38+
(Just
4039
CondNode {
4140
condTreeData = Library {
4241
libName = LMainLibName,
@@ -52,11 +51,11 @@ GenericPackageDescription {
5251
buildTools = [],
5352
buildToolDepends = [],
5453
cppOptions = [],
55-
jsppOptions = [],
5654
asmOptions = [],
5755
cmmOptions = [],
5856
ccOptions = [],
5957
cxxOptions = [],
58+
jsppOptions = [],
6059
ldOptions = [],
6160
hsc2hsOptions = [],
6261
pkgconfigDepends = [],
@@ -113,9 +112,9 @@ GenericPackageDescription {
113112
(PackageName "base")
114113
(OrLaterVersion (mkVersion [0]))
115114
mainLibSet],
116-
condTreeComponents = []},
117-
condSubLibraries = [],
118-
condForeignLibs = [],
119-
condExecutables = [],
120-
condTestSuites = [],
121-
condBenchmarks = []}
115+
condTreeComponents = []})
116+
[]
117+
[]
118+
[]
119+
[]
120+
[]

Cabal-tests/tests/ParserTests/regressions/big-version.expr

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
GenericPackageDescription {
2-
packageDescription =
1+
_×_×_×_×_×_×_×_×_
32
PackageDescription {
43
specVersion = CabalSpecV3_0,
54
package = PackageIdentifier {
@@ -35,10 +34,10 @@ GenericPackageDescription {
3534
extraSrcFiles = [],
3635
extraTmpFiles = [],
3736
extraDocFiles = [],
38-
extraFiles = []},
39-
gpdScannedVersion = Nothing,
40-
genPackageFlags = [],
41-
condLibrary = Just
37+
extraFiles = []}
38+
Nothing
39+
[]
40+
(Just
4241
CondNode {
4342
condTreeData = Library {
4443
libName = LMainLibName,
@@ -53,11 +52,11 @@ GenericPackageDescription {
5352
buildTools = [],
5453
buildToolDepends = [],
5554
cppOptions = [],
56-
jsppOptions = [],
5755
asmOptions = [],
5856
cmmOptions = [],
5957
ccOptions = [],
6058
cxxOptions = [],
59+
jsppOptions = [],
6160
ldOptions = [],
6261
hsc2hsOptions = [],
6362
pkgconfigDepends = [],
@@ -106,9 +105,9 @@ GenericPackageDescription {
106105
targetBuildDepends = [],
107106
mixins = []}},
108107
condTreeConstraints = [],
109-
condTreeComponents = []},
110-
condSubLibraries = [],
111-
condForeignLibs = [],
112-
condExecutables = [],
113-
condTestSuites = [],
114-
condBenchmarks = []}
108+
condTreeComponents = []})
109+
[]
110+
[]
111+
[]
112+
[]
113+
[]

0 commit comments

Comments
 (0)