Skip to content

Commit

Permalink
Merge pull request #88 from serokell/vrom911/64-series-test
Browse files Browse the repository at this point in the history
[#64] Add property tests for unitsP . unitsF = id
  • Loading branch information
chshersh authored Feb 11, 2018
2 parents 3ab2865 + d404f77 commit 0b81d73
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The change log is available [on GitHub][2].
0.1.0
=====

* [#64](https://github.com/serokell/o-clock/issues/64):
Add property tests for `unitsP . unitsF ≡ id`
* [#63](https://github.com/serokell/o-clock/issues/63):
Rename `Formatting` module to `Series`.
Add `SeriesP` class for parsing time.
Expand Down
18 changes: 16 additions & 2 deletions test/Test/Time/Property.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Test.Tasty.Hedgehog (testProperty)

import Time (Day, Fortnight, Hour, KnownRat, KnownRatName, Microsecond,
Millisecond, Minute, Nanosecond, Picosecond, Rat, RatioNat, Second,
Time (..), Week, toUnit)
Time (..), Week, toUnit, unitsF, unitsP)
#if ( __GLASGOW_HASKELL__ >= 804 )
import Time (withRuntimeDivRat)
#endif
Expand All @@ -28,14 +28,17 @@ import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range

hedgehogTestTrees :: [TestTree]
hedgehogTestTrees = [readShowTestTree, toUnitTestTree]
hedgehogTestTrees = [readShowTestTree, toUnitTestTree, seriesTestTree]

readShowTestTree :: TestTree
readShowTestTree = testProperty "Hedgehog read . show == id" prop_readShowUnit

toUnitTestTree :: TestTree
toUnitTestTree = testProperty "Hedgehog toUnit @to @from . toUnit @from @to ≡ id' property" prop_toUnit

seriesTestTree :: TestTree
seriesTestTree = testProperty "Hedgehog unitsP . unitsF ≡ id" prop_series

-- | Existential data type for 'Unit's.
data AnyTime = forall (unit :: Rat) . (KnownRatName unit)
=> MkAnyTime (Time unit)
Expand Down Expand Up @@ -78,6 +81,14 @@ verifyToUnit (MkAnyTime t1) (MkAnyTime t2) = checkToUnit t1 t2
#endif
toUnit (toUnit @unitTo t) === t

-- | Verifier for @ seriesP . seriesF @.
verifySeries :: forall m . (MonadTest m) => AnyTime -> m ()
verifySeries (MkAnyTime anyT) = checkSeries anyT
where
checkSeries :: forall (unit :: Rat) . KnownRatName unit
=> Time unit -> m ()
checkSeries t = unitsP @unit (unitsF t) === Just t

-- | Generates random natural number up to 10^20.
-- it receives the lower bound so that it wouldn't be possible
-- to get 0 for denominator.
Expand Down Expand Up @@ -108,3 +119,6 @@ prop_toUnit = property $ do
t1 <- genAnyTime
t2 <- genAnyTime
verifyToUnit t1 t2

prop_series :: Property
prop_series = property $ genAnyTime >>= verifySeries

0 comments on commit 0b81d73

Please sign in to comment.