From 83af3e6b4eb1dc72da9cdfb68100a230e8558acf Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Fri, 27 Jan 2023 17:33:38 +0100 Subject: [PATCH] tests: Extract Test struct that contains all the info for a test --- tests/tests/regression_tests.rs | 51 +++++++++++++++---------------- tests/tests/util/mod.rs | 5 +++ tests/tests/{ => util}/options.rs | 0 tests/tests/util/test.rs | 35 +++++++++++++++++++++ 4 files changed, 64 insertions(+), 27 deletions(-) create mode 100644 tests/tests/util/mod.rs rename tests/tests/{ => util}/options.rs (100%) create mode 100644 tests/tests/util/test.rs diff --git a/tests/tests/regression_tests.rs b/tests/tests/regression_tests.rs index 6380afe25..5faad8775 100644 --- a/tests/tests/regression_tests.rs +++ b/tests/tests/regression_tests.rs @@ -17,8 +17,8 @@ use ruffle_core::tag_utils::SwfMovie; use ruffle_core::{Player, PlayerBuilder, PlayerEvent}; use ruffle_input_format::{AutomatedEvent, InputInjector, MouseButton as InputMouseButton}; +use anyhow::Context; use libtest_mimic::{Arguments, Trial}; -use options::TestOptions; #[cfg(feature = "imgtests")] use ruffle_render_wgpu::backend::WgpuRenderBackend; #[cfg(feature = "imgtests")] @@ -29,8 +29,9 @@ use std::path::Path; use std::rc::Rc; use std::sync::{Arc, Mutex}; use std::time::Duration; +use util::test::Test; -mod options; +mod util; const RUN_IMG_TESTS: bool = cfg!(feature = "imgtests"); @@ -657,35 +658,35 @@ impl ExternalInterfaceProvider for ExternalInterfaceTestProvider { } } -fn run_test(options: TestOptions, root: &Path) -> Result<(), libtest_mimic::Failed> { +fn run_test(test: Test) -> Result<(), libtest_mimic::Failed> { set_logger(); - if let Some(approximations) = &options.approximations { + if let Some(approximations) = &test.options.approximations { test_swf_approx( - root.join("test.swf").to_str().unwrap(), - options.num_frames, - root.join("input.json").to_str().unwrap(), - root.join("output.txt").to_str().unwrap(), + test.swf_path.to_str().unwrap(), + test.options.num_frames, + test.input_path.to_str().unwrap(), + test.output_path.to_str().unwrap(), &approximations.number_patterns(), - options.image, + test.options.image, |actual, expected| approximations.compare(actual, expected), ) .map_err(|e| e.to_string().into()) } else { test_swf_with_hooks( - root.join("test.swf").to_str().unwrap(), - options.num_frames, - root.join("input.json").to_str().unwrap(), - root.join("output.txt").to_str().unwrap(), + test.swf_path.to_str().unwrap(), + test.options.num_frames, + test.input_path.to_str().unwrap(), + test.output_path.to_str().unwrap(), |player| { - if let Some(player_options) = &options.player_options { + if let Some(player_options) = &test.options.player_options { player_options.setup(player); } Ok(()) }, |_| Ok(()), - options.image, - options.sleep_to_meet_frame_rate, + test.options.image, + test.options.sleep_to_meet_frame_rate, ) .map_err(|e| e.to_string().into()) } @@ -700,19 +701,15 @@ fn main() { .map(Result::unwrap) .filter(|entry| entry.file_type().is_file() && entry.file_name() == "test.toml") .map(|file| { - let options = TestOptions::read(file.path()).unwrap(); - let test_dir = file.path().parent().unwrap().to_owned(); - let name = test_dir - .strip_prefix(root) - .unwrap() - .to_string_lossy() - .replace('\\', "/"); - let ignore = options.ignore || (options.image && !RUN_IMG_TESTS); - let mut test = Trial::test(name, move || run_test(options, &test_dir)); + let test = Test::from_options(file.path(), root) + .context("Couldn't create test") + .unwrap(); + let ignore = test.options.ignore || (test.options.image && !RUN_IMG_TESTS); + let mut trial = Trial::test(test.name.to_string(), move || run_test(test)); if ignore { - test = test.with_ignored_flag(true); + trial = trial.with_ignored_flag(true); } - test + trial }) .collect(); diff --git a/tests/tests/util/mod.rs b/tests/tests/util/mod.rs new file mode 100644 index 000000000..a1baa0792 --- /dev/null +++ b/tests/tests/util/mod.rs @@ -0,0 +1,5 @@ +// Despite being the older method of defining modules, this is required for test modules +// https://doc.rust-lang.org/book/ch11-03-test-organization.html + +pub mod options; +pub mod test; diff --git a/tests/tests/options.rs b/tests/tests/util/options.rs similarity index 100% rename from tests/tests/options.rs rename to tests/tests/util/options.rs diff --git a/tests/tests/util/test.rs b/tests/tests/util/test.rs new file mode 100644 index 000000000..69f2fea53 --- /dev/null +++ b/tests/tests/util/test.rs @@ -0,0 +1,35 @@ +use crate::util::options::TestOptions; +use anyhow::{Context, Result}; +use std::path::{Path, PathBuf}; + +pub struct Test { + pub options: TestOptions, + pub swf_path: PathBuf, + pub input_path: PathBuf, + pub output_path: PathBuf, + pub name: String, +} + +impl Test { + pub fn from_options(options_path: &Path, root_dir: &Path) -> Result { + let test_dir = options_path + .parent() + .context("Couldn't get test directory")?; + let options = TestOptions::read(options_path).context("Couldn't load test options")?; + let swf_path = test_dir.join("test.swf"); + let input_path = test_dir.join("input.json"); + let output_path = test_dir.join("output.txt"); + let name = test_dir + .strip_prefix(root_dir) + .context("Couldn't strip root prefix from test dir")? + .to_string_lossy() + .replace('\\', "/"); + Ok(Self { + options, + swf_path, + input_path, + output_path, + name, + }) + } +}