Skip to content

Commit

Permalink
Create Cabal-tree-diff package
Browse files Browse the repository at this point in the history
Move Cabal instances from cabal-install TreeDiffInstances into it.
Remove defaultToExprViaShow to get better diffs and dumps

Also rename Cabal-QuickCheck (proper case), and add GenericArbitrary
module there.
  • Loading branch information
phadej committed May 12, 2020
1 parent 22405ee commit 6748dce
Show file tree
Hide file tree
Showing 59 changed files with 2,933 additions and 1,797 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cabal-version: 2.2
name: Cabal-quickcheck
name: Cabal-QuickCheck
version: 3.3.0.0
synopsis: QuickCheck instances for types in Cabal
category: Testing
Expand All @@ -15,4 +15,6 @@ library
, Cabal ^>=3.3.0.0
, QuickCheck ^>=2.13.2

exposed-modules: Test.QuickCheck.Instances.Cabal
exposed-modules:
Test.QuickCheck.GenericArbitrary
Test.QuickCheck.Instances.Cabal
49 changes: 49 additions & 0 deletions Cabal/Cabal-QuickCheck/src/Test/QuickCheck/GenericArbitrary.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeOperators #-}
module Test.QuickCheck.GenericArbitrary (
genericArbitrary,
GArbitrary,
) where

import GHC.Generics
import Test.QuickCheck

#if !MIN_VERSION_base(4,8,0)
import Control.Applicative (pure, (<$>), (<*>))
#endif

-- Generic arbitary for non-recursive types
genericArbitrary :: (Generic a, GArbitrary (Rep a)) => Gen a
genericArbitrary = fmap to garbitrary

class GArbitrary f where
garbitrary :: Gen (f ())

class GArbitrarySum f where
garbitrarySum :: [Gen (f ())]

class GArbitraryProd f where
garbitraryProd :: Gen (f ())

instance (GArbitrarySum f, i ~ D) => GArbitrary (M1 i c f) where
garbitrary = fmap M1 (oneof garbitrarySum)

instance (GArbitraryProd f, i ~ C) => GArbitrarySum (M1 i c f) where
garbitrarySum = [fmap M1 garbitraryProd]

instance (GArbitrarySum f, GArbitrarySum g) => GArbitrarySum (f :+: g) where
garbitrarySum = map (fmap L1) garbitrarySum ++ map (fmap R1) garbitrarySum

instance (GArbitraryProd f, i ~ S) => GArbitraryProd (M1 i c f) where
garbitraryProd = fmap M1 garbitraryProd

instance GArbitraryProd U1 where
garbitraryProd = pure U1

instance (GArbitraryProd f, GArbitraryProd g) => GArbitraryProd (f :*: g) where
garbitraryProd = (:*:) <$> garbitraryProd <*> garbitraryProd

instance (Arbitrary a) => GArbitraryProd (K1 i a) where
garbitraryProd = fmap K1 arbitrary
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeOperators #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Test.QuickCheck.Instances.Cabal () where
Expand All @@ -11,8 +9,6 @@ import Data.List (intercalate)
import Distribution.Utils.Generic (lowercase)
import Test.QuickCheck

import GHC.Generics

import Distribution.CabalSpecVersion
import Distribution.Compiler
import Distribution.ModuleName
Expand All @@ -32,6 +28,8 @@ import Distribution.Types.VersionRange.Internal
import Distribution.Verbosity
import Distribution.Version

import Test.QuickCheck.GenericArbitrary

#if !MIN_VERSION_base(4,8,0)
import Control.Applicative (pure, (<$>), (<*>))
#endif
Expand Down Expand Up @@ -342,38 +340,3 @@ shortListOf1 :: Int -> Gen a -> Gen [a]
shortListOf1 bound gen = sized $ \n -> do
k <- choose (1, 1 `max` ((n `div` 2) `min` bound))
vectorOf k gen

-------------------------------------------------------------------------------
-- Generic Arbitrary
-------------------------------------------------------------------------------

-- Generic arbitary for non-recursive types
genericArbitrary :: (Generic a, GArbitrary (Rep a)) => Gen a
genericArbitrary = fmap to garbitrary

class GArbitrary f where
garbitrary :: Gen (f ())

class GArbitrarySum f where
garbitrarySum :: [Gen (f ())]

class GArbitraryProd f where
garbitraryProd :: Gen (f ())

instance (GArbitrarySum f, i ~ D) => GArbitrary (M1 i c f) where
garbitrary = fmap M1 (oneof garbitrarySum)

instance (GArbitraryProd f, i ~ C) => GArbitrarySum (M1 i c f) where
garbitrarySum = [fmap M1 garbitraryProd]

instance (GArbitrarySum f, GArbitrarySum g) => GArbitrarySum (f :+: g) where
garbitrarySum = map (fmap L1) garbitrarySum ++ map (fmap R1) garbitrarySum

instance (GArbitraryProd f, i ~ S) => GArbitraryProd (M1 i c f) where
garbitraryProd = fmap M1 garbitraryProd

instance (GArbitraryProd f, GArbitraryProd g) => GArbitraryProd (f :*: g) where
garbitraryProd = liftA2 (:*:) garbitraryProd garbitraryProd

instance (Arbitrary a) => GArbitraryProd (K1 i a) where
garbitraryProd = fmap K1 arbitrary
21 changes: 21 additions & 0 deletions Cabal/Cabal-tree-diff/Cabal-tree-diff.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
cabal-version: 2.2
name: Cabal-tree-diff
version: 3.3.0.0
synopsis: QuickCheck instances for types in Cabal
category: Testing
description: Provides tree-diff ToExpr instances for some types in Cabal

library
default-language: Haskell2010
hs-source-dirs: src
ghc-options: -Wall
build-depends:
, base
, Cabal ^>=3.3.0.0
, tree-diff ^>=0.1

exposed-modules: Data.TreeDiff.Instances.Cabal
other-modules:
Data.TreeDiff.Instances.CabalLanguage
Data.TreeDiff.Instances.CabalSPDX
Data.TreeDiff.Instances.CabalVersion
136 changes: 136 additions & 0 deletions Cabal/Cabal-tree-diff/src/Data/TreeDiff/Instances/Cabal.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 800
{-# OPTIONS_GHC -freduction-depth=0 #-}
#else
{-# OPTIONS_GHC -fcontext-stack=151 #-}
#endif
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Data.TreeDiff.Instances.Cabal () where

import Data.TreeDiff

import Data.TreeDiff.Instances.CabalLanguage ()
import Data.TreeDiff.Instances.CabalSPDX ()
import Data.TreeDiff.Instances.CabalVersion ()

-------------------------------------------------------------------------------

import Distribution.Backpack (OpenModule, OpenUnitId)
import Distribution.CabalSpecVersion (CabalSpecVersion)
import Distribution.Compiler (CompilerFlavor, CompilerId, PerCompilerFlavor)
import Distribution.InstalledPackageInfo (AbiDependency, ExposedModule, InstalledPackageInfo)
import Distribution.ModuleName (ModuleName)
import Distribution.Package (Dependency, PackageIdentifier, PackageName)
import Distribution.PackageDescription
import Distribution.Simple.Compiler (DebugInfoLevel, OptimisationLevel, ProfDetailLevel)
import Distribution.Simple.Flag (Flag)
import Distribution.Simple.InstallDirs
import Distribution.Simple.InstallDirs.Internal
import Distribution.Simple.Setup (HaddockTarget, TestShowDetails)
import Distribution.System
import Distribution.Types.AbiHash (AbiHash)
import Distribution.Types.ComponentId (ComponentId)
import Distribution.Types.CondTree
import Distribution.Types.ExecutableScope
import Distribution.Types.ExeDependency
import Distribution.Types.ForeignLib
import Distribution.Types.ForeignLibOption
import Distribution.Types.ForeignLibType
import Distribution.Types.IncludeRenaming (IncludeRenaming)
import Distribution.Types.LegacyExeDependency
import Distribution.Types.LibraryVisibility (LibraryVisibility)
import Distribution.Types.Mixin
import Distribution.Types.PackageVersionConstraint
import Distribution.Types.PkgconfigDependency (PkgconfigDependency)
import Distribution.Types.PkgconfigName (PkgconfigName)
import Distribution.Types.PkgconfigVersion (PkgconfigVersion)
import Distribution.Types.PkgconfigVersionRange (PkgconfigVersionRange)
import Distribution.Types.UnitId (DefUnitId, UnitId)
import Distribution.Types.UnqualComponentName
import Distribution.Utils.NubList (NubList)
import Distribution.Utils.ShortText (ShortText, fromShortText)
import Distribution.Verbosity
import Distribution.Verbosity.Internal

-------------------------------------------------------------------------------
-- instances
-------------------------------------------------------------------------------

instance (Eq a, Show a) => ToExpr (Condition a) where toExpr = defaultExprViaShow
instance (Show a, ToExpr b, ToExpr c, Show b, Show c, Eq a, Eq c, Eq b) => ToExpr (CondTree a b c)
instance (Show a, ToExpr b, ToExpr c, Show b, Show c, Eq a, Eq c, Eq b) => ToExpr (CondBranch a b c)
instance (ToExpr a) => ToExpr (NubList a)
instance (ToExpr a) => ToExpr (Flag a)

instance ToExpr a => ToExpr (PerCompilerFlavor a)

instance ToExpr AbiDependency
instance ToExpr AbiHash
instance ToExpr Arch
instance ToExpr Benchmark
instance ToExpr BenchmarkInterface
instance ToExpr BenchmarkType
instance ToExpr BuildInfo
instance ToExpr BuildType
instance ToExpr CabalSpecVersion
instance ToExpr CompilerFlavor
instance ToExpr CompilerId
instance ToExpr ComponentId
instance ToExpr DebugInfoLevel
instance ToExpr DefUnitId
instance ToExpr Dependency
instance ToExpr ExeDependency
instance ToExpr Executable
instance ToExpr ExecutableScope
instance ToExpr ExposedModule
instance ToExpr FlagAssignment
instance ToExpr FlagName
instance ToExpr ForeignLib
instance ToExpr ForeignLibOption
instance ToExpr ForeignLibType
instance ToExpr GenericPackageDescription
instance ToExpr HaddockTarget
instance ToExpr IncludeRenaming
instance ToExpr InstalledPackageInfo
instance ToExpr KnownRepoType
instance ToExpr LegacyExeDependency
instance ToExpr LibVersionInfo
instance ToExpr Library
instance ToExpr LibraryName
instance ToExpr LibraryVisibility
instance ToExpr Mixin
instance ToExpr ModuleName
instance ToExpr ModuleReexport
instance ToExpr ModuleRenaming
instance ToExpr OS
instance ToExpr OpenModule
instance ToExpr OpenUnitId
instance ToExpr OptimisationLevel
instance ToExpr PackageDescription
instance ToExpr PackageFlag
instance ToExpr PackageIdentifier
instance ToExpr PackageName
instance ToExpr PackageVersionConstraint
instance ToExpr PathComponent
instance ToExpr PathTemplate
instance ToExpr PathTemplateVariable
instance ToExpr PkgconfigDependency
instance ToExpr PkgconfigName
instance ToExpr PkgconfigVersion
instance ToExpr PkgconfigVersionRange
instance ToExpr ProfDetailLevel
instance ToExpr RepoKind
instance ToExpr RepoType
instance ToExpr SetupBuildInfo
instance ToExpr SourceRepo
instance ToExpr TestShowDetails
instance ToExpr TestSuite
instance ToExpr TestSuiteInterface
instance ToExpr TestType
instance ToExpr UnitId
instance ToExpr UnqualComponentName
instance ToExpr Verbosity
instance ToExpr VerbosityFlag
instance ToExpr VerbosityLevel

instance ToExpr ShortText where toExpr = toExpr . fromShortText
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
{-# OPTIONS_GHC -fcontext-stack=151 #-}
#endif
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Instances.TreeDiff.Language where
module Data.TreeDiff.Instances.CabalLanguage () where

import Data.TreeDiff
import Language.Haskell.Extension (Extension, KnownExtension, Language)

-- This are big enums, so they are in separate file.
-- These are big enums, so they are in separate file.
--
instance ToExpr Extension
instance ToExpr KnownExtension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
{-# OPTIONS_GHC -fcontext-stack=151 #-}
#endif
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Instances.TreeDiff.SPDX where
module Data.TreeDiff.Instances.CabalSPDX () where

import Data.TreeDiff
import Distribution.License (License)

import Instances.TreeDiff.Version ()
import Data.TreeDiff.Instances.CabalVersion ()

import qualified Distribution.SPDX as SPDX

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
{-# OPTIONS_GHC -fcontext-stack=151 #-}
#endif
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Instances.TreeDiff.Version where
module Data.TreeDiff.Instances.CabalVersion where

import Data.TreeDiff
import Distribution.Version (Version, VersionRange)
import Distribution.Version (Version, VersionRange, versionNumbers)

instance ToExpr Version where toExpr = defaultExprViaShow
instance ToExpr Version where toExpr v = App "mkVersion" [toExpr $ versionNumbers v]
instance ToExpr VersionRange
26 changes: 12 additions & 14 deletions Cabal/Cabal.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,9 @@ test-suite unit-tests
UnitTests.Orphans

-- Cabal-quickcheck
hs-source-dirs: Cabal-quickcheck/src
hs-source-dirs: Cabal-QuickCheck/src
other-modules:
Test.QuickCheck.GenericArbitrary
Test.QuickCheck.Instances.Cabal

main-is: UnitTests.hs
Expand Down Expand Up @@ -687,11 +688,6 @@ test-suite unit-tests
if !impl(ghc >= 7.10)
build-depends: void

-- Cabal-quickcheck
hs-source-dirs: Cabal-quickcheck/src
other-modules:
Test.QuickCheck.Instances.Cabal

test-suite parser-tests
type: exitcode-stdio-1.0
hs-source-dirs: tests
Expand All @@ -717,11 +713,12 @@ test-suite parser-tests
if impl(ghc >= 7.8)
build-depends:
tree-diff >= 0.1 && <0.2
hs-source-dirs: Cabal-tree-diff/src
other-modules:
Instances.TreeDiff
Instances.TreeDiff.Language
Instances.TreeDiff.SPDX
Instances.TreeDiff.Version
Data.TreeDiff.Instances.Cabal
Data.TreeDiff.Instances.CabalLanguage
Data.TreeDiff.Instances.CabalSPDX
Data.TreeDiff.Instances.CabalVersion

test-suite check-tests
type: exitcode-stdio-1.0
Expand Down Expand Up @@ -786,11 +783,12 @@ test-suite hackage-tests
if impl(ghc >= 7.8)
build-depends:
tree-diff >= 0.1 && <0.2
hs-source-dirs: Cabal-tree-diff/src
other-modules:
Instances.TreeDiff
Instances.TreeDiff.Language
Instances.TreeDiff.SPDX
Instances.TreeDiff.Version
Data.TreeDiff.Instances.Cabal
Data.TreeDiff.Instances.CabalLanguage
Data.TreeDiff.Instances.CabalSPDX
Data.TreeDiff.Instances.CabalVersion

ghc-options: -Wall -rtsopts -threaded
default-extensions: CPP
Expand Down
Loading

0 comments on commit 6748dce

Please sign in to comment.