File: Generic.hs

package info (click to toggle)
haskell-hedgehog-classes 0.2.5.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 508 kB
  • sloc: haskell: 6,010; makefile: 8
file content (45 lines) | stat: -rw-r--r-- 884 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{-# LANGUAGE DeriveGeneric #-}

module Spec.Generic (testGeneric) where

import Hedgehog
import Hedgehog.Classes
import qualified Hedgehog.Gen as Gen

import GHC.Generics (Generic(..))

testGeneric :: [(String, [Laws])]
testGeneric =
  [ ("E", listE)
  , ("Bool", listBool) 
  , ("Maybe Bool", listMaybe) 
  ]

listE :: [Laws]
listE = [genericLaws genE (genRep genE)]

listBool :: [Laws]
listBool = [genericLaws Gen.bool (genRep Gen.bool)]

listMaybe :: [Laws]
listMaybe = [genericLaws (Gen.maybe Gen.bool) (genRep (Gen.maybe Gen.bool))]

data E = E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8
  deriving (Eq, Show, Generic)

genRep :: Generic a => Gen a -> Gen (Rep a ())
genRep gen = do
  x <- gen
  pure (from x)

genE :: Gen E
genE = Gen.frequency
  [ (1, pure E1)
  , (1, pure E2)
  , (1, pure E3)
  , (1, pure E4)
  , (1, pure E5)
  , (1, pure E6)
  , (1, pure E7)
  , (1, pure E8)
  ]