1
use nomiscript::{Expr, Fraction, Number};
2

            
3
use super::{expect_plist, take_plist_i64, take_plist_list, take_plist_number, take_plist_string};
4

            
5
8
fn plist(value: Expr) -> Vec<(String, Expr)> {
6
8
    vec![("KEY".to_string(), value)]
7
8
}
8

            
9
#[test]
10
1
fn a_non_plist_payload_prints_as_a_datum() {
11
1
    assert_eq!(
12
1
        expect_plist(Expr::Number(Number::Index(5.into())), "payload"),
13
1
        Err("payload: expected plist, got 5".to_string())
14
    );
15
1
}
16

            
17
#[test]
18
1
fn a_non_keyword_key_prints_as_a_datum() {
19
1
    assert_eq!(
20
1
        expect_plist(
21
1
            Expr::List(vec![Expr::Number(Number::Index(5.into())), Expr::Nil]),
22
1
            "payload"
23
1
        ),
24
1
        Err("payload: expected :keyword, got 5".to_string())
25
    );
26
1
}
27

            
28
#[test]
29
1
fn a_mistyped_plist_value_prints_as_a_datum() {
30
1
    assert_eq!(
31
1
        take_plist_string(&plist(Expr::Number(Number::Index(5.into()))), "key"),
32
1
        Err(":key must be string, got 5".to_string())
33
    );
34
1
    assert_eq!(
35
1
        take_plist_number(&plist(Expr::String("x".into())), "key"),
36
1
        Err(":key must be number, got \"x\"".to_string())
37
    );
38
1
    assert_eq!(
39
1
        take_plist_list(&plist(Expr::Symbol("A".into())), "key"),
40
1
        Err(":key must be list, got A".to_string())
41
    );
42
1
    assert_eq!(
43
1
        take_plist_i64(
44
1
            &plist(Expr::List(vec![Expr::Number(Number::Index(1.into()))])),
45
1
            "key"
46
1
        ),
47
1
        Err(":key must be integer, got (1)".to_string())
48
    );
49
1
}
50

            
51
#[test]
52
1
fn a_number_is_read_whatever_its_dimension() {
53
1
    let scalar = Expr::Number(Number::Scalar(Fraction::from_integer(2_000_000_000)));
54
1
    assert_eq!(
55
1
        take_plist_number(&plist(scalar.clone()), "key"),
56
1
        Ok(Some(Fraction::from_integer(2_000_000_000)))
57
    );
58
1
    assert_eq!(
59
1
        take_plist_number(&plist(Expr::Number(Number::Index(3.into()))), "key"),
60
1
        Ok(Some(Fraction::from_integer(3)))
61
    );
62
1
    assert!(take_plist_i64(&plist(scalar), "key").is_err());
63
1
}
64

            
65
#[test]
66
1
fn a_whole_scalar_is_not_an_integer_and_prints_its_denominator() {
67
1
    assert_eq!(
68
1
        take_plist_i64(
69
1
            &plist(Expr::Number(Number::Scalar(Fraction::from_integer(2)))),
70
1
            "key"
71
1
        ),
72
1
        Err(":key must be integer, got 2/1".to_string())
73
    );
74
1
}