Add tests

This commit is contained in:
s464968 2024-05-25 22:26:37 +02:00
parent 0f41cc33c3
commit c2a0a64a20
6 changed files with 63 additions and 13 deletions

View File

@ -1,6 +1,6 @@
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
module Main where module Main(main,parseTask, serializeTask, getPriority, comparePriority, TaskMap) where
import System.IO import System.IO
import System.Directory import System.Directory
@ -43,7 +43,7 @@ displayTasks = do
if exists if exists
then do then do
contents <- TIO.readFile todoFile contents <- TIO.readFile todoFile
let taskMap = parseTasks contents let taskMap = parseTask contents
putStrLn "Tasks:" putStrLn "Tasks:"
mapM_ (putStrLn . showTask) (Map.toList taskMap) mapM_ (putStrLn . showTask) (Map.toList taskMap)
else putStrLn "No tasks found." else putStrLn "No tasks found."
@ -58,10 +58,10 @@ addTask = do
if exists if exists
then do then do
contents <- TIO.readFile todoFile contents <- TIO.readFile todoFile
let taskMap = parseTasks contents let taskMap = parseTask contents
newId = if Map.null taskMap then 1 else fst (Map.findMax taskMap) + 1 newId = if Map.null taskMap then 1 else fst (Map.findMax taskMap) + 1
newTaskMap = Map.insert newId (pack task) taskMap newTaskMap = Map.insert newId (pack task) taskMap
TIO.writeFile todoFile (serializeTasks newTaskMap) TIO.writeFile todoFile (serializeTask newTaskMap)
else TIO.writeFile todoFile (pack task <> "\n") else TIO.writeFile todoFile (pack task <> "\n")
putStrLn "Task added." putStrLn "Task added."
main main
@ -75,7 +75,7 @@ filterTasksByTag = do
if exists if exists
then do then do
contents <- TIO.readFile todoFile contents <- TIO.readFile todoFile
let taskMap = parseTasks contents let taskMap = parseTask contents
filteredTasks = Map.filter (isInfixOf (pack tag)) taskMap filteredTasks = Map.filter (isInfixOf (pack tag)) taskMap
putStrLn "Filtered tasks:" putStrLn "Filtered tasks:"
mapM_ (putStrLn . showTask) (Map.toList filteredTasks) mapM_ (putStrLn . showTask) (Map.toList filteredTasks)
@ -88,7 +88,7 @@ sortTasksByPriority = do
if exists if exists
then do then do
contents <- TIO.readFile todoFile contents <- TIO.readFile todoFile
let taskMap = parseTasks contents let taskMap = parseTask contents
sortedTasks = sortBy (comparePriority `on` snd) (Map.toList taskMap) sortedTasks = sortBy (comparePriority `on` snd) (Map.toList taskMap)
putStrLn "Sorted tasks by priority:" putStrLn "Sorted tasks by priority:"
mapM_ (putStrLn . showTask) sortedTasks mapM_ (putStrLn . showTask) sortedTasks
@ -103,11 +103,11 @@ getPriority t = case unpack (strip t) of
('(':p:')':_) | isAlpha p -> Just p ('(':p:')':_) | isAlpha p -> Just p
_ -> Nothing _ -> Nothing
parseTasks :: Text -> TaskMap parseTask :: Text -> TaskMap
parseTasks = Map.fromList . zip [1..] . Data.Text.lines parseTask = Map.fromList . zip [1..] . Data.Text.lines
serializeTasks :: TaskMap -> Text serializeTask :: TaskMap -> Text
serializeTasks = Data.Text.unlines . map snd . Map.toList serializeTask = Data.Text.unlines . map snd . Map.toList
showTask :: (Int, Text) -> String showTask :: (Int, Text) -> String
showTask (_, task) = unpack task showTask (_, task) = unpack task

View File

@ -53,10 +53,14 @@ executables:
tests: tests:
todo-app-test: todo-app-test:
main: Spec.hs main: Spec.hs
source-dirs: test source-dirs: test, app
ghc-options: ghc-options:
- -threaded - -threaded
- -rtsopts - -rtsopts
- -with-rtsopts=-N - -with-rtsopts=-N
dependencies: dependencies:
- todo-app - todo-app
- hspec
# packages:
# - .

View File

@ -36,6 +36,7 @@ packages:
# forks / in-progress versions pinned to a git hash. For example: # forks / in-progress versions pinned to a git hash. For example:
# #
# extra-deps: # extra-deps:
# - hspec-2.9.4
# - acme-missiles-0.3 # - acme-missiles-0.3
# - git: https://github.com/commercialhaskell/stack.git # - git: https://github.com/commercialhaskell/stack.git
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a # commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a

View File

@ -1,2 +1,45 @@
{-# 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 :: IO ()
main = putStrLn "Test suite not yet implemented" 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

View File

@ -65,12 +65,13 @@ test-suite todo-app-test
autogen-modules: autogen-modules:
Paths_todo_app Paths_todo_app
hs-source-dirs: hs-source-dirs:
test test, app
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 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: build-depends:
base >=4.7 && <5 base >=4.7 && <5
, containers , containers
, directory , directory
, hspec
, text , text
, todo-app , todo-app
default-language: Haskell2010 default-language: Haskell2010

View File

@ -4,3 +4,4 @@ task +tag
(A) wyniesc smieci (A) wyniesc smieci
(C) dodac kod (C) dodac kod
(B) obrac ziemniaki (B) obrac ziemniaki
(A) asdasd +das