From c8ccba5535f7853845f7ab0517f61d7e570db307 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Wed, 20 Aug 2025 14:03:51 +0200 Subject: [PATCH] Use `mouse::Button::Left` by default in ice tests --- examples/todos/tests/carl_sagan.ice | 6 +++--- test/src/instruction.rs | 30 ++++++++++++++++++++++------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/examples/todos/tests/carl_sagan.ice b/examples/todos/tests/carl_sagan.ice index 7c94ff3a..1a1b7f08 100644 --- a/examples/todos/tests/carl_sagan.ice +++ b/examples/todos/tests/carl_sagan.ice @@ -2,11 +2,11 @@ viewport: 512x768 mode: impatient preset: Empty ----- -click left at "What needs to be done?" +click at "What needs to be done?" type "Create the universe" type enter type "Make an apple pie" type enter -click left at "Create the universe" -click left at "Make an apple pie" +click at "Create the universe" +click at "Make an apple pie" expect "0 tasks left" diff --git a/test/src/instruction.rs b/test/src/instruction.rs index 539c61c5..e356e028 100644 --- a/test/src/instruction.rs +++ b/test/src/instruction.rs @@ -329,16 +329,22 @@ mod format { use super::*; pub fn button_at(button: mouse::Button, at: Option<&Target>) -> String { + let button = self::button(button); + if let Some(at) = at { - format!("{} at {}", self::button(button), at) + if button.is_empty() { + format!("at {}", at) + } else { + format!("{} at {}", button, at) + } } else { - self::button(button).to_owned() + button.to_owned() } } pub fn button(button: mouse::Button) -> &'static str { match button { - mouse::Button::Left => "left", + mouse::Button::Left => "", mouse::Button::Right => "right", mouse::Button::Middle => "middle", mouse::Button::Back => "back", @@ -388,7 +394,8 @@ mod parser { use nom::branch::alt; use nom::bytes::complete::tag; use nom::character::complete::{char, multispace0, satisfy}; - use nom::combinator::{cut, map, opt, recognize}; + use nom::combinator::{cut, map, opt, recognize, success}; + use nom::error::ParseError; use nom::multi::many0; use nom::number::float; use nom::sequence::{delimited, preceded, separated_pair}; @@ -447,7 +454,7 @@ mod parser { fn target(input: &str) -> IResult<&str, Target> { preceded( - tag(" at "), + whitespace(tag("at ")), cut(alt((string.map(Target::Text), point.map(Target::Point)))), ) .parse(input) @@ -455,8 +462,8 @@ mod parser { fn mouse_button(input: &str) -> IResult<&str, mouse::Button> { alt(( - tag("left").map(|_| mouse::Button::Left), tag("right").map(|_| mouse::Button::Right), + success(mouse::Button::Left), )) .parse(input) } @@ -497,7 +504,7 @@ mod parser { } fn point(input: &str) -> IResult<&str, Point> { - let comma = (multispace0, char(','), multispace0); + let comma = whitespace(char(',')); map( delimited( @@ -509,4 +516,13 @@ mod parser { ) .parse(input) } + + pub fn whitespace<'a, O, E: ParseError<&'a str>, F>( + inner: F, + ) -> impl Parser<&'a str, Output = O, Error = E> + where + F: Parser<&'a str, Output = O, Error = E>, + { + delimited(multispace0, inner, multispace0) + } }