1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-01 22:30:03 -04:00

Add SnapshotResults struct to egui_kittest (#5672)

I got annoyed by all the slightly different variations of "collect
snapshot results and unwrap them at the end of test" I've written, so I
added a struct to make this nice and simple.

One controversial thing: It panics when dropped. I wanted to ensure
people cannot forget to unwrap the results at the end, and this was the
best thing I could come up with. I don't think this is possible via
clippy lint or something like that.

* [x] I have followed the instructions in the PR template
This commit is contained in:
lucasmerlin
2025-02-04 14:01:32 +01:00
committed by GitHub
parent 23ed49334e
commit b8051cc301
10 changed files with 135 additions and 53 deletions

View File

@@ -366,13 +366,13 @@ mod tests {
use crate::{demo::demo_app_windows::DemoGroups, Demo};
use egui::Vec2;
use egui_kittest::kittest::Queryable;
use egui_kittest::{Harness, SnapshotOptions};
use egui_kittest::{Harness, SnapshotOptions, SnapshotResults};
#[test]
fn demos_should_match_snapshot() {
let demos = DemoGroups::default().demos;
let mut errors = Vec::new();
let mut results = SnapshotResults::new();
for mut demo in demos.demos {
// Widget Gallery needs to be customized (to set a specific date) and has its own test
@@ -406,12 +406,7 @@ mod tests {
options.threshold = 2.1;
}
let result = harness.try_snapshot_options(&format!("demos/{name}"), &options);
if let Err(err) = result {
errors.push(err.to_string());
}
results.add(harness.try_snapshot_options(&format!("demos/{name}"), &options));
}
assert!(errors.is_empty(), "Errors: {errors:#?}");
}
}

View File

@@ -165,7 +165,7 @@ mod tests {
use egui::accesskit::Role;
use egui::Key;
use egui_kittest::kittest::Queryable;
use egui_kittest::Harness;
use egui_kittest::{Harness, SnapshotResults};
#[test]
fn clicking_escape_when_popup_open_should_not_close_modal() {
@@ -233,22 +233,18 @@ mod tests {
initial_state,
);
let mut results = Vec::new();
let mut results = SnapshotResults::new();
harness.run();
results.push(harness.try_snapshot("modals_1"));
results.add(harness.try_snapshot("modals_1"));
harness.get_by_label("Save").click();
harness.run_ok();
results.push(harness.try_snapshot("modals_2"));
results.add(harness.try_snapshot("modals_2"));
harness.get_by_label("Yes Please").click();
harness.run_ok();
results.push(harness.try_snapshot("modals_3"));
for result in results {
result.unwrap();
}
results.add(harness.try_snapshot("modals_3"));
}
// This tests whether the backdrop actually prevents interaction with lower layers.

View File

@@ -23,6 +23,7 @@ pub struct WidgetGallery {
#[cfg_attr(feature = "serde", serde(skip))]
date: Option<chrono::NaiveDate>,
#[cfg(feature = "chrono")]
with_date_button: bool,
}

View File

@@ -688,10 +688,11 @@ fn mul_color_gamma(left: Color32, right: Color32) -> Color32 {
mod tests {
use crate::ColorTest;
use egui_kittest::kittest::Queryable as _;
use egui_kittest::SnapshotResults;
#[test]
pub fn rendering_test() {
let mut errors = vec![];
let mut results = SnapshotResults::new();
for dpi in [1.0, 1.25, 1.5, 1.75, 1.6666667, 2.0] {
let mut color_test = ColorTest::default();
let mut harness = egui_kittest::Harness::builder()
@@ -708,12 +709,7 @@ mod tests {
harness.fit_contents();
let result = harness.try_snapshot(&format!("rendering_test/dpi_{dpi:.2}"));
if let Err(err) = result {
errors.push(err);
}
results.add(harness.try_snapshot(&format!("rendering_test/dpi_{dpi:.2}")));
}
assert!(errors.is_empty(), "Errors: {errors:#?}");
}
}