Use Tasty

This commit is contained in:
George Thomas 2025-12-02 08:23:53 +00:00
parent c0e76ae1c5
commit 1e1d4ee88c
4 changed files with 30 additions and 18 deletions

View File

@ -15,6 +15,7 @@ executable aoc
MultiWayIf MultiWayIf
NoFieldSelectors NoFieldSelectors
OverloadedRecordDot OverloadedRecordDot
OverloadedStrings
ViewPatterns ViewPatterns
ghc-options: ghc-options:
-Wall -Wall
@ -36,5 +37,8 @@ executable aoc
safe, safe,
stm, stm,
text, text,
tasty,
tasty-golden,
tasty-hunit,
time, time,
transformers, transformers,

View File

@ -2,24 +2,30 @@ module Main (main) where
import Control.Monad.State import Control.Monad.State
import Data.Bifunctor import Data.Bifunctor
import Data.Foldable import Data.ByteString.Lazy qualified as BL
import Data.Functor
import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.Encoding (encodeUtf8)
import Test.Tasty
import Test.Tasty.Golden (goldenVsString)
import Text.Read (readMaybe) import Text.Read (readMaybe)
main :: IO () main :: IO ()
main = do main =
runTests puzzle1 defaultMain $
testGroup
"tests"
[ puzzleTest puzzle1
]
runTests :: Puzzle a -> IO () puzzleTest :: Puzzle a -> TestTree
runTests p = do puzzleTest p =
Just input <- p.parse <$> readFile ("inputs/examples/" <> show p.number) withResource (maybe (fail "parse failure") pure . p.parse =<< readFile ("inputs/examples/" <> show p.number)) mempty \input ->
for_ [(1 :: Word, p.part1), (2, p.part2)] \(n, pp) -> testGroup (show p.number) $
let r = pp.solve input [(1 :: Word, p.part1), (2, p.part2)] <&> \(n, pp) ->
in putStrLn $ goldenVsString (show n) ("outputs/examples/" <> show p.number <> "/" <> show n) $
show n BL.fromStrict . encodeUtf8 . pp.solve <$> input
<> ": "
<> if r == pp.expected
then "correct!"
else "got " <> r <> ", expected " <> pp.expected
data Puzzle input = Puzzle data Puzzle input = Puzzle
{ number :: Word { number :: Word
@ -28,8 +34,8 @@ data Puzzle input = Puzzle
, part2 :: Part input , part2 :: Part input
} }
data Part input = Part data Part input = Part
{ solve :: input -> String { solve :: input -> Text
, expected :: String , expected :: Text
} }
puzzle1 :: Puzzle [(Direction, Inc)] puzzle1 :: Puzzle [(Direction, Inc)]
@ -47,7 +53,7 @@ puzzle1 =
, part1 = , part1 =
Part Part
{ solve = { solve =
show T.show
. sum . sum
. flip evalState 50 . flip evalState 50
. traverse \(d, i) -> state \p -> . traverse \(d, i) -> state \p ->
@ -58,7 +64,7 @@ puzzle1 =
, part2 = , part2 =
Part Part
{ solve = { solve =
show T.show
. sum . sum
. flip evalState 50 . flip evalState 50
. traverse \(d, i) -> state \p -> . traverse \(d, i) -> state \p ->

1
outputs/examples/1/1 Normal file
View File

@ -0,0 +1 @@
3

1
outputs/examples/1/2 Normal file
View File

@ -0,0 +1 @@
6