quox/tests/TAP.idr

290 lines
6.7 KiB
Idris
Raw Normal View History

module TAP
2022-05-24 06:09:17 -04:00
-- [todo] extract this to its own package?
2022-05-24 06:09:17 -04:00
import Options
import public Control.Monad.Either
import Data.String
2022-05-24 06:09:17 -04:00
import Data.List
import Data.List.Elem
2022-05-04 11:09:49 -04:00
import Data.SnocList
import Control.Monad.Reader
import Control.Monad.State
2022-05-24 06:09:17 -04:00
import Control.ANSI
import System
2022-05-24 06:09:17 -04:00
%default total
2022-04-27 09:07:32 -04:00
public export
Info : Type
Info = List (String, String)
private
data Result = Tried Bool Info | Skip String | Todo String
private
record TestBase where
constructor MakeTest
label : String
run : IO Result
2022-04-27 08:57:41 -04:00
private
toLines1 : (String, String) -> List String
toLines1 (k, v) =
let vs = lines v in
if length vs == 1
then ["\{k}: \{v}"]
else "\{k}: |" :: map (indent 2) vs
private
toLines : Info -> List String
toLines [] = []
2022-04-27 08:57:41 -04:00
toLines xs = "---" :: concatMap toLines1 xs <+> ["..."]
public export interface ToInfo e where toInfo : e -> Info
2022-05-24 06:09:17 -04:00
export ToInfo () where toInfo () = []
2022-05-24 06:09:17 -04:00
export Show a => ToInfo (List (String, a)) where toInfo = map (map show)
2022-04-27 14:06:39 -04:00
export
2022-05-25 10:04:51 -04:00
data Test
= One TestBase
| Group String (List Test)
| Note String
export
isRealTest : Test -> Bool
isRealTest (One _) = True
isRealTest (Group _ _) = True
isRealTest (Note _) = False
2022-05-24 06:09:17 -04:00
private
result : ToInfo a => Bool -> a -> IO Result
result ok = pure . Tried ok . toInfo
2022-04-27 14:06:39 -04:00
2022-05-24 06:09:17 -04:00
private
2022-04-27 14:06:39 -04:00
lazyToIO : Lazy a -> IO a
lazyToIO val = primIO $ \w => MkIORes (force val) w
export
testIO : (ToInfo e, ToInfo a) => String -> EitherT e IO a -> Test
testIO label act = One $ MakeTest label $ do
case !(runEitherT act) of
2022-05-24 06:09:17 -04:00
Right val => result True val
Left err => result False err
2022-05-24 06:09:17 -04:00
export
test : (ToInfo e, ToInfo a) => String -> Lazy (Either e a) -> Test
2022-05-24 06:09:17 -04:00
test label val = testIO label $ MkEitherT $ lazyToIO val
2022-05-24 06:09:17 -04:00
export
todoWith : String -> String -> Test
todoWith label reason = One $ MakeTest label $ pure $ Todo reason
2022-05-24 06:09:17 -04:00
export
todo : String -> Test
todo label = todoWith label ""
2022-05-24 06:09:17 -04:00
private
2022-05-01 20:27:03 -04:00
makeSkip : String -> String -> Test
makeSkip label reason = One $ MakeTest label $ pure $ Skip reason
2022-05-24 06:09:17 -04:00
export
2022-05-01 20:27:03 -04:00
skipWith : Test -> String -> Test
skipWith (One t) reason = makeSkip t.label reason
skipWith (Group l _) reason = makeSkip l reason
2022-05-25 10:04:51 -04:00
skipWith (Note n) _ = Note n
2022-05-24 06:09:17 -04:00
export
2022-05-01 20:27:03 -04:00
skip : Test -> Test
skip test = skipWith test ""
2022-04-27 14:06:39 -04:00
export
testThrows : (ToInfo e, Show a) =>
String -> (e -> Bool) -> Lazy (Either e a) -> Test
testThrows label p act = One $ MakeTest label $ do
case !(lazyToIO act) of
2022-05-24 06:09:17 -04:00
Left err => if p err then result True () else result False err
Right val => result False [("success", val)]
2022-04-27 14:06:39 -04:00
infix 1 :-
2022-05-24 06:09:17 -04:00
export
(:-) : String -> List Test -> Test
(:-) = Group
2022-05-24 06:09:17 -04:00
export
2022-04-27 14:06:39 -04:00
bailOut : Test
bailOut = One $ MakeTest "bail out" $ do
putStrLn "Bail out!"
exitFailure
2022-05-25 10:04:51 -04:00
export
note : String -> Test
note = Note
2022-05-24 06:09:17 -04:00
export
2022-05-25 10:04:51 -04:00
header : List Test -> String
header tests =
let count = length $ filter isRealTest tests in
"1..\{show count}"
2022-05-24 06:09:17 -04:00
private
withPrefix : SnocList String -> TestBase -> Test
2022-05-25 10:05:20 -04:00
withPrefix pfx = One . {label $= (makePrefix pfx ++)}
where makePrefix = concatMap $ \s => "\{s} "
mutual
2022-05-24 06:09:17 -04:00
export
flattenWith : SnocList String -> List Test -> List Test
2022-05-24 06:09:17 -04:00
flattenWith pfx tests =
concatMap (\t => flatten1With pfx (assert_smaller tests t)) tests
export
flatten1With : SnocList String -> Test -> List Test
flatten1With pfx (One t) = [withPrefix pfx t]
flatten1With pfx (Group x ts) = flattenWith (pfx :< x) ts
2022-05-25 10:04:51 -04:00
flatten1With pfx (Note n) = [Note n]
2022-05-24 06:09:17 -04:00
export
flatten : List Test -> List Test
flatten = flattenWith [<]
2022-05-24 06:09:17 -04:00
export
flatten1 : Test -> List Test
flatten1 = flatten1With [<]
2022-05-24 06:09:17 -04:00
private
record RunnerEnv where
constructor RE
indent : Nat
color : Bool
private
Runner : Type -> Type
2022-05-24 06:09:17 -04:00
Runner = ReaderT RunnerEnv IO
2022-05-24 06:09:17 -04:00
private
putIndentLines : List String -> Runner ()
2022-05-24 06:09:17 -04:00
putIndentLines xs = traverse_ (putStrLn . indent (!ask).indent) xs
2022-05-24 06:09:17 -04:00
private
isOk : Bool -> String
isOk b = if b then "ok" else "not ok"
2022-05-24 06:09:17 -04:00
private
toBool : Result -> Bool
toBool (Tried ok _) = ok
2022-05-24 06:09:17 -04:00
toBool _ = True
private
2022-05-25 10:04:51 -04:00
numbered : (a -> Bool) -> List a -> List (Nat, a)
numbered p = go 1 where
go : Nat -> List a -> List (Nat, a)
go _ [] = []
2022-05-25 10:04:51 -04:00
go i (x :: xs) =
if p x then (i, x) :: go (S i) xs
else (0, x) :: go i xs
2022-05-24 06:09:17 -04:00
private
col : Color -> String -> Runner String
col c str = pure $ if (!ask).color then show $ colored c str else str
private
putColor : Color -> String -> Runner ()
putColor c str = putIndentLines [!(col c str)]
private
okCol : Bool -> Color
okCol True = Green
okCol False = Red
private
putOk' : Color -> Bool -> Nat -> String -> Runner ()
putOk' c ok index label =
putIndentLines [!(col c "\{isOk ok} \{show index}") ++ " - \{label}"]
private
putOk : Bool -> Nat -> String -> Runner ()
putOk ok = putOk' (okCol ok) ok
private
putVersion : TAPVersion -> Runner ()
putVersion ver = putColor Cyan "TAP version \{show ver}"
private
run1' : (Nat, TestBase) -> Runner Bool
run1' (index, test) = do
res <- liftIO test.run
case res of
2022-04-27 09:03:29 -04:00
Tried ok info => do
2022-05-24 06:09:17 -04:00
putOk ok index test.label
local {indent $= plus 2} $ putIndentLines $ toLines info
Skip reason =>
putOk' Yellow True index "test.label # skip \{reason}"
Todo reason =>
putOk' Yellow True index "test.label # todo \{reason}"
pure $ toBool res
mutual
private
run' : (Nat, Test) -> Runner Bool
run' (index, One test) = run1' (index, test)
run' (index, Group label tests) = do
2022-05-25 10:05:20 -04:00
putIndentLines [!(col Magenta "# Subtest: ") ++ label]
2022-05-24 06:09:17 -04:00
res <- local {indent $= plus 4} $ runList tests
putOk res index label
pure res
2022-05-25 10:04:51 -04:00
run' (_, Note note) = do
putIndentLines [!(col Magenta "# ") ++ note]
pure True
private
runList : List Test -> Runner Bool
runList tests = do
2022-05-24 06:09:17 -04:00
putColor Cyan $ header tests
2022-05-25 10:04:51 -04:00
let tests' = numbered isRealTest tests
all id <$> traverse (\t => run' (assert_smaller tests t)) tests'
2022-05-24 06:09:17 -04:00
mutual
export
filterMatch : Maybe String -> List Test -> List Test
filterMatch Nothing tests = tests
filterMatch (Just pat) tests =
mapMaybe (\t => filterMatch1 pat (assert_smaller tests t)) tests
export
filterMatch1 : String -> Test -> Maybe Test
filterMatch1 pat test@(One base) =
guard (pat `isInfixOf` base.label) $> test
filterMatch1 pat all@(Group label tests) =
if pat `isInfixOf` label then Just all else
case filterMatch (Just pat) tests of
[] => Nothing
res => Just $ Group label res
2022-05-25 10:04:51 -04:00
filterMatch1 pat note@(Note _) = Just note
export
2022-05-24 06:09:17 -04:00
main : Options -> List Test -> IO ExitCode
main opts tests = do
let tests = filterMatch opts.pattern $
case opts.version of V13 => flatten tests; V14 => tests
let act = do putVersion opts.version; runList tests
pure $ if !(runReaderT (RE 0 opts.color) act)
then ExitSuccess
else ExitFailure 70