packages feed

liblawless-0.13.3: Tests/TestTime.hs

{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module TestTime where

import Test.Framework
import Test.Framework.TH
import Test.Framework.Providers.QuickCheck2 (testProperty)
import Test.QuickCheck

import qualified Data.ByteString.Lazy as L

import Data.Time.Clock
import Data.Time.Calendar
import Data.Binary

import Lawless
import Time

instance Arbitrary Day where
  arbitrary = toEnum <$> arbitrary

instance Arbitrary DiffTime where
  arbitrary = fromRational <$> suchThat arbitrary (\t -> t >= 0.0 && t <= 86401.0)

instance Arbitrary UTCTime where
  arbitrary =
    UTCTime <$> arbitrary <*> arbitrary

instance Arbitrary Time where
  arbitrary = review _Time <$> arbitrary

prop_PrismToTime :: UTCTime -> Property
prop_PrismToTime (ut :: UTCTime) =
  ut ^.re _Time ^. day === utctDay ut .&&.
  ut ^.re _Time ^. time === utctDayTime ut

prop_PrismFromTime :: Time -> Property
prop_PrismFromTime (t :: Time) =
  utctDay (t ^. _Time) === t ^. day .&&.
  utctDayTime (t ^. _Time) === t ^. time

prop_Serialize :: Time -> Property
prop_Serialize (t :: Time) =
  let
    sz = L.toStrict $ encode t
    de = decode $ L.fromStrict sz
  in
    de === t

properties ∷ Test
properties = $(testGroupGenerator)