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
use crate::{commander::Error, Commander, LocalnetHandle};
use fehler::throws;
use log::debug;
use std::{borrow::Cow, mem};

/// `Tester` is used primarily by [`#[trdelnik_test]`](trdelnik_test::trdelnik_test) macro.
///
/// There should be no need to use `Tester` directly.
#[derive(Default)]
pub struct Tester {
    root: Cow<'static, str>,
}

impl Tester {
    pub fn new() -> Self {
        Self {
            root: "../../".into(),
        }
    }

    pub fn with_root(root: impl Into<Cow<'static, str>>) -> Self {
        Self { root: root.into() }
    }

    #[throws]
    pub async fn before(&mut self) -> LocalnetHandle {
        debug!("_____________________");
        debug!("____ BEFORE TEST ____");
        let commander = Commander::with_root(mem::take(&mut self.root));
        commander.start_localnet().await?
    }

    #[throws]
    pub async fn after(&self, localnet_handle: LocalnetHandle) {
        debug!("____ AFTER TEST ____");
        localnet_handle.stop_and_remove_ledger().await?;
        debug!("_____________________");
    }
}