kpreid / yield-progress

Progress reporting and cooperative task yielding in Rust
Apache License 2.0
2 stars 0 forks source link

Add doc-tests #7

Open kpreid opened 10 months ago

kpreid commented 10 months ago

The functions should have code samples and they should be tested.

kpreid commented 10 months ago

In order to test, they will need some kind of construction of a YieldProgress with readable output. Should they visibly have some kind of let (progress, cell) = yield_progress::testing::ProgressCell::new(); or should they hide that as a distraction?

I do think that we want testing utilities so that code which consumes YieldProgress can be tested easily.

kpreid commented 6 months ago

I tried to sketch a test with no additional dependencies...

use std::sync;
use std::future::Future;

fn labeled_progress(progress: YieldProgress) {
    progress.set_label("one");
    progress.progress(0.0).await;
    progress.set_label("two");
    progress.progress(0.5).await;
    progress.finish().await;
}

// Create a `YieldProgress` we can look at.
let current_label: sync::Arc<sync::Mutex<String>>> = Default::default();
let progress = yield_progress::Builder::new()
    .progress_using({
        let current_label = current_label.clone();
        move |info| *current_label.lock().unwrap() = info.label_str().to_owned()
    });

let task = labeled_progress(progress);
tokio::pin!(task);
task.poll(...)

Doesn't seem very nice to read and we still need a noop waker to finish implementing it.