fhttp_core/
test_utils.rs

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
54
#[cfg(test)] extern crate temp_dir;
use std::path::PathBuf;
use crate::path_utils::{CanonicalizedPathBuf, canonicalize};

#[cfg(test)] use anyhow::Result;
#[cfg(test)] use temp_dir::TempDir;

pub fn root() -> CanonicalizedPathBuf {
    canonicalize(
        PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .parent().unwrap()
    ).unwrap()
}

#[cfg(test)]
pub fn errmsg<T>(r: anyhow::Result<T>) -> String {
    match r {
        Ok(_) => panic!("expected an Err!"),
        Err(e) => e.to_string(),
    }
}

#[cfg(test)]
macro_rules! assert_err(
    ($code:expr, $expectation:expr)=>{
        assert_eq!(
            crate::test_utils::errmsg(
                $code
            ),
            $expectation
        );
    };
);

#[cfg(test)]
macro_rules! assert_ok(
    ($code:expr, $expectation:expr)=>{
        match $code {
            Ok(value) => assert_eq!(value, $expectation),
            Err(_) => panic!("expected an ok value"),
        };
    };
);

#[cfg(test)]
pub fn write_test_file<S: AsRef<str>>(
    workdir: &TempDir,
    filename: S,
    content: S,
) -> Result<CanonicalizedPathBuf> {
    let file = workdir.child(filename.as_ref());
    std::fs::write(&file, content.as_ref().as_bytes())?;
    canonicalize(&file)
}