Skip to content

Commit

Permalink
Add tests for roundtrip textual encoding of Port and Network types.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanknowles committed Apr 8, 2019
1 parent a3ed727 commit 368116a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions cardano-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ test-suite unit
Cardano.Wallet.CoinSelectionSpec
Cardano.Wallet.CoinSelection.LargestFirstSpec
Cardano.Wallet.CoinSelection.RandomSpec
Cardano.Wallet.CLI.TypesSpec
Cardano.Wallet.DBSpec
Cardano.Wallet.DB.MVarSpec
Cardano.Wallet.NetworkSpec
Expand Down
6 changes: 5 additions & 1 deletion src/Cardano/Wallet/CLI/Types.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}

Expand All @@ -17,6 +18,8 @@ import Prelude

import Data.Text.Class
( FromText (..), TextDecodingError (..), ToText (..) )
import GHC.Generics
( Generic )
import GHC.TypeLits
( Symbol )

Expand All @@ -25,7 +28,7 @@ import qualified Data.Text as T
-- | Available network options. 'Local' means a local cluster running on the
-- host machine.
data Network = Mainnet | Testnet | Staging | Local
deriving (Show, Enum)
deriving (Eq, Generic, Show, Enum)

instance ToText Network where
toText = \case
Expand All @@ -46,6 +49,7 @@ instance FromText Network where

-- | Port number with a tag for describing what it is used for
newtype Port (tag :: Symbol) = Port Int
deriving (Eq, Generic, Show)

instance FromText (Port tag) where
fromText = fmap Port . fromText
Expand Down
35 changes: 35 additions & 0 deletions test/unit/Cardano/Wallet/CLI/TypesSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Cardano.Wallet.CLI.TypesSpec (spec) where

import Prelude

import Cardano.Wallet.CLI.Types
( Network, Port (..) )
import Data.Proxy
( Proxy (..) )
import Test.Hspec
( Spec, describe )
import Test.QuickCheck
( Arbitrary (..) )
import Test.QuickCheck.Arbitrary.Generic
( genericArbitrary, genericShrink )
import Test.Text.Roundtrip
( textRoundtrip )

spec :: Spec
spec = do
describe "Can perform roundtrip textual encoding & decoding" $ do
textRoundtrip $ Proxy @Network
textRoundtrip $ Proxy @(Port "test")

instance Arbitrary Network where
arbitrary = genericArbitrary
shrink = genericShrink

instance Arbitrary (Port "test") where
arbitrary = genericArbitrary
shrink = genericShrink

0 comments on commit 368116a

Please sign in to comment.