Skip to content

Commit

Permalink
Add property test checking for 'parseNetworkParameters'
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed May 4, 2021
1 parent ea8fdd7 commit 8069a78
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions server/package.yaml
Expand Up @@ -138,5 +138,6 @@ tests:
- relude
- shelley-spec-ledger
- shelley-spec-ledger-test
- time
build-tools:
- hspec-discover
1 change: 1 addition & 0 deletions server/src/Ogmios/App/Options.hs
Expand Up @@ -27,6 +27,7 @@ module Ogmios.App.Options
, NetworkParameters (..)
, NetworkMagic (..)
, EpochSlots (..)
, SystemStart (..)
, envOgmiosNetwork
, lookupNetworkParameters
, parseNetworkParameters
Expand Down
57 changes: 57 additions & 0 deletions server/test/unit/Ogmios/App/OptionsSpec.hs
@@ -0,0 +1,57 @@
-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.

module Ogmios.App.OptionsSpec
( spec
) where

import Ogmios.Prelude

import Ogmios.App.Options
( EpochSlots (..)
, NetworkMagic (..)
, NetworkParameters (..)
, SystemStart (..)
, parseNetworkParameters
)

import Data.Time.Clock.POSIX
( posixSecondsToUTCTime )
import Test.Hspec
( Spec, context, parallel )
import Test.Hspec.QuickCheck
( prop )
import Test.QuickCheck
( Arbitrary (..), Gen, Property, choose, conjoin, forAll, property, (===) )

spec :: Spec
spec = parallel $ do
context "parseNetworkParameters" $ do
prop
"Parse any combination of colon separated naturals"
prop_anyParseNetworkParameters

prop_anyParseNetworkParameters :: Property
prop_anyParseNetworkParameters =
forAll genRawParameters $ \(a,b,c) -> do
case parseNetworkParameters (intercalate ":" [show a, show b, show c]) of
Nothing ->
property False
Just params -> conjoin
[ networkMagic params === NetworkMagic a
, systemStart params === mkSystemStart b
, slotsPerEpoch params === EpochSlots c
]
where
mkSystemStart :: Int -> SystemStart
mkSystemStart =
SystemStart . posixSecondsToUTCTime . toPicoResolution . toEnum
where
toPicoResolution = (*1000000000000)

genRawParameters :: Gen (Word32, Int, Word64)
genRawParameters = (,,)
<$> arbitrary
<*> choose (0, fromIntegral (maxBound :: Word32))
<*> choose (0, 100000)

0 comments on commit 8069a78

Please sign in to comment.