icedwright-probe 0.1.4

What an iced app links for icedwright: semantic annotations, typed test ids and named tasks, all pass-through unless a feature turns them on
Documentation
//! Task names and the `Named` wrapper in every feature set.

use std::future::Future;
use std::pin::pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::task::{Context, Poll, Waker};

use icedwright_probe::{Fallible, NamedTask, TaskName};

mod api {
    #[derive(Debug, Clone, PartialEq)]
    pub struct Note(pub &'static str);

    #[derive(Debug, Clone, PartialEq)]
    pub enum ApiError {
        Offline,
    }

    icedwright_probe::task_name! {
        /// Saves a note.
        pub SaveNote: Result<Note, ApiError>;
        pub ListNotes: Result<Vec<Note>, ApiError>;
    }

    icedwright_probe::task_name!(pub PickFile: Option<std::path::PathBuf>);
}

use api::{ApiError, ListNotes, Note, PickFile, SaveNote};

fn poll_once<F: Future>(future: F) -> Poll<F::Output> {
    let mut future = pin!(future);
    future
        .as_mut()
        .poll(&mut Context::from_waker(Waker::noop()))
}

#[test]
fn names_are_module_paths_with_the_declared_output() {
    assert_eq!(SaveNote::NAME, "tasks::api::SaveNote");
    assert_eq!(ListNotes::NAME, "tasks::api::ListNotes");
    assert_eq!(PickFile::NAME, "tasks::api::PickFile");
    let output: <SaveNote as TaskName>::Output = Ok(Note("x"));
    assert_eq!(output, Ok(Note("x")));
    let _: Option<std::path::PathBuf> = <PickFile as TaskName>::Output::None;
}

#[test]
fn names_are_plain_copyable_values() {
    let a = SaveNote;
    let b = a;
    assert_eq!(a, b);
    fn default_of<T: Default>() -> T {
        T::default()
    }
    assert_eq!(SaveNote, default_of());
    assert_eq!(format!("{a:?}"), "SaveNote");
}

#[test]
fn fallible_names_build_both_outcomes() {
    assert_eq!(SaveNote::from_ok(Note("a")), Ok(Note("a")));
    assert_eq!(
        SaveNote::from_err(ApiError::Offline),
        Err(ApiError::Offline)
    );
}

#[test]
fn an_uncontrolled_named_task_returns_its_future_output() {
    let named = SaveNote::task(async { Ok(Note("saved")) }).detail(|| "n1".to_owned());
    assert_eq!(poll_once(named), Poll::Ready(Ok(Note("saved"))));
}

#[test]
fn a_pending_inner_future_keeps_the_task_pending() {
    let named = PickFile::task(std::future::pending()).external();
    assert_eq!(poll_once(named), Poll::Pending);
}

#[test]
fn a_named_task_is_send_when_its_future_is() {
    fn send<T: Send + 'static>(_: &T) {}
    let named = ListNotes::task(async { Ok(Vec::new()) });
    send(&named);
}

#[cfg(not(feature = "control"))]
#[test]
fn without_control_the_task_is_the_future_and_detail_never_runs() {
    use std::mem::size_of_val;

    let ran = Arc::new(AtomicBool::new(false));
    let flag = ran.clone();
    // Large enough that a box or any added field would change the size.
    let buffer = [7u8; 256];
    let future = async move { Ok(Note(if buffer[0] == 7 { "x" } else { "y" })) };
    let size = size_of_val(&future);
    let named = SaveNote::task(future)
        .detail(move || {
            flag.store(true, Ordering::SeqCst);
            String::new()
        })
        .external();
    assert!(size >= 256);
    assert_eq!(size_of_val(&named), size);
    assert_eq!(poll_once(named), Poll::Ready(Ok(Note("x"))));
    assert!(!ran.load(Ordering::SeqCst));
}

#[cfg(feature = "control")]
#[test]
fn without_a_controller_detail_never_runs() {
    let ran = Arc::new(AtomicBool::new(false));
    let flag = ran.clone();
    let named = SaveNote::task(async { Ok(Note("x")) }).detail(move || {
        flag.store(true, Ordering::SeqCst);
        String::new()
    });
    assert_eq!(poll_once(named), Poll::Ready(Ok(Note("x"))));
    assert!(!ran.load(Ordering::SeqCst));
}