git.iko.soy/typeable/generic-arbitrary
on master help sign in
Deriving via newtype (#8)
commit1fa1e9baa4765aec09b000fa4a493102ff8eb679 authoriko <ilyakooo0@gmail.com> dateSun 17 Jan 2021, 11:12 (6 years ago) parent1ae5965
3 files changed+31−2
generic-arbitrary.cabal+1−1 src/Test/QuickCheck/Arbitrary/Generic.hs+26−1 stack.yaml+4
generic-arbitrary.cabal+1−1
@@ -25,6 +25,6 @@ tested-with: GHC == 7.10.3
2525
2626library
2727 exposed-modules: Test.QuickCheck.Arbitrary.Generic
28− build-depends: QuickCheck >= 2.8, base >=4.8 && <5
28+ build-depends: QuickCheck >= 2.14, base >=4.8 && <5
2929 hs-source-dirs: src
3030 default-language: Haskell2010
src/Test/QuickCheck/Arbitrary/Generic.hs+26−1
@@ -15,6 +15,16 @@ instance Arbitrary Foo where
1515 shrink = genericShrink
1616@
1717
18+This instance can also be derived using DerivingVia language extension
19+
20+@
21+data Foo = Foo
22+ { _fooX :: X
23+ , _fooY :: Y
24+ } deriving (Generic)
25+ deriving (Arbitrary) via GenericArbitrary Foo
26+@
27+
1828The generated 'arbitrary' method is equivalent to
1929
2030@Foo <$> arbitrary <*> arbitrary@.
@@ -22,16 +32,31 @@ The generated 'arbitrary' method is equivalent to
2232-}
2333
2434module Test.QuickCheck.Arbitrary.Generic
25− ( Arbitrary(..)
35+ ( GenericArbitrary(..)
36+ , Arbitrary(..)
2637 , genericArbitrary
2738 , genericShrink
2839 ) where
2940
3041import Control.Applicative
42+import Data.Coerce (coerce)
3143import Data.Proxy
3244import GHC.Generics as G
3345import GHC.TypeLits
3446import Test.QuickCheck as QC
47+import Test.QuickCheck.Arbitrary (GSubterms, RecursivelyShrink)
48+
49+newtype GenericArbitrary a = GenericArbitrary { unGenericArbitrary :: a }
50+ deriving (Show, Eq)
51+
52+instance
53+ ( Generic a,
54+ GArbitrary (Rep a),
55+ RecursivelyShrink (Rep a),
56+ GSubterms (Rep a) a
57+ ) => Arbitrary (GenericArbitrary a) where
58+ arbitrary = coerce (genericArbitrary :: Gen a)
59+ shrink = coerce (genericShrink :: a -> [a])
3560
3661class GArbitrary a where
3762 gArbitrary :: QC.Gen (a x)
stack.yaml+4
@@ -1 +1,5 @@
11resolver: lts-6.30
2+
3+extra-deps:
4+ - QuickCheck-2.14
5+ - splitmix-0.0.5