Skip to content

Commit 5de23f7

Browse files
authored
[Refac] Make UPLC and TPLC tests more uniform (#7159)
* [Refac] Make UPLC and TPLC tests more uniform * Document 'regenConstantUntil' * Fix warnings
1 parent dcf2ffc commit 5de23f7

File tree

21 files changed

+375
-340
lines changed

21 files changed

+375
-340
lines changed

plutus-core/plutus-core.cabal

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,6 @@ library untyped-plutus-core-testlib
459459
Evaluation.Machines
460460
Evaluation.Regressions
461461
Flat.Spec
462-
Generators.Lib
463462
Generators.Spec
464463
Scoping.Spec
465464
Transform.CaseOfCase.Spec
@@ -551,7 +550,6 @@ library plutus-ir
551550
PlutusIR.Transform.RewriteRules.CommuteFnWithConst
552551
PlutusIR.Transform.RewriteRules.RemoveTrace
553552
PlutusIR.Transform.StrictifyBindings
554-
PlutusIR.Transform.Substitute
555553
PlutusIR.Transform.ThunkRecursions
556554
PlutusIR.Transform.Unwrap
557555
PlutusIR.TypeCheck
@@ -800,7 +798,7 @@ library plutus-core-testlib
800798
PlutusIR.Pass.Test
801799
PlutusIR.Test
802800
Test.Tasty.Extras
803-
UntypedPlutusCore.Generators.Hedgehog
801+
UntypedPlutusCore.Generators.Hedgehog.AST
804802
UntypedPlutusCore.Test.DeBruijn.Bad
805803
UntypedPlutusCore.Test.DeBruijn.Good
806804

plutus-core/plutus-core/src/PlutusCore/Subst.hs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ module PlutusCore.Subst
2424
, vTerm
2525
, tvTerm
2626
, tvTy
27-
, purely
27+
, substConstantA
28+
, substConstant
29+
, termSubstConstantsM
30+
, termSubstConstants
2831
) where
2932

3033
import PlutusPrelude
@@ -37,8 +40,7 @@ import PlutusCore.Name.Unique (HasUnique)
3740
import PlutusCore.Name.UniqueSet (UniqueSet)
3841
import PlutusCore.Name.UniqueSet qualified as USet
3942

40-
purely :: ((a -> Identity b) -> c -> Identity d) -> (a -> b) -> c -> d
41-
purely = coerce
43+
import Universe
4244

4345
-- | Applicatively replace a type variable using the given function.
4446
substTyVarA
@@ -277,3 +279,34 @@ tvTerm = termSubtypesDeep . typeTyVars
277279
-- | Get all the type variables in a type.
278280
tvTy :: Fold (Type tyname uni ann) tyname
279281
tvTy = typeSubtypesDeep . typeTyVars
282+
283+
-- | Applicatively replace a constant using the given function.
284+
substConstantA
285+
:: Applicative f
286+
=> (ann -> Some (ValueOf uni) -> f (Maybe (Term tyname name uni fun ann)))
287+
-> Term tyname name uni fun ann
288+
-> f (Term tyname name uni fun ann)
289+
substConstantA valF t@(Constant ann val) = fromMaybe t <$> valF ann val
290+
substConstantA _ t = pure t
291+
292+
-- | Replace a constant using the given function.
293+
substConstant
294+
:: (ann -> Some (ValueOf uni) -> Maybe (Term tyname name uni fun ann))
295+
-> Term tyname name uni fun ann
296+
-> Term tyname name uni fun ann
297+
substConstant = purely (substConstantA . curry) . uncurry
298+
299+
-- | Monadically substitute constants using the given function.
300+
termSubstConstantsM
301+
:: Monad m
302+
=> (ann -> Some (ValueOf uni) -> m (Maybe (Term tyname name uni fun ann)))
303+
-> Term tyname name uni fun ann
304+
-> m (Term tyname name uni fun ann)
305+
termSubstConstantsM = transformMOf termSubterms . substConstantA
306+
307+
-- | Substitute constants using the given function.
308+
termSubstConstants
309+
:: (ann -> Some (ValueOf uni) -> Maybe (Term tyname name uni fun ann))
310+
-> Term tyname name uni fun ann
311+
-> Term tyname name uni fun ann
312+
termSubstConstants = purely (termSubstConstantsM . curry) . uncurry

plutus-core/plutus-core/test/Names/Spec.hs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ import PlutusCore.Test (BindingRemoval (BindingRemovalNotOk), Prerename (Prerena
2626
import Control.Monad.Except (modifyError)
2727
import Data.String (IsString (fromString))
2828
import Data.Text qualified as Text
29-
import Hedgehog (Gen, Property, assert, forAll, property, tripping)
30-
import Hedgehog.Gen qualified as Gen
29+
import Hedgehog (Gen, Property, forAll, property, tripping, (/==), (===))
3130
import Test.Tasty (TestTree, testGroup)
3231
import Test.Tasty.Hedgehog (testPropertyNamed)
3332
import Test.Tasty.HUnit (assertBool, testCase, (@?=))
@@ -53,24 +52,23 @@ test_DeBruijnInteresting =
5352

5453
test_mangle :: TestTree
5554
test_mangle =
56-
testPropertyNamed "equality does not survive mangling" "equality_mangling" $ property do
57-
(term, termMangled) <- forAll . Gen.just $ runAstGen do
55+
testPropertyNamed "equality does not survive mangling" "equality_mangling" . property $ do
56+
(term, termMangled) <- forAll . runAstGen $ do
5857
term <- AST.genTerm
59-
mayTermMang <- mangleNames term
60-
pure $ do
61-
termMang <- mayTermMang
62-
Just (term, termMang)
63-
assert $ term /= termMangled && termMangled /= term
58+
(,) term <$> mangleNames term
59+
term /== termMangled
60+
termMangled /== term
6461

6562
-- | Test equality of a program and its renamed version, given a renamer.
6663
prop_equalityFor
67-
:: (program ~ Program TyName Name DefaultUni DefaultFun ())
64+
:: program ~ Program TyName Name DefaultUni DefaultFun ()
6865
=> (program -> Quote program)
6966
-> Property
7067
prop_equalityFor ren = property do
7168
prog <- forAllPretty $ runAstGen genProgram
7269
let progRen = runQuote $ ren prog
73-
assert $ progRen == prog && prog == progRen
70+
progRen === prog
71+
prog === progRen
7472

7573
test_equalityRename :: TestTree
7674
test_equalityRename =

plutus-core/plutus-core/test/Parser/Spec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import Test.Tasty.Hedgehog
2020
propTermSrcSpan :: Property
2121
propTermSrcSpan = property $ do
2222
term <- _progTerm <$>
23-
forAllWith display (runAstGen $ discardIfAnyConstant (not . isSerialisable) genProgram)
23+
forAllWith display (runAstGen $ regenConstantsUntil isSerialisable =<< genProgram)
2424
let code = display (term :: Term TyName Name DefaultUni DefaultFun ())
2525
let (endingLine, endingCol) = length &&& T.length . last $ T.lines code
2626
trailingSpaces <- forAll $ Gen.text (Range.linear 0 10) (Gen.element [' ', '\n'])

plutus-core/plutus-core/test/Spec.hs

Lines changed: 4 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -65,83 +65,10 @@ main = do
6565
includingOptions [Option $ Proxy @NEAT.GenMode, Option $ Proxy @NEAT.GenDepth]
6666
: defaultIngredients
6767

68-
compareName :: Name -> Name -> Bool
69-
compareName = (==) `on` _nameText
70-
71-
compareTyName :: TyName -> TyName -> Bool
72-
compareTyName (TyName n) (TyName n') = compareName n n'
73-
74-
compareTerm ::
75-
(GEq uni, Closed uni, uni `Everywhere` Eq, Eq fun, Eq a) =>
76-
Term TyName Name uni fun a ->
77-
Term TyName Name uni fun a ->
78-
Bool
79-
compareTerm (Var _ n) (Var _ n') = compareName n n'
80-
compareTerm (TyAbs _ n k t) (TyAbs _ n' k' t') =
81-
compareTyName n n' && k == k' && compareTerm t t'
82-
compareTerm (LamAbs _ n ty t) (LamAbs _ n' ty' t') =
83-
compareName n n' && compareType ty ty' && compareTerm t t'
84-
compareTerm (Apply _ t t'') (Apply _ t' t''') =
85-
compareTerm t t' && compareTerm t'' t'''
86-
compareTerm (Constant _ x) (Constant _ y) = x == y
87-
compareTerm (Builtin _ bi) (Builtin _ bi') = bi == bi'
88-
compareTerm (TyInst _ t ty) (TyInst _ t' ty') =
89-
compareTerm t t' && compareType ty ty'
90-
compareTerm (Unwrap _ t) (Unwrap _ t') = compareTerm t t'
91-
compareTerm (IWrap _ pat1 arg1 t1) (IWrap _ pat2 arg2 t2) =
92-
compareType pat1 pat2 && compareType arg1 arg2 && compareTerm t1 t2
93-
compareTerm (Constr _ ty i es) (Constr _ ty' i' es') =
94-
compareType ty ty' && i == i' && maybe False (all (uncurry compareTerm)) (zipExact es es')
95-
compareTerm (Case _ ty arg cs) (Case _ ty' arg' cs') =
96-
compareType ty ty'
97-
&& compareTerm arg arg'
98-
&& maybe False (all (uncurry compareTerm)) (zipExact cs cs')
99-
compareTerm (Error _ ty) (Error _ ty') = compareType ty ty'
100-
compareTerm _ _ = False
101-
102-
compareType ::
103-
(GEq uni, Closed uni, uni `Everywhere` Eq, Eq a) =>
104-
Type TyName uni a ->
105-
Type TyName uni a ->
106-
Bool
107-
compareType (TyVar _ n) (TyVar _ n') = compareTyName n n'
108-
compareType (TyFun _ t s) (TyFun _ t' s') =
109-
compareType t t' && compareType s s'
110-
compareType (TyIFix _ pat1 arg1) (TyIFix _ pat2 arg2) =
111-
compareType pat1 pat2 && compareType arg1 arg2
112-
compareType (TyForall _ n k t) (TyForall _ n' k' t') =
113-
compareTyName n n' && k == k' && compareType t t'
114-
compareType (TyBuiltin _ x) (TyBuiltin _ y) = x == y
115-
compareType (TyLam _ n k t) (TyLam _ n' k' t') =
116-
compareTyName n n' && k == k' && compareType t t'
117-
compareType (TyApp _ t t') (TyApp _ t'' t''') =
118-
compareType t t'' && compareType t' t'''
119-
compareType (TySOP _ tyls) (TySOP _ tyls') =
120-
maybe
121-
False
122-
(all (\(tyl1, tyl2) -> maybe False (all (uncurry compareType)) (zipExact tyl1 tyl2)))
123-
(zipExact tyls tyls')
124-
compareType _ _ = False
125-
126-
compareProgram ::
127-
(GEq uni, Closed uni, uni `Everywhere` Eq, Eq fun, Eq a) =>
128-
Program TyName Name uni fun a ->
129-
Program TyName Name uni fun a ->
130-
Bool
131-
compareProgram (Program _ v t) (Program _ v' t') = v == v' && compareTerm t t'
132-
133-
-- | A 'Program' which we compare using textual equality of names rather than alpha-equivalence.
134-
newtype TextualProgram a = TextualProgram
135-
{unTextualProgram :: Program TyName Name DefaultUni DefaultFun a}
136-
deriving stock (Show)
137-
138-
instance (Eq a) => Eq (TextualProgram a) where
139-
(TextualProgram p1) == (TextualProgram p2) = compareProgram p1 p2
140-
14168
propFlat :: Property
14269
propFlat = property $ do
14370
prog <- forAllPretty . runAstGen $
144-
discardIfAnyConstant (not . isSerialisable) $ genProgram @DefaultFun
71+
regenConstantsUntil isSerialisable =<< genProgram @DefaultFun
14572
Hedgehog.tripping prog Flat.flat Flat.unflat
14673

14774
{- The following tests check that (A) the parser can
@@ -223,18 +150,11 @@ text, hopefully returning the same thing.
223150
-}
224151
propParser :: Property
225152
propParser = property $ do
226-
prog <- TextualProgram <$>
227-
forAllPretty (runAstGen $ discardIfAnyConstant (not . isSerialisable) genProgram)
228-
Hedgehog.tripping
229-
prog
230-
(displayPlc . unTextualProgram)
231-
(\p -> fmap (TextualProgram . void) (parseProg p))
153+
prog <- forAllPretty . runAstGen $ regenConstantsUntil isSerialisable =<< genProgram
154+
Hedgehog.tripping prog displayPlc (fmap void . parseProg)
232155
where
233156
parseProg ::
234-
T.Text ->
235-
Either
236-
ParserErrorBundle
237-
(Program TyName Name DefaultUni DefaultFun SrcSpan)
157+
T.Text -> Either ParserErrorBundle (Program TyName Name DefaultUni DefaultFun SrcSpan)
238158
parseProg = runQuoteT . parseProgram
239159

240160
type TestFunction = T.Text -> Either DefaultError T.Text

plutus-core/plutus-ir/src/PlutusIR/Compiler/Datatype.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import PlutusIR.Compiler.Provenance
2323
import PlutusIR.Compiler.Types
2424
import PlutusIR.Error
2525
import PlutusIR.MkPir qualified as PIR
26-
import PlutusIR.Transform.Substitute
26+
import PlutusIR.Subst
2727

2828
import PlutusCore.Core qualified as PLC
2929
import PlutusCore.MkPlc qualified as PLC

0 commit comments

Comments
 (0)