Skip to content

Commit

Permalink
Serialise instance of Data is broken
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mueller committed Oct 26, 2020
1 parent 99f3a16 commit a91569c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
16 changes: 16 additions & 0 deletions plutus-tx/plutus-tx.cabal
Expand Up @@ -88,3 +88,19 @@ library
lens -any,
ghc-prim -any,
aeson -any

test-suite plutus-tx-test
import: lang
type: exitcode-stdio-1.0
main-is: Spec.hs
hs-source-dirs: test
build-depends:
base >=4.9 && <5,
plutus-tx -any,
bytestring -any,
hedgehog -any,
tasty -any,
tasty-hunit -any,
tasty-hedgehog -any,
serialise -any,

43 changes: 43 additions & 0 deletions plutus-tx/test/Spec.hs
@@ -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
]

0 comments on commit a91569c

Please sign in to comment.