Skip to content

Commit

Permalink
Add test to ensure tera templates are in sync
Browse files Browse the repository at this point in the history
  • Loading branch information
senekor committed Mar 27, 2024
1 parent d8b7fb2 commit 13794a4
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 26 deletions.
10 changes: 10 additions & 0 deletions exercises/practice/knapsack/tests/knapsack.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
use knapsack::*;

#[test]
fn test_no_items() {
let max_weight = 100;
let items = [];
let output = maximum_value(max_weight, &items);
let expected = 0;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn test_one_item_too_heavy() {
let max_weight = 10;
let items = [Item {
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
@@ -1,4 +1,4 @@
use queen_attack::{ChessPiece, ChessPosition, Queen};
use queen_attack::*;

{% for test in cases %}
#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use variable_length_quantity as vlq;
fn {{ test.description | slugify | replace(from="-", to="_") }}() {
{%- if test.property == "encode" %}
let input = &{{ test.input.integers | json_encode() }};
let output = vlq::{{ fn_names[0] }}(input);
let output = vlq::to_bytes(input);
let expected = vec![
{%- for byte in test.expected -%}
0x{{ byte | to_hex }},
Expand All @@ -19,7 +19,7 @@ fn {{ test.description | slugify | replace(from="-", to="_") }}() {
0x{{ byte | to_hex }},
{%- endfor -%}
];
let output = vlq::{{ fn_names[1] }}(input);
let output = vlq::from_bytes(input);
let expected = {% if test.expected is object -%}
Err(vlq::Error::IncompleteNumber)
{%- else -%}
Expand Down
28 changes: 28 additions & 0 deletions exercises/practice/wordy/.meta/additional-tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[
{
"uuid": "88bf4b28-0de3-4883-93c7-db1b14aa806e",
"description": "exponential",
"comments": [
"This test case was added a long time ago.",
"Upstreaming it would make the exercise more difficult."
],
"property": "exponentials",
"input": {
"question": "What is 2 raised to the 5th power?"
},
"expected": 32
},
{
"uuid": "bb8c655c-cf42-4dfc-90e0-152fcfd8d4e0",
"description": "addition and exponential",
"comments": [
"This test case was added a long time ago.",
"Upstreaming it would make the exercise more difficult."
],
"property": "exponentials",
"input": {
"question": "What is 1 plus 2 raised to the 2nd power?"
},
"expected": 9
}
]
5 changes: 4 additions & 1 deletion exercises/practice/wordy/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
{% if loop.index != 1 -%}
#[ignore]
{% endif -%}
{% if test.property == "exponentials" -%}
#[cfg(feature = "exponentials")]
{% endif -%}
fn {{ test.description | slugify | replace(from="-", to="_") }}() {
let input = {{ test.input | json_encode() }};
let input = {{ test.input.question | json_encode() }};
let output = {{ crate_name }}::{{ fn_names[0] }}(input);
let expected = {% if test.expected is object -%}
None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,22 @@ It probably generates invalid Rust code."
pub fn get_test_template(slug: &str) -> Option<Tera> {
Some(Tera::new(format!("exercises/practice/{slug}/.meta/*.tera").as_str()).unwrap())
}

pub fn read_fn_names_from_lib_rs(slug: &str) -> Vec<String> {
let lib_rs =
std::fs::read_to_string(format!("exercises/practice/{}/src/lib.rs", slug)).unwrap();

lib_rs
.split("fn ")
.skip(1)
.map(|f| {
let tmp = f.split_once('(').unwrap().0;
// strip generics
if let Some((res, _)) = tmp.split_once('<') {
res.to_string()
} else {
tmp.to_string()
}
})
.collect()
}
24 changes: 2 additions & 22 deletions rust-tooling/generate/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use cli::{AddArgs, FullAddArgs, UpdateArgs};
use models::track_config::{self, TRACK_CONFIG};

mod cli;
mod exercise_generation;

fn main() {
utils::fs::cd_into_repo_root();
Expand Down Expand Up @@ -79,12 +78,12 @@ fn make_configlet_generate_what_it_can(slug: &str) {

fn generate_exercise_files(slug: &str, is_update: bool) {
let fn_names = if is_update {
read_fn_names_from_lib_rs(slug)
generate::read_fn_names_from_lib_rs(slug)
} else {
vec!["TODO".to_string()]
};

let exercise = exercise_generation::new(slug, fn_names);
let exercise = generate::new(slug, fn_names);

let exercise_path = PathBuf::from("exercises/practice").join(slug);

Expand All @@ -108,22 +107,3 @@ fn generate_exercise_files(slug: &str, is_update: bool) {
)
.unwrap();
}

fn read_fn_names_from_lib_rs(slug: &str) -> Vec<String> {
let lib_rs =
std::fs::read_to_string(format!("exercises/practice/{}/src/lib.rs", slug)).unwrap();

lib_rs
.split("fn ")
.skip(1)
.map(|f| {
let tmp = f.split_once('(').unwrap().0;
// strip generics
if let Some((res, _)) = tmp.split_once('<') {
res.to_string()
} else {
tmp.to_string()
}
})
.collect()
}
23 changes: 23 additions & 0 deletions rust-tooling/generate/tests/tera_templates_are_in_sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//! This test is not run in CI. Doing so would slow down CI noticeably, because
//! exercise generation depends on `tera`, which takes a while to compile.

use glob::glob;
use utils::fs::cd_into_repo_root;

#[test]
fn tera_templates_are_in_sync() {
cd_into_repo_root();
for entry in glob("exercises/*/*/.meta/test_template.tera").unwrap() {
let path = entry.unwrap();
let exercise_dir = path.parent().unwrap().parent().unwrap();
let slug = exercise_dir.file_name().unwrap().to_string_lossy();

let fn_names = generate::read_fn_names_from_lib_rs(&slug);
let generated = generate::new(&slug, fn_names);

let test_path = exercise_dir.join("tests").join(format!("{slug}.rs"));
let on_disk = std::fs::read_to_string(test_path).unwrap();

assert_eq!(generated.tests, on_disk);
}
}

0 comments on commit 13794a4

Please sign in to comment.