Skip to content

Commit

Permalink
cardano-api-test: Add AssetName roundtrip tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cblp committed Oct 11, 2021
1 parent 57da24d commit 6f349c5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
2 changes: 2 additions & 0 deletions cardano-api/cardano-api.cabal
Expand Up @@ -210,7 +210,9 @@ test-suite cardano-api-test
, cardano-ledger-shelley
, cardano-ledger-shelley-test
, tasty
, tasty-hedgehog
, tasty-quickcheck
, tasty-th
, time

other-modules: Test.Cardano.Api.Crypto
Expand Down
5 changes: 3 additions & 2 deletions cardano-api/gen/Gen/Cardano/Api/Typed.hs
Expand Up @@ -30,6 +30,7 @@ module Gen.Cardano.Api.Typed
, genScriptHash
, genScriptData

, genAssetName
, genOperationalCertificate
, genOperationalCertificateIssueCounter
, genShelleyWitness
Expand Down Expand Up @@ -227,8 +228,8 @@ genAssetName =
Gen.frequency
-- mostly from a small number of choices, so we get plenty of repetition
[ (9, Gen.element ["", "a", "b", "c"])
, (1, AssetName <$> Gen.utf8 (Range.singleton 32) Gen.alphaNum)
, (1, AssetName <$> Gen.utf8 (Range.constant 1 31) Gen.alphaNum)
, (1, AssetName <$> Gen.bytes (Range.singleton 32))
, (1, AssetName <$> Gen.bytes (Range.constant 1 31))
]

genPolicyId :: Gen PolicyId
Expand Down
27 changes: 22 additions & 5 deletions cardano-api/test/Test/Cardano/Api/Typed/Value.hs
Expand Up @@ -4,16 +4,19 @@ module Test.Cardano.Api.Typed.Value
( tests
) where

import Cardano.Api.Shelley
import Data.Aeson
import Prelude

import Data.Aeson (eitherDecode, encode)
import Data.List (groupBy, sort)
import Gen.Cardano.Api.Typed
import qualified Data.Map.Strict as Map
import Gen.Tasty.Hedgehog.Group (fromGroup)
import Hedgehog (Property, discover, forAll, property, tripping, (===))
import Prelude
import Test.Tasty (TestTree)

import qualified Data.Map.Strict as Map
import Cardano.Api (ValueNestedBundle (ValueNestedBundle, ValueNestedBundleAda),
ValueNestedRep (..), valueFromNestedRep, valueToNestedRep)

import Gen.Cardano.Api.Typed (genAssetName, genValueDefault, genValueNestedRep)

prop_roundtrip_Value_JSON :: Property
prop_roundtrip_Value_JSON =
Expand Down Expand Up @@ -65,6 +68,20 @@ canonicalise =
isZeroOrEmpty (ValueNestedBundleAda q) = q == 0
isZeroOrEmpty (ValueNestedBundle _ as) = Map.null as


prop_roundtrip_AssetName_JSON :: Property
prop_roundtrip_AssetName_JSON =
property $ do
v <- forAll genAssetName
tripping v encode eitherDecode

prop_roundtrip_AssetName_JSONKey :: Property
prop_roundtrip_AssetName_JSONKey =
property $ do
v <- forAll genAssetName
tripping (Map.singleton v ()) encode eitherDecode


-- -----------------------------------------------------------------------------

tests :: TestTree
Expand Down

0 comments on commit 6f349c5

Please sign in to comment.