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
use crate::*;
#[derive(Debug)]
pub enum EcsError {
NotInitialized,
AlreadyBorrowed,
DispatcherExecutionFailed(Vec<EcsError>),
SystemError(Box<dyn Error+Send>),
}
pub type SystemResult = Result<(), EcsError>;
#[macro_export]
macro_rules! system_error {
($err:expr) => {
return Err(EcsError::SystemError(Box::new($err)));
};
}
#[cfg(test)]
mod tests {
use crate::*;
use wasm_bindgen_test::*;
#[test]
#[wasm_bindgen_test]
fn system_return_custom_error() {
#[derive(Debug)]
struct CustomError;
impl std::fmt::Display for CustomError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Custom error crash")
}
}
impl Error for CustomError {}
fn sys() -> SystemResult {
system_error!(CustomError)
}
let mut s = sys.system();
let result = s.run(&World::default());
match result {
Err(EcsError::SystemError(err)) => assert_eq!(err.to_string(), "Custom error crash"),
_ => unreachable!(),
}
}
}