Reject index-states after last known index-state (#8944)
[cabal.git] / cabal-install / tests / UnitTests / Distribution / Client / IndexUtils / Timestamp.hs
blob29c9fe587e0a1f0c36bdcfe3c90b4258fee32f64
1 module UnitTests.Distribution.Client.IndexUtils.Timestamp (tests) where
3 import Data.Time
4 import Data.Time.Clock.POSIX
5 import Distribution.Parsec (simpleParsec)
6 import Distribution.Pretty (prettyShow)
8 import Distribution.Client.IndexUtils.Timestamp
10 import Test.Tasty
11 import Test.Tasty.QuickCheck
13 tests :: [TestTree]
14 tests =
15 [ testProperty "Timestamp1" prop_timestamp1
16 , testProperty "Timestamp2" prop_timestamp2
17 , testProperty "Timestamp3" prop_timestamp3
18 , testProperty "Timestamp4" prop_timestamp4
19 , testProperty "Timestamp5" prop_timestamp5
22 -- test unixtime format parsing
23 prop_timestamp1 :: NonNegative Int -> Bool
24 prop_timestamp1 (NonNegative t0) = Just t == simpleParsec ('@' : show t0)
25 where
26 t = epochTimeToTimestamp $ toEnum t0 :: Timestamp
28 -- test prettyShow/simpleParse roundtrip
29 prop_timestamp2 :: Int -> Bool
30 prop_timestamp2 t0 = simpleParsec (prettyShow t) == Just t
31 where
32 t = epochTimeToTimestamp $ toEnum t0 :: Timestamp
34 -- test prettyShow against reference impl
35 prop_timestamp3 :: Int -> Bool
36 prop_timestamp3 t0 = refDisp t == prettyShow t
37 where
38 t = epochTimeToTimestamp $ toEnum t0 :: Timestamp
40 refDisp =
41 maybe undefined (formatTime undefined "%FT%TZ")
42 . timestampToUTCTime
44 -- test utcTimeToTimestamp/timestampToUTCTime roundtrip
45 prop_timestamp4 :: Int -> Bool
46 prop_timestamp4 t0 =
47 (utcTimeToTimestamp <$> timestampToUTCTime t) == Just t
48 where
49 t = epochTimeToTimestamp $ toEnum t0 :: Timestamp
51 prop_timestamp5 :: Int -> Bool
52 prop_timestamp5 t0 = timestampToUTCTime t == Just ut
53 where
54 t = epochTimeToTimestamp $ toEnum t0 :: Timestamp
55 ut = posixSecondsToUTCTime (fromIntegral t0)