imp: errors: improve, test timeclock errors

This commit is contained in:
Simon Michael 2022-07-14 11:55:21 +01:00
parent 27142896b3
commit 32c7f6300b
3 changed files with 38 additions and 11 deletions

View File

@ -28,7 +28,6 @@ import Hledger.Data.Types
import Hledger.Data.Dates import Hledger.Data.Dates
import Hledger.Data.Amount import Hledger.Data.Amount
import Hledger.Data.Posting import Hledger.Data.Posting
import Hledger.Data.Transaction
instance Show TimeclockEntry where instance Show TimeclockEntry where
show t = printf "%s %s %s %s" (show $ tlcode t) (show $ tldatetime t) (tlaccount t) (tldescription t) show t = printf "%s %s %s %s" (show $ tlcode t) (show $ tldatetime t) (tlaccount t) (tldescription t)
@ -66,7 +65,7 @@ timeclockEntriesToTransactions now [i]
i' = i{tldatetime=itime{localDay=addDays 1 idate, localTimeOfDay=midnight}} i' = i{tldatetime=itime{localDay=addDays 1 idate, localTimeOfDay=midnight}}
timeclockEntriesToTransactions now (i:o:rest) timeclockEntriesToTransactions now (i:o:rest)
| tlcode i /= In = errorExpectedCodeButGot In i | tlcode i /= In = errorExpectedCodeButGot In i
| tlcode o /= Out =errorExpectedCodeButGot Out o | tlcode o /= Out = errorExpectedCodeButGot Out o
| odate > idate = entryFromTimeclockInOut i o' : timeclockEntriesToTransactions now (i':o:rest) | odate > idate = entryFromTimeclockInOut i o' : timeclockEntriesToTransactions now (i':o:rest)
| otherwise = entryFromTimeclockInOut i o : timeclockEntriesToTransactions now rest | otherwise = entryFromTimeclockInOut i o : timeclockEntriesToTransactions now rest
where where
@ -76,10 +75,16 @@ timeclockEntriesToTransactions now (i:o:rest)
i' = i{tldatetime=itime{localDay=addDays 1 idate, localTimeOfDay=midnight}} i' = i{tldatetime=itime{localDay=addDays 1 idate, localTimeOfDay=midnight}}
{- HLINT ignore timeclockEntriesToTransactions -} {- HLINT ignore timeclockEntriesToTransactions -}
errorExpectedCodeButGot expected actual = errorWithSourceLine line $ "expected timeclock code " ++ (show expected) ++ " but got " ++ show (tlcode actual) errorExpectedCodeButGot :: TimeclockCode -> TimeclockEntry -> a
where line = unPos . sourceLine $ tlsourcepos actual errorExpectedCodeButGot expected actual = error' $ printf
("%s:\n%s\n\nExpected timeclock %s entry but got %s.\n"
errorWithSourceLine line msg = error $ "line " ++ show line ++ ": " ++ msg ++"Only one session may be clocked in at a time, so please alternate i and o.")
(sourcePosPretty $ tlsourcepos actual)
(show l ++ " | " ++ show actual)
(show expected)
(show $ tlcode actual)
where
l = unPos $ sourceLine $ tlsourcepos actual
-- | Convert a timeclock clockin and clockout entry to an equivalent journal -- | Convert a timeclock clockin and clockout entry to an equivalent journal
-- transaction, representing the time expenditure. Note this entry is not balanced, -- transaction, representing the time expenditure. Note this entry is not balanced,
@ -87,9 +92,18 @@ errorWithSourceLine line msg = error $ "line " ++ show line ++ ": " ++ msg
entryFromTimeclockInOut :: TimeclockEntry -> TimeclockEntry -> Transaction entryFromTimeclockInOut :: TimeclockEntry -> TimeclockEntry -> Transaction
entryFromTimeclockInOut i o entryFromTimeclockInOut i o
| otime >= itime = t | otime >= itime = t
| otherwise = error' . T.unpack $ | otherwise =
"clock-out time less than clock-in time in:\n" <> showTransaction t -- PARTIAL: -- Clockout time earlier than clockin is an error.
-- (Clockin earlier than preceding clockin/clockout is allowed.)
error' $ printf
("%s:\n%s\nThis clockout time (%s) is earlier than the previous clockin.\n"
++"Please adjust it to be later than %s.")
(sourcePosPretty $ tlsourcepos o)
(unlines [replicate (length l) ' '++ " | " ++ show i, l ++ " | " ++ show o])
(show $ tldatetime o)
(show $ tldatetime i)
where where
l = show $ unPos $ sourceLine $ tlsourcepos o
t = Transaction { t = Transaction {
tindex = 0, tindex = 0,
tsourcepos = (tlsourcepos i, tlsourcepos i), tsourcepos = (tlsourcepos i, tlsourcepos i),

View File

@ -0,0 +1,5 @@
#!/usr/bin/env -S hledger check -f
# Clockout time before previous clockin.
i 2022/01/01 00:01:00
o 2022/01/01 00:00:00

View File

@ -0,0 +1,8 @@
#!/usr/bin/env -S hledger check -f
# Clockin/clockout out of order:
# two clockins without intervening clockout,
# two clockouts without intervening clockin,
# or an initial clockout with no preceding clockin.
i 2022/01/01 00:00:00
i 2022/01/01 00:01:00