From 5bbedefa8816074b23cc75a64fb3f4741e6ca2a2 Mon Sep 17 00:00:00 2001 From: Romeo Disca Date: Mon, 30 Oct 2023 15:21:40 +0100 Subject: [PATCH] refactor: test generation --- build.rs | 18 ++++++++---- build_templates/run_test.rs | 23 --------------- build_templates/test_fn.rs | 5 ++++ build_templates/test_fn_experimental.rs | 7 ----- build_templates/test_fn_stable.rs | 6 ---- build_templates/tests_mod.rs | 38 +++++++++++++++++++++++-- 6 files changed, 53 insertions(+), 44 deletions(-) create mode 100644 build_templates/test_fn.rs delete mode 100644 build_templates/test_fn_experimental.rs delete mode 100644 build_templates/test_fn_stable.rs diff --git a/build.rs b/build.rs index cf2e600a..e5cfe3d6 100644 --- a/build.rs +++ b/build.rs @@ -45,15 +45,23 @@ fn get_run_test() -> String { #[cfg(not(feature = "skip-test-generation"))] fn get_test_fn_template(dir_name: &str, test_type: TestType) -> String { let test_name = dir_name.replace('-', "_"); - let mut test_fn_template: String = match test_type { - TestType::Stable => include_str!("build_templates/test_fn_stable.rs").to_string(), - TestType::Experimental => { - include_str!("build_templates/test_fn_experimental.rs").to_string() - } + let mut test_fn_template: String = if let TestType::Experimental = test_type { + " #[cfg_attr(not(feature = \"experimental-testset\"), ignore)]\n".to_string() + } else { + String::new() }; + test_fn_template.push_str(include_str!("build_templates/test_fn.rs")); test_fn_template = test_fn_template.replace("TEST_NAME", &test_name); test_fn_template = test_fn_template.replace("DIR_NAME", dir_name); + test_fn_template = test_fn_template.replace( + "FOLDER_NAME", + if let TestType::Experimental = test_type { + "experimental" + } else { + "stable" + }, + ); test_fn_template } diff --git a/build_templates/run_test.rs b/build_templates/run_test.rs index 1cb077f3..3d5a2cd8 100644 --- a/build_templates/run_test.rs +++ b/build_templates/run_test.rs @@ -4,29 +4,6 @@ extern crate scheduler; extern crate soft; mod common; -use scheduler::models::{input::Input, output::FinalTasks}; -use std::path::Path; - /// AUTO-GENERATED FILE. Do not change. /// Will be overwritten on build. Edit the file in build_templates or change test generation in build.rs -/// Function to run tests -fn run_test(directory: &str) -> (String, String) { - let input_path_str = format!("./tests/jsons/{}/input.json", directory); - let output_path_str = format!("./tests/jsons/{}/expected.json", directory); - let actual_output_path_str = format!("./tests/jsons/{}/observed.json", directory); - - let input_path = Path::new(&input_path_str[..]); - let output_path = Path::new(&output_path_str[..]); - let actual_output_path = Path::new(&actual_output_path_str[..]); - - let input: Input = common::get_input_from_json(input_path).unwrap(); - let desired_output: String = common::get_output_string_from_json(output_path).unwrap(); - - let output: FinalTasks = scheduler::run_scheduler(input); - let actual_output = serde_json::to_string_pretty(&output).unwrap(); - - common::write_to_file(actual_output_path, &actual_output).unwrap(); - - (actual_output, desired_output) -} diff --git a/build_templates/test_fn.rs b/build_templates/test_fn.rs new file mode 100644 index 00000000..7dd9ca89 --- /dev/null +++ b/build_templates/test_fn.rs @@ -0,0 +1,5 @@ + #[test] + fn TEST_NAME() { + test("FOLDER_NAME/DIR_NAME"); + } + diff --git a/build_templates/test_fn_experimental.rs b/build_templates/test_fn_experimental.rs deleted file mode 100644 index 2d067585..00000000 --- a/build_templates/test_fn_experimental.rs +++ /dev/null @@ -1,7 +0,0 @@ - - #[test] - #[cfg_attr(not(feature = "experimental-testset"), ignore)] - fn TEST_NAME() { - let (actual_output, desired_output) = run_test("experimental/DIR_NAME"); - assert_eq!(actual_output, desired_output); - } diff --git a/build_templates/test_fn_stable.rs b/build_templates/test_fn_stable.rs deleted file mode 100644 index a43f9ee4..00000000 --- a/build_templates/test_fn_stable.rs +++ /dev/null @@ -1,6 +0,0 @@ - - #[test] - fn TEST_NAME() { - let (actual_output, desired_output) = run_test("stable/DIR_NAME"); - assert_eq!(actual_output, desired_output); - } diff --git a/build_templates/tests_mod.rs b/build_templates/tests_mod.rs index 480ff21d..970f69c2 100644 --- a/build_templates/tests_mod.rs +++ b/build_templates/tests_mod.rs @@ -1,7 +1,39 @@ #[cfg(test)] mod TEST_MODULE_NAME { - use crate::run_test; - //TEST_FUNCTIONS_STABLE - //TEST_FUNCTIONS_EXPERIMENTAL + // stable tests +//TEST_FUNCTIONS_STABLE + + // experimental tests +//TEST_FUNCTIONS_EXPERIMENTAL + + use scheduler::models::{input::Input, output::FinalTasks}; + use std::path::Path; + use crate::common; + + fn test(folder: &str) { + let (actual_output, desired_output) = generate_outputs(folder); + assert_eq!(actual_output, desired_output); + } + + /// Function to generate outputs + fn generate_outputs(directory: &str) -> (String, String) { + let input_path_str = format!("./tests/jsons/{}/input.json", directory); + let output_path_str = format!("./tests/jsons/{}/expected.json", directory); + let actual_output_path_str = format!("./tests/jsons/{}/observed.json", directory); + + let input_path = Path::new(&input_path_str[..]); + let output_path = Path::new(&output_path_str[..]); + let actual_output_path = Path::new(&actual_output_path_str[..]); + + let input: Input = common::get_input_from_json(input_path).unwrap(); + let desired_output: String = common::get_output_string_from_json(output_path).unwrap(); + + let output: FinalTasks = scheduler::run_scheduler(input); + let actual_output = serde_json::to_string_pretty(&output).unwrap(); + + common::write_to_file(actual_output_path, &actual_output).unwrap(); + + (actual_output, desired_output) + } }