1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use tap_harness::TestRunner;
use wick_config::config::TestConfiguration;

use crate::{ComponentFactory, TestError, TestGroup};

#[derive(Debug, Default)]
#[must_use]
pub struct TestSuite<'a> {
  tests: Vec<TestGroup<'a>>,
}

impl<'a> TestSuite<'a> {
  pub fn from_configuration<'b>(configurations: &'b [TestConfiguration]) -> Result<Self, TestError>
  where
    'b: 'a,
  {
    let defs: Vec<TestGroup<'b>> = configurations
      .iter()
      .map(|config| {
        Ok(TestGroup::from_test_cases(
          config.config().and_then(|c| c.value().cloned()),
          config.cases(),
        ))
      })
      .collect::<Result<_, _>>()?;
    Ok(Self { tests: defs })
  }

  pub fn add_configuration<'b>(&mut self, config: &'b TestConfiguration) -> Result<(), TestError>
  where
    'b: 'a,
  {
    self.tests.push(TestGroup::from_test_cases(
      config.config().and_then(|c| c.value().cloned()),
      config.cases(),
    ));
    Ok(())
  }

  pub async fn run(
    &'a mut self,
    factory: ComponentFactory<'a>,
    filter: Vec<String>,
  ) -> Result<Vec<TestRunner>, TestError> {
    let mut runners = Vec::new();
    for group in &mut self.tests {
      let component = factory(group.root_config.clone());

      runners.push(group.run(None, component.await?, &filter).await?);
    }
    Ok(runners)
  }
}