| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
PlutusConformance.Common
Description
Plutus conformance test suite library.
Synopsis
- shownParseError ∷ Text
- shownEvaluationFailure ∷ Text
- parseTxt ∷ Text → Either ParserErrorBundle (Program Name DefaultUni DefaultFun SrcSpan)
- type UplcProg = Program Name DefaultUni DefaultFun ()
- data Format
- formatExtension ∷ Format → String
- data EvaluationResult res
- type UplcEvaluatorFun res = UplcProg → EvaluationResult res
- data UplcEvaluator
- dirsWithNoFlatFiles ∷ [FilePath]
- discoverTests ∷ Format → UplcEvaluator → CostModelParams → (FilePath → Bool) → (FilePath → Bool) → FilePath → IO TestTree
- looksLikeUplcProgram ∷ Text → Bool
- expectedToProg ∷ Text → Either Text UplcProg
- decodeFlatExpected ∷ ByteString → Either Text UplcProg
- getExpectedProg ∷ Format → FilePath → IO (Either Text UplcProg)
- getInputProg ∷ Format → FilePath → IO (Either Text UplcProg)
- getTestedValue ∷ Format → UplcEvaluatorFun res → FilePath → IO (Either Text res)
- compareAlphaEq ∷ Either Text UplcProg → Either Text UplcProg → Maybe String
- updateGoldenFile ∷ Format → FilePath → Either Text UplcProg → IO ()
- representativeGoldenTest ∷ TestTree
- runUplcEvalTests ∷ UplcEvaluator → (FilePath → Bool) → (FilePath → Bool) → IO ()
- unDeBruijnProgram ∷ Program NamedDeBruijn DefaultUni DefaultFun () → Either FreeVariableError UplcProg
- decodeFlatProg ∷ ByteString → Either String UplcProg
- encodeFlatProg ∷ UplcProg → ByteString
Documentation
shownParseError ∷ Text Source #
The text shown when a file fails to parse or decode. We don't want to show the detailed errors so that users of the test suite can produce the expected output more easily. This is used in .uplc.expected and .budget.expected files.
shownEvaluationFailure ∷ Text Source #
The text shown when evaluation fails. This is used in .uplc.expected and .budget.expected files.
parseTxt ∷ Text → Either ParserErrorBundle (Program Name DefaultUni DefaultFun SrcSpan) Source #
The default parser to parse UPLC program inputs. FIXME: unlike the flat decoder, this does not detect free variables: they will only be detected if/when we deBruijnify the program.
type UplcProg = Program Name DefaultUni DefaultFun () Source #
The input/output UPLC program type.
The format of the test-case input files that the tests should be run
against: either the textual `.uplc` representation or the flat-encoded
`.flat` representation of the same program. See formatExtension.
Instances
| Show Format Source # | |
| Eq Format Source # | |
| IsOption Format Source # | This instance allows |
Defined in PlutusConformance.Common | |
formatExtension ∷ Format → String Source #
The filename extension (without the leading dot) used for a given Format.
data EvaluationResult res Source #
Constructors
| BadMachineParameters | |
| DecodeError | |
| EvalFailure | |
| EvalSuccess res |
Instances
| Functor EvaluationResult Source # | |
Defined in PlutusConformance.Common Methods fmap ∷ (a → b) → EvaluationResult a → EvaluationResult b Source # (<$) ∷ a → EvaluationResult b → EvaluationResult a Source # | |
type UplcEvaluatorFun res = UplcProg → EvaluationResult res Source #
data UplcEvaluator Source #
The evaluator to be tested.
Constructors
| UplcEvaluatorWithoutCosting (UplcEvaluatorFun UplcProg) | An evaluator that just produces an output program, or fails. |
| UplcEvaluatorWithCosting (CostModelParams → UplcEvaluatorFun (UplcProg, ExBudget)) | An evaluator that produces an output program along with the cost of evaluating it, or fails. Note that nothing cares about the cost of failing programs, so we don't test for conformance there. |
dirsWithNoFlatFiles ∷ [FilePath] Source #
Directories under which no `.flat` input files are expected and hence
shouldn't lead to errors. These test the textual parser's handling of
constants, and there are generally no flat equivalents for these tests. This
applies to the directory itself and everything below it.
Arguments
| ∷ Format | The format of the test-case input files to run the tests against (.uplc or .flat). |
| → UplcEvaluator | The evaluator to be tested. |
| → CostModelParams | |
| → (FilePath → Bool) | A function that takes a test directory and returns a Bool indicating whether the evaluation test for the file in that directory is expected to fail. |
| → (FilePath → Bool) | A function that takes a test directory and returns a Bool indicating whether the budget test for the file in that directory is expected to fail. |
| → FilePath | The directory to search for tests. |
| → IO TestTree |
Walk a file tree, making test groups for directories with subdirectories,
and test cases for directories without. We expect every test directory to
contain a single input file, in the given Format, whose name matches that
of the directory. For example, if the Format is UPLC then the directory
`modInteger-15` should contain `modInteger-15.uplc`, and that file should
contain a textual UPLC program; if the Format is Flat then it should
instead contain `modInteger-15.flat`, a flat-encoded UPLC program. The
evaluation golden file is named to match: `modInteger-15.uplc.expected`
for Textual, or `modInteger-15.flat.expected` for Flat. The budget
golden file, however, is always `modInteger-15.budget.expected`
regardless of format, since there's no per-format budget convention (the
budget only depends on the AST, not on how it was obtained). These golden
files will be created by the testing machinery if they aren't already
present.
Every test-case directory is expected to have an input file for the requested
Format; a missing input file is treated as an error, except under the
directories listed in dirsWithNoFlatFiles, where (in Flat mode only) no
`.flat` files is expected and the directory is skipped instead (for example
`.flat` files don't make sense for the tests under
`test-casesuplcevaluationbuiltinparser`, which test the handling of
constants by the textual parser).
looksLikeUplcProgram ∷ Text → Bool Source #
Check whether some text looks like it's meant to be a UPLC program, ie,
whether it begins with `(program` once whitespace and comments (which may
appear before the `(` and/or between the `(` and program, as `--` line
comments or `{- -}` block comments -- possibly nested, matching the real
lexer's whitespace parser in PlutusCore.Parser.ParserCommon -- are
ignored).
expectedToProg ∷ Text → Either Text UplcProg Source #
Turn the expected file content in text to a UplcProg unless the expected
result is a parse or evaluation error. We use the same shape-based check as
getInputProg (looksLikeUplcProgram) to decide whether the content
represents a program at all, rather than just trying to parse it and seeing
whether that fails: this way, things like the literal `"parse error"` and
`"evaluation failure"` markers are recognised as failures without needing to
attempt (and fail) a real parse.
decodeFlatExpected ∷ ByteString → Either Text UplcProg Source #
Decode the content of a `.flat.expected` golden file. A `.flat.expected`
file records either a successful evaluation result (as flat-encoded
bytes) or a failure (as the UTF8-encoded text of shownParseError or
shownEvaluationFailure) -- exactly mirroring the `.uplc.expected`
convention (see expectedToProg), rather than the old convention of an
empty file standing in for "some failure, reason unspecified". We check for
the text markers first (a valid flat encoding could coincidentally also be
valid UTF8, but it will essentially never happen to be the exact text of
one of the two markers).
If the content is neither a recognised failure marker nor a valid flat
encoding (for example because the golden file is empty, using the old
convention, or has been corrupted), we don't fail outright: instead we
return Left with the flat decoder's error text as the "expected" reason.
This will essentially never match a real tested value (which is always
either a real program or exactly shownParseError/shownEvaluationFailure),
so it surfaces as an ordinary golden-mismatch test failure -- visible on a
normal run, and fixable with `--accept` like any other outdated golden file
(which is how the old empty-file goldens get migrated to the new
convention), rather than a special-cased crash.
getExpectedProg ∷ Format → FilePath → IO (Either Text UplcProg) Source #
Obtain the expected UplcProg from a golden `.expected` file in the given
Format: parsed as text for Textual (via expectedToProg), or decoded
via decodeFlatExpected for Flat.
getInputProg ∷ Format → FilePath → IO (Either Text UplcProg) Source #
Obtain the input UplcProg from a test-case input file in the given
Format, either by parsing it (for textual) or by flat-decoding it (for
Flat). Rather than relying on the parser or decoder itself to fail, we check
directly whether the file looks like it's even meant to contain a program: a
`.uplc` file is expected to begin with `(program` (once any leading
whitespace and comments are ignored: see looksLikeUplcProgram), and a
`.flat` file is expected to be non-empty. If a file doesn't meet this
expectation, we treat it as shownParseError without attempting to
parse or decode it. Otherwise, we go ahead and parse/decode it to get the
actual program (this may still fail, for example if the program contains an
ill-formed constant).
getTestedValue ∷ Format → UplcEvaluatorFun res → FilePath → IO (Either Text res) Source #
Get the tested value from a test-case input file in the given Format.
The tested value is either the shown parse error or evaluation error, or a
res.
Arguments
| ∷ Either Text UplcProg | golden value |
| → Either Text UplcProg | tested value |
| → Maybe String | If two values are the same, it returns |
The comparison function used for the golden test.
This function checks alpha-equivalence of programs when the output is a program.
Both Textual and Flat golden values now record the failure reason precisely
(see decodeFlatExpected), so in both cases we require it to match.
Update the golden file with the tested value, in the given Format: as
text for Textual (unchanged from before), or, for Flat, as flat-encoded
bytes on success or the UTF8-encoded failure-reason text on failure (see
decodeFlatExpected).
TODO abstract out for other tests.
representativeGoldenTest ∷ TestTree Source #
A golden test that is never actually run: it exists only so that it can be
passed to parseOptions to make tasty register the Golden test provider's
own options (`--accept`, `--no-create`, `--size-cutoff`, `--delete-output`)
before the real test tree (which needs the parsed `--format` option to be
built in the first place) exists. See the comment in runUplcEvalTests.
Arguments
| ∷ UplcEvaluator | The action to run the input through for the tests. |
| → (FilePath → Bool) | A function that takes a test name and returns
whether it should labelled as |
| → (FilePath → Bool) | A function that takes a test name and returns
whether it should labelled as |
| → IO () |
unDeBruijnProgram ∷ Program NamedDeBruijn DefaultUni DefaultFun () → Either FreeVariableError UplcProg Source #
Turn a Program using de Bruijn-indexed variables (as decoded from a
`.flat` file) into the Name-based representation used elsewhere in this
module, so that it can be compared with a program obtained by parsing a
textual `.uplc` file.
decodeFlatProg ∷ ByteString → Either String UplcProg Source #
Decode a flat-encoded UPLC program. We use the UnrestrictedProgram
wrapper so that the decoding doesn't reject programs on the grounds of using
builtins or term constructs which are unavailable in the version declared by
the program: we just want to know whether the bytes decode to the same AST as
the textual program that they're supposed to correspond to, not whether
they're a valid on-chain script.
encodeFlatProg ∷ UplcProg → ByteString Source #
Encode a UplcProg as flat bytes: the inverse of decodeFlatProg.
Converts the program's names to de Bruijn indices first (that's the
representation flat actually encodes), then encodes it via the same
UnrestrictedProgram wrapper decodeFlatProg uses, for the same reason
(avoiding rejecting programs on the grounds of builtins/term constructs
unavailable in the declared version). Used to write `.flat.expected` golden
files when accepting a Flat-format test result.