{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE TypeApplications #-}

-- | Tests for Arbitrary instances involving Validity
--
-- You will need @TypeApplications@ to use these.
module Test.Syd.Validity.Arbitrary
    ( arbitrarySpec
    , arbitraryGeneratesOnlyValid
    ) where

import Data.Data

import Data.GenValidity

import Test.Syd
import Test.QuickCheck

import Test.Syd.Validity.GenValidity
import Test.Syd.Validity.Utils

-- | A @Spec@ that specifies that @arbitrary@ only generates data that
-- satisfy @isValid@
--
-- Example usage:
--
-- > arbitrarySpec @Int
arbitrarySpec ::
       forall a. (Typeable a, Show a, Validity a, Arbitrary a)
    => Spec
arbitrarySpec :: Spec
arbitrarySpec = do
    let name :: String
name = Typeable a => String
forall k (a :: k). Typeable a => String
nameOf @a
    String -> Spec -> Spec
forall (outers :: [*]) inner.
String -> TestDefM outers inner () -> TestDefM outers inner ()
describe (String
"Arbitrary " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
name) (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$
        String -> Spec -> Spec
forall (outers :: [*]) inner.
String -> TestDefM outers inner () -> TestDefM outers inner ()
describe (String
"arbitrary :: Gen " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
name) (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$
        String -> Property -> Spec
forall (outers :: [*]) inner test.
(HasCallStack, IsTest test, Arg1 test ~ (), Arg2 test ~ inner) =>
String -> test -> TestDefM outers inner ()
it String
"only generates valid values" (Property -> Spec) -> Property -> Spec
forall a b. (a -> b) -> a -> b
$ (Show a, Validity a, Arbitrary a) => Property
forall a. (Show a, Validity a, Arbitrary a) => Property
arbitraryGeneratesOnlyValid @a

-- | @arbitrary@ only generates valid data
--
-- prop> arbitraryGeneratesOnlyValid @Int
arbitraryGeneratesOnlyValid ::
       forall a. (Show a, Validity a, Arbitrary a)
    => Property
arbitraryGeneratesOnlyValid :: Property
arbitraryGeneratesOnlyValid = Gen a -> Property
forall a. (Show a, Validity a) => Gen a -> Property
genGeneratesValid @a Gen a
forall a. Arbitrary a => Gen a
arbitrary