Skip to content

Commit

Permalink
adding last test for all exported functions in DecimalTime.hs
Browse files Browse the repository at this point in the history
  • Loading branch information
travgm committed Nov 3, 2024
1 parent 667e2d0 commit b990fa0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
![haskell workflow](https://github.com/travgm/dclock/actions/workflows/haskell.yml/badge.svg)

dclock
======

![haskell workflow](https://github.com/travgm/dclock/actions/workflows/haskell.yml/badge.svg)

This was mostly created for my own edification. This takes a simple calculation converting system time
to usable decimal minutes left in the day. I wanted to utilize the machines package to create a nice
compositional monadic pipeline and learn some new concepts.
Expand Down
32 changes: 30 additions & 2 deletions test/DecimalTimeSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ instance Arbitrary TimeOfDay where
<*> choose (0, 59)
<*> (fromInteger <$> choose (0, 59))

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

instance Arbitrary Day where
arbitrary = ModifiedJulianDay <$> arbitrary

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

instance Arbitrary ZonedTime where
arbitrary = do
timeZone <- arbitrary
localTime <- arbitrary
return $ ZonedTime localTime timeZone

makeTestState :: TimeOfDay -> ClockState
makeTestState tod =
ClockState False Nothing (Just $ LocalTime (fromGregorian 2024 1 1) tod)
Expand All @@ -33,10 +48,16 @@ spec = do
Right s -> s ^. decimalTime `shouldBe` Just (ValidDecimalTime (DecimalTime 500))
Left err -> expectationFailure $ "Expected Right but got Left: " ++ err

it "converts teatime to 333" $ do
let state = makeTestState (TimeOfDay 16 0 0)
case localTimeToDecimal state of
Right s -> s ^. decimalTime `shouldBe` Just (ValidDecimalTime (DecimalTime 333))
Left err -> expectationFailure $ "Expected Right but got Left: " ++ err

it "fails when currentDate is Nothing" $ do
let state = ClockState False Nothing Nothing
case localTimeToDecimal state of
Left _ -> return ()
Left _ -> return ()
Right _ -> expectationFailure "Expected Left but got Right"

it "always produces value between 0 and 1000" $ property $ \tod ->
Expand All @@ -52,4 +73,11 @@ spec = do
let state = (makeTestState tod) {_extendedFlag = extended}
in case localTimeToDecimal state of
Right s -> s ^. extendedFlag == extended
Left _ -> True
Left _ -> True

describe "updateCurrentDateWithZonedTime returns proper date" $ do
it "returns the date from the ZonedTime" $ property $ \tod zt ->
let state = makeTestState tod
localTime = zonedTimeToLocalTime zt
updatedState = updateCurrentDateWithZonedTime zt state
in updatedState ^. currentDate == Just localTime

0 comments on commit b990fa0

Please sign in to comment.