41 lines
1019 B
Haskell
41 lines
1019 B
Haskell
{-# LANGUAGE NoImplicitPrelude #-}
|
|
|
|
module CronSpec where
|
|
|
|
import TestImport
|
|
|
|
import Cron
|
|
import Numeric.Natural
|
|
|
|
import Data.Time
|
|
import Data.Time.Clock.System
|
|
import Data.Time.Zones
|
|
|
|
import Data.List (iterate)
|
|
|
|
|
|
baseTime :: UTCTime
|
|
baseTime = UTCTime (addDays 58400 systemEpochDay) 50000
|
|
|
|
|
|
sampleCron :: Natural -> Cron -> [UTCTime]
|
|
sampleCron n = go n baseTime Nothing
|
|
where
|
|
go 0 _ _ _ = []
|
|
go n t mPrev cron = case nextCronMatch utcTZ mPrev t cron of
|
|
MatchAsap -> t : go (pred n) t (Just t) cron
|
|
MatchAt t' -> t' : go (pred n) t' (Just t') cron
|
|
MatchNone -> []
|
|
|
|
|
|
spec :: Spec
|
|
spec = do
|
|
describe "Cron" $ do
|
|
it "generates correct example series" . mapM_ seriesExample $
|
|
[ (Cron CronAsap Nothing CronScheduleBefore, [baseTime])
|
|
, (Cron CronAsap (Just $ CronPeriod 10 CronAsap) CronScheduleBefore, iterate (addUTCTime 10) baseTime)
|
|
]
|
|
|
|
seriesExample :: (Cron, [UTCTime]) -> Expectation
|
|
seriesExample (cron, res) = example $ sampleCron 10 cron `shouldBe` take 10 res
|