Copyright | Plow Technologies LLC |
---|---|
License | BSD3 |
Maintainer | mchaver@gmail.com |
Stability | Beta |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Test.QuickCheck.Arbitrary.ADT
Description
Type classes to assist random generation of values for various types of abstract data types.
Synopsis
- data ConstructorArbitraryPair a = ConstructorArbitraryPair {
- capConstructor :: String
- capArbitrary :: a
- data ADTArbitrarySingleton a = ADTArbitrarySingleton {}
- data ADTArbitrary a = ADTArbitrary {}
- class ToADTArbitrary a where
- toADTArbitrarySingleton :: Proxy a -> Gen (ADTArbitrarySingleton a)
- toADTArbitrary :: Proxy a -> Gen (ADTArbitrary a)
- class GToADTArbitrarySingleton rep where
- gToADTArbitrarySingleton :: Proxy rep -> Gen (ADTArbitrarySingleton (rep a))
- class GToADTArbitrary rep where
- gToADTArbitrary :: Proxy rep -> Gen (ADTArbitrary (rep a))
- class GArbitrary rep where
- gArbitrary :: Gen (rep a)
- genericArbitrary :: (Generic a, GArbitrary (Rep a)) => Gen a
How to use this library
How to use ToADTArbitrary
with Generic.
{-# LANGUAGE DeriveGeneric #-} import Data.Proxy import GHC.Generics import Test.QuickCheck import Test.QuickCheck.Arbitrary.ADT -- Sum Type, multiple constructors with parameters data Fruit = Apple Int | Orange String Int | PassionFruit Int String Int deriving (Generic, Show) -- Product Type, single constructor data Person = Person { name :: String , age :: Int } deriving (Generic, Show)
Any type that implements ToADTArbitrary
must also implement Arbitrary
.
These examples all require that the data type is an instance of Generic
.
instance Arbitrary Fruit where arbitrary =genericArbitrary
instanceToADTArbitrary
Fruit instance Arbitrary Person where arbitrary =genericArbitrary
instanceToADTArbitrary
Person
Now we can use toADTArbitrarySingleton
to produce an arbitrary value of
one random constructor along with some metadata. toADTArbitrary
will
produce an arbitrary value for each constructor and return it along with
a String of the constructor name.
λ> generate (toADTArbitrarySingleton (Proxy :: Proxy Fruit)) ADTArbitrarySingleton { adtasModuleName = "Ghci1" , adtasTypeName = "Fruit" , adtasCAP = ConstructorArbitraryPair { capConstructor = "Apple", capArbitrary = Apple 30}} λ> generate (toADTArbitrary (Proxy :: Proxy Fruit)) ADTArbitrary { adtModuleName = "Ghci1" , adtTypeName = "Fruit" , adtCAPs = [ ConstructorArbitraryPair { capConstructor = "Apple" , capArbitrary = Apple 17} , ConstructorArbitraryPair { capConstructor = "Orange" , capArbitrary = Orange "abcdef" 18} , ConstructorArbitraryPair { capConstructor = "PassionFruit" , capArbitrary = PassionFruit 16 "datadata" 6}]} λ> generate (toADTArbitrarySingleton (Proxy :: Proxy Person)) ADTArbitrarySingleton { adtasModuleName = "Ghci1" , adtasTypeName = "Person" , adtasCAP = ConstructorArbitraryPair {capConstructor = "Person", capArbitrary = Person {name = "John Doe", age = 30}}} λ> generate (toADTArbitrary (Proxy :: Proxy Person)) ADTArbitrary { adtModuleName = "Ghci1" , adtTypeName = "Person" , adtCAPs = [ConstructorArbitraryPair {capConstructor = "Person", capArbitrary = Person {name = "Jane Doe", age = 15}}]}
Data types
data ConstructorArbitraryPair a Source #
ConstructorArbitraryPair holds the construct name as a string and an arbitrary instance of that constructor.
Constructors
ConstructorArbitraryPair | |
Fields
|
Instances
data ADTArbitrarySingleton a Source #
ADTArbitrarySingleton holds the type name and one ConstructorArbitraryPair.
Constructors
ADTArbitrarySingleton | |
Fields |
Instances
data ADTArbitrary a Source #
ADTArbitrary holds the type name and a ConstructorArbitraryPair for each constructor.
Constructors
ADTArbitrary | |
Fields
|
Instances
Type classes
class ToADTArbitrary a where Source #
ToADTArbitrary generalizes the production of arbitrary values for Sum types. and Product types.
Minimal complete definition
Nothing
Methods
toADTArbitrarySingleton :: Proxy a -> Gen (ADTArbitrarySingleton a) Source #
produce an arbitrary instance of one random constructor
default toADTArbitrarySingleton :: (Generic a, GToADTArbitrarySingleton (Rep a)) => Proxy a -> Gen (ADTArbitrarySingleton a) Source #
toADTArbitrary :: Proxy a -> Gen (ADTArbitrary a) Source #
produce an arbitrary instance for each constructor in type a.
default toADTArbitrary :: (Generic a, GToADTArbitrary (Rep a)) => Proxy a -> Gen (ADTArbitrary a) Source #
Generic type classes
class GToADTArbitrarySingleton rep where Source #
GToADTArbitrarySingleton creates an arbitrary value and returns the name of the constructor that was used to create it and the type name.
Methods
gToADTArbitrarySingleton :: Proxy rep -> Gen (ADTArbitrarySingleton (rep a)) Source #
Instances
class GToADTArbitrary rep where Source #
GToADTArbitrary is a typeclass for generalizing the creation of a list of arbitrary values for each constructor of a type. It also returns the name of the constructor and the type name for reference and file creation.
Methods
gToADTArbitrary :: Proxy rep -> Gen (ADTArbitrary (rep a)) Source #
Instances
class GArbitrary rep where Source #
GArbitrary is a typeclass for generalizing the creation of single arbitrary
product and sum types. It creates an arbitrary generating function of this
style: TypeName <$> arbitrary <*> arbitrary
.
Methods
gArbitrary :: Gen (rep a) Source #
Instances
GArbitrary (U1 :: Type -> Type) Source # | |
Defined in Test.QuickCheck.Arbitrary.ADT Methods gArbitrary :: Gen (U1 a) Source # | |
Arbitrary a => GArbitrary (K1 i a :: Type -> Type) Source # | |
Defined in Test.QuickCheck.Arbitrary.ADT Methods gArbitrary :: Gen (K1 i a a0) Source # | |
(GArbitrary l, GArbitrary r) => GArbitrary (l :+: r) Source # | |
Defined in Test.QuickCheck.Arbitrary.ADT Methods gArbitrary :: Gen ((l :+: r) a) Source # | |
(GArbitrary l, GArbitrary r) => GArbitrary (l :*: r) Source # | |
Defined in Test.QuickCheck.Arbitrary.ADT Methods gArbitrary :: Gen ((l :*: r) a) Source # | |
GArbitrary rep => GArbitrary (M1 i t rep) Source # | |
Defined in Test.QuickCheck.Arbitrary.ADT Methods gArbitrary :: Gen (M1 i t rep a) Source # |
genericArbitrary :: (Generic a, GArbitrary (Rep a)) => Gen a Source #
Create a arbitrary generator for a specified a type in a naive way. Please be careful when using this function, particularly for recursive types.