2022-04-26 12:17:59 -04:00
|
|
|
module TAP
|
2022-05-24 06:09:17 -04:00
|
|
|
-- [todo] extract this to its own package?
|
2022-04-26 12:17:59 -04:00
|
|
|
|
2022-05-24 06:09:17 -04:00
|
|
|
import Options
|
2022-05-06 15:23:58 -04:00
|
|
|
import public Control.Monad.Either
|
2022-04-26 12:17:59 -04:00
|
|
|
import Data.String
|
2022-05-24 06:09:17 -04:00
|
|
|
import Data.List
|
2022-04-26 12:17:59 -04:00
|
|
|
import Data.List.Elem
|
2022-05-04 11:09:49 -04:00
|
|
|
import Data.SnocList
|
2022-04-26 12:17:59 -04:00
|
|
|
import Control.Monad.Reader
|
|
|
|
import Control.Monad.State
|
2022-05-24 06:09:17 -04:00
|
|
|
import Control.ANSI
|
2022-04-27 09:04:10 -04:00
|
|
|
import System
|
2022-04-26 12:17:59 -04:00
|
|
|
|
2022-05-24 06:09:17 -04:00
|
|
|
%default total
|
|
|
|
|
|
|
|
|
2022-04-27 09:07:32 -04:00
|
|
|
public export
|
|
|
|
Info : Type
|
2022-04-26 12:17:59 -04:00
|
|
|
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
|
|
|
|
|
2022-04-26 12:17:59 -04:00
|
|
|
private
|
|
|
|
toLines : Info -> List String
|
|
|
|
toLines [] = []
|
2022-04-27 08:57:41 -04:00
|
|
|
toLines xs = "---" :: concatMap toLines1 xs <+> ["..."]
|
|
|
|
|
2022-04-26 12:17:59 -04:00
|
|
|
|
|
|
|
public export interface ToInfo e where toInfo : e -> Info
|
|
|
|
|
2022-05-24 06:09:17 -04:00
|
|
|
export ToInfo () where toInfo () = []
|
2022-04-26 12:17:59 -04:00
|
|
|
|
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
|
|
|
|
|
|
|
|
2022-04-26 12:17:59 -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-04-26 12:17:59 -04:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-04-26 12:17:59 -04:00
|
|
|
export
|
2022-05-06 15:23:58 -04:00
|
|
|
testIO : (ToInfo e, ToInfo a) => String -> EitherT e IO a -> Test
|
2022-04-26 12:17:59 -04:00
|
|
|
testIO label act = One $ MakeTest label $ do
|
2022-05-06 15:23:58 -04:00
|
|
|
case !(runEitherT act) of
|
2022-05-24 06:09:17 -04:00
|
|
|
Right val => result True val
|
|
|
|
Left err => result False err
|
2022-04-26 12:17:59 -04:00
|
|
|
|
2022-05-24 06:09:17 -04:00
|
|
|
export
|
2022-05-06 15:23:58 -04:00
|
|
|
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-04-26 12:17:59 -04:00
|
|
|
|
2022-05-24 06:09:17 -04:00
|
|
|
export
|
2022-04-26 12:17:59 -04:00
|
|
|
todoWith : String -> String -> Test
|
|
|
|
todoWith label reason = One $ MakeTest label $ pure $ Todo reason
|
|
|
|
|
2022-05-24 06:09:17 -04:00
|
|
|
export
|
2022-04-26 12:17:59 -04:00
|
|
|
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-04-26 12:17:59 -04:00
|
|
|
|
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-26 12:17:59 -04:00
|
|
|
|
2022-04-27 14:06:39 -04:00
|
|
|
export
|
2022-05-06 15:23:58 -04:00
|
|
|
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
|
|
|
|
2022-05-07 15:30:41 -04:00
|
|
|
infix 1 :-
|
2022-05-24 06:09:17 -04:00
|
|
|
export
|
2022-04-26 12:17:59 -04:00
|
|
|
(:-) : 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-04-26 12:17:59 -04:00
|
|
|
|
|
|
|
|
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-04-26 12:17:59 -04:00
|
|
|
|
2022-05-01 18:19:09 -04:00
|
|
|
|
2022-05-24 06:09:17 -04:00
|
|
|
private
|
2022-05-01 18:19:09 -04:00
|
|
|
withPrefix : SnocList String -> TestBase -> Test
|
2022-05-25 10:05:20 -04:00
|
|
|
withPrefix pfx = One . {label $= (makePrefix pfx ++)}
|
|
|
|
where makePrefix = concatMap $ \s => "\{s} ⟫ "
|
2022-05-01 18:19:09 -04:00
|
|
|
|
|
|
|
mutual
|
2022-05-24 06:09:17 -04:00
|
|
|
export
|
2022-05-01 18:19:09 -04:00
|
|
|
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
|
2022-05-01 18:19:09 -04:00
|
|
|
|
|
|
|
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-01 18:19:09 -04:00
|
|
|
|
2022-05-24 06:09:17 -04:00
|
|
|
export
|
2022-05-01 18:19:09 -04:00
|
|
|
flatten : List Test -> List Test
|
|
|
|
flatten = flattenWith [<]
|
|
|
|
|
2022-05-24 06:09:17 -04:00
|
|
|
export
|
2022-05-01 18:19:09 -04:00
|
|
|
flatten1 : Test -> List Test
|
|
|
|
flatten1 = flatten1With [<]
|
|
|
|
|
2022-04-26 12:17:59 -04:00
|
|
|
|
2022-05-24 06:09:17 -04:00
|
|
|
private
|
|
|
|
record RunnerEnv where
|
|
|
|
constructor RE
|
|
|
|
indent : Nat
|
|
|
|
color : Bool
|
|
|
|
|
|
|
|
|
2022-04-26 12:17:59 -04:00
|
|
|
private
|
|
|
|
Runner : Type -> Type
|
2022-05-24 06:09:17 -04:00
|
|
|
Runner = ReaderT RunnerEnv IO
|
2022-04-26 12:17:59 -04:00
|
|
|
|
2022-05-24 06:09:17 -04:00
|
|
|
private
|
2022-04-26 12:17:59 -04:00
|
|
|
putIndentLines : List String -> Runner ()
|
2022-05-24 06:09:17 -04:00
|
|
|
putIndentLines xs = traverse_ (putStrLn . indent (!ask).indent) xs
|
2022-04-26 12:17:59 -04:00
|
|
|
|
2022-05-24 06:09:17 -04:00
|
|
|
private
|
2022-04-26 12:17:59 -04:00
|
|
|
isOk : Bool -> String
|
|
|
|
isOk b = if b then "ok" else "not ok"
|
|
|
|
|
2022-05-24 06:09:17 -04:00
|
|
|
private
|
2022-04-26 12:17:59 -04:00
|
|
|
toBool : Result -> Bool
|
|
|
|
toBool (Tried ok _) = ok
|
2022-05-24 06:09:17 -04:00
|
|
|
toBool _ = True
|
2022-04-26 12:17:59 -04:00
|
|
|
|
|
|
|
|
|
|
|
private
|
2022-05-25 10:04:51 -04:00
|
|
|
numbered : (a -> Bool) -> List a -> List (Nat, a)
|
|
|
|
numbered p = go 1 where
|
2022-04-26 12:17:59 -04:00
|
|
|
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-04-26 12:17:59 -04:00
|
|
|
|
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}"
|
|
|
|
|
2022-04-26 12:17:59 -04:00
|
|
|
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}"
|
2022-04-26 12:17:59 -04:00
|
|
|
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
|
2022-04-26 12:17:59 -04:00
|
|
|
pure res
|
2022-05-25 10:04:51 -04:00
|
|
|
run' (_, Note note) = do
|
|
|
|
putIndentLines [!(col Magenta "# ") ++ note]
|
|
|
|
pure True
|
2022-04-26 12:17:59 -04:00
|
|
|
|
|
|
|
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-04-26 12:17:59 -04:00
|
|
|
|
|
|
|
|
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
|
2022-05-01 18:19:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
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
|