Skip to content

Commit

Permalink
optimize test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
henrytill committed Nov 10, 2024
1 parent eca7dbb commit 64d700b
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module Main (main) where

import Control.Monad (unless)
import qualified Data.List as List
import Data.Map.Strict (Map)
import Data.Text (Text)
import qualified Data.Text.IO as TIO
Expand Down Expand Up @@ -131,25 +130,29 @@ testArrayKey1 kv =
expected = [1, 2, 3]
actual = kv ^.. mapAt "array" . valueAt "key1" . _List . traverse . _Integer

makeFolder :: Table -> (String, Bool) -> (Table -> Test) -> (String, Bool)
makeFolder kv (output, isPassed) test =
let result = runTest (test kv)
in (output <> "\n" <> resultToString result, isPassed && resultIsPassed result)
tests :: [Table -> Test]
tests =
[ testTableKey,
testTableZoo,
testTableSubtableKey,
testTableInlineNameFirst,
testTableInlinePointY,
testStringBasicBasic,
testStringMultiline,
testStringMultilineContinued,
testArrayKey1
]

step :: Result -> (ShowS, Bool) -> (ShowS, Bool)
step result (f, allPassed) =
( showString (resultToString result) . showChar '\n' . f,
resultIsPassed result && allPassed
)

runTests :: Table -> (String, Bool)
runTests kv = List.foldl' (makeFolder kv) (mempty, True) tests
runTests kv = (buildString mempty, passed)
where
tests =
[ testTableKey,
testTableZoo,
testTableSubtableKey,
testTableInlineNameFirst,
testTableInlinePointY,
testStringBasicBasic,
testStringMultiline,
testStringMultilineContinued,
testArrayKey1
]
(buildString, passed) = foldr (step . runTest . ($ kv)) (mempty, True) tests

readTomlFile :: String -> IO Table
readTomlFile file = TIO.readFile file >>= parse >>= handleError
Expand All @@ -161,5 +164,5 @@ main :: IO ()
main = do
ex <- readTomlFile "./example/example-v0.4.0.toml"
let (output, passed) = runTests ex
putStrLn output
putStr output
unless passed exitFailure

0 comments on commit 64d700b

Please sign in to comment.