haskell_todo_app/test/Spec.hs
2024-05-25 22:26:37 +02:00

45 lines
2.3 KiB
Haskell

{-# LANGUAGE OverloadedStrings #-}
module Spec (main, spec) where
import Test.Hspec
import Data.Text (Text, pack)
import qualified Data.Map as Map
import Main (parseTask, serializeTask, getPriority, comparePriority, TaskMap)
main :: IO ()
main = hspec spec
spec :: Spec
spec = do
describe "parseTask" $ do
it "parses tasks into a TaskMap" $ do
let tasksText = "1: (A) Thank Mom for the meatballs\n2: (B) Schedule Goodwill pickup +GarageSale\n"
expectedMap = Map.fromList [(1, "(A) Thank Mom for the meatballs"), (2, "(B) Schedule Goodwill pickup +GarageSale")]
parseTask tasksText `shouldBe` expectedMap
describe "serializeTask" $ do
it "serializes a TaskMap into text" $ do
let taskMap = Map.fromList [(1, "(A) Thank Mom for the meatballs"), (2, "(B) Schedule Goodwill pickup +GarageSale")]
expectedText = "1: (A) Thank Mom for the meatballs\n2: (B) Schedule Goodwill pickup +GarageSale\n"
serializeTask taskMap `shouldBe` expectedText
describe "getPriority" $ do
it "extracts the priority from a task" $ do
getPriority "(A) Thank Mom for the meatballs" `shouldBe` Just 'A'
getPriority "(B) Schedule Goodwill pickup +GarageSale" `shouldBe` Just 'B'
getPriority "Thank Mom for the meatballs" `shouldBe` Nothing
describe "comparePriority" $ do
it "compares tasks based on their priority" $ do
comparePriority "(A) Thank Mom for the meatballs" "(B) Schedule Goodwill pickup +GarageSale" `shouldBe` LT
comparePriority "(B) Schedule Goodwill pickup +GarageSale" "(A) Thank Mom for the meatballs" `shouldBe` GT
comparePriority "(A) Thank Mom for the meatballs" "(A) Schedule Goodwill pickup +GarageSale" `shouldBe` EQ
describe "TaskMap operations" $ do
it "parses and serializes tasks correctly" $ do
let tasksText = "1: (A) Thank Mom for the meatballs\n2: (B) Schedule Goodwill pickup +GarageSale\n"
taskMap = parseTask tasksText
expectedMap = Map.fromList [(1, "(A) Thank Mom for the meatballs"), (2, "(B) Schedule Goodwill pickup +GarageSale")]
taskMap `shouldBe` expectedMap
serializeTask taskMap `shouldBe` tasksText