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

            
4
#[derive(Serialize, Deserialize, Debug)]
5
#[serde(rename = "Hello World")]
6
struct InvalidStruct;
7

            
8
#[derive(Serialize, Deserialize, Debug)]
9
#[serde(rename = "")]
10
struct EmptyStruct;
11

            
12
28
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
13
#[serde(rename = "Hello+World")]
14
#[serde(deny_unknown_fields)]
15
struct RawStruct {
16
    #[serde(rename = "ab.cd-ef")]
17
    field: bool,
18
    really_not_raw: i32,
19
}
20

            
21
20
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
22
enum RawEnum {
23
    #[serde(rename = "Hello-World")]
24
    RawVariant,
25
}
26

            
27
#[test]
28
4
fn test_invalid_identifiers() {
29
4
    let ser = ron::ser::to_string_pretty(
30
4
        &InvalidStruct,
31
4
        ron::ser::PrettyConfig::default().struct_names(true),
32
4
    );
33
4
    assert_eq!(
34
4
        ser,
35
4
        Err(Error::InvalidIdentifier(String::from("Hello World")))
36
4
    );
37

            
38
4
    let ser = ron::ser::to_string_pretty(
39
4
        &EmptyStruct,
40
4
        ron::ser::PrettyConfig::default().struct_names(true),
41
4
    );
42
4
    assert_eq!(ser, Err(Error::InvalidIdentifier(String::from(""))));
43

            
44
4
    let de = ron::from_str::<InvalidStruct>("Hello World").unwrap_err();
45
4
    assert_eq!(
46
4
        de,
47
4
        SpannedError {
48
4
            code: Error::ExpectedDifferentStructName {
49
4
                expected: "Hello World",
50
4
                found: String::from("Hello"),
51
4
            },
52
4
            position: Position { line: 1, col: 6 },
53
4
        }
54
4
    );
55

            
56
4
    let de = ron::from_str::<EmptyStruct>("").unwrap_err();
57
4
    assert_eq!(
58
4
        de,
59
4
        SpannedError {
60
4
            code: Error::ExpectedUnit,
61
4
            position: Position { line: 1, col: 1 },
62
4
        }
63
4
    );
64

            
65
4
    let de = ron::from_str::<EmptyStruct>("r#").unwrap_err();
66
4
    assert_eq!(
67
4
        format!("{}", de),
68
4
        "1:1: Expected only opening `(`, no name, for un-nameable struct"
69
4
    );
70

            
71
4
    let de = ron::from_str::<RawStruct>("").unwrap_err();
72
4
    assert_eq!(
73
4
        de,
74
4
        SpannedError {
75
4
            code: Error::ExpectedNamedStructLike("Hello+World"),
76
4
            position: Position { line: 1, col: 1 },
77
4
        },
78
4
    );
79

            
80
4
    let de = ron::from_str::<RawStruct>("r#").unwrap_err();
81
4
    assert_eq!(
82
4
        de,
83
4
        SpannedError {
84
4
            code: Error::ExpectedNamedStructLike("Hello+World"),
85
4
            position: Position { line: 1, col: 1 },
86
4
        },
87
4
    );
88

            
89
4
    let de = ron::from_str::<RawStruct>("Hello+World").unwrap_err();
90
4
    assert_eq!(
91
4
        de,
92
4
        SpannedError {
93
4
            code: Error::SuggestRawIdentifier(String::from("Hello+World")),
94
4
            position: Position { line: 1, col: 1 },
95
4
        }
96
4
    );
97

            
98
4
    let de = ron::from_str::<RawStruct>(
99
4
        "r#Hello+World(
100
4
        ab.cd-ef: true,
101
4
    )",
102
4
    )
103
4
    .unwrap_err();
104
4
    assert_eq!(
105
4
        de,
106
4
        SpannedError {
107
4
            code: Error::SuggestRawIdentifier(String::from("ab.cd-ef")),
108
4
            position: Position { line: 2, col: 9 },
109
4
        }
110
4
    );
111

            
112
4
    let de = ron::from_str::<RawStruct>(
113
4
        "r#Hello+World(
114
4
        rab.cd-ef: true,
115
4
    )",
116
4
    )
117
4
    .unwrap_err();
118
4
    assert_eq!(
119
4
        de,
120
4
        SpannedError {
121
4
            code: Error::SuggestRawIdentifier(String::from("rab.cd-ef")),
122
4
            position: Position { line: 2, col: 9 },
123
4
        }
124
4
    );
125

            
126
4
    let de = ron::from_str::<RawStruct>(
127
4
        "r#Hello+World(
128
4
        r#ab.cd+ef: true,
129
4
    )",
130
4
    )
131
4
    .unwrap_err();
132
4
    assert_eq!(
133
4
        de,
134
4
        SpannedError {
135
4
            code: Error::NoSuchStructField {
136
4
                expected: &["ab.cd-ef", "really_not_raw"],
137
4
                found: String::from("ab.cd+ef"),
138
4
                outer: Some(String::from("Hello+World")),
139
4
            },
140
4
            position: Position { line: 2, col: 19 },
141
4
        }
142
4
    );
143

            
144
4
    let de = ron::from_str::<RawEnum>("Hello-World").unwrap_err();
145
4
    assert_eq!(
146
4
        de,
147
4
        SpannedError {
148
4
            code: Error::SuggestRawIdentifier(String::from("Hello-World")),
149
4
            position: Position { line: 1, col: 1 },
150
4
        }
151
4
    );
152

            
153
4
    let de = ron::from_str::<RawEnum>("r#Hello+World").unwrap_err();
154
4
    assert_eq!(
155
4
        de,
156
4
        SpannedError {
157
4
            code: Error::NoSuchEnumVariant {
158
4
                expected: &["Hello-World"],
159
4
                found: String::from("Hello+World"),
160
4
                outer: Some(String::from("RawEnum")),
161
4
            },
162
4
            position: Position { line: 1, col: 14 },
163
4
        }
164
4
    );
165

            
166
4
    let de = ron::from_str::<EmptyStruct>("r#+").unwrap_err();
167
4
    assert_eq!(
168
4
        format!("{}", de),
169
4
        r#"1:4: Expected only opening `(`, no name, for un-nameable struct"#,
170
4
    );
171
4
}
172

            
173
#[test]
174
4
fn test_raw_identifier_roundtrip() {
175
4
    let val = RawStruct {
176
4
        field: true,
177
4
        really_not_raw: 42,
178
4
    };
179
4

            
180
4
    let ser =
181
4
        ron::ser::to_string_pretty(&val, ron::ser::PrettyConfig::default().struct_names(true))
182
4
            .unwrap();
183
4
    assert_eq!(
184
4
        ser,
185
4
        "r#Hello+World(\n    r#ab.cd-ef: true,\n    really_not_raw: 42,\n)"
186
4
    );
187

            
188
4
    let de: RawStruct = ron::from_str(&ser).unwrap();
189
4
    assert_eq!(de, val);
190

            
191
4
    let val = RawEnum::RawVariant;
192
4

            
193
4
    let ser = ron::ser::to_string(&val).unwrap();
194
4
    assert_eq!(ser, "r#Hello-World");
195

            
196
4
    let de: RawEnum = ron::from_str(&ser).unwrap();
197
4
    assert_eq!(de, val);
198
4
}