Skip to content

Commit

Permalink
Veryfy no unexpected events in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
avsaase committed Dec 2, 2023
1 parent 71a2373 commit aa0b11c
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions tests/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ async fn short_press() {

let event = button.update().await;
assert_matches!(event, ButtonEvent::ShortPress { count: 1 });

verify_no_event(&mut button).await;
}

#[tokio::test]
Expand All @@ -53,10 +55,8 @@ async fn double_press() {

let event = button.update().await;
assert_matches!(event, ButtonEvent::ShortPress { count: 2 });
assert_err!(
timeout(Duration::from_millis(500), button.update()).await,
"Unexpected event"
);

verify_no_event(&mut button).await;
}

#[tokio::test]
Expand All @@ -76,10 +76,8 @@ async fn long_press() {

let event = button.update().await;
assert_matches!(event, ButtonEvent::LongPress);
assert_err!(
timeout(Duration::from_millis(500), button.update()).await,
"Unexpected event"
);

verify_no_event(&mut button).await;
}

#[tokio::test]
Expand All @@ -105,10 +103,8 @@ async fn two_short_presses() {
assert_matches!(event, ButtonEvent::ShortPress { count: 1 });
let event = button.update().await;
assert_matches!(event, ButtonEvent::ShortPress { count: 1 });
assert_err!(
timeout(Duration::from_millis(500), button.update()).await,
"Unexpected event"
);

verify_no_event(&mut button).await;
}

#[tokio::test]
Expand All @@ -130,10 +126,8 @@ async fn debounce() {

let event = button.update().await;
assert_matches!(event, ButtonEvent::ShortPress { count: 1 });
assert_err!(
timeout(Duration::from_millis(500), button.update()).await,
"Unexpected event"
);

verify_no_event(&mut button).await;
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -219,3 +213,10 @@ impl Error for MockError {
async fn sleep_millis(millis: u64) {
tokio::time::sleep(Duration::from_millis(millis)).await;
}

async fn verify_no_event(button: &mut Button<MockPin>) {
assert_err!(
timeout(Duration::from_millis(500), button.update()).await,
"Unexpected event"
);
}

0 comments on commit aa0b11c

Please sign in to comment.