1
use ron::error::{Error, Position, SpannedError};
2
use serde::Deserialize;
3

            
4
8
#[derive(Debug, Deserialize, PartialEq)]
5
struct MyUnitStruct;
6

            
7
16
#[derive(Debug, Deserialize, PartialEq)]
8
struct MyTupleStruct(bool, i32);
9

            
10
8
#[derive(Debug, Deserialize, PartialEq)]
11
struct MyNewtypeStruct(MyTupleStruct);
12

            
13
24
#[derive(Debug, Deserialize, PartialEq)]
14
struct MyStruct {
15
    a: bool,
16
    b: i32,
17
}
18

            
19
#[test]
20
4
fn test_unit_struct_name_mismatch() {
21
4
    assert_eq!(ron::from_str::<MyUnitStruct>("()"), Ok(MyUnitStruct),);
22
4
    assert_eq!(
23
4
        ron::from_str::<MyUnitStruct>("MyUnitStruct"),
24
4
        Ok(MyUnitStruct),
25
4
    );
26
4
    assert_eq!(
27
4
        ron::from_str::<MyUnitStruct>("MyUnit Struct"),
28
4
        Err(SpannedError {
29
4
            code: Error::ExpectedDifferentStructName {
30
4
                expected: "MyUnitStruct",
31
4
                found: String::from("MyUnit")
32
4
            },
33
4
            position: Position { line: 1, col: 7 }
34
4
        }),
35
4
    );
36
4
    assert_eq!(
37
4
        ron::from_str::<MyUnitStruct>("42"),
38
4
        Err(SpannedError {
39
4
            code: Error::ExpectedNamedStructLike("MyUnitStruct"),
40
4
            position: Position { line: 1, col: 1 }
41
4
        }),
42
4
    );
43
4
}
44

            
45
#[test]
46
4
fn test_tuple_struct_name_mismatch() {
47
4
    assert_eq!(
48
4
        ron::from_str::<MyTupleStruct>("(true, 42)"),
49
4
        Ok(MyTupleStruct(true, 42)),
50
4
    );
51
4
    assert_eq!(
52
4
        ron::from_str::<MyTupleStruct>("MyTupleStruct(true, 42)"),
53
4
        Ok(MyTupleStruct(true, 42)),
54
4
    );
55
4
    assert_eq!(
56
4
        ron::from_str::<MyTupleStruct>("MyTypleStruct(true, 42)"),
57
4
        Err(SpannedError {
58
4
            code: Error::ExpectedDifferentStructName {
59
4
                expected: "MyTupleStruct",
60
4
                found: String::from("MyTypleStruct")
61
4
            },
62
4
            position: Position { line: 1, col: 14 }
63
4
        }),
64
4
    );
65
4
    assert_eq!(
66
4
        ron::from_str::<MyTupleStruct>("42"),
67
4
        Err(SpannedError {
68
4
            code: Error::ExpectedNamedStructLike("MyTupleStruct"),
69
4
            position: Position { line: 1, col: 1 }
70
4
        }),
71
4
    );
72
4
}
73

            
74
#[test]
75
4
fn test_newtype_struct_name_mismatch() {
76
4
    assert_eq!(
77
4
        ron::from_str::<MyNewtypeStruct>("((true, 42))"),
78
4
        Ok(MyNewtypeStruct(MyTupleStruct(true, 42))),
79
4
    );
80
4
    assert_eq!(
81
4
        ron::from_str::<MyNewtypeStruct>("MyNewtypeStruct((true, 42))"),
82
4
        Ok(MyNewtypeStruct(MyTupleStruct(true, 42))),
83
4
    );
84
4
    assert_eq!(
85
4
        ron::from_str::<MyNewtypeStruct>("MyNewtypeStrucl((true, 42))"),
86
4
        Err(SpannedError {
87
4
            code: Error::ExpectedDifferentStructName {
88
4
                expected: "MyNewtypeStruct",
89
4
                found: String::from("MyNewtypeStrucl")
90
4
            },
91
4
            position: Position { line: 1, col: 16 }
92
4
        }),
93
4
    );
94
4
    assert_eq!(
95
4
        ron::from_str::<MyNewtypeStruct>("42"),
96
4
        Err(SpannedError {
97
4
            code: Error::ExpectedNamedStructLike("MyNewtypeStruct"),
98
4
            position: Position { line: 1, col: 1 }
99
4
        }),
100
4
    );
101
4
}
102

            
103
#[test]
104
4
fn test_struct_name_mismatch() {
105
4
    assert_eq!(
106
4
        ron::from_str::<MyStruct>("(a: true, b: 42)"),
107
4
        Ok(MyStruct { a: true, b: 42 }),
108
4
    );
109
4
    assert_eq!(
110
4
        ron::from_str::<MyStruct>("MyStruct(a: true, b: 42)"),
111
4
        Ok(MyStruct { a: true, b: 42 }),
112
4
    );
113
4
    assert_eq!(
114
4
        ron::from_str::<MyStruct>("MuStryct(a: true, b: 42)"),
115
4
        Err(SpannedError {
116
4
            code: Error::ExpectedDifferentStructName {
117
4
                expected: "MyStruct",
118
4
                found: String::from("MuStryct")
119
4
            },
120
4
            position: Position { line: 1, col: 9 }
121
4
        }),
122
4
    );
123
4
    assert_eq!(
124
4
        ron::from_str::<MyStruct>("42"),
125
4
        Err(SpannedError {
126
4
            code: Error::ExpectedNamedStructLike("MyStruct"),
127
4
            position: Position { line: 1, col: 1 }
128
4
        }),
129
4
    );
130
4
}