{-# LANGUAGE GADTs #-}

-- |
-- Module      : Test.Dwergaz
-- Description : A minimal testing library
-- Copyright   : (c) 2017-2022, Henry Till
-- License     : BSD3
-- Maintainer  : henrytill@gmail.com
-- Stability   : experimental
--
-- = Usage:
--
-- See the <https://github.com/henrytill/dwergaz/blob/master/tests/Main.hs tests> for a usage example.
--
module Test.Dwergaz
  ( Test(..)
  , Result
  , isPassed
  , runTest
  ) where


data Test where
  Predicate :: (Eq a, Show a) => String -> (a -> Bool) -> a -> Test
  Expect    :: (Eq a, Show a) => String -> (a -> a -> Bool) -> a -> a -> Test

data Result where
  Passed :: String -> Result
  Failed :: Show a => String -> a -> a -> Result

instance Show Result where
  show :: Result -> String
show (Failed String
n a
e a
a) =
    String
"FAILED:   "     String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
n      String -> ShowS
forall a. [a] -> [a] -> [a]
++
    String
"\nEXPECTED:   " String -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
e String -> ShowS
forall a. [a] -> [a] -> [a]
++
    String
"\nACTUAL:     " String -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
a
  show (Passed String
n) =
    String
"PASSED:   "     String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
n

isPassed :: Result -> Bool
isPassed :: Result -> Bool
isPassed (Passed String
_) = Bool
True
isPassed Result
_          = Bool
False

runTest :: Test -> Result
runTest :: Test -> Result
runTest (Predicate String
n a -> Bool
p a
v)
  | a -> Bool
p a
v                   = String -> Result
Passed String
n
  | Bool
otherwise             = String -> Bool -> Bool -> Result
forall a. Show a => String -> a -> a -> Result
Failed String
n Bool
True Bool
False
runTest (Expect String
n a -> a -> Bool
f a
e a
a)
  | a -> a -> Bool
f a
e a
a                 = String -> Result
Passed String
n
  | Bool
otherwise             = String -> a -> a -> Result
forall a. Show a => String -> a -> a -> Result
Failed String
n a
e a
a