-
Notifications
You must be signed in to change notification settings - Fork 463
/
PlcTestUtils.hs
330 lines (284 loc) · 11.2 KB
/
PlcTestUtils.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module PlcTestUtils (
checkFails,
ToTPlc(..),
ToUPlc(..),
pureTry,
catchAll,
rethrow,
runTPlc,
runUPlc,
goldenTPlc,
goldenTPlcCatch,
goldenUPlc,
goldenUPlcCatch,
goldenTEval,
goldenUEval,
goldenTEvalCatch,
goldenUEvalCatch,
goldenUEvalProfile,
NoMarkRenameT(..),
noMarkRename,
NoRenameT(..),
noRename,
BrokenRenameT(..),
runBrokenRenameT,
runUPlcProfile,
runUPlcProfileExec,
brokenRename,
prop_scopingFor,
test_scopingGood,
test_scopingBad
) where
import PlutusPrelude
import Common
import PlutusCore qualified as TPLC
import PlutusCore.Check.Scoping
import PlutusCore.DeBruijn
import PlutusCore.Default.Universe
import PlutusCore.Evaluation.Machine.Ck qualified as TPLC
import PlutusCore.Generators
import PlutusCore.Generators.AST
import PlutusCore.Pretty
import PlutusCore.Rename.Monad qualified as TPLC
import UntypedPlutusCore qualified as UPLC
import UntypedPlutusCore.Evaluation.Machine.Cek qualified as UPLC
import Control.Exception
import Control.Lens
import Control.Monad.Except
import Control.Monad.Reader
import Control.Monad.State
import Data.Text (Text)
import Hedgehog
import Prettyprinter qualified as PP
import System.IO.Unsafe
import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.Hedgehog
import Hedgehog.Internal.Config
import Hedgehog.Internal.Region
import Hedgehog.Internal.Report
import Hedgehog.Internal.Runner
-- | @check@ is supposed to just check if the property fails or not, but for some stupid reason it
-- also performs shrinking and prints the counterexample and other junk. This function is like
-- @check@, but doesn't do any of that.
checkQuiet :: MonadIO m => Property -> m Bool
checkQuiet prop = do
color <- detectColor
-- This is what causes @hedgehog@ to shut up.
region <- liftIO newEmptyRegion
-- For some reason @hedgehog@ thinks it's a good idea to shrink a counterexample in case of
-- an expected failure, so we suppress that.
let propNoShrink = withShrinks 0 prop
liftIO $ (== OK) . reportStatus <$> checkNamed region color Nothing propNoShrink
-- | Check that the given 'Property' fails.
checkFails :: Property -> IO ()
checkFails = checkQuiet >=> \res -> res @?= False
-- | Class for ad-hoc overloading of things which can be turned into a PLC program. Any errors
-- from the process should be caught.
class ToTPlc a uni fun | a -> uni fun where
toTPlc :: a -> ExceptT SomeException IO (TPLC.Program TPLC.TyName TPLC.Name uni fun ())
instance ToTPlc a uni fun => ToTPlc (ExceptT SomeException IO a) uni fun where
toTPlc a = a >>= toTPlc
instance ToTPlc (TPLC.Program TPLC.TyName TPLC.Name uni fun ()) uni fun where
toTPlc = pure
class ToUPlc a uni fun | a -> uni fun where
toUPlc :: a -> ExceptT SomeException IO (UPLC.Program TPLC.Name uni fun ())
instance ToUPlc a uni fun => ToUPlc (ExceptT SomeException IO a) uni fun where
toUPlc a = a >>= toUPlc
instance ToUPlc (UPLC.Program TPLC.Name uni fun ()) uni fun where
toUPlc = pure
instance ToUPlc (UPLC.Program UPLC.NamedDeBruijn uni fun ()) uni fun where
toUPlc p = withExceptT @_ @FreeVariableError toException $ TPLC.runQuoteT $ traverseOf UPLC.progTerm UPLC.unDeBruijnTerm p
pureTry :: Exception e => a -> Either e a
pureTry = unsafePerformIO . try . evaluate
catchAll :: a -> ExceptT SomeException IO a
catchAll value = ExceptT $ try @SomeException (evaluate value)
rethrow :: ExceptT SomeException IO a -> IO a
rethrow = fmap (either throw id) . runExceptT
runTPlc
:: ToTPlc a DefaultUni TPLC.DefaultFun
=> [a]
-> ExceptT SomeException IO (TPLC.EvaluationResult (TPLC.Term TPLC.TyName TPLC.Name DefaultUni TPLC.DefaultFun ()))
runTPlc values = do
ps <- traverse toTPlc values
let (TPLC.Program _ _ t) = foldl1 TPLC.applyProgram ps
liftEither $ first toException $ TPLC.extractEvaluationResult $ TPLC.evaluateCkNoEmit TPLC.defaultBuiltinsRuntime t
runUPlc
:: ToUPlc a DefaultUni TPLC.DefaultFun
=> [a]
-> ExceptT SomeException IO (UPLC.EvaluationResult (UPLC.Term TPLC.Name DefaultUni TPLC.DefaultFun ()))
runUPlc values = do
ps <- traverse toUPlc values
let (UPLC.Program _ _ t) = foldl1 UPLC.applyProgram ps
liftEither $ first toException $ TPLC.extractEvaluationResult $ UPLC.evaluateCekNoEmit TPLC.defaultCekParameters t
-- For golden tests of profiling.
runUPlcProfile :: ToUPlc a DefaultUni UPLC.DefaultFun =>
[a]
-> ExceptT
SomeException
IO
(UPLC.Term UPLC.Name DefaultUni UPLC.DefaultFun (), [Text])
runUPlcProfile values = do
ps <- traverse toUPlc values
let (UPLC.Program _ _ t) = foldl1 UPLC.applyProgram ps
(result, logOut) = UPLC.evaluateCek UPLC.logEmitter TPLC.defaultCekParameters t
res <- either (throwError . SomeException) pure result
pure (res, logOut)
-- For the profiling executable.
runUPlcProfileExec :: ToUPlc a DefaultUni UPLC.DefaultFun =>
[a]
-> ExceptT
SomeException
IO
(UPLC.Term UPLC.Name DefaultUni UPLC.DefaultFun (), [Text])
runUPlcProfileExec values = do
ps <- traverse toUPlc values
let (UPLC.Program _ _ t) = foldl1 UPLC.applyProgram ps
(result, logOut) = UPLC.evaluateCek UPLC.logWithTimeEmitter TPLC.defaultCekParameters t
res <- either (throwError . SomeException) pure result
pure (res, logOut)
ppCatch :: PrettyPlc a => ExceptT SomeException IO a -> IO (Doc ann)
ppCatch value = either (PP.pretty . show) prettyPlcClassicDebug <$> runExceptT value
ppThrow :: PrettyPlc a => ExceptT SomeException IO a -> IO (Doc ann)
ppThrow value = rethrow $ prettyPlcClassicDebug <$> value
goldenTPlc
:: ToTPlc a DefaultUni TPLC.DefaultFun
=> String -> a -> TestNested
goldenTPlc name value = nestedGoldenVsDocM name $ ppThrow $ do
p <- toTPlc value
withExceptT @_ @FreeVariableError toException $ traverseOf TPLC.progTerm deBruijnTerm p
goldenTPlcCatch
:: ToTPlc a DefaultUni TPLC.DefaultFun
=> String -> a -> TestNested
goldenTPlcCatch name value = nestedGoldenVsDocM name $ ppCatch $ do
p <- toTPlc value
withExceptT @_ @FreeVariableError toException $ traverseOf TPLC.progTerm deBruijnTerm p
goldenUPlc
:: ToUPlc a DefaultUni TPLC.DefaultFun
=> String -> a -> TestNested
goldenUPlc name value = nestedGoldenVsDocM name $ ppThrow $ do
p <- toUPlc value
withExceptT @_ @FreeVariableError toException $ traverseOf UPLC.progTerm UPLC.deBruijnTerm p
goldenUPlcCatch
:: ToUPlc a DefaultUni TPLC.DefaultFun
=> String -> a -> TestNested
goldenUPlcCatch name value = nestedGoldenVsDocM name $ ppCatch $ do
p <- toUPlc value
withExceptT @_ @FreeVariableError toException $ traverseOf UPLC.progTerm UPLC.deBruijnTerm p
goldenTEval
:: ToTPlc a DefaultUni TPLC.DefaultFun
=> String -> [a] -> TestNested
goldenTEval name values = nestedGoldenVsDocM name $ prettyPlcClassicDebug <$> (rethrow $ runTPlc values)
goldenUEval
:: ToUPlc a DefaultUni TPLC.DefaultFun
=> String -> [a] -> TestNested
goldenUEval name values = nestedGoldenVsDocM name $ prettyPlcClassicDebug <$> (rethrow $ runUPlc values)
goldenTEvalCatch
:: ToTPlc a DefaultUni TPLC.DefaultFun
=> String -> [a] -> TestNested
goldenTEvalCatch name values = nestedGoldenVsDocM name $ ppCatch $ runTPlc values
goldenUEvalCatch
:: ToUPlc a DefaultUni TPLC.DefaultFun
=> String -> [a] -> TestNested
goldenUEvalCatch name values = nestedGoldenVsDocM name $ ppCatch $ runUPlc values
-- | Similar to @goldenUEval@ but with profiling turned on.
goldenUEvalProfile
:: ToUPlc a DefaultUni TPLC.DefaultFun
=> String -> [a] -> TestNested
goldenUEvalProfile name values = nestedGoldenVsDocM name $ pretty . view _2 <$> (rethrow $ runUPlcProfile values)
-- See Note [Marking].
-- | A version of 'RenameT' that fails to take free variables into account.
newtype NoMarkRenameT ren m a = NoMarkRenameT
{ unNoMarkRenameT :: TPLC.RenameT ren m a
} deriving newtype
( Functor, Applicative, Alternative, Monad
, MonadReader ren
, TPLC.MonadQuote
)
noMarkRename
:: Monoid ren
=> (t -> NoMarkRenameT ren m t)
-> t
-> m t
noMarkRename renM = TPLC.runRenameT . unNoMarkRenameT . renM
-- | A version of 'RenameT' that does not perform any renaming at all.
newtype NoRenameT ren m a = NoRenameT
{ unNoRenameT :: m a
} deriving newtype
( Functor, Applicative, Alternative, Monad
, TPLC.MonadQuote
)
instance (Monad m, Monoid ren) => MonadReader ren (NoRenameT ren m) where
ask = pure mempty
local _ = id
noRename
:: TPLC.MonadQuote m
=> (t -> m ())
-> (t -> NoRenameT ren m t)
-> t
-> m t
noRename mark renM = through mark >=> unNoRenameT . renM
-- | A broken version of 'RenameT' whose 'local' updates the scope globally (as opposed to locally).
newtype BrokenRenameT ren m a = BrokenRenameT
{ unBrokenRenameT :: StateT ren m a
} deriving newtype
( Functor, Applicative, Alternative, Monad
, MonadState ren
, TPLC.MonadQuote
)
instance Monad m => MonadReader ren (BrokenRenameT ren m) where
ask = get
local f a = modify f *> a
runBrokenRenameT :: (Monad m, Monoid ren) => BrokenRenameT ren m a -> m a
runBrokenRenameT = flip evalStateT mempty . unBrokenRenameT
brokenRename
:: (TPLC.MonadQuote m, Monoid ren)
=> (t -> m ())
-> (t -> BrokenRenameT ren m t)
-> t
-> m t
brokenRename mark renM = through mark >=> runBrokenRenameT . renM
-- | Test scoping for a renamer.
prop_scopingFor
:: (Pretty (t ann), Scoping t)
=> AstGen (t ann) -> (t NameAnn -> TPLC.Quote (t NameAnn)) -> Property
prop_scopingFor gen ren = property $ do
prog <- forAllPretty $ runAstGen gen
let catchEverything = unsafePerformIO . try @SomeException . evaluate
case catchEverything $ checkRespectsScoping (TPLC.runQuote . ren) prog of
Left exc -> fail $ show exc
Right (Left err) -> fail $ show err
Right (Right ()) -> success
-- | Test that a good renamer does not destroy scoping.
test_scopingGood
:: (Pretty (t ann), Scoping t)
=> AstGen (t ann)
-> (t NameAnn -> TPLC.Quote (t NameAnn))
-> TestTree
test_scopingGood gen ren =
testProperty "renamer does not destroy scoping" $
prop_scopingFor gen ren
-- | Test that a renaming machinery destroys scoping when a bad renamer is chosen.
test_scopingBad
:: (Pretty (t ann), Scoping t, Monoid ren)
=> AstGen (t ann)
-> (t NameAnn -> TPLC.Quote ())
-> (forall m. (TPLC.MonadQuote m, MonadReader ren m) => t NameAnn -> m (t NameAnn))
-> TestTree
test_scopingBad gen mark renM =
testGroup "scoping bad"
[ testCase "wrong renaming destroys scoping" $
checkFails . prop_scopingFor gen $ brokenRename mark renM
, testCase "no renaming does not result in global uniqueness" $
checkFails . prop_scopingFor gen $ noRename mark renM
, testCase "renaming with no marking destroys scoping" $
checkFails . prop_scopingFor gen $ noMarkRename renM
]