1
use super::*;
2

            
3
9
fn condition() -> HostCondition {
4
9
    HostCondition {
5
9
        code: "form-invalid".into(),
6
9
        message: "2 fields invalid".into(),
7
9
        detail: Some(EvalValue::List(vec![EvalValue::List(vec![
8
9
            EvalValue::Symbol(":field".into()),
9
9
            EvalValue::String("a".into()),
10
9
        ])])),
11
9
    }
12
9
}
13

            
14
4
fn expected() -> EngineError {
15
4
    EngineError::ScriptRaised {
16
4
        code: "FORM-INVALID".into(),
17
4
        message: "2 fields invalid".into(),
18
4
        detail: condition().detail,
19
4
    }
20
4
}
21

            
22
#[test]
23
1
fn direct_host_condition_is_classified() {
24
1
    let err = wasmtime::Error::new(condition());
25
1
    assert_eq!(classify_runtime_error(&err), expected());
26
1
}
27

            
28
#[test]
29
1
fn context_wrapped_host_condition_is_classified() {
30
1
    let err = wasmtime::Error::new(condition()).context("error while executing at wasm backtrace");
31
1
    assert_eq!(classify_runtime_error(&err), expected());
32
1
}
33

            
34
#[test]
35
1
fn host_condition_without_detail_stays_none() {
36
1
    let err = wasmtime::Error::new(HostCondition {
37
1
        detail: None,
38
1
        ..condition()
39
1
    });
40
1
    assert!(matches!(
41
1
        classify_runtime_error(&err),
42
        EngineError::ScriptRaised { detail: None, .. }
43
    ));
44
1
}
45

            
46
#[test]
47
1
fn trap_wins_over_host_condition() {
48
1
    let err = wasmtime::Error::new(condition()).context(wasmtime::Trap::OutOfFuel);
49
1
    assert!(matches!(
50
1
        classify_runtime_error(&err),
51
        EngineError::OutOfFuel
52
    ));
53
1
}
54

            
55
#[test]
56
1
fn host_condition_wins_over_script_raise() {
57
1
    let raise = ScriptRaise {
58
1
        code: "BOOM".into(),
59
1
        message: "from the script".into(),
60
1
        detail: None,
61
1
    };
62
1
    let err = wasmtime::Error::new(condition()).context(raise);
63
1
    assert_eq!(classify_runtime_error(&err), expected());
64
1
}
65

            
66
#[test]
67
1
fn err_code_and_message_drops_detail() {
68
1
    let (code, message) = err_code_and_message(&expected());
69
1
    assert_eq!(code, "FORM-INVALID");
70
1
    assert_eq!(message, "2 fields invalid");
71
1
}
72

            
73
#[test]
74
1
fn a_plain_no_price_text_is_only_a_trap() {
75
1
    let err = wasmtime::Error::msg("convert-commodity: no Price row links them");
76
1
    assert!(matches!(classify_runtime_error(&err), EngineError::Trap(_)));
77
1
}