@@ -33,6 +33,7 @@ Run `just add-practice-exercise` and you'll be prompted for the minimal
3333information required to generate the exercise stub for you.
3434After that, jump in the generated exercise and fill in any todos you find.
3535This includes most notably:
36+
3637- adding an example solution in ` .meta/example.rs `
3738- Adjusting ` .meta/test_template.tera `
3839
@@ -41,12 +42,14 @@ The input of the template is the canonical data from [`problem-specifications`].
4142if you want to exclude certain tests from being generated,
4243you have to set ` include = false ` in ` .meta/tests.toml ` .
4344
45+ Find some tips about writing tera templates [ here] ( #tera-templates ) .
46+
4447[ Tera ] : https://keats.github.io/tera/docs/
4548[ `problem-specifications` ] : https://github.com/exercism/problem-specifications/
4649
4750Many aspects of a correctly implemented exercises are checked in CI.
4851I recommend that instead of spending lots of time studying and writing
49- documentation about the process, * just do it * .
52+ documentation about the process, _ just do it _ .
5053If something breaks, fix it and add a test / automation
5154so it won't happen anymore.
5255
@@ -87,6 +90,70 @@ Run `just update-practice-exercise` to update an exercise.
8790This outsources most work to ` configlet sync --update `
8891and runs the test generator again.
8992
93+ When updaing an exercise that doesn't have a tera template yet,
94+ a new one will be generated for you.
95+ You will likely have to adjust it to some extent.
96+
97+ Find some tips about writing tera templates [ in the next section] ( #tera-templates ) .
98+
99+ ## Tera templates
100+
101+ The full documentation for tera templates is [ here] [ tera-docs ] .
102+ Following are some approaches that have worked for our specific needs.
103+
104+ You will likely want to look at the exercise's ` canonical-data.json `
105+ to see what structure your input data has.
106+ You can use ` bin/symlink_problem_specifications.sh ` to have this data
107+ symlinked into the actual exercise directory. Handy!
108+
109+ The name of the input property is different for each exercise.
110+ The default template will be something like this:
111+
112+ ``` txt
113+ let input = {{ test.input | json_encode() }};
114+ ```
115+
116+ You will have to add the specific field of input for this exercise, e.g.
117+
118+ ``` txt
119+ let input = {{ test.input.integers | json_encode() }};
120+ ```
121+
122+ Some exercises may have error return values.
123+ You can use an if-else to render something different,
124+ depending on the structure of the data:
125+
126+ ``` txt
127+ let expected = {% if test.expected is object -%}
128+ None
129+ {%- else -%}
130+ Some({{ test.expected }})
131+ {%- endif %};
132+ ```
133+
134+ If every test case needs to do some crunching of the inputs,
135+ you can add utils functions at the top of the tera template.
136+ See [ ` word-count ` 's template] [ word-count-tmpl ] for an example.
137+
138+ Some exercises have multiple functions that need to be implemented
139+ by the student and therefore tested.
140+ The canonical data contains a field ` property ` in that case.
141+ The template also has access to a value ` fn_names ` ,
142+ which is an array of functions found in ` lib.rs ` .
143+ So, you can construct if-else-chains based on ` test.property `
144+ and render a different element of ` fn_names ` based on that.
145+ See [ ` variable-length-quantity ` 's template] [ var-len-q-tmpl ] for an example.
146+
147+ There is a custom tera fiter ` to_hex ` , which formats ints in hexadecimal.
148+ Feel free to add your own in the crate ` rust-tooling ` .
149+ Custom filters added there will be available to all templates.
150+ How to create such custom filters is documented int he [ tera docs] [ tera-docs-filters ] .
151+
152+ [ tera-docs ] : https://keats.github.io/tera/docs/#templates
153+ [ word-count-tmpl ] : /exercises/practice/word-count/.meta/test_template.tera
154+ [ var-len-q-tmpl ] : /exercises/practice/variable-length-quantity/.meta/test_template.tera
155+ [ tera-docs-filters ] : https://keats.github.io/tera/docs/#filters
156+
90157## Syllabus
91158
92159The syllabus is currently deactivated due to low quality.
0 commit comments