use std::{future::{pending, ready}, time::Duration};
use floop::floop;
use smol::{Timer, block_on, future::poll_once};
#[test]
fn return_() {
async fn test() -> i32 {
let mut counter = 2;
let result = floop! {
unbiased
return
_ in Timer::after(Duration::from_millis(34)) => {
counter *= 2;
}
_ in ready(()) => {
break "foo";
}
_ in Timer::after(Duration::from_millis(41)) => {
if counter == 32 {
return counter;
}
}
};
let _: &str = result.1;
match result.0 {}
}
assert_eq!(block_on(test()), 32);
}
#[test]
fn return_break() {
let fut = async |i: i32| {
floop! {
biased
return
_ in Timer::after(Duration::from_millis(10)) => {
if i == 5 {
return "return";
} else {
break;
}
}
() in ready(()) => {
break "break";
}
}.1
};
block_on(async {
assert_eq!(fut(5).await, "return");
assert_eq!(fut(10).await, "break");
})
}
#[test]
fn return_empty() {
let fut = async {
floop! {
biased
return
}
};
assert_eq!(block_on(poll_once(fut)), Some(()));
}
#[test]
fn question_mark_operator() {
async fn test() -> Result<&'static str, i32> {
floop! {
biased
return
() in ready(()) => {
Err(5)?;
}
() in pending::<()>() => {
return Ok("this will never run");
}
}.0
}
assert_eq!(block_on(test()), Err(5));
}