From f929a20d29689af4ac873c1b34282c1f37f38f45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Wed, 28 May 2025 03:25:39 +0200 Subject: [PATCH] Change `From` for `Selector` to `Text` in `iced_test` --- core/src/widget/id.rs | 6 ++++++ examples/counter/src/main.rs | 9 ++++----- examples/todos/src/main.rs | 6 +++--- test/src/selector.rs | 11 ++++++++--- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/core/src/widget/id.rs b/core/src/widget/id.rs index ae739bb7..e03ded9d 100644 --- a/core/src/widget/id.rs +++ b/core/src/widget/id.rs @@ -23,6 +23,12 @@ impl Id { } } +impl From<&'static str> for Id { + fn from(value: &'static str) -> Self { + Self::new(value) + } +} + #[derive(Debug, Clone, PartialEq, Eq, Hash)] enum Internal { Unique(usize), diff --git a/examples/counter/src/main.rs b/examples/counter/src/main.rs index d097e052..1bb17af5 100644 --- a/examples/counter/src/main.rs +++ b/examples/counter/src/main.rs @@ -42,7 +42,6 @@ impl Counter { #[cfg(test)] mod tests { use super::*; - use iced_test::selector::text; use iced_test::{Error, simulator}; #[test] @@ -50,9 +49,9 @@ mod tests { let mut counter = Counter { value: 0 }; let mut ui = simulator(counter.view()); - let _ = ui.click(text("Increment"))?; - let _ = ui.click(text("Increment"))?; - let _ = ui.click(text("Decrement"))?; + let _ = ui.click("Increment")?; + let _ = ui.click("Increment")?; + let _ = ui.click("Decrement")?; for message in ui.into_messages() { counter.update(message); @@ -61,7 +60,7 @@ mod tests { assert_eq!(counter.value, 1); let mut ui = simulator(counter.view()); - assert!(ui.find(text("1")).is_ok(), "Counter should display 1!"); + assert!(ui.find("1").is_ok(), "Counter should display 1!"); Ok(()) } diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 8dcbd000..8a4bc2eb 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -577,7 +577,7 @@ mod tests { use super::*; use iced::{Settings, Theme}; - use iced_test::selector::text; + use iced_test::selector::id; use iced_test::{Error, Simulator}; fn simulator(todos: &Todos) -> Simulator { @@ -596,7 +596,7 @@ mod tests { let _command = todos.update(Message::Loaded(Err(LoadError::File))); let mut ui = simulator(&todos); - let _input = ui.click("new-task")?; + let _input = ui.click(id("new-task"))?; let _ = ui.typewrite("Create the universe"); let _ = ui.tap_key(keyboard::key::Named::Enter); @@ -606,7 +606,7 @@ mod tests { } let mut ui = simulator(&todos); - let _ = ui.find(text("Create the universe"))?; + let _ = ui.find("Create the universe")?; let snapshot = ui.snapshot(&Theme::Dark)?; assert!( diff --git a/test/src/selector.rs b/test/src/selector.rs index 7b8dcb7e..58e0fca4 100644 --- a/test/src/selector.rs +++ b/test/src/selector.rs @@ -18,12 +18,17 @@ impl From for Selector { } impl From<&'static str> for Selector { - fn from(id: &'static str) -> Self { - Self::Id(widget::Id::new(id)) + fn from(text: &'static str) -> Self { + Self::Text(text.into()) } } -/// Creates [`Selector`] that finds the widget containing the given text fragment. +/// Creates a [`Selector`] that finds the widget with the given [`widget::Id`]. +pub fn id(id: impl Into) -> Selector { + Selector::Id(id.into()) +} + +/// Creates a [`Selector`] that finds the widget containing the given text fragment. pub fn text(fragment: impl text::IntoFragment<'static>) -> Selector { Selector::Text(fragment.into_fragment()) }