Skip to content

Commit d3cc240

Browse files
committed
fix(flat): address PR review feedback on encoding tests
Rename testNewEncodings to testEncodingStability for clarity. Fix plutus-core ^>=1.59 version bound to ^>=1.60. Remove unnecessary -Wno-* flags and unused text dependency from flat-encoding-generator. Improve executable comment with golden testing context and Wikipedia reference.
1 parent 3909198 commit d3cc240

4 files changed

Lines changed: 42 additions & 23 deletions

File tree

plutus-core/flat/test/Spec.hs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,13 @@ testEncDec = testGroup
114114

115115
testFlat = testGroup
116116
"flat/unflat"
117-
[testSize, testLargeEnum, testContainers, flatUnflatRT, flatTests, testNewEncodings]
117+
[ testSize
118+
, testLargeEnum
119+
, testContainers
120+
, flatUnflatRT
121+
, flatTests
122+
, testEncodingStability
123+
]
118124

119125
-- Flat.Endian tests (to run, need to modify imports and cabal file)
120126
testEndian = testGroup
@@ -367,7 +373,7 @@ testContainers =
367373
-- | Stable byte encoding tests for flat library container/composite types.
368374
-- Wrapper types (Identity, All, Any, Dual, etc.) only have roundtrip tests
369375
-- since their encoding stability is not critical (they are never on-chain).
370-
testNewEncodings = testGroup "stable byte encodings"
376+
testEncodingStability = testGroup "stable byte encodings"
371377
$ concat
372378
[ -- Maybe Bool
373379
encRaw (Nothing :: Maybe Bool) [0]

plutus-core/plutus-core.cabal

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -936,9 +936,19 @@ executable print-cost-model
936936
, bytestring
937937
, plutus-core ^>=1.60
938938

939-
-- | Reproducible encoding generator for Flat test coverage.
940-
-- Run: cabal run flat-encoding-generator
941-
-- Prints expected byte sequences for all Flat test values.
939+
-- Golden file generator for Flat encoding stability tests.
940+
--
941+
-- The encoding stability tests compare encoded values against expected
942+
-- byte sequences captured as test fixtures. This tool regenerates those
943+
-- fixtures from source, so they don't remain opaque blobs of bits.
944+
--
945+
-- Example: if a bug in a Flat instance is found and the encoding must
946+
-- change in a backward-incompatible way, the stability tests will fail.
947+
-- Run this tool to see the new wire format, update the test fixtures,
948+
-- and confirm the encoding change was intentional.
949+
--
950+
-- See: https://en.wikipedia.org/wiki/Characterization_test
951+
-- Usage: cabal run flat-encoding-generator
942952
executable flat-encoding-generator
943953
import: lang
944954
main-is: GenerateEncodings.hs
@@ -948,13 +958,8 @@ executable flat-encoding-generator
948958
, bytestring
949959
, containers
950960
, dlist
951-
, plutus-core ^>=1.59
961+
, plutus-core ^>=1.60
952962
, plutus-core:flat
953-
, text
954-
955-
ghc-options:
956-
-Wno-missing-signatures -Wno-missing-deriving-strategies
957-
-Wno-unused-packages
958963

959964
----------------------------------------------
960965
-- satint

plutus-core/tools/GenerateEncodings.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,10 @@ main = do
139139
putStrLn ""
140140
putStrLn "=== Section E: Value types ==="
141141
putStrLn ""
142-
let Just myK = V.k "abc"
143-
Just myQ = V.quantity 42
144-
p "K \"abc\"" (serRaw myK)
145-
p "Quantity 42" (serRaw myQ)
142+
case (V.k "abc", V.quantity 42) of
143+
(Just myK, Just myQ) -> do
144+
p "K \"abc\"" (serRaw myK)
145+
p "Quantity 42" (serRaw myQ)
146+
_ -> error "V.k or V.quantity returned Nothing for known-good inputs"
146147
p "ByteString \"abc\" (for K reference)" (serRaw ("abc" :: B.ByteString))
147148
p "Integer 42 (for Quantity reference)" (serRaw (42 :: Integer))

plutus-core/untyped-plutus-core/testlib/Flat/Spec.hs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE AllowAmbiguousTypes #-}
2+
{-# LANGUAGE BlockArguments #-}
23
{-# LANGUAGE FlexibleInstances #-}
34
{-# LANGUAGE OverloadedStrings #-}
45
{-# LANGUAGE ScopedTypeVariables #-}
@@ -12,7 +13,6 @@ import Data.ByteString qualified as BS
1213
import Data.ByteString.Lazy qualified as BSL
1314
import Data.Char (ord)
1415
import Data.Word
15-
import PlutusCore.Core (Version (..))
1616
import PlutusCore.Data (Data)
1717
import PlutusCore.DeBruijn
1818
import PlutusCore.Default (DefaultFun (..), DefaultUni (..))
@@ -24,10 +24,11 @@ import Test.Tasty
2424
import Test.Tasty.HUnit
2525
import Test.Tasty.QuickCheck
2626
import Universe (Some (..), ValueOf (..))
27-
import UntypedPlutusCore (Program (..))
28-
import UntypedPlutusCore.Core (UnrestrictedProgram (..))
2927
import UntypedPlutusCore.Core.Type
3028

29+
-- Also brings the Flat (Strict.Vector a) orphan instance into scope:
30+
import UntypedPlutusCore (UnrestrictedProgram (..))
31+
3132
test_deBruijnIso :: TestTree
3233
test_deBruijnIso = testProperty "deBruijnIso" $ \d ->
3334
d === fromFake (toFake d)
@@ -341,11 +342,12 @@ test_nonCanonicalByteStringDecoding =
341342
in testGroup
342343
"Non-canonical bytestring encodings decode succesfully"
343344
[ testProperty "Data via lazy bytestrings" $
344-
withMaxSuccess 5000 $
345-
forAll (arbitrary @Data) (\d -> Right d === unflat (flat $ (serialise d :: BSL.ByteString)))
345+
withMaxSuccess 5000 $ forAll (arbitrary @Data) \d ->
346+
Right d === unflat (flat (serialise d :: BSL.ByteString))
346347
, testProperty "Arbitrary lazy bytestrings" $
347348
withMaxSuccess 10000 $
348-
forAll (arbitrary @BSL.ByteString) (\bs -> Right (BSL.toStrict bs) === unflat (flat bs))
349+
forAll (arbitrary @BSL.ByteString) \bs ->
350+
Right (BSL.toStrict bs) === unflat (flat bs)
349351
, testCase "Explicit input 1" $ mkTest input1
350352
, testCase "Explicit input 2" $ mkTest input2
351353
, testCase "Explicit input 3" $ mkTest input3
@@ -399,7 +401,8 @@ test_binderNewtypeRoundtrip =
399401
in unflat (flat v) @?= Right v
400402
]
401403

402-
-- | Roundtrip and stable byte test for a minimal UPLC program: (program 1.1.0 (con integer 0))
404+
{-| Roundtrip and stable byte test for a minimal UPLC program:
405+
(program 1.1.0 (con integer 0)) -}
403406
test_uplcProgramFlat :: TestTree
404407
test_uplcProgramFlat =
405408
testGroup
@@ -411,7 +414,11 @@ test_uplcProgramFlat =
411414
flatBytes (UnrestrictedProgram prog) @?= [1, 1, 0, 72, 0, 0]
412415
]
413416
where
414-
prog = Program () (Version 1 1 0) (Constant () (Some (ValueOf DefaultUniInteger (0 :: Integer))))
417+
prog :: Program DeBruijn DefaultUni DefaultFun () =
418+
Program
419+
()
420+
(Version 1 1 0)
421+
(Constant () (Some (ValueOf DefaultUniInteger (0 :: Integer))))
415422

416423
test_flat :: TestTree
417424
test_flat =

0 commit comments

Comments
 (0)