| 1 | use ron::error::{Position, Span, 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 | #[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 | #[derive(Serialize, Deserialize, PartialEq, Eq, Debug)] |
| 22 | enum RawEnum { |
| 23 | #[serde(rename = "Hello-World")] |
| 24 | RawVariant, |
| 25 | } |
| 26 | |
| 27 | #[test] |
| 28 | fn test_invalid_identifiers() { |
| 29 | let ser = ron::ser::to_string_pretty( |
| 30 | &InvalidStruct, |
| 31 | ron::ser::PrettyConfig::default().struct_names(true), |
| 32 | ); |
| 33 | assert_eq!( |
| 34 | ser, |
| 35 | Err(ron::Error::InvalidIdentifier(String::from("Hello World"))) |
| 36 | ); |
| 37 | |
| 38 | let ser = ron::ser::to_string_pretty( |
| 39 | &EmptyStruct, |
| 40 | ron::ser::PrettyConfig::default().struct_names(true), |
| 41 | ); |
| 42 | assert_eq!(ser, Err(ron::Error::InvalidIdentifier(String::from("")))); |
| 43 | |
| 44 | let de = ron::from_str::<InvalidStruct>("Hello World").unwrap_err(); |
| 45 | assert_eq!( |
| 46 | de, |
| 47 | SpannedError { |
| 48 | code: ron::Error::ExpectedDifferentStructName { |
| 49 | expected: "Hello World", |
| 50 | found: String::from("Hello"), |
| 51 | }, |
| 52 | span: Span { |
| 53 | start: Position { line: 1, col: 1 }, |
| 54 | end: Position { line: 1, col: 6 }, |
| 55 | } |
| 56 | } |
| 57 | ); |
| 58 | |
| 59 | let de = ron::from_str::<EmptyStruct>("").unwrap_err(); |
| 60 | assert_eq!( |
| 61 | de, |
| 62 | SpannedError { |
| 63 | code: ron::Error::ExpectedUnit, |
| 64 | span: Span { |
| 65 | start: Position { line: 1, col: 1 }, |
| 66 | end: Position { line: 1, col: 1 }, |
| 67 | } |
| 68 | } |
| 69 | ); |
| 70 | |
| 71 | let de = ron::from_str::<EmptyStruct>("r#").unwrap_err(); |
| 72 | assert_eq!( |
| 73 | format!("{}", de), |
| 74 | "1:1: Expected only opening `(`, no name, for un-nameable struct" |
| 75 | ); |
| 76 | |
| 77 | let de = ron::from_str::<RawStruct>("").unwrap_err(); |
| 78 | assert_eq!( |
| 79 | de, |
| 80 | SpannedError { |
| 81 | code: ron::Error::ExpectedNamedStructLike("Hello+World"), |
| 82 | span: Span { |
| 83 | start: Position { line: 1, col: 1 }, |
| 84 | end: Position { line: 1, col: 1 }, |
| 85 | } |
| 86 | }, |
| 87 | ); |
| 88 | |
| 89 | let de = ron::from_str::<RawStruct>("r#").unwrap_err(); |
| 90 | assert_eq!( |
| 91 | de, |
| 92 | SpannedError { |
| 93 | code: ron::Error::ExpectedNamedStructLike("Hello+World"), |
| 94 | span: Span { |
| 95 | start: Position { line: 1, col: 1 }, |
| 96 | end: Position { line: 1, col: 1 }, |
| 97 | } |
| 98 | }, |
| 99 | ); |
| 100 | |
| 101 | let de = ron::from_str::<RawStruct>("Hello+World").unwrap_err(); |
| 102 | assert_eq!( |
| 103 | de, |
| 104 | SpannedError { |
| 105 | code: ron::Error::SuggestRawIdentifier(String::from("Hello+World")), |
| 106 | span: Span { |
| 107 | start: Position { line: 1, col: 1 }, |
| 108 | end: Position { line: 1, col: 1 }, |
| 109 | } |
| 110 | } |
| 111 | ); |
| 112 | |
| 113 | let de = ron::from_str::<RawStruct>( |
| 114 | "r#Hello+World( |
| 115 | ab.cd-ef: true, |
| 116 | )", |
| 117 | ) |
| 118 | .unwrap_err(); |
| 119 | assert_eq!( |
| 120 | de, |
| 121 | SpannedError { |
| 122 | code: ron::Error::SuggestRawIdentifier(String::from("ab.cd-ef")), |
| 123 | span: Span { |
| 124 | start: Position { line: 1, col: 15 }, |
| 125 | end: Position { line: 2, col: 9 }, |
| 126 | } |
| 127 | } |
| 128 | ); |
| 129 | |
| 130 | let de = ron::from_str::<RawStruct>( |
| 131 | "r#Hello+World( |
| 132 | rab.cd-ef: true, |
| 133 | )", |
| 134 | ) |
| 135 | .unwrap_err(); |
| 136 | assert_eq!( |
| 137 | de, |
| 138 | SpannedError { |
| 139 | code: ron::Error::SuggestRawIdentifier(String::from("rab.cd-ef")), |
| 140 | span: Span { |
| 141 | start: Position { line: 1, col: 15 }, |
| 142 | end: Position { line: 2, col: 9 }, |
| 143 | } |
| 144 | } |
| 145 | ); |
| 146 | |
| 147 | let de = ron::from_str::<RawStruct>( |
| 148 | "r#Hello+World( |
| 149 | r#ab.cd+ef: true, |
| 150 | )", |
| 151 | ) |
| 152 | .unwrap_err(); |
| 153 | assert_eq!( |
| 154 | de, |
| 155 | SpannedError { |
| 156 | code: ron::Error::NoSuchStructField { |
| 157 | expected: &["ab.cd-ef", "really_not_raw"], |
| 158 | found: String::from("ab.cd+ef"), |
| 159 | outer: Some(String::from("Hello+World")), |
| 160 | }, |
| 161 | span: Span { |
| 162 | start: Position { line: 2, col: 11 }, |
| 163 | end: Position { line: 2, col: 19 }, |
| 164 | } |
| 165 | } |
| 166 | ); |
| 167 | |
| 168 | let de = ron::from_str::<RawEnum>("Hello-World").unwrap_err(); |
| 169 | assert_eq!( |
| 170 | de, |
| 171 | SpannedError { |
| 172 | code: ron::Error::SuggestRawIdentifier(String::from("Hello-World")), |
| 173 | span: Span { |
| 174 | start: Position { line: 1, col: 1 }, |
| 175 | end: Position { line: 1, col: 1 }, |
| 176 | } |
| 177 | } |
| 178 | ); |
| 179 | |
| 180 | let de = ron::from_str::<RawEnum>("r#Hello+World").unwrap_err(); |
| 181 | assert_eq!( |
| 182 | de, |
| 183 | SpannedError { |
| 184 | code: ron::Error::NoSuchEnumVariant { |
| 185 | expected: &["Hello-World"], |
| 186 | found: String::from("Hello+World"), |
| 187 | outer: Some(String::from("RawEnum")), |
| 188 | }, |
| 189 | span: Span { |
| 190 | start: Position { line: 1, col: 3 }, |
| 191 | end: Position { line: 1, col: 14 }, |
| 192 | } |
| 193 | } |
| 194 | ); |
| 195 | |
| 196 | let de = ron::from_str::<EmptyStruct>("r#+").unwrap_err(); |
| 197 | assert_eq!( |
| 198 | format!("{}", de), |
| 199 | r#"1:3-1:4: Expected only opening `(`, no name, for un-nameable struct"#, |
| 200 | ); |
| 201 | } |
| 202 | |
| 203 | #[test] |
| 204 | fn test_raw_identifier_roundtrip() { |
| 205 | let val = RawStruct { |
| 206 | field: true, |
| 207 | really_not_raw: 42, |
| 208 | }; |
| 209 | |
| 210 | let ser = |
| 211 | ron::ser::to_string_pretty(&val, ron::ser::PrettyConfig::default().struct_names(true)) |
| 212 | .unwrap(); |
| 213 | assert_eq!( |
| 214 | ser, |
| 215 | "r#Hello+World(\n r#ab.cd-ef: true,\n really_not_raw: 42,\n)" |
| 216 | ); |
| 217 | |
| 218 | let de: RawStruct = ron::from_str(&ser).unwrap(); |
| 219 | assert_eq!(de, val); |
| 220 | |
| 221 | let val = RawEnum::RawVariant; |
| 222 | |
| 223 | let ser = ron::ser::to_string(&val).unwrap(); |
| 224 | assert_eq!(ser, "r#Hello-World"); |
| 225 | |
| 226 | let de: RawEnum = ron::from_str(&ser).unwrap(); |
| 227 | assert_eq!(de, val); |
| 228 | } |