-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from Plutonomicon/tracing
Add tracing utilities; with cabal flags switching
- Loading branch information
Showing
4 changed files
with
150 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{-# LANGUAGE CPP #-} | ||
|
||
module Plutarch.Trace (ptrace, ptraceIfTrue, ptraceIfFalse, ptraceError) where | ||
|
||
-- CPP support isn't great in fourmolu. | ||
{- ORMOLU_DISABLE -} | ||
|
||
#ifdef Development | ||
import Plutarch (punsafeBuiltin) | ||
#endif | ||
#ifdef Development | ||
import Plutarch.Bool (PBool, pif) | ||
#else | ||
import Plutarch.Bool (PBool) | ||
#endif | ||
import Plutarch.Prelude | ||
import Plutarch.String (PString) | ||
|
||
#ifdef Development | ||
import qualified PlutusCore as PLC | ||
#endif | ||
|
||
#ifdef Development | ||
ptrace' :: Term s (PString :--> a :--> a) | ||
ptrace' = phoistAcyclic $ pforce $ punsafeBuiltin PLC.Trace | ||
#endif | ||
|
||
-- | Trace the given message before evaluating the argument. | ||
ptrace :: Term s PString -> Term s a -> Term s a | ||
#ifdef Development | ||
ptrace s a = pforce $ ptrace' # s # pdelay a | ||
#else | ||
ptrace _ a = a | ||
#endif | ||
|
||
-- | Trace the given message and terminate evaluation with a 'perror'. | ||
ptraceError :: Term s PString -> Term s a | ||
#ifdef Development | ||
ptraceError s = pforce $ ptrace' # s # pdelay perror | ||
#else | ||
ptraceError _ = perror | ||
#endif | ||
|
||
-- | Trace the given message if the argument evaluates to true. | ||
ptraceIfTrue :: Term s PString -> Term s PBool -> Term s PBool | ||
#ifdef Development | ||
ptraceIfTrue s a' = plet a' $ \a -> pif a (ptrace' # s # a) a | ||
#else | ||
ptraceIfTrue _ a = a | ||
#endif | ||
|
||
-- | Trace the given message if the argument evaluates to False. | ||
ptraceIfFalse :: Term s PString -> Term s PBool -> Term s PBool | ||
#ifdef Development | ||
ptraceIfFalse s a' = plet a' $ \a -> pif a a (ptrace' # s # a) | ||
#else | ||
ptraceIfFalse _ a = a | ||
#endif |
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,76 @@ | ||
{-# LANGUAGE CPP #-} | ||
|
||
module Plutarch.Spec.Tracing (traceTests) where | ||
|
||
import Test.Tasty.HUnit | ||
|
||
import Data.Text (Text) | ||
import Plutarch | ||
import Plutarch.Bool (PBool (PFalse, PTrue)) | ||
import Plutarch.Evaluate (evaluateScript) | ||
import Plutarch.Trace (ptrace, ptraceIfFalse, ptraceIfTrue) | ||
import Plutarch.Unit (PUnit (PUnit)) | ||
|
||
traces :: ClosedTerm a -> [Text] -> Assertion | ||
traces x sl = | ||
case evaluateScript $ compile x of | ||
Left e -> assertFailure $ "Script evaluation failed: " <> show e | ||
Right (_, traceLog, _) -> traceLog @?= sl | ||
|
||
traceTests :: IO () | ||
traceTests = do | ||
|
||
-- CPP support isn't great in fourmolu. | ||
{- ORMOLU_DISABLE -} | ||
ptrace "foo" (pcon PUnit) `traces` | ||
#ifdef Development | ||
["foo"] | ||
#else | ||
[] | ||
#endif | ||
|
||
ptrace "foo" (ptrace "bar" $ pcon PUnit) `traces` | ||
#ifdef Development | ||
["foo", "bar"] | ||
#else | ||
[] | ||
#endif | ||
|
||
ptraceIfTrue "foo" (pcon PTrue) `traces` | ||
#ifdef Development | ||
["foo"] | ||
#else | ||
[] | ||
#endif | ||
|
||
ptraceIfTrue "foo" (pcon PFalse) `traces` [] | ||
|
||
ptraceIfTrue "foo" (ptraceIfTrue "bar" $ pcon PTrue) `traces` | ||
#ifdef Development | ||
["bar", "foo"] | ||
#else | ||
[] | ||
#endif | ||
|
||
ptraceIfTrue "foo" (ptraceIfTrue "bar" $ pcon PFalse) `traces` [] | ||
ptraceIfFalse "foo" (ptraceIfTrue "bar" $ pcon PFalse) `traces` | ||
#ifdef Development | ||
["foo"] | ||
#else | ||
[] | ||
#endif | ||
|
||
ptrace "foo" (ptraceIfTrue "bar" (pcon PTrue)) `traces` | ||
#ifdef Development | ||
["foo", "bar"] | ||
#else | ||
[] | ||
#endif | ||
|
||
ptrace "foo" (ptraceIfTrue "bar" (pcon PFalse)) `traces` | ||
#ifdef Development | ||
["foo"] | ||
#else | ||
[] | ||
#endif | ||
{- ORMOLU_ENABLE -} |
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