{-# LANGUAGE ScopedTypeVariables #-}{-# LANGUAGE CPP #-}module Data.InstanceSpec ( spec ) whereimport Data.Dataimport Data.Int#if MIN_VERSION_base(4,9,0)import Data.List.NonEmpty (NonEmpty)#endifimport Data.Ratioimport Data.Wordimport Control.Monadimport Test.Hspecimport Test.QuickCheckimport Control.Applicative (pure)import Data.GenValidityspec :: Specspec = do twoTests (Proxy :: Proxy ()) twoTests (Proxy :: Proxy Bool) twoTests (Proxy :: Proxy Ordering) twoTests (Proxy :: Proxy Char) twoTests (Proxy :: Proxy Word) twoTests (Proxy :: Proxy Word8) twoTests (Proxy :: Proxy Word16) twoTests (Proxy :: Proxy Word32) twoTests (Proxy :: Proxy Word64) twoTests (Proxy :: Proxy Int) twoTests (Proxy :: Proxy Int8) twoTests (Proxy :: Proxy Int16) twoTests (Proxy :: Proxy Int32) twoTests (Proxy :: Proxy Int64) twoTests (Proxy :: Proxy Integer) threeTests (Proxy :: Proxy Float) threeTests (Proxy :: Proxy Double) threeTests (Proxy :: Proxy (Double, Double)) threeTests (Proxy :: Proxy (Double, Double, Double)) threeTests (Proxy :: Proxy (Double, Double, Double, Double)) threeTests (Proxy :: Proxy (Either Double Double)) threeTests (Proxy :: Proxy (Maybe Double)) threeTests (Proxy :: Proxy [Double]) threeTests (Proxy :: Proxy (Ratio Integer)) threeTests (Proxy :: Proxy (Ratio Int))#if MIN_VERSION_base(4,9,0) threeTests (Proxy :: Proxy (NonEmpty Double))#endiftwoTests :: forall a. (Show a, Typeable a, GenValid a) => Proxy a -> SpectwoTests proxy = describe (nameOf proxy) $ do genUncheckedTest proxy genValidTest proxythreeTests :: forall a. (Show a, Typeable a, GenValid a, GenInvalid a) => Proxy a -> SpecthreeTests proxy = describe (nameOf proxy) $ do genUncheckedTest proxy genValidTest proxy genInvalidTest proxygenUncheckedTest :: forall a. (Show a, Typeable a, GenValid a) => Proxy a -> SpecgenUncheckedTest proxy = do it (unwords ["genUnchecked of", nameOf proxy, "does not crash while validating"]) $ forAll genUnchecked $ \a -> case prettyValidation (a :: a) of Right _ -> True Left err -> seq err True it (unwords [ "shrinkUnchecked of" , nameOf proxy , "only produces values that do not crash while validating" ]) $ forAll genUnchecked $ \a -> forM_ (shrinkUnchecked a) $ \v -> case prettyValidation (v :: a) of Right _ -> pure () :: IO () Left err -> seq err $ pure ()genValidTest :: forall a. (Show a, Typeable a, GenValid a) => Proxy a -> SpecgenValidTest proxy = do it (unwords ["genValid of", nameOf proxy, "generates only valid values"]) $ forAll genValid $ \a -> case prettyValidation (a :: a) of Right _ -> pure () Left err -> expectationFailure $ unlines [ "'validate' reported this value to be invalid:" , show a , err , "" ] it (unwords ["shrinkValid of", nameOf proxy, "shrinks to only valid values"]) $ forAll genValid $ \a -> forM_ (shrinkValid a) $ \v -> case prettyValidation (v :: a) of Right _ -> pure () Left err -> expectationFailure $ unlines [ "'validate' reported this value to be invalid:" , show v , err , "" ]genInvalidTest :: forall a. (Show a, Typeable a, GenInvalid a) => Proxy a -> SpecgenInvalidTest proxy = do it (unwords ["genInvalid of", nameOf proxy, "generates only invalid values"]) $ forAll genInvalid $ \a -> case prettyValidation (a :: a) of Right _ -> expectationFailure $ unlines ["'validate' reported this value to be valid: ", show a] Left _ -> pure () it (unwords ["shrinkInvalid of", nameOf proxy, "shrinks to only invalid values"]) $ forAll genInvalid $ \a -> forM_ (shrinkInvalid a) $ \v -> case prettyValidation (v :: a) of Right _ -> expectationFailure $ unlines [ "'validate' reported this value to be valid: " , show v ] Left _ -> pure ()nameOf :: forall a. Typeable a => Proxy a -> StringnameOf = show . typeRep