Skip to content

Commit

Permalink
generator: remove custom context (#1886)
Browse files Browse the repository at this point in the history
[no important files changed]
  • Loading branch information
senekor authored Apr 2, 2024
1 parent 9c9a5bd commit 37449b6
Show file tree
Hide file tree
Showing 53 changed files with 348 additions and 334 deletions.
12 changes: 1 addition & 11 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,10 @@ let expected = {% if test.expected is object -%}
{%- endif %};
```

If every test case needs to do some crunching of the inputs,
you can add utils functions at the top of the tera template.
See [`word-count`'s template][word-count-tmpl] for an example.

Some exercises have multiple functions that need to be implemented
by the student and therefore tested.
The canonical data contains a field `property` in that case.
The template also has access to a value `fn_names`,
which is an array of functions found in `lib.rs`.
So, you can construct if-else-chains based on `test.property`
and render a different element of `fn_names` based on that.
See [`variable-length-quantity`'s template][var-len-q-tmpl] for an example.
You can construct if-else-chains based on `test.property` and render a different function based on that.

There is a custom tera fiter `to_hex`, which formats ints in hexadecimal.
Feel free to add your own in the crate `rust-tooling`.
Expand All @@ -157,8 +149,6 @@ It organizes the test cases in modules and dynamically detects which tests to pu
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
[word-count-tmpl]: /exercises/practice/word-count/.meta/test_template.tera
[var-len-q-tmpl]: /exercises/practice/variable-length-quantity/.meta/test_template.tera
[tera-docs-filters]: https://keats.github.io/tera/docs/#filters

## Syllabus
Expand Down
4 changes: 3 additions & 1 deletion exercises/practice/acronym/.meta/test_template.tera
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use acronym::*;

{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | slugify | replace(from="-", to="_") }}() {
let input = {{ test.input.phrase | json_encode() }};
let output = {{ crate_name }}::{{ fn_names[0] }}(input);
let output = abbreviate(input);
let expected = {{ test.expected | json_encode() }};
assert_eq!(output, expected);
}
Expand Down
22 changes: 12 additions & 10 deletions exercises/practice/acronym/tests/acronym.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use acronym::*;

#[test]
fn basic() {
let input = "Portable Network Graphics";
let output = acronym::abbreviate(input);
let output = abbreviate(input);
let expected = "PNG";
assert_eq!(output, expected);
}
Expand All @@ -10,7 +12,7 @@ fn basic() {
#[ignore]
fn lowercase_words() {
let input = "Ruby on Rails";
let output = acronym::abbreviate(input);
let output = abbreviate(input);
let expected = "ROR";
assert_eq!(output, expected);
}
Expand All @@ -19,7 +21,7 @@ fn lowercase_words() {
#[ignore]
fn punctuation() {
let input = "First In, First Out";
let output = acronym::abbreviate(input);
let output = abbreviate(input);
let expected = "FIFO";
assert_eq!(output, expected);
}
Expand All @@ -28,7 +30,7 @@ fn punctuation() {
#[ignore]
fn all_caps_word() {
let input = "GNU Image Manipulation Program";
let output = acronym::abbreviate(input);
let output = abbreviate(input);
let expected = "GIMP";
assert_eq!(output, expected);
}
Expand All @@ -37,7 +39,7 @@ fn all_caps_word() {
#[ignore]
fn punctuation_without_whitespace() {
let input = "Complementary metal-oxide semiconductor";
let output = acronym::abbreviate(input);
let output = abbreviate(input);
let expected = "CMOS";
assert_eq!(output, expected);
}
Expand All @@ -46,7 +48,7 @@ fn punctuation_without_whitespace() {
#[ignore]
fn very_long_abbreviation() {
let input = "Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me";
let output = acronym::abbreviate(input);
let output = abbreviate(input);
let expected = "ROTFLSHTMDCOALM";
assert_eq!(output, expected);
}
Expand All @@ -55,7 +57,7 @@ fn very_long_abbreviation() {
#[ignore]
fn consecutive_delimiters() {
let input = "Something - I made up from thin air";
let output = acronym::abbreviate(input);
let output = abbreviate(input);
let expected = "SIMUFTA";
assert_eq!(output, expected);
}
Expand All @@ -64,7 +66,7 @@ fn consecutive_delimiters() {
#[ignore]
fn apostrophes() {
let input = "Halley's Comet";
let output = acronym::abbreviate(input);
let output = abbreviate(input);
let expected = "HC";
assert_eq!(output, expected);
}
Expand All @@ -73,7 +75,7 @@ fn apostrophes() {
#[ignore]
fn underscore_emphasis() {
let input = "The Road _Not_ Taken";
let output = acronym::abbreviate(input);
let output = abbreviate(input);
let expected = "TRNT";
assert_eq!(output, expected);
}
Expand All @@ -82,7 +84,7 @@ fn underscore_emphasis() {
#[ignore]
fn camelcase() {
let input = "HyperText Markup Language";
let output = acronym::abbreviate(input);
let output = abbreviate(input);
let expected = "HTML";
assert_eq!(output, expected);
}
4 changes: 3 additions & 1 deletion exercises/practice/affine-cipher/.meta/test_template.tera
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use affine_cipher::*;

use affine_cipher::AffineCipherError::NotCoprime;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | slugify | replace(from="-", to="_") }}() {
let phrase = {{ test.input.phrase | json_encode() }};
let (a, b) = ({{ test.input.key.a }}, {{ test.input.key.b }});
let output = {{ crate_name }}::{{ test.property }}(phrase, a, b);
let output = {{ test.property }}(phrase, a, b);
let expected = {% if test.expected is object -%}
Err(NotCoprime({{ test.input.key.a }}))
{%- else -%}
Expand Down
34 changes: 18 additions & 16 deletions exercises/practice/affine-cipher/tests/affine-cipher.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use affine_cipher::*;

use affine_cipher::AffineCipherError::NotCoprime;

#[test]
fn encode_yes() {
let phrase = "yes";
let (a, b) = (5, 7);
let output = affine_cipher::encode(phrase, a, b);
let output = encode(phrase, a, b);
let expected = Ok("xbt".into());
assert_eq!(output, expected);
}
Expand All @@ -14,7 +16,7 @@ fn encode_yes() {
fn encode_no() {
let phrase = "no";
let (a, b) = (15, 18);
let output = affine_cipher::encode(phrase, a, b);
let output = encode(phrase, a, b);
let expected = Ok("fu".into());
assert_eq!(output, expected);
}
Expand All @@ -24,7 +26,7 @@ fn encode_no() {
fn encode_omg() {
let phrase = "OMG";
let (a, b) = (21, 3);
let output = affine_cipher::encode(phrase, a, b);
let output = encode(phrase, a, b);
let expected = Ok("lvz".into());
assert_eq!(output, expected);
}
Expand All @@ -34,7 +36,7 @@ fn encode_omg() {
fn encode_o_m_g() {
let phrase = "O M G";
let (a, b) = (25, 47);
let output = affine_cipher::encode(phrase, a, b);
let output = encode(phrase, a, b);
let expected = Ok("hjp".into());
assert_eq!(output, expected);
}
Expand All @@ -44,7 +46,7 @@ fn encode_o_m_g() {
fn encode_mindblowingly() {
let phrase = "mindblowingly";
let (a, b) = (11, 15);
let output = affine_cipher::encode(phrase, a, b);
let output = encode(phrase, a, b);
let expected = Ok("rzcwa gnxzc dgt".into());
assert_eq!(output, expected);
}
Expand All @@ -54,7 +56,7 @@ fn encode_mindblowingly() {
fn encode_numbers() {
let phrase = "Testing,1 2 3, testing.";
let (a, b) = (3, 4);
let output = affine_cipher::encode(phrase, a, b);
let output = encode(phrase, a, b);
let expected = Ok("jqgjc rw123 jqgjc rw".into());
assert_eq!(output, expected);
}
Expand All @@ -64,7 +66,7 @@ fn encode_numbers() {
fn encode_deep_thought() {
let phrase = "Truth is fiction.";
let (a, b) = (5, 17);
let output = affine_cipher::encode(phrase, a, b);
let output = encode(phrase, a, b);
let expected = Ok("iynia fdqfb ifje".into());
assert_eq!(output, expected);
}
Expand All @@ -74,7 +76,7 @@ fn encode_deep_thought() {
fn encode_all_the_letters() {
let phrase = "The quick brown fox jumps over the lazy dog.";
let (a, b) = (17, 33);
let output = affine_cipher::encode(phrase, a, b);
let output = encode(phrase, a, b);
let expected = Ok("swxtj npvyk lruol iejdc blaxk swxmh qzglf".into());
assert_eq!(output, expected);
}
Expand All @@ -84,7 +86,7 @@ fn encode_all_the_letters() {
fn encode_with_a_not_coprime_to_m() {
let phrase = "This is a test.";
let (a, b) = (6, 17);
let output = affine_cipher::encode(phrase, a, b);
let output = encode(phrase, a, b);
let expected = Err(NotCoprime(6));
assert_eq!(output, expected);
}
Expand All @@ -94,7 +96,7 @@ fn encode_with_a_not_coprime_to_m() {
fn decode_exercism() {
let phrase = "tytgn fjr";
let (a, b) = (3, 7);
let output = affine_cipher::decode(phrase, a, b);
let output = decode(phrase, a, b);
let expected = Ok("exercism".into());
assert_eq!(output, expected);
}
Expand All @@ -104,7 +106,7 @@ fn decode_exercism() {
fn decode_a_sentence() {
let phrase = "qdwju nqcro muwhn odqun oppmd aunwd o";
let (a, b) = (19, 16);
let output = affine_cipher::decode(phrase, a, b);
let output = decode(phrase, a, b);
let expected = Ok("anobstacleisoftenasteppingstone".into());
assert_eq!(output, expected);
}
Expand All @@ -114,7 +116,7 @@ fn decode_a_sentence() {
fn decode_numbers() {
let phrase = "odpoz ub123 odpoz ub";
let (a, b) = (25, 7);
let output = affine_cipher::decode(phrase, a, b);
let output = decode(phrase, a, b);
let expected = Ok("testing123testing".into());
assert_eq!(output, expected);
}
Expand All @@ -124,7 +126,7 @@ fn decode_numbers() {
fn decode_all_the_letters() {
let phrase = "swxtj npvyk lruol iejdc blaxk swxmh qzglf";
let (a, b) = (17, 33);
let output = affine_cipher::decode(phrase, a, b);
let output = decode(phrase, a, b);
let expected = Ok("thequickbrownfoxjumpsoverthelazydog".into());
assert_eq!(output, expected);
}
Expand All @@ -134,7 +136,7 @@ fn decode_all_the_letters() {
fn decode_with_no_spaces_in_input() {
let phrase = "swxtjnpvyklruoliejdcblaxkswxmhqzglf";
let (a, b) = (17, 33);
let output = affine_cipher::decode(phrase, a, b);
let output = decode(phrase, a, b);
let expected = Ok("thequickbrownfoxjumpsoverthelazydog".into());
assert_eq!(output, expected);
}
Expand All @@ -144,7 +146,7 @@ fn decode_with_no_spaces_in_input() {
fn decode_with_too_many_spaces() {
let phrase = "vszzm cly yd cg qdp";
let (a, b) = (15, 16);
let output = affine_cipher::decode(phrase, a, b);
let output = decode(phrase, a, b);
let expected = Ok("jollygreengiant".into());
assert_eq!(output, expected);
}
Expand All @@ -154,7 +156,7 @@ fn decode_with_too_many_spaces() {
fn decode_with_a_not_coprime_to_m() {
let phrase = "Test";
let (a, b) = (13, 5);
let output = affine_cipher::decode(phrase, a, b);
let output = decode(phrase, a, b);
let expected = Err(NotCoprime(13));
assert_eq!(output, expected);
}
2 changes: 1 addition & 1 deletion exercises/practice/allergies/.meta/test_template.tera
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use {{ crate_name }}::*;
use allergies::*;

fn compare_allergy_vectors(expected: &[Allergen], actual: &[Allergen]) {
for element in expected {
Expand Down
4 changes: 2 additions & 2 deletions exercises/practice/anagram/.meta/test_template.tera
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use {{ crate_name }}::*;
use anagram::*;
use std::collections::HashSet;

{% for test in cases %}
Expand All @@ -7,7 +7,7 @@ use std::collections::HashSet;
fn {{ test.description | slugify | replace(from="-", to="_") }}() {
let word = {{ test.input.subject | json_encode() }};
let inputs = &{{ test.input.candidates | json_encode() }};
let output = {{ fn_names[0] }}(word, inputs);
let output = anagrams_for(word, inputs);
let expected = HashSet::from_iter({{ test.expected | json_encode() }});
assert_eq!(output, expected);
}
Expand Down
4 changes: 3 additions & 1 deletion exercises/practice/book-store/.meta/test_template.tera
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use book_store::*;

{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | slugify | replace(from="-", to="_") }}() {
let input = &{{ test.input.basket | json_encode() }};
let output = {{ crate_name }}::{{ fn_names[0] }}(input);
let output = lowest_price(input);
let expected = {{ test.expected | json_encode() }};
assert_eq!(output, expected);
}
Expand Down
Loading

0 comments on commit 37449b6

Please sign in to comment.