From 796eb829c5a915567fe9fa94897d33f84a763119 Mon Sep 17 00:00:00 2001 From: Victor Martinez <49537445+JasterV@users.noreply.github.com> Date: Thu, 13 Mar 2025 18:41:24 +0100 Subject: [PATCH] feat: setup tests and implement them for each day --- aoc2024.cabal | 6 ++++-- package.yaml | 1 - src/Day1.hs | 5 +++-- src/Day2.hs | 4 ++-- test/Day1Spec.hs | 23 +++++++++++++++++++++++ test/Day2Spec.hs | 22 ++++++++++++++++++++++ test/Day3Spec.hs | 16 ++++++++++++++++ test/Spec.hs | 17 +---------------- 8 files changed, 71 insertions(+), 23 deletions(-) create mode 100644 test/Day1Spec.hs create mode 100644 test/Day2Spec.hs create mode 100644 test/Day3Spec.hs diff --git a/aoc2024.cabal b/aoc2024.cabal index 3ebd261..4d95d27 100644 --- a/aoc2024.cabal +++ b/aoc2024.cabal @@ -44,6 +44,9 @@ test-suite aoc2024-test type: exitcode-stdio-1.0 main-is: Spec.hs other-modules: + Day1Spec + Day2Spec + Day3Spec Paths_aoc2024 autogen-modules: Paths_aoc2024 @@ -51,8 +54,7 @@ test-suite aoc2024-test test ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N build-depends: - HUnit >=1.6.2.0 && <1.7 - , QuickCheck >=2.14.3 && <2.15 + QuickCheck >=2.14.3 && <2.15 , aoc2024 , base >=4.7 && <5 , hspec >=2.0.0 diff --git a/package.yaml b/package.yaml index 45c9262..33b9d07 100644 --- a/package.yaml +++ b/package.yaml @@ -48,5 +48,4 @@ tests: dependencies: - aoc2024 - QuickCheck ^>= 2.14.3 - - HUnit ^>= 1.6.2.0 - hspec >=2.0.0 diff --git a/src/Day1.hs b/src/Day1.hs index 33bbf28..474878e 100644 --- a/src/Day1.hs +++ b/src/Day1.hs @@ -1,6 +1,7 @@ module Day1 ( partOne, partTwo, + Error, ) where @@ -16,10 +17,10 @@ type LocationID = Int data ParseError = ParseLocationError String String | ParseLineError String - deriving (Show) + deriving (Show, Eq) newtype Error = ParseInputError ParseError - deriving (Show) + deriving (Show, Eq) partOne :: String -> Either Error Int partOne contents = calculateScore <$> BF.first ParseInputError (parseInput contents) diff --git a/src/Day2.hs b/src/Day2.hs index 03b0302..f422ba2 100644 --- a/src/Day2.hs +++ b/src/Day2.hs @@ -1,10 +1,10 @@ -module Day2 (partOne, partTwo) where +module Day2 (partOne, partTwo, Error) where import qualified Data.Bifunctor as BF import Text.Read (readEither) newtype Error = ParseError String - deriving (Show) + deriving (Show, Eq) type Level = Int diff --git a/test/Day1Spec.hs b/test/Day1Spec.hs new file mode 100644 index 0000000..d9e3868 --- /dev/null +++ b/test/Day1Spec.hs @@ -0,0 +1,23 @@ +module Day1Spec (spec) where + +import Day1 (partOne, partTwo) +import Test.Hspec + +spec :: Spec +spec = do + describe "PartOne" $ do + it "Given two valid lists it returns the total distance" $ do + let expectedResult = 11 + partOne testInput `shouldBe` Right expectedResult + + describe "PartTwo" $ do + it "Given two valid lists it returns their similarity score" $ do + partTwo testInput `shouldBe` Right 31 + where + testInput = + "3 4\n\ + \4 3\n\ + \2 5\n\ + \1 3\n\ + \3 9\n\ + \3 3" diff --git a/test/Day2Spec.hs b/test/Day2Spec.hs new file mode 100644 index 0000000..cd8c7b8 --- /dev/null +++ b/test/Day2Spec.hs @@ -0,0 +1,22 @@ +module Day2Spec (spec) where + +import Day2 (partOne, partTwo) +import Test.Hspec + +spec :: Spec +spec = do + describe "PartOne" $ do + it "Given a list of reports, it returns how many are valid" $ do + partOne testInput `shouldBe` Right 2 + + describe "PartTwo" $ do + it "Given a list of reports, it returns how many are valid" $ do + partTwo testInput `shouldBe` Right 4 + where + testInput = + "7 6 4 2 1\n\ + \1 2 7 8 9\n\ + \9 7 6 2 1\n\ + \1 3 2 4 5\n\ + \8 6 4 4 1\n\ + \1 3 6 7 9" diff --git a/test/Day3Spec.hs b/test/Day3Spec.hs new file mode 100644 index 0000000..f66d8fe --- /dev/null +++ b/test/Day3Spec.hs @@ -0,0 +1,16 @@ +module Day3Spec (spec) where + +import Day3 (partOne, partTwo) +import Test.Hspec + +spec :: Spec +spec = do + describe "PartOne" $ do + it "Given a corrupted program, it extracts the valid operations and runs them" $ do + let input = "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))" + partOne input `shouldBe` (2 * 4 + 5 * 5 + 11 * 8 + 8 * 5) + + describe "PartTwo" $ do + it "Given a corrupted program, it extracts the valid operations and runs them" $ do + let input = "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))" + partTwo input `shouldBe` (2 * 4 + 8 * 5) diff --git a/test/Spec.hs b/test/Spec.hs index 5d33250..a824f8c 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -1,16 +1 @@ -import Control.Exception (evaluate) -import Test.Hspec -import Test.QuickCheck - -main :: IO () -main = hspec $ do - describe "Prelude.head" $ do - it "returns the first element of a list" $ do - head [23 ..] `shouldBe` (23 :: Int) - - it "returns the first element of an *arbitrary* list" $ - property $ - \x xs -> head (x : xs) == (x :: Int) - - it "throws an exception if used with an empty list" $ do - evaluate (head []) `shouldThrow` anyException +{-# OPTIONS_GHC -F -pgmF hspec-discover #-}