Skip to content

Commit 6907d0f

Browse files
committed
Fail test suite when lawsCheck fails.
1 parent cce8446 commit 6907d0f

1 file changed

Lines changed: 37 additions & 24 deletions

File tree

test/Main.hs

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
{-# LANGUAGE TypeApplications #-}
44
module Main where
55

6+
import Control.Monad (forM_, unless)
7+
import Data.IORef (IORef, newIORef, readIORef, writeIORef)
68
import Data.Proxy
79
import Spec (spec)
10+
import System.Exit (exitFailure)
811
import Test.Hspec (hspec)
912
import Test.QuickCheck
1013
import Test.QuickCheck.Classes
@@ -55,33 +58,43 @@ instance (A.AFType a, Arbitrary a) => Arbitrary (Scalar a) where
5558
x : _ -> shrink x
5659
[] -> []
5760

61+
-- Run a Laws check, print results in the same format as lawsCheck, and mark
62+
-- the IORef False on any failure so we can call exitFailure at the end.
63+
checkLaws :: IORef Bool -> Laws -> IO ()
64+
checkLaws ref laws = do
65+
let cls = lawsTypeclass laws
66+
forM_ (lawsProperties laws) $ \(name, prop) -> do
67+
putStr $ cls ++ ": " ++ name ++ " "
68+
r <- quickCheckWithResult stdArgs { chatty = False } prop
69+
putStr (output r)
70+
unless (isSuccess r) (writeIORef ref False)
71+
5872
main :: IO ()
5973
main = do
60-
hspec spec
74+
ref <- newIORef True
75+
let check = checkLaws ref
6176
-- IEEE 754 is not an exact ring; only Eq laws for floating-point arrays.
62-
lawsCheck (eqLaws (Proxy :: Proxy (Array Double)))
63-
lawsCheck (eqLaws (Proxy :: Proxy (Array Float)))
64-
lawsCheck (showLaws (Proxy :: Proxy (Array Float)))
65-
lawsCheck (showLaws (Proxy :: Proxy (Array Double)))
77+
check (eqLaws (Proxy :: Proxy (Array Double)))
78+
check (eqLaws (Proxy :: Proxy (Array Float)))
6679
-- Complex: Eq only (IEEE 754 + gt/lt undefined for complex numbers).
67-
lawsCheck (eqLaws (Proxy :: Proxy (Array (A.Complex Double))))
68-
lawsCheck (eqLaws (Proxy :: Proxy (Array (A.Complex Float))))
69-
lawsCheck (showLaws (Proxy :: Proxy (Array (A.Complex Double))))
70-
lawsCheck (showLaws (Proxy :: Proxy (Array (A.Complex Float))))
80+
check (eqLaws (Proxy :: Proxy (Array (A.Complex Double))))
81+
check (eqLaws (Proxy :: Proxy (Array (A.Complex Float))))
7182
-- Integral types: exact ring laws via Scalar, Eq laws via multi-dim Array.
72-
intChecks (Proxy :: Proxy Int)
73-
intChecks (Proxy :: Proxy A.Int16)
74-
intChecks (Proxy :: Proxy A.Int32)
75-
intChecks (Proxy :: Proxy A.Int64)
76-
intChecks (Proxy :: Proxy A.Word8)
77-
intChecks (Proxy :: Proxy A.Word16)
78-
intChecks (Proxy :: Proxy A.Word32)
79-
intChecks (Proxy :: Proxy A.Word64)
80-
intChecks (Proxy :: Proxy Word)
81-
intChecks (Proxy :: Proxy A.CBool)
83+
intChecks ref (Proxy :: Proxy Int)
84+
intChecks ref (Proxy :: Proxy A.Int16)
85+
intChecks ref (Proxy :: Proxy A.Int32)
86+
intChecks ref (Proxy :: Proxy A.Int64)
87+
intChecks ref (Proxy :: Proxy A.Word8)
88+
intChecks ref (Proxy :: Proxy A.Word16)
89+
intChecks ref (Proxy :: Proxy A.Word32)
90+
intChecks ref (Proxy :: Proxy A.Word64)
91+
intChecks ref (Proxy :: Proxy Word)
92+
intChecks ref (Proxy :: Proxy A.CBool)
93+
hspec spec
94+
ok <- readIORef ref
95+
unless ok exitFailure
8296

83-
intChecks :: forall a. (A.AFType a, Arbitrary a, Num a, Eq a) => Proxy a -> IO ()
84-
intChecks _ = do
85-
lawsCheck (showLaws (Proxy :: Proxy (Array a)))
86-
lawsCheck (numLaws (Proxy :: Proxy (Scalar a)))
87-
lawsCheck (eqLaws (Proxy :: Proxy (Array a)))
97+
intChecks :: forall a. (A.AFType a, Arbitrary a, Num a, Eq a) => IORef Bool -> Proxy a -> IO ()
98+
intChecks ref _ = do
99+
checkLaws ref (numLaws (Proxy :: Proxy (Scalar a)))
100+
checkLaws ref (eqLaws (Proxy :: Proxy (Array a)))

0 commit comments

Comments
 (0)