Skip to content

Commit

Permalink
Enable generating track-specific test cases (#1774)
Browse files Browse the repository at this point in the history
  • Loading branch information
senekor authored Nov 14, 2023
1 parent 22c94f6 commit cbd1748
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
3 changes: 3 additions & 0 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ The tests are generated using the template engine [Tera].
The input of the template is the canonical data from [`problem-specifications`].
if you want to exclude certain tests from being generated,
you have to set `include = false` in `.meta/tests.toml`.
Please include a comment about the reason for excluding it.
If you want to add additional track-specific tests, you can do so in a file `.meta/additional-tests.json`.
Only do this with a good reason for not upstreaming these tests.

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

Expand Down
8 changes: 6 additions & 2 deletions rust-tooling/src/exercise_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tera::Context;

use crate::{
exercise_config::{get_excluded_tests, get_test_emplate},
problem_spec::{get_canonical_data, SingleTestCase, TestCase},
problem_spec::{get_additional_test_cases, get_canonical_data, SingleTestCase, TestCase},
};

pub struct GeneratedExercise {
Expand Down Expand Up @@ -95,7 +95,11 @@ fn to_hex(value: &tera::Value, _args: &HashMap<String, tera::Value>) -> tera::Re
}

fn generate_tests(slug: &str, fn_names: Vec<String>) -> String {
let cases = get_canonical_data(slug).cases;
let cases = {
let mut cases = get_canonical_data(slug).cases;
cases.extend_from_slice(&get_additional_test_cases(slug));
cases
};
let excluded_tests = get_excluded_tests(slug);
let mut template = get_test_emplate(slug).unwrap();
if template.get_template_names().next().is_none() {
Expand Down
19 changes: 19 additions & 0 deletions rust-tooling/src/problem_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ pub fn get_canonical_data(slug: &str) -> CanonicalData {
})
}

/// The Rust track may define additional test cases in
/// `.meta/additional-tests.json`, which are not appropriate
/// to upstream for all other languages to implement.
pub fn get_additional_test_cases(slug: &str) -> Vec<TestCase> {
let path = std::path::PathBuf::from("exercises/practice")
.join(slug)
.join(".meta/additional-tests.json");
if !path.exists() {
return Vec::new();
}
let contents = std::fs::read_to_string(&path).unwrap();
serde_json::from_str(contents.as_str()).unwrap_or_else(|e| {
panic!(
"should deserialize additional tests for {}: {e}",
path.display()
)
})
}

#[test]
fn deserialize_canonical_data() {
crate::fs_utils::cd_into_repo_root();
Expand Down

0 comments on commit cbd1748

Please sign in to comment.