diff options
| author | pml68 <contact@pml68.dev> | 2025-07-23 01:33:32 +0200 |
|---|---|---|
| committer | pml68 <contact@pml68.dev> | 2025-07-23 01:44:48 +0200 |
| commit | ac6a60347a2d044926ec200665cea430f12b34fe (patch) | |
| tree | 662da341972abb5134e9ab6f03d5956548288f01 | |
| parent | style: organize imports (diff) | |
| download | iced_dialog-ac6a60347a2d044926ec200665cea430f12b34fe.tar.gz | |
ci: add unit test(s) for `save` example
| -rw-r--r-- | .github/workflows/ci.yml | 4 | ||||
| -rw-r--r-- | Cargo.toml | 4 | ||||
| -rw-r--r-- | README.md | 16 | ||||
| -rw-r--r-- | examples/save.rs | 69 |
4 files changed, 73 insertions, 20 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 154ee76..809b8cc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,6 +16,8 @@ jobs: - name: Check lints run: cargo lint - name: Run tests - run: cargo test --verbose + run: | + cargo test --verbose --doc + cargo test --verbose --all-targets - name: Build example run: cargo build --example save @@ -16,7 +16,7 @@ exclude = [ ".github/*", ".gitignore", "examples/*", - "rustfmt.toml" + "rustfmt.toml", ] [dependencies] @@ -25,6 +25,7 @@ iced_core = { version = "0.14.0-dev", features = ["advanced"] } [dev-dependencies] iced = "0.14.0-dev" +iced_test = "0.14.0-dev" [[example]] name = "save" @@ -60,3 +61,4 @@ broken_intra_doc_links = "forbid" iced_widget = { git = "https://github.com/iced-rs/iced", branch = "master" } iced_core = { git = "https://github.com/iced-rs/iced", branch = "master" } iced = { git = "https://github.com/iced-rs/iced", branch = "master" } +iced_test = { git = "https://github.com/iced-rs/iced", branch = "master" } @@ -8,7 +8,7 @@ It's mostly the dialog from @frgp42's [Fluent Iced Gallery](https://github.com/f ```rust,no_run use iced::{ - Element, Length, Task, + Element, Task, widget::{button, center, column, text}, }; use iced_dialog::dialog; @@ -16,7 +16,7 @@ use iced_dialog::dialog; #[derive(Default)] struct State { is_open: bool, - action_text: String, + action_text: &'static str, } #[derive(Debug, Clone)] @@ -35,11 +35,11 @@ impl State { match message { Message::OpenDialog => self.is_open = true, Message::Saved => { - self.action_text = "User saved their work".to_owned(); + self.action_text = "User saved their work"; self.is_open = false; } Message::Cancelled => { - self.action_text = "User cancelled the dialog".to_owned(); + self.action_text = "User cancelled the dialog"; self.is_open = false; } } @@ -49,19 +49,17 @@ impl State { fn view(&self) -> Element<'_, Message> { let base = center( column![ - text(&self.action_text), + text(self.action_text), button("Open Dialog").on_press(Message::OpenDialog) ] .spacing(14.0), - ) - .width(Length::Fill) - .height(Length::Fill); + ); let dialog_content = text("Do you want to save?"); dialog(self.is_open, base, dialog_content) .title("Save") - .push_button(iced_dialog::button("Save", Message::Saved)) + .push_button(iced_dialog::button("Save work", Message::Saved)) .push_button(iced_dialog::button("Cancel", Message::Cancelled)) .width(350) .height(234) diff --git a/examples/save.rs b/examples/save.rs index 6eb31b6..060b1c1 100644 --- a/examples/save.rs +++ b/examples/save.rs @@ -1,6 +1,6 @@ #![allow(missing_docs)] use iced::{ - Element, Length, Task, + Element, Task, widget::{button, center, column, text}, }; use iced_dialog::dialog; @@ -8,7 +8,7 @@ use iced_dialog::dialog; #[derive(Default)] struct State { is_open: bool, - action_text: String, + action_text: &'static str, } #[derive(Debug, Clone)] @@ -27,11 +27,11 @@ impl State { match message { Message::OpenDialog => self.is_open = true, Message::Saved => { - self.action_text = "User saved their work".to_owned(); + self.action_text = "User saved their work"; self.is_open = false; } Message::Cancelled => { - self.action_text = "User cancelled the dialog".to_owned(); + self.action_text = "User cancelled the dialog"; self.is_open = false; } } @@ -41,19 +41,17 @@ impl State { fn view(&self) -> Element<'_, Message> { let base = center( column![ - text(&self.action_text), + text(self.action_text), button("Open Dialog").on_press(Message::OpenDialog) ] .spacing(14.0), - ) - .width(Length::Fill) - .height(Length::Fill); + ); let dialog_content = text("Do you want to save?"); dialog(self.is_open, base, dialog_content) .title("Save") - .push_button(iced_dialog::button("Save", Message::Saved)) + .push_button(iced_dialog::button("Save work", Message::Saved)) .push_button(iced_dialog::button("Cancel", Message::Cancelled)) .width(350) .height(234) @@ -61,3 +59,56 @@ impl State { .into() } } + +#[cfg(test)] +mod tests { + use iced_test::{Error, simulator}; + + use super::*; + + #[test] + fn dialog_opens() -> Result<(), Error> { + let mut save = State { + action_text: "", + is_open: false, + }; + let mut ui = simulator(save.view()); + + let _ = ui.click("Open Dialog")?; + for message in ui.into_messages() { + let _ = save.update(message); + } + + let mut ui = simulator(save.view()); + assert!(save.is_open); + assert!( + ui.find("Do you want to save?").is_ok(), + "Dialog should be open" + ); + + Ok(()) + } + + #[test] + fn dialog_closes() -> Result<(), Error> { + let mut save = State { + action_text: "", + is_open: true, + }; + let mut ui = simulator(save.view()); + + let _ = ui.click("Save work")?; + for message in ui.into_messages() { + let _ = save.update(message); + } + + let mut ui = simulator(save.view()); + assert_eq!(save.action_text, "User saved their work"); + assert!( + ui.find("User saved their work").is_ok(), + "Dialog should be closed" + ); + + Ok(()) + } +} |
