forked from stalwartlabs/mail-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
72 lines (64 loc) · 1.53 KB
/
mod.rs
File metadata and controls
72 lines (64 loc) · 1.53 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*/
pub mod address;
mod content_type;
pub mod date;
mod id;
mod list;
mod raw;
mod received;
pub mod thread;
mod unstructured;
#[cfg(test)]
use serde::{Deserialize, Serialize};
#[cfg(test)]
#[derive(Debug, Serialize, Deserialize)]
pub struct Test<T> {
pub header: String,
pub expected: T,
}
#[cfg(test)]
pub fn load_tests<T: serde::de::DeserializeOwned>(test_name: &str) -> Vec<Test<T>> {
serde_json::from_slice::<Vec<Test<T>>>(
&std::fs::read(
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("resources")
.join(test_name),
)
.unwrap(),
)
.unwrap()
}
#[cfg(test)]
#[derive(Debug, Default)]
pub struct TestBuilder<T: Serialize> {
test: std::path::PathBuf,
tests: Vec<Test<T>>,
}
#[cfg(test)]
impl<T: Serialize> TestBuilder<T> {
pub fn new(test: impl AsRef<str>) -> Self {
Self {
test: std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("resources")
.join(test.as_ref()),
tests: Vec::new(),
}
}
pub fn add(&mut self, header: impl Into<String>, expected: T) {
self.tests.push(Test {
header: header.into(),
expected,
});
}
pub fn write(&self) {
std::fs::write(
&self.test,
serde_json::to_string_pretty(&self.tests).unwrap(),
)
.unwrap();
}
}