Debian Codegraph @

third_party/rust/ron/tests/393_serde_errors.rs

raw
1 use ron::error::{Error, Position, SpannedError};
2
3 #[derive(Debug, serde::Deserialize, PartialEq)]
4 #[serde(deny_unknown_fields)]
5 enum TestEnum {
6 StructVariant { a: bool, b: char, c: i32 },
7 NewtypeVariant(TestStruct),
8 }
9
10 #[derive(Debug, serde::Deserialize, PartialEq)]
11 #[serde(tag = "type")]
12 enum TestEnumInternal {
13 StructVariant { a: bool },
14 }
15
16 #[derive(Debug, serde::Deserialize, PartialEq)]
17 #[serde(tag = "type", content = "content")]
18 enum TestEnumAdjacent {
19 StructVariant { a: bool },
20 }
21
22 #[derive(Debug, serde::Deserialize, PartialEq)]
23 #[serde(untagged)]
24 enum TestEnumUntagged {
25 StructVariant { a: bool },
26 }
27
28 #[derive(Debug, serde::Deserialize, PartialEq)]
29 #[serde(deny_unknown_fields)]
30 struct TestStruct {
31 a: bool,
32 b: char,
33 c: i32,
34 }
35
36 #[test]
37 fn test_unknown_enum_variant() {
38 assert_eq!(
39 ron::from_str::<TestEnum>("NotAVariant"),
40 Err(SpannedError {
41 code: Error::NoSuchEnumVariant {
42 expected: &["StructVariant", "NewtypeVariant"],
43 found: String::from("NotAVariant"),
44 outer: Some(String::from("TestEnum")),
45 },
46 position: Position { line: 1, col: 12 },
47 })
48 );
49 }
50
51 #[test]
52 fn test_struct_enum_fields() {
53 assert_eq!(
54 ron::from_str::<TestEnum>("StructVariant(a: true, b: 'b', c: -42, d: \"gotcha\")"),
55 Err(SpannedError {
56 code: Error::NoSuchStructField {
57 expected: &["a", "b", "c"],
58 found: String::from("d"),
59 outer: Some(String::from("StructVariant")),
60 },
61 position: Position { line: 1, col: 41 },
62 })
63 );
64
65 assert_eq!(
66 ron::from_str::<TestEnum>("StructVariant(a: true, c: -42)"),
67 Err(SpannedError {
68 code: Error::MissingStructField {
69 field: "b",
70 outer: Some(String::from("StructVariant")),
71 },
72 position: Position { line: 1, col: 30 },
73 })
74 );
75
76 assert_eq!(
77 ron::from_str::<TestEnum>("StructVariant(a: true, b: 'b', a: false, c: -42)"),
78 Err(SpannedError {
79 code: Error::DuplicateStructField {
80 field: "a",
81 outer: Some(String::from("StructVariant")),
82 },
83 position: Position { line: 1, col: 33 },
84 })
85 );
86 }
87
88 #[test]
89 fn test_newtype_enum_fields() {
90 assert_eq!(
91 ron::from_str::<TestEnum>("#![enable(unwrap_variant_newtypes)] NewtypeVariant(a: true, b: 'b', c: -42, d: \"gotcha\")"),
92 Err(SpannedError {
93 code: Error::NoSuchStructField {
94 expected: &["a", "b", "c"],
95 found: String::from("d"),
96 outer: Some(String::from("NewtypeVariant")),
97 },
98 position: Position { line: 1, col: 78 },
99 })
100 );
101
102 assert_eq!(
103 ron::from_str::<TestEnum>(
104 "#![enable(unwrap_variant_newtypes)] NewtypeVariant(a: true, c: -42)"
105 ),
106 Err(SpannedError {
107 code: Error::MissingStructField {
108 field: "b",
109 outer: Some(String::from("NewtypeVariant")),
110 },
111 position: Position { line: 1, col: 67 },
112 })
113 );
114
115 assert_eq!(
116 ron::from_str::<TestEnum>(
117 "#![enable(unwrap_variant_newtypes)] NewtypeVariant(a: true, b: 'b', a: false, c: -42)"
118 ),
119 Err(SpannedError {
120 code: Error::DuplicateStructField {
121 field: "a",
122 outer: Some(String::from("NewtypeVariant")),
123 },
124 position: Position { line: 1, col: 70 },
125 })
126 );
127 }
128
129 #[test]
130 fn test_struct_fields() {
131 assert_eq!(
132 ron::from_str::<TestStruct>("TestStruct(a: true, b: 'b', c: -42, d: \"gotcha\")"),
133 Err(SpannedError {
134 code: Error::NoSuchStructField {
135 expected: &["a", "b", "c"],
136 found: String::from("d"),
137 outer: Some(String::from("TestStruct")),
138 },
139 position: Position { line: 1, col: 38 },
140 })
141 );
142
143 assert_eq!(
144 ron::from_str::<TestStruct>("TestStruct(a: true, c: -42)"),
145 Err(SpannedError {
146 code: Error::MissingStructField {
147 field: "b",
148 outer: Some(String::from("TestStruct")),
149 },
150 position: Position { line: 1, col: 27 },
151 })
152 );
153
154 assert_eq!(
155 ron::from_str::<TestStruct>("TestStruct(a: true, b: 'b', a: false, c: -42)"),
156 Err(SpannedError {
157 code: Error::DuplicateStructField {
158 field: "a",
159 outer: Some(String::from("TestStruct")),
160 },
161 position: Position { line: 1, col: 30 },
162 })
163 );
164 }
165
166 #[test]
167 fn test_internally_tagged_enum() {
168 // Note: Not extracting the variant type is not great,
169 // but at least not wrong either
170 // Since the error occurs in serde-generated user code,
171 // after successfully deserialising, we cannot annotate
172
173 assert_eq!(
174 ron::from_str::<TestEnumInternal>("(type: \"StructVariant\")"),
175 Err(SpannedError {
176 code: Error::MissingStructField {
177 field: "a",
178 outer: None,
179 },
180 position: Position { line: 1, col: 24 },
181 })
182 );
183 }
184
185 #[test]
186 fn test_adjacently_tagged_enum() {
187 // Note: TestEnumAdjacent makes sense here since we are now treating
188 // the enum as a struct
189
190 assert_eq!(
191 ron::from_str::<TestEnumAdjacent>("(type: StructVariant, content: (d: 4))"),
192 Err(SpannedError {
193 code: Error::MissingStructField {
194 field: "a",
195 outer: Some(String::from("TestEnumAdjacent")),
196 },
197 position: Position { line: 1, col: 37 },
198 })
199 );
200 }
201
202 #[test]
203 fn test_untagged_enum() {
204 // Note: Errors inside untagged enums are not bubbled up
205
206 assert_eq!(
207 ron::from_str::<TestEnumUntagged>("(a: true, a: false)"),
208 Err(SpannedError {
209 code: Error::Message(String::from(
210 "data did not match any variant of untagged enum TestEnumUntagged"
211 )),
212 position: Position { line: 1, col: 20 },
213 })
214 );
215 }