generic-arbitrary (empty) → 0.1.0
raw patch · 5 files changed
+116/−0 lines, 5 filesdep +QuickCheckdep +basesetup-changed
Dependencies added: QuickCheck, base
Files
- CHANGELOG.md +4/−0
- LICENSE +20/−0
- Setup.hs +2/−0
- generic-arbitrary.cabal +23/−0
- src/Test/QuickCheck/Arbitrary/Generic.hs +67/−0
+ CHANGELOG.md view
@@ -0,0 +1,4 @@+0.1.0+-----++* Public release
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2016 Typeable.io contributors++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ generic-arbitrary.cabal view
@@ -0,0 +1,23 @@+name: generic-arbitrary+version: 0.1.0+synopsis: Generic implementation for QuickCheck's Arbitrary+description:+ Generic implementations of methods of the 'Arbitrary' class from the+ QuickCheck library. The approach taken here can lead to diverging instances+ for recursive types but is safe for non-recursive ones and guarantees+ flat distribution for constructors of sum-types.++license: MIT+license-file: LICENSE+author: Typeable.io contributors+maintainer: makeit@typeable.io+category: Generic+build-type: Simple+extra-source-files: CHANGELOG.md+cabal-version: >=1.22++library+ exposed-modules: Test.QuickCheck.Arbitrary.Generic+ build-depends: QuickCheck >= 2.8, base >=4.8 && <5+ hs-source-dirs: src+ default-language: Haskell2010
+ src/Test/QuickCheck/Arbitrary/Generic.hs view
@@ -0,0 +1,67 @@+{-# LANGUAGE UndecidableInstances, TypeOperators, DataKinds, TypeFamilies, ScopedTypeVariables#-}++{- |++Generic implementation of the 'arbitrary' method. Example usage:++@+data Foo = Foo+ { _fooX :: X+ , _fooY :: Y+ } deriving (Generic)++instance Arbitrary Foo where+ arbitrary = genericArbitrary+ shrink = genericShrink+@++The generated 'arbitrary' method is equivalent to++@Foo <$> arbitrary <*> arbitrary@.++-}++module Test.QuickCheck.Arbitrary.Generic+ ( Arbitrary(..)+ , genericArbitrary+ , genericShrink+ ) where++import Control.Applicative+import Data.Proxy+import GHC.Generics as G+import GHC.TypeLits+import Test.QuickCheck as QC++class GArbitrary a where+ gArbitrary :: QC.Gen (a x)++instance GArbitrary G.U1 where+ gArbitrary = pure G.U1++instance Arbitrary c => GArbitrary (G.K1 i c) where+ gArbitrary = G.K1 <$> arbitrary++instance GArbitrary f => GArbitrary (G.M1 i c f) where+ gArbitrary = G.M1 <$> gArbitrary++instance (GArbitrary a, GArbitrary b) => GArbitrary (a G.:*: b) where+ gArbitrary = liftA2 (G.:*:) gArbitrary gArbitrary++-- | Calculates count of constructors encoded by particular ':+:'.+-- Internal use only.+type family SumLen a :: Nat where+ SumLen (a G.:+: b) = (SumLen a) + (SumLen b)+ SumLen a = 1++instance (GArbitrary a, GArbitrary b, KnownNat (SumLen a), KnownNat (SumLen b)+ ) => GArbitrary (a G.:+: b) where+ gArbitrary = frequency+ [ (lfreq, G.L1 <$> gArbitrary)+ , (rfreq, G.R1 <$> gArbitrary) ]+ where+ lfreq = fromIntegral $ natVal (Proxy :: Proxy (SumLen a))+ rfreq = fromIntegral $ natVal (Proxy :: Proxy (SumLen b))++genericArbitrary :: (Generic a, GArbitrary ga, ga ~ G.Rep a) => Gen a+genericArbitrary = G.to <$> gArbitrary