Change From<str> for Selector to Text in iced_test

This commit is contained in:
Héctor Ramón Jiménez 2025-05-28 03:25:39 +02:00
parent 03326b955b
commit f929a20d29
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
4 changed files with 21 additions and 11 deletions

View file

@ -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)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum Internal { enum Internal {
Unique(usize), Unique(usize),

View file

@ -42,7 +42,6 @@ impl Counter {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use iced_test::selector::text;
use iced_test::{Error, simulator}; use iced_test::{Error, simulator};
#[test] #[test]
@ -50,9 +49,9 @@ mod tests {
let mut counter = Counter { value: 0 }; let mut counter = Counter { value: 0 };
let mut ui = simulator(counter.view()); let mut ui = simulator(counter.view());
let _ = ui.click(text("Increment"))?; let _ = ui.click("Increment")?;
let _ = ui.click(text("Increment"))?; let _ = ui.click("Increment")?;
let _ = ui.click(text("Decrement"))?; let _ = ui.click("Decrement")?;
for message in ui.into_messages() { for message in ui.into_messages() {
counter.update(message); counter.update(message);
@ -61,7 +60,7 @@ mod tests {
assert_eq!(counter.value, 1); assert_eq!(counter.value, 1);
let mut ui = simulator(counter.view()); 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(()) Ok(())
} }

View file

@ -577,7 +577,7 @@ mod tests {
use super::*; use super::*;
use iced::{Settings, Theme}; use iced::{Settings, Theme};
use iced_test::selector::text; use iced_test::selector::id;
use iced_test::{Error, Simulator}; use iced_test::{Error, Simulator};
fn simulator(todos: &Todos) -> Simulator<Message> { fn simulator(todos: &Todos) -> Simulator<Message> {
@ -596,7 +596,7 @@ mod tests {
let _command = todos.update(Message::Loaded(Err(LoadError::File))); let _command = todos.update(Message::Loaded(Err(LoadError::File)));
let mut ui = simulator(&todos); 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.typewrite("Create the universe");
let _ = ui.tap_key(keyboard::key::Named::Enter); let _ = ui.tap_key(keyboard::key::Named::Enter);
@ -606,7 +606,7 @@ mod tests {
} }
let mut ui = simulator(&todos); let mut ui = simulator(&todos);
let _ = ui.find(text("Create the universe"))?; let _ = ui.find("Create the universe")?;
let snapshot = ui.snapshot(&Theme::Dark)?; let snapshot = ui.snapshot(&Theme::Dark)?;
assert!( assert!(

View file

@ -18,12 +18,17 @@ impl From<widget::Id> for Selector {
} }
impl From<&'static str> for Selector { impl From<&'static str> for Selector {
fn from(id: &'static str) -> Self { fn from(text: &'static str) -> Self {
Self::Id(widget::Id::new(id)) 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<widget::Id>) -> 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 { pub fn text(fragment: impl text::IntoFragment<'static>) -> Selector {
Selector::Text(fragment.into_fragment()) Selector::Text(fragment.into_fragment())
} }