Skip to content

Commit

Permalink
Add tests for the new Parsec parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
smelc committed Aug 22, 2024
1 parent 8fe2f7d commit 13f04eb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ test-suite cardano-cli-test
filepath,
hedgehog,
hedgehog-extras ^>=0.6.1.0,
parsec,
regex-tdfa,
tasty,
tasty-hedgehog,
Expand All @@ -340,6 +341,7 @@ test-suite cardano-cli-test
Test.Cli.ITN
Test.Cli.Json
Test.Cli.MonadWarning
Test.Cli.Parser
Test.Cli.Pioneers.Exercise1
Test.Cli.Pioneers.Exercise2
Test.Cli.Pioneers.Exercise3
Expand Down
66 changes: 66 additions & 0 deletions cardano-cli/test/cardano-cli-test/Test/Cli/Parser.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{-# LANGUAGE TypeApplications #-}

module Test.Cli.Parser
( hprop_integral_reader
, hprop_pair_integral_reader
)
where

import Cardano.CLI.EraBased.Options.Common (integralParsecParser,
pairIntegralParsecParser)

import Data.Bits (Bits)
import Data.Data (Typeable)
import Data.Either (isLeft, isRight)
import Data.Word (Word16)
import qualified Text.Parsec as Parsec

import Hedgehog (Property, assert)
import Hedgehog.Extras (propertyOnce)

-- | Execute me with:
-- @cabal test cardano-cli-test --test-options '-p "/integral reader/"'@
hprop_integral_reader :: Property
hprop_integral_reader = propertyOnce $ do
assert $ isRight $ parse @Word "0"
assert $ isRight $ parse @Word "42"
assert $ isLeft $ parse @Word16 "-1"
assert $ isLeft $ parse @Word "18446744073709551616"
assert $ isLeft $ parse @Word "-1987090"

assert $ isRight $ parse @Word16 "0"
assert $ isRight $ parse @Word16 "42"
assert $ isLeft $ parse @Word16 "-1"
assert $ isLeft $ parse @Word16 "65536"
assert $ isLeft $ parse @Word16 "298709870987"
assert $ isLeft $ parse @Word16 "-1987090"
where
parse :: (Typeable a, Integral a, Bits a) => String -> Either String a
parse s =
case Parsec.runParser integralParsecParser () "" s of
Left parsecError -> Left $ show parsecError
Right x -> Right x

-- | Execute me with:
-- @cabal test cardano-cli-test --test-options '-p "/pair integral reader/"'@
hprop_pair_integral_reader :: Property
hprop_pair_integral_reader = propertyOnce $ do
assert $ isRight $ parse @Word "(0, 0)"
assert $ isRight $ parse @Word " ( 0 , 0 )"
assert $ isRight $ parse @Word " (18446744073709551615 , 18446744073709551614 ) "

assert $ isLeft $ parse @Word "(0, 0, 0)"
assert $ isLeft $ parse @Word "(-1, 0)"
assert $ isLeft $ parse @Word "(18446744073709551616, 0)"
assert $ isLeft $ parse @Word "(0, 18446744073709551616)"
assert $ isLeft $ parse @Word "(0, -1)"
assert $ isLeft $ parse @Word "0, 0)"
assert $ isLeft $ parse @Word "(0, 0"
assert $ isLeft $ parse @Word "(0 0)"
assert $ isLeft $ parse @Word "( 0, 0"
where
parse :: (Typeable a, Integral a, Bits a) => String -> Either String (a, a)
parse s =
case Parsec.runParser pairIntegralParsecParser () "" s of
Left parsecError -> Left $ show parsecError
Right x -> Right x

0 comments on commit 13f04eb

Please sign in to comment.