-
Notifications
You must be signed in to change notification settings - Fork 721
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement To/FromJSON instances for Value (Multi-Asset Value)
Add round-trip tests. Improve the generators for Value and related types: sample the PolicyId and AssetName from a smaller range of values so that for things like Value we get more "interesting" values with duplicate policy ids or asset names. Add a canonicalise function for ValueNestedRep which should be the equivalent of converting from ValueNestedRep to Value and back.
- Loading branch information
Showing
6 changed files
with
249 additions
and
31 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
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
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,79 @@ | ||
{-# LANGUAGE TemplateHaskell #-} | ||
|
||
module Test.Cardano.Api.Typed.Value | ||
( tests | ||
) where | ||
|
||
import Prelude | ||
|
||
import Data.List (sort, groupBy) | ||
import Data.Aeson | ||
import qualified Data.Map.Strict as Map | ||
|
||
import Cardano.Api.Typed | ||
|
||
import Hedgehog (Property, discover, forAll, property, tripping, (===)) | ||
import Test.Cardano.Api.Typed.Gen | ||
|
||
import Test.Tasty (TestTree) | ||
import Test.Tasty.Hedgehog.Group (fromGroup) | ||
|
||
prop_roundtrip_Value_JSON :: Property | ||
prop_roundtrip_Value_JSON = | ||
property $ do v <- forAll genValue | ||
tripping v encode eitherDecode | ||
|
||
|
||
prop_roundtrip_Value_flatten_unflatten :: Property | ||
prop_roundtrip_Value_flatten_unflatten = | ||
property $ do v <- forAll genValue | ||
valueFromNestedRep (valueToNestedRep v) === v | ||
|
||
prop_roundtrip_Value_unflatten_flatten :: Property | ||
prop_roundtrip_Value_unflatten_flatten = | ||
property $ do | ||
v <- forAll genValueNestedRep | ||
canonicalise v === valueToNestedRep (valueFromNestedRep v) | ||
|
||
canonicalise :: ValueNestedRep -> ValueNestedRep | ||
canonicalise = | ||
ValueNestedRep | ||
. filter (not . isZeroOrEmpty) | ||
. map filterZeros | ||
. map (foldl1 mergeBundle) | ||
. groupBy samePolicyId | ||
. sort | ||
. (\(ValueNestedRep bundles) -> bundles) | ||
where | ||
samePolicyId ValueNestedBundleAda{} | ||
ValueNestedBundleAda{} = True | ||
samePolicyId (ValueNestedBundle pid _) | ||
(ValueNestedBundle pid' _) = pid == pid' | ||
samePolicyId _ _ = False | ||
|
||
-- Merge together bundles that have already been grouped by same PolicyId: | ||
mergeBundle (ValueNestedBundleAda q) | ||
(ValueNestedBundleAda q') = | ||
ValueNestedBundleAda (q <> q') | ||
|
||
mergeBundle (ValueNestedBundle pid as) | ||
(ValueNestedBundle pid' as') | pid == pid' = | ||
ValueNestedBundle pid (Map.unionWith (<>) as as') | ||
|
||
mergeBundle _ _ = error "canonicalise.mergeBundle: impossible" | ||
|
||
filterZeros b@ValueNestedBundleAda{} = b | ||
filterZeros (ValueNestedBundle pid as) = | ||
ValueNestedBundle pid (Map.filter (/=0) as) | ||
|
||
isZeroOrEmpty (ValueNestedBundleAda q) = q == 0 | ||
isZeroOrEmpty (ValueNestedBundle _ as) = Map.null as | ||
|
||
{-# ANN canonicalise "HLint: ignore Use map once" #-} | ||
|
||
|
||
-- ----------------------------------------------------------------------------- | ||
|
||
tests :: TestTree | ||
tests = fromGroup $$discover | ||
|
Oops, something went wrong.