Skip to content

Commit

Permalink
Add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
carbolymer committed Aug 26, 2024
1 parent a70b647 commit 10c5809
Show file tree
Hide file tree
Showing 7 changed files with 970 additions and 23 deletions.
3 changes: 3 additions & 0 deletions cardano-api/cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ library internal
ouroboros-network-protocols,
parsec,
plutus-ledger-api:{plutus-ledger-api, plutus-ledger-api-testlib} ^>=1.31,
pretty-simple,
prettyprinter,
prettyprinter-ansi-terminal,
prettyprinter-configurable ^>=1.31,
Expand Down Expand Up @@ -317,6 +318,8 @@ test-suite cardano-api-test
cardano-ledger-api ^>=1.9,
cardano-ledger-binary,
cardano-ledger-core:{cardano-ledger-core, testlib} >=1.8,
cardano-ledger-mary,
cardano-ledger-shelley,
cardano-protocol-tpraos,
cardano-slotting,
cborg,
Expand Down
24 changes: 13 additions & 11 deletions cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ genPlutusScript _ =
genScriptDataSchema :: Gen ScriptDataJsonSchema
genScriptDataSchema = Gen.element [ScriptDataJsonNoSchema, ScriptDataJsonDetailedSchema]

genHashableScriptData :: Gen HashableScriptData
genHashableScriptData :: HasCallStack => Gen HashableScriptData
genHashableScriptData = do
sd <- genScriptData
case deserialiseFromCBOR AsHashableScriptData $ serialiseToCBOR sd of
Expand Down Expand Up @@ -365,8 +365,9 @@ genSignedQuantity = genQuantity (Range.constantFrom 0 (-2) 2)
genSignedNonZeroQuantity :: Gen Quantity
genSignedNonZeroQuantity =
Gen.choice
[ genQuantity (Range.constant (-2) (-1))
, genQuantity (Range.constant 1 2)
-- FIXME negative quantity does not compact in ledger and throws an error here - needs investigation
-- [ genQuantity (Range.constant (-2) (-1))
[ genQuantity (Range.constant 1 2)
]

genUnsignedQuantity :: Gen Quantity
Expand All @@ -380,7 +381,7 @@ genValue
genValue w genAId genQuant =
toLedgerValue w . valueFromList
<$> Gen.list
(Range.constant 0 10)
(Range.constant 1 10)
((,) <$> genAId <*> genQuant)

-- | Generate a 'Value' with any asset ID and a positive or negative quantity.
Expand Down Expand Up @@ -546,10 +547,12 @@ genTxOutUTxOContext era =
genReferenceScript :: ShelleyBasedEra era -> Gen (ReferenceScript era)
genReferenceScript era = scriptInEraToRefScript <$> genScriptInEra era

genUTxO :: ShelleyBasedEra era -> Gen (UTxO era)
genUTxO era =
genUTxO :: ShelleyBasedEra era
-> Range Int -- ^ the range of UTXOs number
-> Gen (UTxO era)
genUTxO era range =
UTxO
<$> Gen.map (Range.constant 0 5) ((,) <$> genTxIn <*> (toCtxUTxOTxOut <$> genTxOutTxContext era))
<$> Gen.map range ((,) <$> genTxIn <*> (toCtxUTxOTxOut <$> genTxOutTxContext era))

genTtl :: Gen SlotNo
genTtl = genSlotNo
Expand Down Expand Up @@ -615,7 +618,6 @@ genTxCertificates =
)

-- TODO: Add remaining certificates
-- TODO: This should be parameterised on ShelleyBasedEra
genCertificate :: ShelleyBasedEra era -> Gen (Certificate era)
genCertificate sbe =
Gen.choice
Expand Down Expand Up @@ -649,11 +651,11 @@ genTxMintValue :: CardanoEra era -> Gen (TxMintValue BuildTx era)
genTxMintValue =
inEonForEra
(pure TxMintNone)
$ \supported ->
$ \w ->
Gen.choice
[ pure TxMintNone
[ -- pure TxMintNone
-- TODO write a generator for the last parameter of 'TxMintValue' constructor
, TxMintValue supported <$> genValueForMinting supported <*> return (pure mempty)
TxMintValue w <$> genValueForMinting w <*> return (pure mempty)
]

genTxBodyContent :: ShelleyBasedEra era -> Gen (TxBodyContent BuildTx era)
Expand Down
7 changes: 5 additions & 2 deletions cardano-api/internal/Cardano/Api/Tx/Body.hs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ import qualified Data.Text as Text
import Data.Type.Equality (TestEquality (..), (:~:) (Refl))
import Data.Word (Word16, Word32, Word64)
import GHC.Exts (IsList (..))
import GHC.Stack
import Lens.Micro hiding (ix)
import Lens.Micro.Extras (view)
import qualified Text.Parsec as Parsec
Expand Down Expand Up @@ -688,7 +689,8 @@ toByronTxOut = \case

toShelleyTxOut
:: forall era ledgerera
. ShelleyLedgerEra era ~ ledgerera
. HasCallStack
=> ShelleyLedgerEra era ~ ledgerera
=> ShelleyBasedEra era
-> TxOut CtxUTxO era
-> Ledger.TxOut ledgerera
Expand Down Expand Up @@ -3038,7 +3040,8 @@ makeShelleyTransactionBody
-- embedded datums (taking only their hash).
toShelleyTxOutAny
:: forall ctx era ledgerera
. ShelleyLedgerEra era ~ ledgerera
. HasCallStack
=> ShelleyLedgerEra era ~ ledgerera
=> ShelleyBasedEra era
-> TxOut ctx era
-> Ledger.TxOut ledgerera
Expand Down
19 changes: 13 additions & 6 deletions cardano-api/internal/Cardano/Api/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,22 @@ newtype Value = Value (Map AssetId Quantity)
instance Show Value where
showsPrec d v =
showParen (d > 10) $
showString "valueFromList " . shows (valueToList v)
showString "valueFromList " . shows (toList v)

instance Semigroup Value where
Value a <> Value b = Value (mergeAssetMaps a b)

instance Monoid Value where
mempty = Value Map.empty

instance IsList Value where
type Item Value = (AssetId, Quantity)
fromList =
Value
. Map.filter (/= 0)
. Map.fromListWith (<>)
toList (Value m) = toList m

{-# NOINLINE mergeAssetMaps #-} -- as per advice in Data.Map.Merge docs
mergeAssetMaps
:: Map AssetId Quantity
Expand All @@ -223,14 +231,13 @@ instance FromJSON Value where
selectAsset :: Value -> (AssetId -> Quantity)
selectAsset (Value m) a = Map.findWithDefault mempty a m

{-# DEPRECATED valueFromList "Use 'fromList' instead." #-}
valueFromList :: [(AssetId, Quantity)] -> Value
valueFromList =
Value
. Map.filter (/= 0)
. Map.fromListWith (<>)
valueFromList = fromList

{-# DEPRECATED valueToList "Use 'toList' instead." #-}
valueToList :: Value -> [(AssetId, Quantity)]
valueToList (Value m) = toList m
valueToList = toList

-- | This lets you write @a - b@ as @a <> negateValue b@.
negateValue :: Value -> Value
Expand Down
3 changes: 2 additions & 1 deletion cardano-api/test/cardano-api-test/Test/Cardano/Api/Json.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Test.Gen.Cardano.Api.Typed

import Hedgehog (Property, forAll, tripping)
import qualified Hedgehog as H
import qualified Hedgehog.Range as H
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.Hedgehog (testProperty)

Expand All @@ -28,7 +29,7 @@ prop_json_roundtrip_alonzo_genesis = H.property $ do

prop_json_roundtrip_utxo :: Property
prop_json_roundtrip_utxo = H.property $ do
utxo <- forAll $ genUTxO ShelleyBasedEraBabbage
utxo <- forAll $ genUTxO ShelleyBasedEraBabbage (H.constant 0 5)
tripping utxo encode eitherDecode

prop_json_roundtrip_reference_scripts :: Property
Expand Down
Loading

0 comments on commit 10c5809

Please sign in to comment.