Skip to content

Commit

Permalink
Merge pull request #161 from input-output-hk/jonathanknowles/text-ins…
Browse files Browse the repository at this point in the history
…tances

Add roundtrip textual encoding tests for `Network` and `Port` CLI types.
  • Loading branch information
KtorZ committed Apr 9, 2019
2 parents 00e5f66 + 0d45704 commit ad9927c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .weeder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- name: Module not compiled
- module: Cardano.Launcher.Windows
- section:
- name: exe:cardano-wallet exe:cardano-wallet-launcher bench:restore
- name: exe:cardano-wallet exe:cardano-wallet-launcher test:unit bench:restore
- message:
- name: Module reused between components
- module: Cardano.CLI
Expand Down
6 changes: 5 additions & 1 deletion app/Cardano/CLI.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
Expand Down Expand Up @@ -32,6 +33,8 @@ import Data.Text.Class
( FromText (..), TextDecodingError (..), ToText (..) )
import Fmt
( Buildable, pretty )
import GHC.Generics
( Generic )
import GHC.TypeLits
( Symbol )
import System.Console.Docopt
Expand All @@ -49,7 +52,7 @@ import qualified System.Console.ANSI as ANSI
-- | Available network options. 'Local' means a local cluster running on the
-- host machine.
data Network = Mainnet | Testnet | Staging | Local
deriving (Show, Enum)
deriving (Generic, Show, Eq, Enum)

instance ToText Network where
toText = \case
Expand All @@ -70,6 +73,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, Show, Generic)

instance FromText (Port tag) where
fromText = fmap Port . fromText
Expand Down
10 changes: 7 additions & 3 deletions cardano-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,20 @@ test-suite unit
ghc-options:
-Werror
build-depends:
aeson
base
, aeson
, aeson-qq
, base
, ansi-terminal
, async
, base58-bytestring
, bytestring
, cardano-crypto
, cardano-wallet
, cborg
, cryptonite
, containers
, cryptonite
, deepseq
, docopt
, exceptions
, file-embed
, fmt
Expand Down Expand Up @@ -140,6 +142,8 @@ test-suite unit
main-is:
Main.hs
other-modules:
Cardano.CLI
Cardano.CLISpec
Cardano.Launcher
Cardano.LauncherSpec
Cardano.WalletSpec
Expand Down
37 changes: 37 additions & 0 deletions test/unit/Cardano/CLISpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Cardano.CLISpec
(spec
) where

import Prelude

import Cardano.CLI
( 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 ad9927c

Please sign in to comment.