|
| 1 | +{-# LANGUAGE CPP #-} |
| 2 | +{-# LANGUAGE TemplateHaskell #-} |
| 3 | +{-# LANGUAGE ConstraintKinds #-} |
| 4 | + |
| 5 | +#if __GLASGOW_HASKELL__ >= 800 |
| 6 | +{-# OPTIONS_GHC -Wno-orphans #-} |
| 7 | +#endif |
| 8 | +{-# LANGUAGE FlexibleInstances #-} |
| 9 | + |
| 10 | +module Main (main) where |
| 11 | + |
| 12 | +#ifndef NO_SEMIGROUP_CLASS |
| 13 | +import Data.List.NonEmpty |
| 14 | +import Data.Semigroup (Semigroup (..)) |
| 15 | +#else |
| 16 | +import Data.Monoid (Monoid (..), (<>)) |
| 17 | +#endif |
| 18 | + |
| 19 | +import Test.QuickCheck |
| 20 | + |
| 21 | +#ifdef NO_SEMIGROUP_CLASS |
| 22 | +type Semigroup = Monoid |
| 23 | +sconcat :: Monoid a => [a] -> a |
| 24 | +sconcat = mconcat |
| 25 | +#endif |
| 26 | + |
| 27 | +instance Arbitrary Every where |
| 28 | + arbitrary = oneof [ pure $ Every True |
| 29 | + , pure $ Every False |
| 30 | + , pure $ Every (counterexample "False" False) |
| 31 | + , pure $ Every (counterexample "True" True) |
| 32 | + , pure $ Every (ioProperty (return True)) |
| 33 | + , pure $ Every (ioProperty (return False)) |
| 34 | + , pure $ Every (checkCoverage $ cover 100 True "" True) |
| 35 | + , pure $ Every (checkCoverage $ cover 100 True "" False) |
| 36 | + , pure $ Every (checkCoverage $ cover 100 False "" False) |
| 37 | + ] |
| 38 | + |
| 39 | + |
| 40 | +instance Arbitrary Some where |
| 41 | + arbitrary = oneof [ pure $ Some True |
| 42 | + , pure $ Some False |
| 43 | + , pure $ Some (counterexample "False" False) |
| 44 | + , pure $ Some (counterexample "True" True) |
| 45 | + , pure $ Some (ioProperty (return True)) |
| 46 | + , pure $ Some (ioProperty (return False)) |
| 47 | + , pure $ Some (checkCoverage $ cover 100 True "" True) |
| 48 | + , pure $ Some (checkCoverage $ cover 100 True "" False) |
| 49 | + , pure $ Some (checkCoverage $ cover 100 False "" True) |
| 50 | + , pure $ Some (checkCoverage $ cover 100 False "" False) |
| 51 | + ] |
| 52 | + |
| 53 | + |
| 54 | +newtype Fail a = Fail a |
| 55 | + |
| 56 | +instance Arbitrary (Fail Every) where |
| 57 | + arbitrary = oneof [ Fail <$> (arbitrary :: Gen Every) |
| 58 | + , pure $ Fail $ Every (checkCoverage $ cover 100 False "" True) |
| 59 | + ] |
| 60 | + |
| 61 | + |
| 62 | +check_associative_law :: (Testable p, Semigroup p) => Blind p -> Blind p -> Blind p -> Property |
| 63 | +check_associative_law (Blind a) (Blind b) (Blind c) = ioProperty $ do |
| 64 | + x <- isSuccess <$> quickCheckWithResult args (a <> (b <> c)) |
| 65 | + y <- isSuccess <$> quickCheckWithResult args ((a <> b) <> c) |
| 66 | + return (x == y) |
| 67 | + |
| 68 | + |
| 69 | +#ifndef NO_SEMIGROUP_SUPERCLASS |
| 70 | +check_unit_law :: (Testable p, Monoid p) => Blind p -> Property |
| 71 | +#else |
| 72 | +check_unit_law :: (Testable p, Monoid p, Semigroup p) => Blind p -> Property |
| 73 | +#endif |
| 74 | +check_unit_law (Blind a) = ioProperty $ do |
| 75 | + x <- isSuccess <$> quickCheckWithResult args (a <> mempty) |
| 76 | + y <- isSuccess <$> quickCheckWithResult args (mempty <> a) |
| 77 | + z <- isSuccess <$> quickCheckWithResult args a |
| 78 | + return (x == y .&&. y == z) |
| 79 | + |
| 80 | + |
| 81 | +#ifndef NO_SEMIGROUP_CLASS |
| 82 | +check_sconcat_law :: (Testable p, Semigroup p) => Blind p -> Blind p -> Property |
| 83 | +check_sconcat_law (Blind a) (Blind b) = ioProperty $ do |
| 84 | + x <- isSuccess <$> quickCheckWithResult args (sconcat $ a :| [b]) |
| 85 | + y <- isSuccess <$> quickCheckWithResult args (a <> b) |
| 86 | + return (x == y) |
| 87 | +#endif |
| 88 | + |
| 89 | + |
| 90 | +#ifndef NO_SEMIGROUP_SUPERCLASS |
| 91 | +check_mconcat_law :: (Testable p, Monoid p) => Blind p -> Blind p -> Property |
| 92 | +#else |
| 93 | +check_mconcat_law :: (Testable p, Monoid p, Semigroup p) => Blind p -> Blind p -> Property |
| 94 | +#endif |
| 95 | +check_mconcat_law (Blind a) (Blind b) = ioProperty $ do |
| 96 | + x <- isSuccess <$> quickCheckWithResult args (mconcat [a, b]) |
| 97 | + y <- isSuccess <$> quickCheckWithResult args (a <> b) |
| 98 | + return (x == y) |
| 99 | + |
| 100 | + |
| 101 | +-- |
| 102 | +-- Auxiliary definitions |
| 103 | +-- |
| 104 | + |
| 105 | +args :: Args |
| 106 | +args = stdArgs { chatty = False, maxShrinks = 0 } |
| 107 | + |
| 108 | +-- |
| 109 | +-- Properties |
| 110 | +-- |
| 111 | + |
| 112 | +prop_every_associative :: Blind Every -> Blind Every -> Blind Every -> Property |
| 113 | +prop_every_associative = check_associative_law |
| 114 | + |
| 115 | +prop_every_unit :: Blind Every -> Property |
| 116 | +prop_every_unit = check_unit_law |
| 117 | + |
| 118 | +prop_every_unit_fail :: Blind (Fail Every) -> Property |
| 119 | +prop_every_unit_fail (Blind (Fail a)) = |
| 120 | + expectFailure $ check_unit_law (Blind a) |
| 121 | + |
| 122 | +#ifndef NO_SEMIGROUP_CLASS |
| 123 | +prop_every_sconcat_law :: Blind Every -> Blind Every -> Property |
| 124 | +prop_every_sconcat_law = check_sconcat_law |
| 125 | +#endif |
| 126 | + |
| 127 | +prop_every_mconcat_law :: Blind Every -> Blind Every -> Property |
| 128 | +prop_every_mconcat_law = check_mconcat_law |
| 129 | + |
| 130 | +prop_some_associative :: Blind Some -> Blind Some -> Blind Some -> Property |
| 131 | +prop_some_associative = check_associative_law |
| 132 | + |
| 133 | +prop_some_unit :: Blind Some -> Property |
| 134 | +prop_some_unit = check_unit_law |
| 135 | + |
| 136 | +#ifndef NO_SEMIGROUP_CLASS |
| 137 | +prop_some_sconcat_law :: Blind Some -> Blind Some -> Property |
| 138 | +prop_some_sconcat_law = check_sconcat_law |
| 139 | +#endif |
| 140 | + |
| 141 | +prop_some_mconcat_law :: Blind Some -> Blind Some -> Property |
| 142 | +prop_some_mconcat_law = check_mconcat_law |
| 143 | + |
| 144 | +return [] |
| 145 | +main = $quickCheckAll |
| 146 | + |
0 commit comments