0 | module Idrall.TestHelper
3 | import Idrall.IOEither
8 | import System.Directory
10 | import System.Directory.Tree
14 | import Data.String.Extra
16 | import Text.PrettyPrint.Prettyprinter.Util
17 | import Text.PrettyPrint.Prettyprinter.Doc
18 | import Text.PrettyPrint.Prettyprinter.Render.String
22 | constructor MkResult
26 | record ResultFail where
27 | constructor MkResultFail
29 | passedButShouldNot : Nat
33 | show (MkResult pass fail) = "Result: " ++ "\n" ++
34 | "Pass: " ++ show pass ++ "\n" ++
35 | "Fail: " ++ show fail
38 | Semigroup Result where
39 | (<+>) (MkResult pass fail) (MkResult pass' fail') = MkResult (pass + pass') (fail + fail')
43 | neutral = MkResult 0 0
46 | foldlMapM : (Foldable g, Monoid b, Applicative m) => (a -> m b) -> g a -> m b
47 | foldlMapM f = foldr f' (pure neutral)
49 | f' : a -> m b -> m b
50 | f' x y = liftA2 (<+>) (f x) y
55 | mkres (MkIOEither x) = do
59 | putStrLn $
!(fancyError e)
67 | mkresFail (MkIOEither x) = do
76 | flipRes : Result -> Result
77 | flipRes (MkResult pass fail) = MkResult fail pass
80 | = MkTestPair String String
83 | show (MkTestPair a b) = a ++ " : " ++ b
85 | fileNameAB : {root : _} -> FileName root -> TestPair
87 | let fileA = show $
toFilePath a
90 | MkTestPair fileA fileB
92 | aToB : String -> String
93 | aToB a = (dropLast 7 a) ++ "B.dhall"
96 | matchFiles : {root : _} -> List String -> FileName root -> Bool
97 | matchFiles [] n = False
98 | matchFiles (x :: xs) n =
99 | case isInfixOf x (fileName n) of
100 | False => matchFiles xs n
103 | defaultFilters : List ({root : _} -> FileName root -> Bool)
104 | defaultFilters = [findAFiles]
106 | findAFiles : {root : _} -> FileName root -> Bool
108 | let fileNameStr = fileName x in
109 | isSuffixOf "A.dhall" fileNameStr
111 | doFilter : {root : _}
112 | -> List ({root : _} -> FileName root -> Bool)
116 | doFilter (f :: xs) x =
117 | doFilter xs (System.Directory.Tree.filter f (\_ => True) x)
120 | runTests' : Pretty a
122 | -> (String -> String -> IOEither Error a)
123 | -> (filters : List ({root : _} -> FileName root -> Bool))
125 | runTests' path f filters =
126 | let dir = explore $
parse path
127 | testFiles = doFilter filters !dir
129 | res <- depthFirst doTest (sort testFiles) $
pure neutral
132 | runTestPair : Pretty a
134 | -> (String -> String -> IOEither Error a)
135 | -> IOEither Error a
136 | runTestPair (MkTestPair a b) f = f a b
137 | doTest : {root : _} -> FileName root -> Lazy (IO Result) -> IO Result
139 | putStrLn $
"Testing: \{show $ toFilePath x}"
140 | res <- mkres $
runTestPair (fileNameAB x) f
141 | pure $
res <+> !next
144 | runTests : Pretty a => (path : String) -> (String -> String -> IOEither Error a) -> IO Result
145 | runTests path f = runTests' path f defaultFilters
148 | runTestsOnly : Pretty a => (onlyList : List String) -> (path : String) -> (String -> String -> IOEither Error a) -> IO Result
149 | runTestsOnly onlyList path f = runTests' path f ((matchFiles onlyList) :: defaultFilters)
151 | runTestFail' : Show a => (path : String)
152 | -> (String -> IOEither Error a)
153 | -> (filters : List ({root : _} -> FileName root -> Bool))
155 | runTestFail' path f filters =
156 | let dir = explore $
parse path
157 | testFiles = doFilter filters !dir
159 | res <- depthFirst doTest (sort testFiles) $
pure neutral
162 | doTest : {root : _} -> FileName root -> Lazy (IO Result) -> IO Result
164 | putStrLn $
"Testing: \{show $ toFilePath x}"
165 | res <- mkresFail $
f (fileName x)
166 | pure $
res <+> !next
169 | runTestFail : Show a
171 | -> (String -> IOEither Error a)
173 | runTestFail path f = runTestFail' path f []
177 | ppResult : Result -> String
178 | ppResult (MkResult pass fail) =
186 | ppResultFail : Result -> String
187 | ppResultFail (MkResult pass fail) =
190 | Failed as intended: \{show fail}
191 | Passed but shouldn't: \{show pass}