Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serialise instance of Data is broken
- Loading branch information
Showing
2 changed files
with
59 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,43 @@ | ||
module Main(main) where | ||
|
||
import Codec.Serialise (deserialiseOrFail, serialise) | ||
import Hedgehog (MonadGen, Property, annotateShow, assert, forAll, property) | ||
import qualified Hedgehog.Gen as Gen | ||
import qualified Hedgehog.Range as Range | ||
import Language.PlutusTx.Data (Data (..)) | ||
import Test.Tasty | ||
import Test.Tasty.Hedgehog (testProperty) | ||
|
||
main :: IO () | ||
main = defaultMain tests | ||
|
||
tests :: TestTree | ||
tests = testGroup "plutus-tx" [ | ||
serdeTests | ||
] | ||
|
||
serdeTests :: TestTree | ||
serdeTests = testGroup "Data serialisation" [ | ||
testProperty "should round-trip" dataRoundTrip | ||
] | ||
|
||
dataRoundTrip :: Property | ||
dataRoundTrip = property $ do | ||
dt <- forAll genData | ||
let res = deserialiseOrFail (serialise dt) | ||
annotateShow res | ||
assert (res == Right dt) | ||
|
||
genData :: MonadGen m => m Data | ||
genData = | ||
let positiveInteger = Gen.integral (Range.linear 0 100000) | ||
constructorArgList = Gen.list (Range.linear 0 50) genData | ||
kvMapList = Gen.list (Range.linear 0 100) ((,) <$> genData <*> genData) | ||
in | ||
Gen.recursive Gen.choice | ||
[ I <$> Gen.integral (Range.linear (-100000) 100000) | ||
, B <$> Gen.bytes (Range.linear 0 1000) ] | ||
[ Constr <$> positiveInteger <*> constructorArgList | ||
, List <$> constructorArgList | ||
, Map <$> kvMapList | ||
] |