Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of how to use with type that don't have Generic instances? #1

Open
saurabhnanda opened this issue Oct 17, 2020 · 1 comment

Comments

@saurabhnanda
Copy link

I'm trying to get this to work with the following types:

import Data.UUID

data EventId = EventId { rawEventId :: UUID } deriving (Eq, Show)

data Event = Event
  { evtId :: !EventId
   , evtFoo:: !String
   } deriving (Eq, Show, Generic)

I've tried all possible combinations of mkGenWith, byType and byField, but cannot get anything to compile. An example would be much appreciated.

@mageshb
Copy link
Contributor

mageshb commented Oct 18, 2020

There was couple of issue with mkGenWith

  1. GenList's (:&) is missing fixity decl, which is forcing us to put parens
  2. It is forcing to provide Gen for primitives (like Int, Text, Word. BTW String is not considered as Primitive) which affects the usability.
    Will push a patch fixing above usability soon. Thanks for reporting

The following code works,

#!/usr/bin/env cabal
{- cabal:
build-depends: base, hedgehog-gen, hedgehog, text, uuid
-}
{-# LANGUAGE DeriveGeneric, TypeApplications, DataKinds, OverloadedStrings #-}
import Hedgehog
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
import Hedgehog.Gen.Generic
import Data.Function
import Data.Text (Text)
import GHC.Generics
import Data.UUID

data Gender = Male | Female | Other
  deriving (Show, Eq, Generic)

data User = User
  { name :: Text
  , age :: Word
  , gender :: Gender
  } deriving (Show, Eq, Generic)

userGen1 :: Hedgehog.Gen User
userGen1 = mkGen emptyGens

userGen2 :: Hedgehog.Gen User
userGen2 = mkGen $ emptyGens
  & byField @User @"age" (Gen.word (Range.constant 1 120))
  & byPos @User @1 (Gen.element ["foo", "bar", "baz"])


---
data EventId = EventId { rawEventId :: UUID } deriving (Eq, Show)

data Event = Event
  { evtId :: !EventId
  , evtFoo:: !String
  } deriving (Eq, Show, Generic)

eventGen :: Hedgehog.Gen Event
eventGen = mkGenWith (eventIdGen :& (stringGen :& GNil)) emptyGens
  where
    eventIdGen = Gen.just $ Gen.element [ fmap EventId $ fromText "c2cc10e1-57d6-4b6f-9899-38d972112d8c"
                                        , fmap EventId $ fromText "550e8400-e29b-41d4-a716-446655440000"
                                        ]
    stringGen :: Gen String
    stringGen = Gen.element ["foo", "bar", "baz"]

main :: IO()
main = do
  print =<< Gen.sample eventGen
Linking /tmp/cabal-repl.-2404/dist-newstyle/build/x86_64-linux/ghc-8.8.4/fake-package-0/x/script/build/script/script ...
Event {evtId = EventId {rawEventId = 550e8400-e29b-41d4-a716-446655440000}, evtFoo = "bar"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants