af_core/test/
output.rs

1// Copyright © 2021 Alexandra Frydl
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7use crate::channel;
8use crate::prelude::*;
9use crate::task::Task;
10use crate::test::Path;
11
12/// A stream of test results.
13pub struct OutputStream {
14  pub(super) remaining: usize,
15  pub(super) rx: channel::Receiver<Output>,
16  pub(super) _task: Task<()>,
17}
18
19/// A single test result.
20#[derive(Debug)]
21pub struct Output {
22  /// The path of the test, including scope names.
23  pub path: Path,
24  /// The result of the test.
25  pub result: fail::Result,
26}
27
28impl OutputStream {
29  /// Returns `true` if no tests remain.
30  pub fn is_empty(&self) -> bool {
31    self.remaining == 0
32  }
33
34  /// Returns the number of tests remaining.
35  pub fn len(&self) -> usize {
36    self.remaining
37  }
38
39  /// Waits for the next test to stop and returns its result.
40  ///
41  /// If all tests have stopped, this function returns `None`.
42  pub async fn next(&mut self) -> Option<Output> {
43    let result = self.rx.recv().await.ok();
44
45    if result.is_some() {
46      self.remaining -= 1;
47    } else if self.remaining > 0 {
48      panic!("OutputStream closed with {} remaining tasks.", self.remaining);
49    }
50
51    result
52  }
53}