nick8325 / quickcheck

Automatic testing of Haskell programs.
Other
713 stars 119 forks source link

Combinators that could be PRs #301

Closed isovector closed 4 years ago

isovector commented 4 years ago

I have a few helpful combinators I use in many projects. If you're interested, I'd be happy to open a PR for each.

-- | Equality that discards the test if it doesn't complete immediately. Useful when working with equalities that can have bad asymptotics for certain inputs.
quickly :: (Eq a, Show a) => a -> a -> Property
quickly a b =
  ioProperty
    $ fmap (fromMaybe discard)
    $ timeout 1
    $ evaluate
    $ a `seq`
      b `seq`
      a === b
isovector commented 4 years ago
-- | Ensures a generator produces every possible data constructor.
producesAllValues :: forall a. Data a => Gen a -> Property
producesAllValues gen = ioProperty $ do
  res <-
    labelledExamplesResult $ forAllShow gen (const "") $ \p ->
      classify True (show $ toConstr p) $ True
  let all_cons =
        S.fromList $ fmap show $ dataTypeConstrs $ dataTypeOf @a undefined
      gen_cons = M.keysSet $ classes res
      missing_cons = all_cons S.\\ gen_cons

  pure $ flip whenFail (null missing_cons) $ do
    putStrLn "Generator failed to produce the following data constructors:"
    for_ missing_cons $ putStrLn . mappend "• "
isovector commented 4 years ago
-- | Lifts QuickSpec's observational equality to a property
(=-=) :: (Show t, Show o, Observe t o a) => a -> a -> Property
a =-= b = property $ \t -> observe t a === observe t b
isovector commented 4 years ago

This first one is just within, and the last is already merged into quickspec