packages feed

coapplicative-0.1.0.0: test/Main.hs

{-# LANGUAGE DeriveAnyClass, DeriveGeneric, DeriveFunctor, DerivingStrategies, DerivingVia #-}
{-# LANGUAGE OverloadedStrings #-}

module Main (main) where

import GHC.Generics
import Control.CoApplicative
import Data.List.NonEmpty
import Data.Functor.Sum
import Data.Functor.Identity
import Control.Comonad
import Data.Bifunctor

import Hedgehog
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range

data Ex a
  = A (Sum Identity Identity a)
  | B a
  | C (NonEmpty a)
  | D (Int, String, a)
  deriving stock (Generic, Generic1, Functor, Show)
  deriving CoApplicative via (Generically1 Ex)

testCompiles :: IO ()
testCompiles = print (split (B x))
 where
  x :: Either Int Bool
  x = Left 4

someInt :: Gen Int
someInt = Gen.int $ Range.constant 0 10

someInt2 :: Gen Int
someInt2 = Gen.int $ Range.constant 11 20

someInt3 :: Gen Int
someInt3 = Gen.int $ Range.constant 21 30

prop_nonEmptyDupSplit :: Property
prop_nonEmptyDupSplit = property $ do
  xs <- forAll $ Gen.nonEmpty (Range.linear 1 20) $ Gen.either someInt someInt
  bimap duplicate duplicate (split xs) === split (fmap split (duplicate xs))

testProps :: IO Bool
testProps =
  checkParallel $ Group "Properties" [
      ("nonempty_dup_split", prop_nonEmptyDupSplit)
    ]

main :: IO ()
main = do
  testCompiles
  _ <- testProps
  pure ()