8000 egui_kittest: more ergonomic functions taking `Impl Into<String>` by emilk · Pull Request #7307 · emilk/egui · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

egui_kittest: more ergonomic functions taking Impl Into<String> #7307

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

Merged
merged 2 commits into from
Jul 7, 2025
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
2 changes: 1 addition & 1 deletion crates/egui_demo_app/tests/test_demo_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ fn test_demo_app() {
// Can't use Harness::run because fractal clock keeps requesting repaints
harness.run_steps(4);

results.add(harness.try_snapshot(&anchor.to_string()));
results.add(harness.try_snapshot(anchor.to_string()));
}
}
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/demo_app_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ mod tests {
options = options.threshold(OsThreshold::new(0.0).linux(2.1));
}

results.add(harness.try_snapshot_options(&format!("demos/{name}"), &options));
results.add(harness.try_snapshot_options(format!("demos/{name}"), &options));
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/demo/tests/tessellation_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ mod tests {
harness.fit_contents();
harness.run();

harness.snapshot(&format!("tessellation_test/{name}"));
harness.snapshot(format!("tessellation_test/{name}"));
}
}
}
2 changes: 1 addition & 1 deletion crates/egui_demo_lib/src/rendering_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ mod tests {

harness.fit_contents();

results.add(harness.try_snapshot(&format!("rendering_test/dpi_{dpi:.2}")));
results.add(harness.try_snapshot(format!("rendering_test/dpi_{dpi:.2}")));
}
}
}
42 changes: 27 additions & 15 deletions crates/egui_kittest/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,15 @@ fn should_update_snapshots() -> bool {
/// reading or writing the snapshot.
pub fn try_image_snapshot_options(
new: &image::RgbaImage,
name: &str,
name: impl Into<String>,
options: &SnapshotOptions,
) -> SnapshotResult {
try_image_snapshot_options_impl(new, name.into(), options)
}

fn try_image_snapshot_options_impl(
new: &image::RgbaImage,
name: String,
options: &SnapshotOptions,
) -> SnapshotResult {
let SnapshotOptions {
Expand All @@ -319,7 +327,7 @@ pub fn try_image_snapshot_options(
failed_pixel_count_threshold,
} = options;

let parent_path = if let Some(parent) = PathBuf::from(name).parent() {
let parent_path = if let Some(parent) = PathBuf::from(&name).parent() {
output_path.join(parent)
} else {
output_path.clone()
Expand Down Expand Up @@ -385,7 +393,7 @@ pub fn try_image_snapshot_options(
return update_snapshot();
} else {
return Err(SnapshotError::SizeMismatch {
name: name.to_owned(),
name,
expected: previous.dimensions(),
actual: new.dimensions(),
});
Expand All @@ -412,7 +420,7 @@ pub fn try_image_snapshot_options(
}

Err(SnapshotError::Diff {
name: name.to_owned(),
name,
diff: num_wrong_pixels,
diff_path,
})
Expand All @@ -435,7 +443,7 @@ pub fn try_image_snapshot_options(
/// # Errors
/// Returns a [`SnapshotError`] if the image does not match the snapshot or if there was an error
/// reading or writing the snapshot.
pub fn try_image_snapshot(current: &image::RgbaImage, name: &str) -> SnapshotResult {
pub fn try_image_snapshot(current: &image::RgbaImage, name: impl Into<String>) -> SnapshotResult {
try_image_snapshot_options(current, name, &SnapshotOptions::default())
}

Expand All @@ -456,7 +464,11 @@ pub fn try_image_snapshot(current: &image::RgbaImage, name: &str) -> SnapshotRes
/// Panics if the image does not match the snapshot or if there was an error reading or writing the
/// snapshot.
#[track_caller]
pub fn image_snapshot_options(current: &image::RgbaImage, name: &str, options: &SnapshotOptions) {
pub fn image_snapshot_options(
current: &image::RgbaImage,
name: impl Into<String>,
options: &SnapshotOptions,
) {
match try_image_snapshot_options(current, name, options) {
Ok(_) => {}
Err(err) => {
Expand All @@ -475,7 +487,7 @@ pub fn image_snapshot_options(current: &image::RgbaImage, name: &str, options: &
/// Panics if the image does not match the snapshot or if there was an error reading or writing the
/// snapshot.
#[track_caller]
pub fn image_snapshot(current: &image::RgbaImage, name: &str) {
pub fn image_snapshot(current: &image::RgbaImage, name: impl Into<String>) {
match try_image_snapshot(current, name) {
Ok(_) => {}
Err(err) => {
Expand Down Expand Up @@ -506,13 +518,13 @@ impl<State> Harness<'_, State> {
/// error reading or writing the snapshot, if the rendering fails or if no default renderer is available.
pub fn try_snapshot_options(
&mut self,
name: &str,
name: impl Into<String>,
options: &SnapshotOptions,
) -> SnapshotResult {
let image = self
.render()
.map_err(|err| SnapshotError::RenderError { err })?;
try_image_snapshot_options(&image, name, options)
try_image_snapshot_options(&image, name.into(), options)
}

/// Render an image using the setup [`crate::TestRenderer`] and compare it to the snapshot.
Expand All @@ -523,7 +535,7 @@ impl<State> Harness<'_, State> {
/// # Errors
/// Returns a [`SnapshotError`] if the image does not match the snapshot, if there was an
/// error reading or writing the snapshot, if the rendering fails or if no default renderer is available.
pub fn try_snapshot(&mut self, name: &str) -> SnapshotResult {
pub fn try_snapshot(&mut self, name: impl Into<String>) -> SnapshotResult {
let image = self
.render()
.map_err(|err| SnapshotError::RenderError { err })?;
Expand All @@ -549,7 +561,7 @@ impl<State> Harness<'_, State> {
/// Panics if the image does not match the snapshot, if there was an error reading or writing the
/// snapshot, if the rendering fails or if no default renderer is available.
#[track_caller]
pub fn snapshot_options(&mut self, name: &str, options: &SnapshotOptions) {
pub fn snapshot_options(&mut self, name: impl Into<String>, options: &SnapshotOptions) {
match self.try_snapshot_options(name, options) {
Ok(_) => {}
Err(err) => {
Expand All @@ -567,7 +579,7 @@ impl<State> Harness<'_, State> {
/// Panics if the image does not match the snapshot, if there was an error reading or writing the
/// snapshot, if the rendering fails or if no default renderer is available.
#[track_caller]
pub fn snapshot(&mut self, name: &str) {
pub fn snapshot(&mut self, name: impl Into<String>) {
match self.try_snapshot(name) {
Ok(_) => {}
Err(err) => {
Expand All @@ -588,7 +600,7 @@ impl<State> Harness<'_, State> {
)]
pub fn try_wgpu_snapshot_options(
&mut self,
name: &str,
name: impl Into<String>,
options: &SnapshotOptions,
) -> SnapshotResult {
self.try_snapshot_options(name, options)
Expand All @@ -598,15 +610,15 @@ impl<State> Harness<'_, State> {
since = "0.31.0",
note = "Use `try_snapshot` instead. This function will be removed in 0.32"
)]
pub fn try_wgpu_snapshot(&mut self, name: &str) -> SnapshotResult {
pub fn try_wgpu_snapshot(&mut self, name: impl Into<String>) - 9E7A > SnapshotResult {
self.try_snapshot(name)
}

#[deprecated(
since = "0.31.0",
note = "Use `snapshot_options` instead. This function will be removed in 0.32"
)]
pub fn wgpu_snapshot_options(&mut self, name: &str, options: &SnapshotOptions) {
pub fn wgpu_snapshot_options(&mut self, name: impl Into<String>, options: &SnapshotOptions) {
self.snapshot_options(name, options);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/egui_tests/tests/test_sides.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ fn test_variants(
harness.fit_contents();
}

results.add(harness.try_snapshot(&format!("sides/{name}_{variant_name}")));
results.add(harness.try_snapshot(format!("sides/{name}_{variant_name}")));
}
}
4 changes: 2 additions & 2 deletions tests/egui_tests/tests/test_widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ fn test_widget_layout(name: &str, mut w: impl FnMut(&mut Ui) -> Response) -> Sna
});

harness.fit_contents();
harness.try_snapshot(&format!("layout/{name}"))
harness.try_snapshot(format!("layout/{name}"))
}

/// Utility to create a snapshot test of the different states of a egui widget.
Expand Down Expand Up @@ -370,7 +370,7 @@ impl<'a> VisualTests<'a> {

harness.fit_contents();

harness.try_snapshot(&format!("visuals/{}", self.name))
harness.try_snapshot(format!("visuals/{}", self.name))
}
}

Expand Down
Loading
0