From c7a88b50fb21319c85b072fdad49a90b992d586e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20Sch=C3=B6ttl?= Date: Sun, 12 Jan 2020 11:26:30 +0100 Subject: [PATCH] Fix timeclock format parsing Fix presumably copy-paste errors timeclock format has only timeclock lines or empty/comment lines Update test format to v3, add new tests Throw error on unexpected clock codes in timeclock format Fix missing case in pattern matching --- hledger-lib/Hledger/Data/Timeclock.hs | 11 +++++ hledger-lib/Hledger/Read/TimeclockReader.hs | 2 +- tests/timeclock.test | 45 +++++++++++++++++---- 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/hledger-lib/Hledger/Data/Timeclock.hs b/hledger-lib/Hledger/Data/Timeclock.hs index 30db87c87..720199c9b 100644 --- a/hledger-lib/Hledger/Data/Timeclock.hs +++ b/hledger-lib/Hledger/Data/Timeclock.hs @@ -57,6 +57,7 @@ instance Read TimeclockCode where timeclockEntriesToTransactions :: LocalTime -> [TimeclockEntry] -> [Transaction] timeclockEntriesToTransactions _ [] = [] timeclockEntriesToTransactions now [i] + | tlcode i /= In = errorExpectedCodeButGot In i | odate > idate = entryFromTimeclockInOut i o' : timeclockEntriesToTransactions now [i',o] | otherwise = [entryFromTimeclockInOut i o] where @@ -67,6 +68,8 @@ timeclockEntriesToTransactions now [i] o' = o{tldatetime=itime{localDay=idate, localTimeOfDay=TimeOfDay 23 59 59}} i' = i{tldatetime=itime{localDay=addDays 1 idate, localTimeOfDay=midnight}} timeclockEntriesToTransactions now (i:o:rest) + | tlcode i /= In = errorExpectedCodeButGot In i + | tlcode o /= Out =errorExpectedCodeButGot Out o | odate > idate = entryFromTimeclockInOut i o' : timeclockEntriesToTransactions now (i':o:rest) | otherwise = entryFromTimeclockInOut i o : timeclockEntriesToTransactions now rest where @@ -76,6 +79,14 @@ timeclockEntriesToTransactions now (i:o:rest) i' = i{tldatetime=itime{localDay=addDays 1 idate, localTimeOfDay=midnight}} {- HLINT ignore timeclockEntriesToTransactions -} +errorExpectedCodeButGot expected actual = errorWithSourceLine line $ "expected timeclock code " ++ (show expected) ++ " but got " ++ show (tlcode actual) + where + line = case tlsourcepos actual of + GenericSourcePos _ l _ -> l + JournalSourcePos _ (l, _) -> l + +errorWithSourceLine line msg = error $ "line " ++ show line ++ ": " ++ msg + -- | Convert a timeclock clockin and clockout entry to an equivalent journal -- transaction, representing the time expenditure. Note this entry is not balanced, -- since we omit the \"assets:time\" transaction for simpler output. diff --git a/hledger-lib/Hledger/Read/TimeclockReader.hs b/hledger-lib/Hledger/Read/TimeclockReader.hs index e182ba37a..ccd57831d 100644 --- a/hledger-lib/Hledger/Read/TimeclockReader.hs +++ b/hledger-lib/Hledger/Read/TimeclockReader.hs @@ -99,7 +99,7 @@ timeclockfilep = do many timeclockitemp timeclockitemp = choice [ void (lift emptyorcommentlinep) , timeclockentryp >>= \e -> modify' (\j -> j{jparsetimeclockentries = e : jparsetimeclockentries j}) - ] "timeclock entry, or default year or historical price directive" + ] "timeclock entry, comment line, or empty line" -- | Parse a timeclock entry. timeclockentryp :: JournalParser m TimeclockEntry diff --git a/tests/timeclock.test b/tests/timeclock.test index 7306e93ad..add9c5002 100644 --- a/tests/timeclock.test +++ b/tests/timeclock.test @@ -1,6 +1,5 @@ # 1. a timeclock session is parsed as a similarly-named transaction with one virtual posting -hledger -f - print -<<< +< i 2009/1/1 08:00:00 o 2009/1/1 09:00:00 stuff on checkout record is ignored @@ -8,7 +7,8 @@ i 2009/1/2 08:00:00 account name o 2009/1/2 09:00:00 i 2009/1/3 08:00:00 some:account name and a description o 2009/1/3 09:00:00 ->>> +$ hledger -f - print +> 2009-01-01 * 08:00-09:00 () 1.00h @@ -18,11 +18,41 @@ o 2009/1/3 09:00:00 2009-01-03 * and a description (some:account name) 1.00h ->>>2 ->>>= 0 +>2 +>= 0 + +# For a missing clock-out, now is implied +< +i 2020/1/1 08:00 +$ hledger -f - balance +> /./ +>= 0 + +# For a log not starting with clock-out, print error +< +o 2020/1/1 08:00 +$ hledger -f - balance +>2 /line 1: expected timeclock code i/ +>= !0 + +# For a different log starting not with clock-out, print error +< +o 2020/1/1 08:00 +o 2020/1/1 09:00 +$ hledger -f - balance +>2 /line 1: expected timeclock code i/ +>= !0 + +# For two consecutive clock-in, print error +< +i 2020/1/1 08:00 +i 2020/1/1 09:00 +$ hledger -f - balance +>2 /line 2: expected timeclock code o/ +>= !0 ## TODO -## 2. multi-day sessions get a new transaction for each day +## multi-day sessions get a new transaction for each day #hledger -f- print #<<< #i 2017/04/20 09:00:00 A @@ -35,7 +65,7 @@ o 2009/1/3 09:00:00 #>>>2 #>>>= # -## 3. unclosed sessions are automatically closed at report time +## unclosed sessions are automatically closed at report time ## TODO this output looks wrong #hledger -f- print #<<< @@ -70,7 +100,6 @@ o 2009/1/3 09:00:00 #>>>2 #>>>= -# 4. # ledger timeclock example from #ledger # ==== consulting.timeclock