Add tests
This commit is contained in:
parent
0f41cc33c3
commit
c2a0a64a20
20
app/Main.hs
20
app/Main.hs
@ -1,6 +1,6 @@
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
|
||||
module Main where
|
||||
module Main(main,parseTask, serializeTask, getPriority, comparePriority, TaskMap) where
|
||||
|
||||
import System.IO
|
||||
import System.Directory
|
||||
@ -43,7 +43,7 @@ displayTasks = do
|
||||
if exists
|
||||
then do
|
||||
contents <- TIO.readFile todoFile
|
||||
let taskMap = parseTasks contents
|
||||
let taskMap = parseTask contents
|
||||
putStrLn "Tasks:"
|
||||
mapM_ (putStrLn . showTask) (Map.toList taskMap)
|
||||
else putStrLn "No tasks found."
|
||||
@ -58,10 +58,10 @@ addTask = do
|
||||
if exists
|
||||
then do
|
||||
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
|
||||
newTaskMap = Map.insert newId (pack task) taskMap
|
||||
TIO.writeFile todoFile (serializeTasks newTaskMap)
|
||||
TIO.writeFile todoFile (serializeTask newTaskMap)
|
||||
else TIO.writeFile todoFile (pack task <> "\n")
|
||||
putStrLn "Task added."
|
||||
main
|
||||
@ -75,7 +75,7 @@ filterTasksByTag = do
|
||||
if exists
|
||||
then do
|
||||
contents <- TIO.readFile todoFile
|
||||
let taskMap = parseTasks contents
|
||||
let taskMap = parseTask contents
|
||||
filteredTasks = Map.filter (isInfixOf (pack tag)) taskMap
|
||||
putStrLn "Filtered tasks:"
|
||||
mapM_ (putStrLn . showTask) (Map.toList filteredTasks)
|
||||
@ -88,7 +88,7 @@ sortTasksByPriority = do
|
||||
if exists
|
||||
then do
|
||||
contents <- TIO.readFile todoFile
|
||||
let taskMap = parseTasks contents
|
||||
let taskMap = parseTask contents
|
||||
sortedTasks = sortBy (comparePriority `on` snd) (Map.toList taskMap)
|
||||
putStrLn "Sorted tasks by priority:"
|
||||
mapM_ (putStrLn . showTask) sortedTasks
|
||||
@ -103,11 +103,11 @@ getPriority t = case unpack (strip t) of
|
||||
('(':p:')':_) | isAlpha p -> Just p
|
||||
_ -> Nothing
|
||||
|
||||
parseTasks :: Text -> TaskMap
|
||||
parseTasks = Map.fromList . zip [1..] . Data.Text.lines
|
||||
parseTask :: Text -> TaskMap
|
||||
parseTask = Map.fromList . zip [1..] . Data.Text.lines
|
||||
|
||||
serializeTasks :: TaskMap -> Text
|
||||
serializeTasks = Data.Text.unlines . map snd . Map.toList
|
||||
serializeTask :: TaskMap -> Text
|
||||
serializeTask = Data.Text.unlines . map snd . Map.toList
|
||||
|
||||
showTask :: (Int, Text) -> String
|
||||
showTask (_, task) = unpack task
|
@ -53,10 +53,14 @@ executables:
|
||||
tests:
|
||||
todo-app-test:
|
||||
main: Spec.hs
|
||||
source-dirs: test
|
||||
source-dirs: test, app
|
||||
ghc-options:
|
||||
- -threaded
|
||||
- -rtsopts
|
||||
- -with-rtsopts=-N
|
||||
dependencies:
|
||||
- todo-app
|
||||
- hspec
|
||||
|
||||
# packages:
|
||||
# - .
|
||||
|
@ -36,6 +36,7 @@ packages:
|
||||
# forks / in-progress versions pinned to a git hash. For example:
|
||||
#
|
||||
# extra-deps:
|
||||
# - hspec-2.9.4
|
||||
# - acme-missiles-0.3
|
||||
# - git: https://github.com/commercialhaskell/stack.git
|
||||
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
|
||||
|
45
test/Spec.hs
45
test/Spec.hs
@ -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 = 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
|
@ -65,12 +65,13 @@ test-suite todo-app-test
|
||||
autogen-modules:
|
||||
Paths_todo_app
|
||||
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
|
||||
build-depends:
|
||||
base >=4.7 && <5
|
||||
, containers
|
||||
, directory
|
||||
, hspec
|
||||
, text
|
||||
, todo-app
|
||||
default-language: Haskell2010
|
||||
|
Loading…
Reference in New Issue
Block a user