Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generator: replace snake_case with make_ident #1969

Merged
merged 2 commits into from
Aug 15, 2024
Merged
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
4 changes: 2 additions & 2 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ In that case, you can use the group description to organize the tests in modules
There are some custom tera filters in [`rust-tooling`](/rust-tooling/generate/src/custom_filters.rs).
Here's the hopefully up-to-date list:
- `to_hex` formats ints in hexadecimal
- `snake_case` massages an arbitrary string into a decent Rust identifier
- `make_test_ident` is like snake case, but prepends `test_` if the string starts with a digit
- `make_ident` turns an arbitrary string into a decent Rust identifier.
Most useful for generating function names from test descriptions.
- `fmt_num` format number literals (insert `_` every third digit)

Feel free to add your own in the crate `rust-tooling`.
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/acronym/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use acronym::*;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
let input = {{ test.input.phrase | json_encode() }};
let output = abbreviate(input);
let expected = {{ test.expected | json_encode() }};
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 @@ -6,7 +6,7 @@ use affine_cipher::AffineCipherError::NotCoprime;
{% for test in test_group.cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
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);
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/all-your-base/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use allyourbase as ayb;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | make_test_ident }}() {
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 }};
Expand Down
4 changes: 2 additions & 2 deletions exercises/practice/allergies/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ fn compare_allergy_vectors(expected: &[Allergen], actual: &[Allergen]) {
#[ignore]
{%- if test.property == "allergicTo" %}
{# canonical data contains multiple cases named "allergic to everything" for different items #}
fn {{ test.description | snake_case }}_{{ test.input.item }}() {
fn {{ test.description | make_ident }}_{{ test.input.item }}() {
let allergies = Allergies::new({{ test.input.score }});
{%- if test.expected %}
assert!(allergies.is_allergic_to(&Allergen::{{ test.input.item | title }}))
{% else %}
assert!(!allergies.is_allergic_to(&Allergen::{{ test.input.item | title }}))
{% endif -%}
{% else %}
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
let allergies = Allergies::new({{ test.input.score }}).allergies();
let expected = &[
{% for allergen in test.expected %}
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 @@ -3,7 +3,7 @@ use alphametics::*;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
let answer = solve({{ test.input.puzzle | json_encode() }});
{%- if test.expected is object %}
let expected = [
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/anagram/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::HashSet;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
let word = {{ test.input.subject | json_encode() }};
let inputs = &{{ test.input.candidates | json_encode() }};
let output = anagrams_for(word, inputs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use armstrong_numbers::*;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
assert!({% if not test.expected %} ! {% endif %}is_armstrong_number({{ test.input.number | fmt_num }}))
}
{% endfor -%}
4 changes: 2 additions & 2 deletions exercises/practice/beer-song/.meta/test_template.tera
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{% for stupid_uselessly_nested_test_group in cases -%}
{% for test_group in stupid_uselessly_nested_test_group.cases -%}
mod {{ test_group.description | snake_case }} {
mod {{ test_group.description | make_ident }} {
use beer_song::*;

{% for test in test_group.cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
assert_eq!(
{% if stupid_uselessly_nested_test_group.description == "verse" -%}
verse({{ test.input.startBottles }}).trim(),
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 @@ -3,7 +3,7 @@ use binary_search::*;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
assert_eq!(find(&{{ test.input.array | json_encode() }}, {{ test.input.value }}), {% if test.expected is object -%}
None
{%- else -%}
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/bob/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bob::*;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
assert_eq!(reply({{ test.input.heyBob | json_encode() }}), "{{ test.expected }}");
}
{% endfor -%}
2 changes: 1 addition & 1 deletion exercises/practice/book-store/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use book_store::*;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
let input = &{{ test.input.basket | json_encode() }};
let output = lowest_price(input);
let expected = {{ test.expected | json_encode() }};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::rc::Rc;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
let mut buffer = CircularBuffer{% if loop.index == 1 %}::<i32>{% endif %}::new({{ test.input.capacity }});
{%- for op in test.input.operations %}
{%- if op.operation == "read" %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use collatz_conjecture::*;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
let output = collatz({{ test.input.number | fmt_num }});
let expected = {% if test.expected is object %}
None
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/custom-set/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use custom_set::CustomSet;
{% for test in test_group.cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
{%- if test.property == "empty" %}
let set = CustomSet::<i32>::new(&{{ test.input.set | json_encode() }});
assert!(
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/eliuds-eggs/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use eliuds_eggs::*;
{% for test in cases %}
#[test]
#[ignore]
fn test_{{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
let input = {{ test.input.number | fmt_num }};
let output = egg_count(input);
let expected = {{ test.expected | json_encode() }};
Expand Down
4 changes: 2 additions & 2 deletions exercises/practice/forth/.meta/test_template.tera
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{% for test_group in cases %}
mod {{ test_group.description | snake_case }} {
mod {{ test_group.description | make_ident }} {
use forth::*;

{% for test in test_group.cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
let mut f = Forth::new();
{% if test.property == "evaluateBoth" -%}
{% for instr in test.input.instructionsFirst -%}
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 @@ -32,7 +32,7 @@ fn grep_returns_result() {

#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
let pattern = {{ test.input.pattern | json_encode() }};
let flags = Flags::new(&{{ test.input.flags | json_encode() }});
let files = Files::new(&[
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/isbn-verifier/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use isbn_verifier::*;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
{% if test.expected %}
assert!(is_valid_isbn("{{ test.input.isbn }}"));
{% else %}
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/isogram/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use isogram::*;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
{% if test.expected %}
assert!(check({{ test.input.phrase | json_encode() }}));
{% else %}
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/knapsack/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use knapsack::*;
{% for test in cases %}
#[test]
#[ignore]
fn test_{{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
let max_weight = {{ test.input.maximumWeight }};
let items = [
{% for item in test.input.items -%}
Expand Down
10 changes: 5 additions & 5 deletions exercises/practice/knapsack/tests/knapsack.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use knapsack::*;

#[test]
fn test_no_items() {
fn no_items() {
let max_weight = 100;
let items = [];
let output = maximum_value(max_weight, &items);
Expand All @@ -11,7 +11,7 @@ fn test_no_items() {

#[test]
#[ignore]
fn test_one_item_too_heavy() {
fn one_item_too_heavy() {
let max_weight = 10;
let items = [Item {
weight: 100,
Expand All @@ -24,7 +24,7 @@ fn test_one_item_too_heavy() {

#[test]
#[ignore]
fn test_five_items_cannot_be_greedy_by_weight() {
fn five_items_cannot_be_greedy_by_weight() {
let max_weight = 10;
let items = [
Item {
Expand Down Expand Up @@ -55,7 +55,7 @@ fn test_five_items_cannot_be_greedy_by_weight() {

#[test]
#[ignore]
fn test_five_items_cannot_be_greedy_by_value() {
fn five_items_cannot_be_greedy_by_value() {
let max_weight = 10;
let items = [
Item {
Expand Down Expand Up @@ -86,7 +86,7 @@ fn test_five_items_cannot_be_greedy_by_value() {

#[test]
#[ignore]
fn test_example_knapsack() {
fn example_knapsack() {
let max_weight = 10;
let items = [
Item {
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/leap/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use leap::*;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
{%- if test.expected %}
assert!(is_leap_year({{ test.input.year }}));
{% else %}
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/matrix/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use matrix::*;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
let matrix = Matrix::new({{ test.input.string | json_encode() }});
{% if test.expected -%}
assert_eq!(matrix.{{ test.property }}({{ test.input.index }}), Some(vec!{{ test.expected | json_encode() }}));
Expand Down
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 @@ -3,7 +3,7 @@ use nth_prime::*;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
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 @@ -26,7 +26,7 @@ fn palindrome_new_return_none() {
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
{%- if test.property == "smallest" %}
let output = palindrome_products({{ test.input.min }}, {{ test.input.max }}).map(|(min, _)| min.into_inner());
{%- else %}
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/pangram/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use pangram::*;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
let sentence = {{ test.input.sentence | json_encode() }};
{% if test.expected -%}
assert!(is_pangram(sentence));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use pascals_triangle::PascalsTriangle;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
let pt = PascalsTriangle::new({{ test.input.count }});
let expected: Vec<Vec<u32>> = vec![{% for row in test.expected -%}
vec!{{ row | json_encode() }},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use perfect_numbers::*;
{% for test in test_group.cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
let input = {{ test.input.number | fmt_num }};
let output = classify(input);
{%- if test.expected is object %}
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 @@ -3,7 +3,7 @@ use phone_number::*;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
let input = {{ test.input.phrase | json_encode() }};
let output = number(input);
{%- if test.expected is object %}
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/pig-latin/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use pig_latin::*;
{% for test in test_group.cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
let input = {{ test.input.phrase | json_encode() }};
let output = translate(input);
let expected = {{ test.expected | json_encode() }};
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/poker/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::HashSet;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
let input = &{{ test.input.hands | json_encode() }};
let output = winning_hands(input).into_iter().collect::<HashSet<_>>();
let expected = {{ test.expected | json_encode() }}.into_iter().collect::<HashSet<_>>();
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/prime-factors/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use prime_factors::*;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
let factors = factors({{ test.input.value | fmt_num }});
let expected = [{% for factor in test.expected -%}
{{ factor | fmt_num }},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use protein_translation::*;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
assert_eq!(
translate({{ test.input.strand | json_encode() }}),
{% if test.expected is object %}
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/proverb/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use proverb::*;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
let input = &{{ test.input.strings | json_encode() }};
let output = build_proverb(input);
{% if test.expected | length == 0 -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::HashSet;
{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
let input = {{ test.input.n | fmt_num }};
let output = find(input);
let expected = [{% for triple in test.expected -%}
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 @@ -4,7 +4,7 @@ use queen_attack::*;
{% for test in test_group.cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
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 %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rail_fence_cipher::RailFence;
{% for test in test_group.cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
fn {{ test.description | make_ident }}() {
let input = {{ test.input.msg | json_encode() }};
let rails = {{ test.input.rails | json_encode() }};
let rail_fence = RailFence::new(rails);
Expand Down
Loading