1
use forms::FieldError;
2

            
3
use super::native_failure;
4
use crate::forms::{Entity, Failure};
5

            
6
1
fn invalid() -> Failure {
7
1
    Failure::Invalid(vec![FieldError {
8
1
        field: "date".to_string(),
9
1
        message: "bad".to_string(),
10
1
    }])
11
1
}
12

            
13
#[test]
14
1
fn native_codes_follow_the_cause() {
15
3
    let code = |failure: Failure| {
16
3
        native_failure(failure)
17
3
            .downcast_ref::<scripting::runtime::HostCondition>()
18
2
            .map(|condition| (condition.code.clone(), condition.message.clone()))
19
3
    };
20
1
    assert_eq!(
21
1
        code(Failure::Missing(Entity::Template)),
22
1
        Some(("not-found".to_string(), "Template not found".to_string()))
23
    );
24
1
    assert_eq!(
25
1
        code(invalid()),
26
1
        Some(("form-invalid".to_string(), "1 field invalid".to_string()))
27
    );
28
1
    assert_eq!(code(Failure::Internal(wasmtime::Error::msg("db"))), None);
29
1
}