aboutsummaryrefslogtreecommitdiff
path: root/test-suite/tests/backcompat.rs
blob: 7d4fcee43583b689805d380c77d1632428b14100 (plain)
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
extern crate serde;
extern crate milf;

use serde::de::Deserialize;

macro_rules! bad {
    ($milf:expr, $msg:expr) => {
        match $milf.parse::<milf::Value>() {
            Ok(s) => panic!("parsed to: {:#?}", s),
            Err(e) => assert_eq!(e.to_string(), $msg),
        }
    };
}

#[test]
fn newlines_after_tables() {
    let s = "
        [a] foo = 1
        [[b]] foo = 1
    ";
    bad!(
        s,
        "expected newline, found an identifier at line 2 column 13"
    );

    let mut d = milf::de::Deserializer::new(s);
    d.set_require_newline_after_table(false);
    let value = milf::Value::deserialize(&mut d).unwrap();
    assert_eq!(value["a"]["foo"].as_integer(), Some(1));
    assert_eq!(value["b"][0]["foo"].as_integer(), Some(1));
}

#[test]
fn allow_duplicate_after_longer() {
    let s = "
        [dependencies.openssl-sys]
        version = 1

        [dependencies]
        libc = 1

        [dependencies]
        bitflags = 1
    ";
    bad!(
        s,
        "redefinition of table `dependencies` for key `dependencies` at line 8 column 9"
    );

    let mut d = milf::de::Deserializer::new(s);
    d.set_allow_duplicate_after_longer_table(true);
    let value = milf::Value::deserialize(&mut d).unwrap();
    assert_eq!(
        value["dependencies"]["openssl-sys"]["version"].as_integer(),
        Some(1)
    );
    assert_eq!(value["dependencies"]["libc"].as_integer(), Some(1));
    assert_eq!(value["dependencies"]["bitflags"].as_integer(), Some(1));
}