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

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

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

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

            
13
#[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
        Ok(MyUnitStruct),
25
    );
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
            span: Span {
34
4
                start: ron::error::Position { line: 1, col: 1 },
35
4
                end: Position { line: 1, col: 7 }
36
4
            }
37
4
        }),
38
    );
39
4
    assert_eq!(
40
4
        ron::from_str::<MyUnitStruct>("42"),
41
        Err(SpannedError {
42
            code: Error::ExpectedNamedStructLike("MyUnitStruct"),
43
            span: Span {
44
                start: ron::error::Position { line: 1, col: 1 },
45
                end: Position { line: 1, col: 1 }
46
            }
47
        }),
48
    );
49
4
}
50

            
51
#[test]
52
4
fn test_tuple_struct_name_mismatch() {
53
4
    assert_eq!(
54
4
        ron::from_str::<MyTupleStruct>("(true, 42)"),
55
        Ok(MyTupleStruct(true, 42)),
56
    );
57
4
    assert_eq!(
58
4
        ron::from_str::<MyTupleStruct>("MyTupleStruct(true, 42)"),
59
        Ok(MyTupleStruct(true, 42)),
60
    );
61
4
    assert_eq!(
62
4
        ron::from_str::<MyTupleStruct>("MyTypleStruct(true, 42)"),
63
4
        Err(SpannedError {
64
4
            code: Error::ExpectedDifferentStructName {
65
4
                expected: "MyTupleStruct",
66
4
                found: String::from("MyTypleStruct")
67
4
            },
68
4
            span: Span {
69
4
                start: ron::error::Position { line: 1, col: 1 },
70
4
                end: Position { line: 1, col: 14 }
71
4
            }
72
4
        }),
73
    );
74
4
    assert_eq!(
75
4
        ron::from_str::<MyTupleStruct>("42"),
76
        Err(SpannedError {
77
            code: Error::ExpectedNamedStructLike("MyTupleStruct"),
78
            span: Span {
79
                start: ron::error::Position { line: 1, col: 1 },
80
                end: Position { line: 1, col: 1 }
81
            }
82
        }),
83
    );
84
4
}
85

            
86
#[test]
87
4
fn test_newtype_struct_name_mismatch() {
88
4
    assert_eq!(
89
4
        ron::from_str::<MyNewtypeStruct>("((true, 42))"),
90
        Ok(MyNewtypeStruct(MyTupleStruct(true, 42))),
91
    );
92
4
    assert_eq!(
93
4
        ron::from_str::<MyNewtypeStruct>("MyNewtypeStruct((true, 42))"),
94
        Ok(MyNewtypeStruct(MyTupleStruct(true, 42))),
95
    );
96
4
    assert_eq!(
97
4
        ron::from_str::<MyNewtypeStruct>("MyNewtypeStrucl((true, 42))"),
98
4
        Err(SpannedError {
99
4
            code: Error::ExpectedDifferentStructName {
100
4
                expected: "MyNewtypeStruct",
101
4
                found: String::from("MyNewtypeStrucl")
102
4
            },
103
4
            span: Span {
104
4
                start: ron::error::Position { line: 1, col: 1 },
105
4
                end: Position { line: 1, col: 16 }
106
4
            }
107
4
        }),
108
    );
109
4
    assert_eq!(
110
4
        ron::from_str::<MyNewtypeStruct>("42"),
111
        Err(SpannedError {
112
            code: Error::ExpectedNamedStructLike("MyNewtypeStruct"),
113
            span: Span {
114
                start: ron::error::Position { line: 1, col: 1 },
115
                end: Position { line: 1, col: 1 }
116
            }
117
        }),
118
    );
119
4
}
120

            
121
#[test]
122
4
fn test_struct_name_mismatch() {
123
4
    assert_eq!(
124
4
        ron::from_str::<MyStruct>("(a: true, b: 42)"),
125
        Ok(MyStruct { a: true, b: 42 }),
126
    );
127
4
    assert_eq!(
128
4
        ron::from_str::<MyStruct>("MyStruct(a: true, b: 42)"),
129
        Ok(MyStruct { a: true, b: 42 }),
130
    );
131
4
    assert_eq!(
132
4
        ron::from_str::<MyStruct>("MuStryct(a: true, b: 42)"),
133
4
        Err(SpannedError {
134
4
            code: Error::ExpectedDifferentStructName {
135
4
                expected: "MyStruct",
136
4
                found: String::from("MuStryct")
137
4
            },
138
4
            span: Span {
139
4
                start: ron::error::Position { line: 1, col: 1 },
140
4
                end: Position { line: 1, col: 9 }
141
4
            }
142
4
        }),
143
    );
144
4
    assert_eq!(
145
4
        ron::from_str::<MyStruct>("42"),
146
        Err(SpannedError {
147
            code: Error::ExpectedNamedStructLike("MyStruct"),
148
            span: Span {
149
                start: ron::error::Position { line: 1, col: 1 },
150
                end: Position { line: 1, col: 1 }
151
            }
152
        }),
153
    );
154
4
}