The C# track uses a test generator to auto-generate practice exercise tests.
It uses the fact that most exercises defined in the problem-specifications repo also have a canonical-data.json file, which contains standardized test inputs and outputs that can be used to implement the exercise.
To generate a practice exercise's tests, the test generator:
- Reads the exercise's test cases from its
canonical-data.jsonfile - Uses
tests.tomlfile to omit and excluded test cases - Renders the test cases using the exercise's generator template
- Format the rendered template using Roslyn
- Writes the formatted template to the exercise's test file
The test generator parses the test cases from the exercise's canonical-data.json using the System.Text.Json namespace.
Since some canonical data uses nesting, the parsed test case includes an additional path field that contains the description properties of any parent elements, as well as the test case's own description property.
Each exercise has a tests.toml file, in which individual tests can be excluded/disabled.
The test generator will remove any test cases that are marked as excluded (include = false).
The (potentially transformed) test cases are then passed to the .meta/Generator.tpl file, which defines how the tests should be rendered based on those test cases.
The rendered template is then formatted using Roslyn. This has the following benefits:
- Exercises are formatted consistently
- You're not required to worry much about whitespace and alignment when writing templates
Finally, the output of the rendered template is written to the exercise's test file.
The templates are rendered using the Scriban library.
There are two ways in which the test generator can be run:
bin/update-tests.ps1: update the tests for all exercises that have a generator templatebin/update-tests.ps1 -e <slug>: update the tests for the specified exercise, if it has a generator template