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

refactor: test generation #400

Merged
merged 1 commit into from
Nov 1, 2023
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
18 changes: 13 additions & 5 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
23 changes: 0 additions & 23 deletions build_templates/run_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
5 changes: 5 additions & 0 deletions build_templates/test_fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[test]
fn TEST_NAME() {
test("FOLDER_NAME/DIR_NAME");
}

7 changes: 0 additions & 7 deletions build_templates/test_fn_experimental.rs

This file was deleted.

6 changes: 0 additions & 6 deletions build_templates/test_fn_stable.rs

This file was deleted.

38 changes: 35 additions & 3 deletions build_templates/tests_mod.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}