Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ The input of the template is the canonical data from [`problem-specifications`].

Find some tips about writing tera templates [here](#tera-templates).

[Tera]: https://keats.github.io/tera/docs/
[Tera]: https://keats.github.io/tera/
[`problem-specifications`]: https://github.com/exercism/problem-specifications/

Many aspects of a correctly implemented exercises are checked in CI.
Expand Down Expand Up @@ -125,7 +125,7 @@ You can use an if-else to render something different,
depending on the structure of the data:

```txt
let expected = {% if test.expected is object -%}
let expected = {% if test.expected is map -%}
None
{%- else -%}
Some({{ test.expected }})
Expand Down Expand Up @@ -157,7 +157,7 @@ For a rather complicated example, check out the test template of `triangle`.
It organizes the test cases in modules and dynamically detects which tests to put behind feature gates.
That exercise also reimplements some test cases from upstream in `additional-tests.json`, in order to add more information to them necessary for generating good tests.

[tera-docs]: https://keats.github.io/tera/docs/#templates
[tera-docs]: https://keats.github.io/tera/#template

## Syllabus

Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/affine-cipher/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn {{ test.description | make_ident }}() {
let phrase = {{ test.input.phrase | json_encode() }};
let (a, b) = ({{ test.input.key.a }}, {{ test.input.key.b }});
let output = {{ test.property }}(phrase, a, b);
let expected = {% if test.expected is object -%}
let expected = {% if test.expected is map -%}
Err(NotCoprime({{ test.input.key.a }}))
{%- else -%}
Ok({{ test.expected | json_encode() }}.into())
Expand Down
4 changes: 2 additions & 2 deletions exercises/practice/all-your-base/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ fn {{ test.description | make_ident }}() {
let input_base = {{ test.input.inputBase }};
let input_digits = &{{ test.input.digits | json_encode() }};
let output_base = {{ test.input.outputBase }};
{%- if not test.expected is object %}
{%- if not test.expected is map %}
let output_digits = vec!{{ test.expected | json_encode() }};
{%- endif %}
assert_eq!(
ayb::convert(input_digits, input_base, output_base),
{%- if not test.expected is object %}
{%- if not test.expected is map %}
Ok(output_digits)
{%- elif test.expected.error == "input base must be >= 2" %}
Err(ayb::Error::InvalidInputBase)
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/alphametics/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use alphametics::*;
#[ignore]
fn {{ test.description | make_ident }}() {
let answer = solve({{ test.input.puzzle | json_encode() }});
{%- if test.expected is object %}
{%- if test.expected is map %}
let expected = [
{%- for key, val in test.expected %}
('{{ key }}', {{val}}),
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/binary-search/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use binary_search::*;
#[test]
#[ignore]
fn {{ test.description | make_ident }}() {
assert_eq!(find(&{{ test.input.array | json_encode() }}, {{ test.input.value }}), {% if test.expected is object -%}
assert_eq!(find(&{{ test.input.array | json_encode() }}, {{ test.input.value }}), {% if test.expected is map -%}
None
{%- else -%}
Some({{ test.expected }})
Expand Down
8 changes: 4 additions & 4 deletions exercises/practice/clock/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use clock::*;
// Clock Creation
//

{% for test in cases.0.cases %}
{% for test in cases[0].cases %}
#[test]
#[ignore]
fn {{ test.description | make_ident }}() {
Expand All @@ -16,7 +16,7 @@ fn {{ test.description | make_ident }}() {
// Clock Math
//

{% for test in cases.1.cases %}
{% for test in cases[1].cases %}
#[test]
#[ignore]
fn {{ test.description | make_ident }}() {
Expand All @@ -25,7 +25,7 @@ fn {{ test.description | make_ident }}() {
}
{% endfor %}

{% for test in cases.2.cases %}
{% for test in cases[2].cases %}
#[test]
#[ignore]
fn {{ test.description | make_ident }}() {
Expand All @@ -38,7 +38,7 @@ fn {{ test.description | make_ident }}() {
// Test Equality
//

{% for test in cases.3.cases %}
{% for test in cases[3].cases %}
#[test]
#[ignore]
fn {{ test.description | make_ident }}() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use collatz_conjecture::*;
#[ignore]
fn {{ test.description | make_ident }}() {
let output = collatz({{ test.input.number | fmt_num }});
let expected = {% if test.expected is object %}
let expected = {% if test.expected is map %}
None
{% else %}
Some({{ test.expected | json_encode() }})
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/diamond/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn {{ test.description | make_ident }}() {
For us that means up to letter E
We also exclude A to keep it inlined
#}
{%- if test.input.letter is matching("[B-D]") %}
{%- if test.input.letter is matching(pat="[B-D]") %}
#[rustfmt::skip]
{%- endif %}
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/dominoes/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
fn {{ test.description | make_ident }}() {
let input = &[
{% for domino in test.input.dominoes %}
({{ domino.0 }}, {{domino.1 }}),
({{ domino[0] }}, {{ domino[1] }}),
{% endfor %}
];
{%- if test.expected %}
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/forth/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn {{ test.description | make_ident }}() {
{% continue %}
{% endif -%}

{% if test.expected is object -%}
{% if test.expected is map -%}
{% if test.expected.error == "empty stack" or test.expected.error == "only one value on the stack" -%}
assert_eq!(f.eval("{{ test.input.instructions[0] }}"), Err(Error::StackUnderflow));
{% elif test.expected.error == "divide by zero" -%}
Expand Down
32 changes: 16 additions & 16 deletions exercises/practice/gigasecond/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
really makes you question your life choices.
#}
{% set date_and_time = test.input.moment | split(pat="T") %}
{% set date = date_and_time.0 | split(pat="-") %}
{% set year = date.0 | int %}
{% set month = date.1 | int %}
{% set day = date.2 | int %}
{% set date = date_and_time[0] | split(pat="-") %}
{% set year = date[0] | int %}
{% set month = date[1] | int %}
{% set day = date[2] | int %}
{% if date_and_time | length >= 2 %}
{% set time = date_and_time.1 | split(pat=":") %}
{% set hour = time.0 | int %}
{% set minute = time.1 | int %}
{% set second = time.2 | int %}
{% set time = date_and_time[1] | split(pat=":") %}
{% set hour = time[0] | int %}
{% set minute = time[1] | int %}
{% set second = time[2] | int %}
{% else %}
{% set hour = 0 %}
{% set minute = 0 %}
Expand All @@ -24,14 +24,14 @@
again for the expected datetime
#}
{% set date_and_time = test.expected | split(pat="T") %}
{% set date = date_and_time.0 | split(pat="-") %}
{% set e_year = date.0 | int %}
{% set e_month = date.1 | int %}
{% set e_day = date.2 | int %}
{% set time = date_and_time.1 | split(pat=":") %}
{% set e_hour = time.0 | int %}
{% set e_minute = time.1 | int %}
{% set e_second = time.2 | int %}
{% set date = date_and_time[0] | split(pat="-") %}
{% set e_year = date[0] | int %}
{% set e_month = date[1] | int %}
{% set e_day = date[2] | int %}
{% set time = date_and_time[1] | split(pat=":") %}
{% set e_hour = time[0] | int %}
{% set e_minute = time[1] | int %}
{% set e_second = time[2] | int %}

#[test]
#[ignore]
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/grade-school/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn {{ test.description | make_ident }}() {
mut
{% endif %} s = School::new();
{% for student in test.input.students -%}
s.add({{ student.1 }}, {{ student.0 | json_encode() }});
s.add({{ student[1] }}, {{ student[0] | json_encode() }});
{% endfor -%}
{% if test.property == "grade" -%}
assert_eq!(
Expand Down
4 changes: 2 additions & 2 deletions exercises/practice/grains/.meta/test_template.tera
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use grains::*;

{% for test in cases.0.cases %}
{% for test in cases[0].cases %}
#[test]
#[ignore]
{% if test.expected is object -%}
{% if test.expected is map -%}
#[should_panic]
{% endif -%}
fn {{ test.description | make_ident }}() {
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/grep/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn {{ test.description | make_ident }}() {
]);
let actual = grep(pattern, &flags, files.as_ref()).unwrap();
let expected: &[&str] = &[
{% if test.input.files | length > 1 or test.input.flags is containing("-l") %}
{% if test.input.files | length > 1 or test.input.flags is containing(pat="-l") %}
{% for line in test.expected %}
"{{ prefix }}-{{ line }}",
{% endfor %}
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/hamming/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use hamming::*;
fn {{ test.description | make_ident }}() {
assert_eq!(
hamming_distance({{ test.input.strand1 | json_encode() }}, {{ test.input.strand2 | json_encode() }}),
{% if test.expected is object -%}
{% if test.expected is map -%}
None
{% else -%}
Some({{ test.expected }})
Expand Down
10 changes: 5 additions & 5 deletions exercises/practice/high-scores/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ use high_scores::HighScores;
#[test]
#[ignore]
fn {{ test_name }}() {
{% if test.property is starting_with("score") %}
{% if test.property is starting_with(pat="score") %}
let expected = {{ test.input.scores }};
let high_scores = HighScores::new(&expected);
{% else %}
let high_scores = HighScores::new(&{{ test.input.scores }});
{% endif %}

{% if test.property is ending_with("AfterTopThree") %}
{% if test.property is ending_with(pat="AfterTopThree") %}
let _ = high_scores.personal_top_three();
{% elif test.property is ending_with("AfterBest") %}
{% elif test.property is ending_with(pat="AfterBest") %}
let _ = high_scores.personal_best();
{% endif %}


{% if test.property is starting_with("score") %}
{% if test.property is starting_with(pat="score") %}
assert_eq!(high_scores.scores(), &expected);
{% elif test.property is starting_with("latest") %}
{% elif test.property is starting_with(pat="latest") %}
assert_eq!(high_scores.latest(), Some({{ test.expected }}));
{% elif test.property == "personalBest" %}
assert_eq!(high_scores.personal_best(), Some({{ test.expected }}));
Expand Down
8 changes: 4 additions & 4 deletions exercises/practice/list-ops/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ fn {{ test.description | make_ident }}() {

let {{arg}} =
{% if is_empty %}
{% if test.property is starting_with("fold") %}
{% if test.property is starting_with(pat="fold") %}
[0.0f64; 0]
{% elif test.property == "concat" %}
[[0i32; 0]; 0]
{% else %}
[0i32; 0]
{% endif %}
{% elif test.property is starting_with("fold") %}
{% elif test.property is starting_with(pat="fold") %}
{% if value is number %}
{{ test.input[arg] }}.0
{% else %}
{{ test.input[arg] | as_str | replace(from=",", to=".0,") | replace(from="]", to=".0]") | replace(from="[.0]", to="[]") }}
{{ test.input[arg] | str | replace(from=",", to=".0,") | replace(from="]", to=".0]") | replace(from="[.0]", to="[]") }}
{% endif %}
{% else %}
{{ test.input[arg] }}
Expand All @@ -56,7 +56,7 @@ fn {{ test.description | make_ident }}() {
{% endif %}
{% endfor %}
);
let expected = {{ test.expected }}{% if test.property is starting_with("fold") %}.0{% endif %};
let expected = {{ test.expected }}{% if test.property is starting_with(pat="fold") %}.0{% endif %};
assert_eq!(
output
{% if test.expected is iterable %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use matching_brackets::brackets_are_balanced;
fn {{ test.description | make_ident }}() {
assert!(
{% if test.expected == false %}!{% endif %}
brackets_are_balanced("{{ test.input.value | replace(from="\", to="\\") }}")
brackets_are_balanced("{{ test.input.value | replace(from="\\", to="\\\\") }}")
);
}
{% endfor -%}
2 changes: 1 addition & 1 deletion exercises/practice/nth-prime/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use nth_prime::*;
#[test]
#[ignore]
fn {{ test.description | make_ident }}() {
let output = nth({{ test.input.number - 1 | fmt_num }});
let output = nth({{ (test.input.number - 1) | fmt_num }});
let expected = {{ test.expected | fmt_num }};
assert_eq!(output, expected);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use perfect_numbers::*;
fn {{ test.description | make_ident }}() {
let input = {{ test.input.number | fmt_num }};
let output = classify(input);
{%- if test.expected is object %}
{%- if test.expected is map %}
assert!(output.is_none());
{% else %}
let expected = Some(Classification::{{ test.expected | title }});
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/phone-number/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use phone_number::*;
fn {{ test.description | make_ident }}() {
let input = {{ test.input.phrase | json_encode() }};
let output = number(input);
{%- if test.expected is object %}
{%- if test.expected is map %}
assert!(output.is_none());
{% else %}
let expected = Some({{ test.expected | json_encode() }}.into());
Expand Down
16 changes: 8 additions & 8 deletions exercises/practice/pov/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ mod equality {
}
}

{% macro render_tree(tree) -%}
{% component render_tree(tree) -%}
Tree::new("{{ tree.label }}")
{%- if tree.children -%}{%- for child in tree.children -%}
.with_child({{ self::render_tree(tree=child) }})
.with_child({{<render_tree tree={child} />}})
{%- endfor -%}{%- endif -%}
{%- endmacro -%}
{%- endcomponent -%}

{%- macro render_vec(values) -%}
{%- component render_vec(values) -%}
vec![
{%- for value in values -%}
&"{{ value }}",
{%- endfor -%}
]
{%- endmacro -%}
{%- endcomponent -%}

{% for test_group in cases %}
/// {{ test_group.description }}
Expand All @@ -52,21 +52,21 @@ mod {{ test_group.cases[0].property | make_ident }} {
#[test]
#[ignore]
fn {{ test.description | make_ident }}() {
let mut tree = {{ self::render_tree(tree=test.input.tree) }};
let mut tree = {{<render_tree tree={test.input.tree} />}};
{%- if test.property == "fromPov" -%}
{%- if not test.expected -%}
assert!(!tree.pov_from(&"{{ test.input.from }}"));
{%- else -%}
assert!(tree.pov_from(&"{{ test.input.from }}"));
let expected = {{ self::render_tree(tree=test.expected) }};
let expected = {{<render_tree tree={test.expected} />}};
assert_eq!(tree, expected);
{%- endif -%}
{%- elif test.property == "pathTo" -%}
let result = tree.path_between(&"{{ test.input.from }}", &"{{ test.input.to }}");
{%- if not test.expected -%}
let expected: Option<Vec<_>> = None;
{%- else -%}
let expected = Some({{ self::render_vec(values=test.expected) }});
let expected = Some({{<render_vec values={test.expected} />}});
{%- endif -%}
assert_eq!(result, expected);
{%- else -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use protein_translation::*;
fn {{ test.description | make_ident }}() {
assert_eq!(
translate({{ test.input.strand | json_encode() }}),
{% if test.expected is object %}
{% if test.expected is map %}
None
{% else %}
Some(vec![
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/queen-attack/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use queen_attack::*;
fn {{ test.description | make_ident }}() {
{% if test.property == "create" %}
let chess_position = ChessPosition::new({{ test.input.queen.position.row }}, {{ test.input.queen.position.column }});
{%- if test.expected is object %}
{%- if test.expected is map %}
assert!(chess_position.is_none());
{% else %}
assert!(chess_position.is_some());
Expand Down
Loading
Loading