packages feed

hedgehog-generic (empty) → 0.1

raw patch · 5 files changed

+172/−0 lines, 5 filesdep +basedep +hedgehog

Dependencies added: base, hedgehog

Files

+ CHANGELOG.md view
@@ -0,0 +1,12 @@+# Changelog++`hedgehog-generic` uses [PVP Versioning][1].+The changelog is available [on GitHub][2].++0.0.0+=====++* Initially created.++[1]: https://pvp.haskell.org+[2]: https://github.com/chessai/hedgehog-generic/releases
+ LICENSE view
@@ -0,0 +1,29 @@+BSD 3-Clause License++Copyright (c) 2019, chessai+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++1. Redistributions of source code must retain the above copyright notice, this+   list of conditions and the following disclaimer.++2. Redistributions in binary form must reproduce the above copyright notice,+   this list of conditions and the following disclaimer in the documentation+   and/or other materials provided with the distribution.++3. Neither the name of the copyright holder nor the names of its+   contributors may be used to endorse or promote products derived from+   this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,6 @@+# hedgehog-generic++[![Hackage](https://img.shields.io/hackage/v/hedgehog-generic.svg)](https://hackage.haskell.org/package/hedgehog-generic)+[![BSD3 license](https://img.shields.io/badge/license-BSD3-blue.svg)](LICENSE)++GHC Generics automatically derived hedgehog generators
+ hedgehog-generic.cabal view
@@ -0,0 +1,51 @@+cabal-version: 2.4+name:+  hedgehog-generic+version:+  0.1+synopsis:+  GHC Generics automatically derived hedgehog generators+description:+  "Free" hedgehog generators for types that implement Generic+homepage:+  https://github.com/chessai/hedgehog-generic+bug-reports:+  https://github.com/chessai/hedgehog-generic/issues+license:+  BSD-3-Clause+license-file:+  LICENSE+author:+  chessai+maintainer:+  chessai <chessai1996@gmail.com>+copyright:+  © 2019 chessai+category:+  Data,Development,Testing+build-type:+  Simple+extra-doc-files:+    README.md+  , CHANGELOG.md+tested-with:+  GHC == 8.2.2, GHC == 8.4.4, GHC == 8.6.4++library+  hs-source-dirs:+    src+  exposed-modules:+    Hedgehog.Generic+  build-depends:+    , base >= 4.10.1.0 && < 4.13+    , hedgehog >= 0.1 && < 0.7+  ghc-options:+    -Wall+  default-language:+    Haskell2010++source-repository head+  type:+    git+  location:+    https://github.com/chessai/hedgehog-generic.git
+ src/Hedgehog/Generic.hs view
@@ -0,0 +1,74 @@+{-# language DataKinds #-}+{-# language FlexibleContexts #-}+{-# language ScopedTypeVariables #-}+{-# language TypeFamilies #-}+{-# language TypeOperators #-}+{-# language UndecidableInstances #-}++{- |++Generic implementation of a generator. Example usage:++@+data Foo = Foo+  { _fooX :: X+  , _fooY :: Y+  } deriving (Generic)++genFoo :: Gen Foo+genFoo = hgen+@++The generated generator is equivalent to++@Foo \<$\> hgen \<*\> hgen@.++-}++module Hedgehog.Generic+  ( HGen(..)+  , hgen+  ) where++import Control.Applicative (liftA2)+import Data.Proxy (Proxy(..))+import GHC.Generics+import GHC.TypeLits+import Hedgehog+import qualified Hedgehog.Gen as Gen++-- | A class used to generate generators for types implementing 'Generic'.+class HGen a where+  hgen' :: Gen (a x)++instance HGen U1 where+  hgen' = pure U1++instance (Generic c, HGen (Rep c)) => HGen (K1 i c) where+  hgen' = K1 <$> hgen++instance HGen f => HGen (M1 i c f) where+  hgen' = M1 <$> hgen'++instance (HGen a, HGen b) => HGen (a :*: b) where+  hgen' = liftA2 (:*:) hgen' hgen'++instance+  forall a b. (KnownNat (SumLen a), KnownNat (SumLen b), HGen a, HGen b)+  => HGen (a :+: b) where+  hgen' = Gen.frequency+    [ (lfreq, L1 <$> hgen')+    , (rfreq, R1 <$> hgen')+    ]+    where+      lfreq = fromIntegral $ natVal (Proxy :: Proxy (SumLen a))+      rfreq = fromIntegral $ natVal (Proxy :: Proxy (SumLen b))++type family SumLen a :: Nat where+  SumLen (a :+: b) = SumLen a + SumLen b+  SumLen _ = 1++-- | If your type implements 'Generic', you can get a generator for+--   your type 'for free' using this function.+hgen :: (Generic a, HGen (Rep a)) => Gen a+hgen = to <$> hgen'