-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for the new Parsec parsers
- Loading branch information
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
{-# LANGUAGE RankNTypes #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
|
||
module Test.Cli.Parser | ||
( hprop_integral_reader | ||
, hprop_integral_pair_reader_positive | ||
, hprop_integral_pair_reader_negative | ||
) | ||
where | ||
|
||
import Cardano.CLI.EraBased.Options.Common (integralParsecParser, | ||
pairIntegralParsecParser) | ||
|
||
import Data.Bits (Bits) | ||
import Data.Data (Proxy (..), Typeable) | ||
import Data.Either (isLeft, isRight) | ||
import Data.Word (Word16) | ||
import qualified Text.Parsec as Parsec | ||
|
||
import Hedgehog (Gen, Property, assert, property) | ||
import Hedgehog.Extras (propertyOnce) | ||
import qualified Hedgehog.Gen as Gen | ||
import Hedgehog.Internal.Property (forAll) | ||
import qualified Hedgehog.Range as Gen | ||
import qualified Hedgehog.Range as Range | ||
|
||
-- | Execute me with: | ||
-- @cabal test cardano-cli-test --test-options '-p "/integral reader/"'@ | ||
hprop_integral_reader :: Property | ||
hprop_integral_reader = property $ do | ||
assert $ isRight $ parse @Word "0" | ||
assert $ isRight $ parse @Word "42" | ||
assert $ isLeft $ parse @Word "-1" | ||
assert $ isLeft $ parse @Word "18446744073709551616" | ||
assert $ isLeft $ parse @Word "-1987090" | ||
|
||
w <- forAll $ Gen.word $ Gen.linear minBound maxBound | ||
assert $ isRight $ parse @Word (show w) | ||
|
||
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 "/integral pair reader positive/"'@ | ||
hprop_integral_pair_reader_positive :: Property | ||
hprop_integral_pair_reader_positive = property $ do | ||
validArbitraryTuple <- forAll $ genNumberTuple (Proxy :: Proxy Word) | ||
assert $ isRight $ parse @Word validArbitraryTuple | ||
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 | ||
|
||
genNumberTuple :: forall a. Integral a => Show a => Proxy a -> Gen String | ||
genNumberTuple _ = do | ||
x :: a <- Gen.integral (Range.linear 0 100) | ||
y :: a <- Gen.integral (Range.linear 0 100) | ||
space0 <- genArbitrarySpace | ||
space1 <- genArbitrarySpace | ||
space2 <- genArbitrarySpace | ||
space3 <- genArbitrarySpace | ||
return $ | ||
space0 ++ "(" ++ space2 ++ show x ++ space1 ++ "," ++ space2 ++ show y ++ space1 ++ ")" ++ space3 | ||
|
||
genArbitrarySpace :: Gen String | ||
genArbitrarySpace = Gen.string (Range.linear 0 5) (return ' ') | ||
|
||
-- | Execute me with: | ||
-- @cabal test cardano-cli-test --test-options '-p "/integral pair reader negative/"'@ | ||
hprop_integral_pair_reader_negative :: Property | ||
hprop_integral_pair_reader_negative = propertyOnce $ do | ||
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 |