Testing a translator
Your translator is correct when its output means the same as the output of the reference implementation:
-
the same expression gives the same value on the same notice;
-
the same template displays the same information;
-
the same rules give the same findings.
This page describes how to check this, with the material that the SDK and the toolkit provide.
Strategy: compare with the reference implementation
The XPath script generator and the Schematron generator of the toolkit, and the XSLT markup generator of the Notice Viewer, are the reference. Test your generator by comparing its results with theirs, rather than by reading the code that it writes:
-
Translate the same EFX with your generator and with the reference one.
-
Run both outputs on the same notices.
-
Compare the results: the values of expressions, the text of displayed notices, or the findings of the validation.
In this way, you check the results that your users see. The test stays valid when either implementation changes the code that it writes.
Reference inputs
The SDK provides the EFX to translate and the notices to run it on:
view-templates-
The template files of all notice subtypes.
validation-
The rules files, by validation stage.
fields/fields.json-
The single expressions of the field constraints, for example the conditions under which a field is mandatory.
examples/notices-
Example notices. Notices whose names start with
INVALID_are invalid on purpose. The noticescn_24_FRA_comments.xmlandcan_24_FRA_comments.xmlare the most complete, with comments that name the business terms. examples/reports-
The validation report of each example notice, in SVRL, the report format of Schematron, made with the Schematron of the SDK. A validator built with your generator should give the same findings.
The source of the toolkit, on GitHub, is a second source of inputs. Its tests are small cases that are easy to read. Each one is an EFX input, with the output that the reference implementation gives:
-
EfxExpressionTranslatorV2Test,EfxTemplateTranslatorV2TestandEfxRulesTranslatorV2Testcover the parts of each flavour. They run on example test fields, such asBT-00-Text, defined in the test resources. The examples of the EFX Language Reference use the same fields. -
EfxExpressionEvaluationTestevaluates the XPath of expressions with Saxon and checks the values. It shows what an expression means, for example how it is grouped and what an index selects. -
SdkCorpusTranslationTesttranslates all the view templates and rules of the SDK, and writes the outputs, so that you can compare two builds file by file. It runs only when you ask for it:mvn test -Dgroups=corpus -DexcludedGroups=. -
SymbolResolverContractTestis an abstract test of the behaviour that a symbol resolver must have. Extend it to test a symbol resolver of your own.
Key areas to test
- Literal conversion
-
Texts with quotes, numbers with a decimal part and a sign, dates, times, and durations, with time zones where they apply. Check that a literal gives the same value in your language as in XPath.
- Operator composition
-
The grouping of operations, which depends on the precedence of each operation (see Precedence). Test chains of comparisons and mixes of
andandor, as in1 == 1 == TRUE, where missing parentheses change the result. - Path navigation and context handling
-
References from different contexts, predicates, indexes, context variables, references from the root, and attribute fields.
- Repeatable and multilingual fields
-
A reference to a repeatable field gives a sequence, and a multilingual field gives one text for each language. Check that your sequences behave as those of XPath, for example in
countandin. - Template output structure
-
The nesting of lines, outline numbers, headings and navigation ids (see Implementing MarkupGenerator), conditional display, and the invocation of callable templates.
- Edge cases
-
Empty values, missing fields, empty sequences, and fields withheld from publication. A field that is not in the notice must give the same result as in XPath: often an empty sequence, not an error.
Common implementation errors
-
White space in values. The XPath implementation reads text values with
normalize-space, so spaces at the start and at the end do not count. Remove these spaces in the same way, and replace each run of white space inside a value with one space. Otherwise, the comparisons give different results. -
Single values and sequences. XPath compares a sequence with a value by checking whether any item matches. If your language does not, make sure that your generator respects the types that the translator gives each expression.
-
Precedence. Most differences in results come from an operand written without the parentheses that it needs.
-
Testing only valid notices. Also test the
INVALID_examples: a validator that reports no findings is not necessarily correct.
To find the cause of a difference, translate the smallest expression or template that shows it, with both generators, and compare the two outputs side by side.
See also: