Add `egui_kittest::Harness::set_options` (#7638)
Makes it easier to set the same options for many tests --------- Co-authored-by: Lucas Meurer <hi@lucasmerlin.me>
This commit is contained in:
parent
e3e7c9758e
commit
9f5b797375
|
|
@ -14,6 +14,9 @@ pub struct HarnessBuilder<State = ()> {
|
||||||
pub(crate) state: PhantomData<State>,
|
pub(crate) state: PhantomData<State>,
|
||||||
pub(crate) renderer: Box<dyn TestRenderer>,
|
pub(crate) renderer: Box<dyn TestRenderer>,
|
||||||
pub(crate) wait_for_pending_images: bool,
|
pub(crate) wait_for_pending_images: bool,
|
||||||
|
|
||||||
|
#[cfg(feature = "snapshot")]
|
||||||
|
pub(crate) default_snapshot_options: crate::SnapshotOptions,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<State> Default for HarnessBuilder<State> {
|
impl<State> Default for HarnessBuilder<State> {
|
||||||
|
|
@ -28,6 +31,9 @@ impl<State> Default for HarnessBuilder<State> {
|
||||||
step_dt: 1.0 / 4.0,
|
step_dt: 1.0 / 4.0,
|
||||||
wait_for_pending_images: true,
|
wait_for_pending_images: true,
|
||||||
os: egui::os::OperatingSystem::Nix,
|
os: egui::os::OperatingSystem::Nix,
|
||||||
|
|
||||||
|
#[cfg(feature = "snapshot")]
|
||||||
|
default_snapshot_options: crate::SnapshotOptions::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -56,6 +62,12 @@ impl<State> HarnessBuilder<State> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set the default options used for snapshot tests on this harness.
|
||||||
|
#[cfg(feature = "snapshot")]
|
||||||
|
pub fn with_options(&mut self, options: crate::SnapshotOptions) {
|
||||||
|
self.default_snapshot_options = options;
|
||||||
|
}
|
||||||
|
|
||||||
/// Override the [`egui::os::OperatingSystem`] reported to egui.
|
/// Override the [`egui::os::OperatingSystem`] reported to egui.
|
||||||
///
|
///
|
||||||
/// This affects e.g. the way shortcuts are displayed. So for snapshot tests,
|
/// This affects e.g. the way shortcuts are displayed. So for snapshot tests,
|
||||||
|
|
|
||||||
|
|
@ -75,6 +75,9 @@ pub struct Harness<'a, State = ()> {
|
||||||
step_dt: f32,
|
step_dt: f32,
|
||||||
wait_for_pending_images: bool,
|
wait_for_pending_images: bool,
|
||||||
queued_events: EventQueue,
|
queued_events: EventQueue,
|
||||||
|
|
||||||
|
#[cfg(feature = "snapshot")]
|
||||||
|
default_snapshot_options: SnapshotOptions,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<State> Debug for Harness<'_, State> {
|
impl<State> Debug for Harness<'_, State> {
|
||||||
|
|
@ -100,6 +103,9 @@ impl<'a, State> Harness<'a, State> {
|
||||||
state: _,
|
state: _,
|
||||||
mut renderer,
|
mut renderer,
|
||||||
wait_for_pending_images,
|
wait_for_pending_images,
|
||||||
|
|
||||||
|
#[cfg(feature = "snapshot")]
|
||||||
|
default_snapshot_options,
|
||||||
} = builder;
|
} = builder;
|
||||||
let ctx = ctx.unwrap_or_default();
|
let ctx = ctx.unwrap_or_default();
|
||||||
ctx.set_theme(theme);
|
ctx.set_theme(theme);
|
||||||
|
|
@ -147,6 +153,9 @@ impl<'a, State> Harness<'a, State> {
|
||||||
step_dt,
|
step_dt,
|
||||||
wait_for_pending_images,
|
wait_for_pending_images,
|
||||||
queued_events: Default::default(),
|
queued_events: Default::default(),
|
||||||
|
|
||||||
|
#[cfg(feature = "snapshot")]
|
||||||
|
default_snapshot_options,
|
||||||
};
|
};
|
||||||
// Run the harness until it is stable, ensuring that all Areas are shown and animations are done
|
// Run the harness until it is stable, ensuring that all Areas are shown and animations are done
|
||||||
harness.run_ok();
|
harness.run_ok();
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ use std::path::PathBuf;
|
||||||
pub type SnapshotResult = Result<(), SnapshotError>;
|
pub type SnapshotResult = Result<(), SnapshotError>;
|
||||||
|
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
pub struct SnapshotOptions {
|
pub struct SnapshotOptions {
|
||||||
/// The threshold for the image comparison.
|
/// The threshold for the image comparison.
|
||||||
/// The default is `0.6` (which is enough for most egui tests to pass across different
|
/// The default is `0.6` (which is enough for most egui tests to pass across different
|
||||||
|
|
@ -556,9 +557,17 @@ pub fn image_snapshot(current: &image::RgbaImage, name: impl Into<String>) {
|
||||||
|
|
||||||
#[cfg(any(feature = "wgpu", feature = "snapshot"))]
|
#[cfg(any(feature = "wgpu", feature = "snapshot"))]
|
||||||
impl<State> Harness<'_, State> {
|
impl<State> Harness<'_, State> {
|
||||||
|
/// The default options used for snapshot tests.
|
||||||
|
/// set by [`crate::HarnessBuilder::with_options`].
|
||||||
|
pub fn options(&self) -> &SnapshotOptions {
|
||||||
|
&self.default_snapshot_options
|
||||||
|
}
|
||||||
|
|
||||||
/// Render an image using the setup [`crate::TestRenderer`] and compare it to the snapshot
|
/// Render an image using the setup [`crate::TestRenderer`] and compare it to the snapshot
|
||||||
/// with custom options.
|
/// with custom options.
|
||||||
///
|
///
|
||||||
|
/// These options will override the ones set by [`crate::HarnessBuilder::with_options`].
|
||||||
|
///
|
||||||
/// If you want to change the default options for your whole project, you could create an
|
/// If you want to change the default options for your whole project, you could create an
|
||||||
/// [extension trait](http://xion.io/post/code/rust-extension-traits.html) to create a
|
/// [extension trait](http://xion.io/post/code/rust-extension-traits.html) to create a
|
||||||
/// new `my_image_snapshot` function on the Harness that calls this function with the desired options.
|
/// new `my_image_snapshot` function on the Harness that calls this function with the desired options.
|
||||||
|
|
@ -586,6 +595,9 @@ impl<State> Harness<'_, State> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Render an image using the setup [`crate::TestRenderer`] and compare it to the snapshot.
|
/// Render an image using the setup [`crate::TestRenderer`] and compare it to the snapshot.
|
||||||
|
///
|
||||||
|
/// This is like [`Self::try_snapshot_options`] but will use the options set by [`crate::HarnessBuilder::with_options`].
|
||||||
|
///
|
||||||
/// The snapshot will be saved under `tests/snapshots/{name}.png`.
|
/// The snapshot will be saved under `tests/snapshots/{name}.png`.
|
||||||
/// The new image from the last test run will be saved under `tests/snapshots/{name}.new.png`.
|
/// The new image from the last test run will be saved under `tests/snapshots/{name}.new.png`.
|
||||||
/// If the new image didn't match the snapshot, a diff image will be saved under `tests/snapshots/{name}.diff.png`.
|
/// If the new image didn't match the snapshot, a diff image will be saved under `tests/snapshots/{name}.diff.png`.
|
||||||
|
|
@ -597,12 +609,14 @@ impl<State> Harness<'_, State> {
|
||||||
let image = self
|
let image = self
|
||||||
.render()
|
.render()
|
||||||
.map_err(|err| SnapshotError::RenderError { err })?;
|
.map_err(|err| SnapshotError::RenderError { err })?;
|
||||||
try_image_snapshot(&image, name)
|
try_image_snapshot_options(&image, name.into(), &self.default_snapshot_options)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Render an image using the setup [`crate::TestRenderer`] and compare it to the snapshot
|
/// Render an image using the setup [`crate::TestRenderer`] and compare it to the snapshot
|
||||||
/// with custom options.
|
/// with custom options.
|
||||||
///
|
///
|
||||||
|
/// These options will override the ones set by [`crate::HarnessBuilder::with_options`].
|
||||||
|
///
|
||||||
/// If you want to change the default options for your whole project, you could create an
|
/// If you want to change the default options for your whole project, you could create an
|
||||||
/// [extension trait](http://xion.io/post/code/rust-extension-traits.html) to create a
|
/// [extension trait](http://xion.io/post/code/rust-extension-traits.html) to create a
|
||||||
/// new `my_image_snapshot` function on the Harness that calls this function with the desired options.
|
/// new `my_image_snapshot` function on the Harness that calls this function with the desired options.
|
||||||
|
|
@ -629,6 +643,9 @@ impl<State> Harness<'_, State> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Render an image using the setup [`crate::TestRenderer`] and compare it to the snapshot.
|
/// Render an image using the setup [`crate::TestRenderer`] and compare it to the snapshot.
|
||||||
|
///
|
||||||
|
/// This is like [`Self::snapshot_options`] but will use the options set by [`crate::HarnessBuilder::with_options`].
|
||||||
|
///
|
||||||
/// The snapshot will be saved under `tests/snapshots/{name}.png`.
|
/// The snapshot will be saved under `tests/snapshots/{name}.png`.
|
||||||
/// The new image from the last test run will be saved under `tests/snapshots/{name}.new.png`.
|
/// The new image from the last test run will be saved under `tests/snapshots/{name}.new.png`.
|
||||||
/// If the new image didn't match the snapshot, a diff image will be saved under `tests/snapshots/{name}.diff.png`.
|
/// If the new image didn't match the snapshot, a diff image will be saved under `tests/snapshots/{name}.diff.png`.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue