Skip to content

Commit 3c0ff33

Browse files
authored
Merge pull request #6783 from phadej/build-reports-parsec
Change BuildReports parse/pretty to use FieldGrammar framework
2 parents c753f62 + 288687e commit 3c0ff33

File tree

17 files changed

+459
-277
lines changed

17 files changed

+459
-277
lines changed

Cabal/Cabal-quickcheck/src/Test/QuickCheck/Instances/Cabal.hs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
{-# LANGUAGE CPP #-}
1+
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE FlexibleContexts #-}
3+
{-# LANGUAGE GADTs #-}
4+
{-# LANGUAGE TypeOperators #-}
25
{-# OPTIONS_GHC -fno-warn-orphans #-}
36
module Test.QuickCheck.Instances.Cabal () where
47

@@ -8,7 +11,10 @@ import Data.List (intercalate)
811
import Distribution.Utils.Generic (lowercase)
912
import Test.QuickCheck
1013

14+
import GHC.Generics
15+
1116
import Distribution.CabalSpecVersion
17+
import Distribution.Compiler
1218
import Distribution.ModuleName
1319
import Distribution.Parsec.Newtypes
1420
import Distribution.Simple.Flag (Flag (..))
@@ -311,6 +317,17 @@ instance Arbitrary LicenseExpression where
311317
shrink (EOr a b) = a : b : map (uncurry EOr) (shrink (a, b))
312318
shrink _ = []
313319

320+
-------------------------------------------------------------------------------
321+
-- Compiler
322+
-------------------------------------------------------------------------------
323+
324+
instance Arbitrary CompilerFlavor where
325+
arbitrary = elements knownCompilerFlavors
326+
327+
instance Arbitrary CompilerId where
328+
arbitrary = genericArbitrary
329+
shrink = genericShrink
330+
314331
-------------------------------------------------------------------------------
315332
-- Helpers
316333
-------------------------------------------------------------------------------
@@ -319,3 +336,38 @@ shortListOf1 :: Int -> Gen a -> Gen [a]
319336
shortListOf1 bound gen = sized $ \n -> do
320337
k <- choose (1, 1 `max` ((n `div` 2) `min` bound))
321338
vectorOf k gen
339+
340+
-------------------------------------------------------------------------------
341+
-- Generic Arbitrary
342+
-------------------------------------------------------------------------------
343+
344+
-- Generic arbitary for non-recursive types
345+
genericArbitrary :: (Generic a, GArbitrary (Rep a)) => Gen a
346+
genericArbitrary = fmap to garbitrary
347+
348+
class GArbitrary f where
349+
garbitrary :: Gen (f ())
350+
351+
class GArbitrarySum f where
352+
garbitrarySum :: [Gen (f ())]
353+
354+
class GArbitraryProd f where
355+
garbitraryProd :: Gen (f ())
356+
357+
instance (GArbitrarySum f, i ~ D) => GArbitrary (M1 i c f) where
358+
garbitrary = fmap M1 (oneof garbitrarySum)
359+
360+
instance (GArbitraryProd f, i ~ C) => GArbitrarySum (M1 i c f) where
361+
garbitrarySum = [fmap M1 garbitraryProd]
362+
363+
instance (GArbitrarySum f, GArbitrarySum g) => GArbitrarySum (f :+: g) where
364+
garbitrarySum = map (fmap L1) garbitrarySum ++ map (fmap R1) garbitrarySum
365+
366+
instance (GArbitraryProd f, i ~ S) => GArbitraryProd (M1 i c f) where
367+
garbitraryProd = fmap M1 garbitraryProd
368+
369+
instance (GArbitraryProd f, GArbitraryProd g) => GArbitraryProd (f :*: g) where
370+
garbitraryProd = liftA2 (:*:) garbitraryProd garbitraryProd
371+
372+
instance (Arbitrary a) => GArbitraryProd (K1 i a) where
373+
garbitraryProd = fmap K1 arbitrary

Cabal/Distribution/Compiler.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import Distribution.Version (Version, mkVersion', nullVersion)
5959
import qualified System.Info (compilerName, compilerVersion)
6060
import Distribution.Parsec (Parsec (..))
6161
import Distribution.Pretty (Pretty (..), prettyShow)
62+
import Distribution.FieldGrammar.Described
6263
import qualified Distribution.Compat.CharParsing as P
6364
import qualified Text.PrettyPrint as Disp
6465

@@ -89,6 +90,12 @@ instance Parsec CompilerFlavor where
8990
cs <- P.munch1 isAlphaNum
9091
if all isDigit cs then fail "all digits compiler name" else return cs
9192

93+
instance Described CompilerFlavor where
94+
describe _ = REUnion
95+
[ fromString (prettyShow c)
96+
| c <- knownCompilerFlavors
97+
]
98+
9299
classifyCompilerFlavor :: String -> CompilerFlavor
93100
classifyCompilerFlavor s =
94101
fromMaybe (OtherCompiler s) $ lookup (lowercase s) compilerMap
@@ -165,6 +172,12 @@ instance Parsec CompilerId where
165172
version <- (P.char '-' >> parsec) <|> return nullVersion
166173
return (CompilerId flavour version)
167174

175+
instance Described CompilerId where
176+
describe _ =
177+
describe (Proxy :: Proxy CompilerFlavor)
178+
<> fromString "-"
179+
<> describe (Proxy :: Proxy Version)
180+
168181
lowercase :: String -> String
169182
lowercase = map toLower
170183

Cabal/tests/UnitTests/Distribution/Described.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ import Test.QuickCheck (Arbitrary (..), Gen, Property, choose, counterexam
1111
import Test.Tasty (TestTree, testGroup)
1212
import Test.Tasty.QuickCheck (testProperty)
1313

14-
import Distribution.FieldGrammar.Described
15-
(Described (..), GrammarRegex (..), reComma, reSpacedComma, reSpacedList)
14+
import Distribution.FieldGrammar.Described (Described (..), GrammarRegex (..), reComma, reSpacedComma, reSpacedList)
1615
import Distribution.Parsec (eitherParsec)
1716
import Distribution.Pretty (prettyShow)
1817

1918
import qualified Distribution.Utils.CharSet as CS
2019

20+
import Distribution.Compiler (CompilerFlavor, CompilerId)
2121
import Distribution.ModuleName (ModuleName)
22+
import Distribution.System (Arch, OS)
2223
import Distribution.Types.Dependency (Dependency)
23-
import Distribution.Types.Flag (FlagName, FlagAssignment)
24+
import Distribution.Types.Flag (FlagAssignment, FlagName)
2425
import Distribution.Types.PackageId (PackageIdentifier)
2526
import Distribution.Types.PackageName (PackageName)
2627
import Distribution.Types.PackageVersionConstraint (PackageVersionConstraint)
2728
import Distribution.Types.Version (Version)
28-
import Distribution.System (OS, Arch)
2929
import Distribution.Types.VersionRange (VersionRange)
3030

3131
import qualified RERE as RE
@@ -47,6 +47,8 @@ tests = testGroup "Described"
4747
, testDescribed (Proxy :: Proxy ModuleName)
4848
, testDescribed (Proxy :: Proxy OS)
4949
, testDescribed (Proxy :: Proxy Arch)
50+
, testDescribed (Proxy :: Proxy CompilerFlavor)
51+
, testDescribed (Proxy :: Proxy CompilerId)
5052
]
5153

5254
-------------------------------------------------------------------------------

0 commit comments

Comments
 (0)