quickcheck schedule parsing
soo many arbitrary instances, so little time!
This commit is contained in:
parent
29ca49dad4
commit
c840d54fab
2 changed files with 60 additions and 13 deletions
2
Test.hs
2
Test.hs
|
@ -59,6 +59,7 @@ import qualified Utility.Env
|
||||||
import qualified Utility.Matcher
|
import qualified Utility.Matcher
|
||||||
import qualified Utility.Exception
|
import qualified Utility.Exception
|
||||||
import qualified Utility.Hash
|
import qualified Utility.Hash
|
||||||
|
import qualified Utility.Scheduled
|
||||||
#ifndef mingw32_HOST_OS
|
#ifndef mingw32_HOST_OS
|
||||||
import qualified GitAnnex
|
import qualified GitAnnex
|
||||||
import qualified Remote.Helper.Encryptable
|
import qualified Remote.Helper.Encryptable
|
||||||
|
@ -138,6 +139,7 @@ quickcheck =
|
||||||
, check "prop_read_show_TrustLevel" Types.TrustLevel.prop_read_show_TrustLevel
|
, check "prop_read_show_TrustLevel" Types.TrustLevel.prop_read_show_TrustLevel
|
||||||
, check "prop_parse_show_TrustLog" Logs.Trust.prop_parse_show_TrustLog
|
, check "prop_parse_show_TrustLog" Logs.Trust.prop_parse_show_TrustLog
|
||||||
, check "prop_hashes_stable" Utility.Hash.prop_hashes_stable
|
, check "prop_hashes_stable" Utility.Hash.prop_hashes_stable
|
||||||
|
, check "prop_schedule_roundtrips" Utility.Scheduled.prop_schedule_roundtrips
|
||||||
]
|
]
|
||||||
where
|
where
|
||||||
check desc prop = do
|
check desc prop = do
|
||||||
|
|
|
@ -10,14 +10,17 @@ module Utility.Scheduled (
|
||||||
Recurrance(..),
|
Recurrance(..),
|
||||||
TimeOfDay(..),
|
TimeOfDay(..),
|
||||||
fromSchedule,
|
fromSchedule,
|
||||||
toSchedule
|
toSchedule,
|
||||||
|
parseSchedule,
|
||||||
|
prop_schedule_roundtrips
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Common
|
import Common
|
||||||
|
import Utility.QuickCheck
|
||||||
|
|
||||||
{- Some sort of scheduled event. -}
|
{- Some sort of scheduled event. -}
|
||||||
data Schedule = Schedule Recurrance TimeOfDay Duration
|
data Schedule = Schedule Recurrance TimeOfDay Duration
|
||||||
deriving (Show)
|
deriving (Eq, Show, Ord)
|
||||||
|
|
||||||
data Recurrance
|
data Recurrance
|
||||||
= Daily
|
= Daily
|
||||||
|
@ -26,7 +29,7 @@ data Recurrance
|
||||||
| Yearly YearDay
|
| Yearly YearDay
|
||||||
-- Divisible 3 Daily is every day of the year evenly divisible by 3
|
-- Divisible 3 Daily is every day of the year evenly divisible by 3
|
||||||
| Divisable Int Recurrance
|
| Divisable Int Recurrance
|
||||||
deriving (Show)
|
deriving (Eq, Show, Ord)
|
||||||
|
|
||||||
type WeekDay = Int
|
type WeekDay = Int
|
||||||
type MonthDay = Int
|
type MonthDay = Int
|
||||||
|
@ -35,14 +38,14 @@ type YearDay = Int
|
||||||
data TimeOfDay
|
data TimeOfDay
|
||||||
= AnyTime
|
= AnyTime
|
||||||
| Hour Int
|
| Hour Int
|
||||||
deriving (Show)
|
deriving (Eq, Show, Ord)
|
||||||
|
|
||||||
data Duration = MinutesDuration Int
|
data Duration = MinutesDuration Int
|
||||||
deriving (Show)
|
deriving (Eq, Show, Ord)
|
||||||
|
|
||||||
fromRecurrance :: Recurrance -> String
|
fromRecurrance :: Recurrance -> String
|
||||||
fromRecurrance (Divisable n r) =
|
fromRecurrance (Divisable n r) =
|
||||||
fromRecurrance' (++ "s divisible by " ++ show n) r
|
fromRecurrance' (\u -> "on " ++ u ++ "s divisible by " ++ show n) r
|
||||||
fromRecurrance r = fromRecurrance' ("every " ++) r
|
fromRecurrance r = fromRecurrance' ("every " ++) r
|
||||||
|
|
||||||
fromRecurrance' :: (String -> String) -> Recurrance -> String
|
fromRecurrance' :: (String -> String) -> Recurrance -> String
|
||||||
|
@ -58,7 +61,7 @@ onday n s = s ++ " on day " ++ show n
|
||||||
toRecurrance :: String -> Maybe Recurrance
|
toRecurrance :: String -> Maybe Recurrance
|
||||||
toRecurrance s = case words s of
|
toRecurrance s = case words s of
|
||||||
("every":something:l) -> parse something l
|
("every":something:l) -> parse something l
|
||||||
(something:"divisible":"by":sn:l) -> do
|
("on":something:"divisible":"by":sn:l) -> do
|
||||||
r <- parse something l
|
r <- parse something l
|
||||||
n <- readish sn
|
n <- readish sn
|
||||||
if n > 0
|
if n > 0
|
||||||
|
@ -105,12 +108,54 @@ fromSchedule (Schedule recurrance timeofday duration) = unwords
|
||||||
]
|
]
|
||||||
|
|
||||||
toSchedule :: String -> Maybe Schedule
|
toSchedule :: String -> Maybe Schedule
|
||||||
toSchedule s = Schedule
|
toSchedule = eitherToMaybe . parseSchedule
|
||||||
<$> toRecurrance (unwords recurrance)
|
|
||||||
<*> toTimeOfDay (unwords timeofday)
|
parseSchedule :: String -> Either String Schedule
|
||||||
<*> toDuration (unwords duration)
|
parseSchedule s = do
|
||||||
|
r <- maybe (Left $ "bad recurrance: " ++ recurrance) Right
|
||||||
|
(toRecurrance recurrance)
|
||||||
|
t <- maybe (Left $ "bad time of day: " ++ timeofday) Right
|
||||||
|
(toTimeOfDay timeofday)
|
||||||
|
d <- maybe (Left $ "bad duration: " ++ duration) Right
|
||||||
|
(toDuration duration)
|
||||||
|
Right $ Schedule r t d
|
||||||
where
|
where
|
||||||
ws = words s
|
ws = words s
|
||||||
(recurrance, ws') = separate (== "at") ws
|
(rws, ws') = separate (== "at") ws
|
||||||
(timeofday, duration) = separate (== "for") ws'
|
(tws, dws) = separate (== "for") ws'
|
||||||
|
recurrance = unwords rws
|
||||||
|
timeofday = unwords tws
|
||||||
|
duration = unwords dws
|
||||||
|
|
||||||
|
instance Arbitrary Schedule where
|
||||||
|
arbitrary = Schedule <$> arbitrary <*> arbitrary <*> arbitrary
|
||||||
|
|
||||||
|
instance Arbitrary Duration where
|
||||||
|
arbitrary = MinutesDuration <$> nonNegative arbitrary
|
||||||
|
|
||||||
|
instance Arbitrary TimeOfDay where
|
||||||
|
arbitrary = oneof
|
||||||
|
[ pure AnyTime
|
||||||
|
, Hour <$> nonNegative arbitrary
|
||||||
|
]
|
||||||
|
|
||||||
|
instance Arbitrary Recurrance where
|
||||||
|
arbitrary = oneof
|
||||||
|
[ pure Daily
|
||||||
|
, Weekly <$> nonNegative arbitrary
|
||||||
|
, Monthly <$> nonNegative arbitrary
|
||||||
|
, Yearly <$> nonNegative arbitrary
|
||||||
|
, Divisable
|
||||||
|
<$> positive arbitrary
|
||||||
|
<*> oneof -- no nested Divisibles
|
||||||
|
[ pure Daily
|
||||||
|
, Weekly <$> nonNegative arbitrary
|
||||||
|
, Monthly <$> nonNegative arbitrary
|
||||||
|
, Yearly <$> nonNegative arbitrary
|
||||||
|
]
|
||||||
|
]
|
||||||
|
|
||||||
|
prop_schedule_roundtrips :: Schedule -> Bool
|
||||||
|
prop_schedule_roundtrips s = case toSchedule $ fromSchedule s of
|
||||||
|
Just s' | s == s' -> True
|
||||||
|
_ -> False
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue