graphql-engine/server/src-test/Test/QuickCheck/Extended.hs
jkachmar df4ca23a39 server: Splits QuickCheck extension and orphan instance modules
## Description

Hopefully this is relatively self-explanatory: this change splits the helper functions we've used to extend QuickCheck from the orphan instances and generators that we have defined for unit tests. These have now been placed in `Test.QuickCheck.Extended` and `Hasura.QuickCheck.Instances`, respectively.

This change also adds some documentation to the functions defined in `Test.QuickCheck.Extended` in the spirit of similar functions defined by `Test.QuickCheck`, itself.

### Motivation

We should adhere to the existing convention of constructing "extension modules" for common libraries separately from the code that takes advantage of these.

Alone, this wouldn't be a reason to split up `Hasura.Generators`, but we should **also** follow a convention of defining **all** orphan instances in modules whose names clearly indicate that they exist solely for the purpose of exporting these orphan instances (e.g. `Hasura.QuickCheck.Instances`).

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/3747
GitOrigin-RevId: fb856a790b4a39163f81481d4f900fafb1797ea6
2022-02-22 15:33:37 +00:00

63 lines
2.2 KiB
Haskell

module Test.QuickCheck.Extended
( -- * Helpers
distinct,
distinct1,
arbitraryExcluding,
distinctExcluding,
distinctExcluding1,
sublistOf1,
-- * Re-exports
module QuickCheck,
)
where
-------------------------------------------------------------------------------
import Data.Containers.ListUtils (nubOrd)
import Test.QuickCheck as QuickCheck
import Prelude
-------------------------------------------------------------------------------
-- Quickcheck helpers
-- | Generates a list of random length with all duplicate elements removed.
--
-- NOTE: This will generate the list /and then/ remove the values without
-- making any guarantees as to the size or distribution of the final list.
distinct :: (Arbitrary a, Ord a) => Gen [a]
distinct = nubOrd <$> arbitrary
-- | Generates a non-empty list of random length with all duplicate elements
-- removed.
--
-- NOTE: This will generate the list /and then/ remove the values without
-- making any guarantees as to the size or distribution of the final list.
distinct1 :: (Arbitrary a, Ord a) => Gen [a]
distinct1 = nubOrd <$> listOf1 arbitrary
-- | Generates a value, exclusive of any values in the given list.
arbitraryExcluding :: (Arbitrary a, Eq a) => [a] -> Gen a
arbitraryExcluding exclusions = arbitrary `suchThat` (`notElem` exclusions)
-- | Generates a list of random length, with all duplicate elements removed,
-- exclusive of any values in the given list.
--
-- NOTE: This will generate the list /and then/ remove the values without
-- making any guarantees as to the size or distribution of the final list.
distinctExcluding :: (Arbitrary a, Ord a) => [a] -> Gen [a]
distinctExcluding = fmap nubOrd . listOf . arbitraryExcluding
-- | Generates a non-empty list of random length, with all duplicate elements
-- removed, exclusive of any values in the given list.
--
-- NOTE: This will generate the list /and then/ remove the values without
-- making any guarantees as to the size or distribution of the final list.
distinctExcluding1 :: (Arbitrary a, Ord a) => [a] -> Gen [a]
distinctExcluding1 = fmap nubOrd . listOf1 . arbitraryExcluding
-- | Generates a random, non-empty subsequence of the given list.
sublistOf1 :: [a] -> Gen [a]
sublistOf1 xs = sublistOf xs `suchThat` (not . null)