| 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 |
#[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(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(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: Error::ExpectedDifferentStructName { |
| 49 |
expected: "Hello World", |
| 50 |
found: String::from("Hello"), |
| 51 |
}, |
| 52 |
position: Position { line: 1, col: 6 }, |
| 53 |
} |
| 54 |
); |
| 55 |
|
| 56 |
let de = ron::from_str::<EmptyStruct>("").unwrap_err(); |
| 57 |
assert_eq!( |
| 58 |
de, |
| 59 |
SpannedError { |
| 60 |
code: Error::ExpectedUnit, |
| 61 |
position: Position { line: 1, col: 1 }, |
| 62 |
} |
| 63 |
); |
| 64 |
|
| 65 |
let de = ron::from_str::<EmptyStruct>("r#").unwrap_err(); |
| 66 |
assert_eq!( |
| 67 |
format!("{}", de), |
| 68 |
"1:1: Expected only opening `(`, no name, for un-nameable struct" |
| 69 |
); |
| 70 |
|
| 71 |
let de = ron::from_str::<RawStruct>("").unwrap_err(); |
| 72 |
assert_eq!( |
| 73 |
de, |
| 74 |
SpannedError { |
| 75 |
code: Error::ExpectedNamedStructLike("Hello+World"), |
| 76 |
position: Position { line: 1, col: 1 }, |
| 77 |
}, |
| 78 |
); |
| 79 |
|
| 80 |
let de = ron::from_str::<RawStruct>("r#").unwrap_err(); |
| 81 |
assert_eq!( |
| 82 |
de, |
| 83 |
SpannedError { |
| 84 |
code: Error::ExpectedNamedStructLike("Hello+World"), |
| 85 |
position: Position { line: 1, col: 1 }, |
| 86 |
}, |
| 87 |
); |
| 88 |
|
| 89 |
let de = ron::from_str::<RawStruct>("Hello+World").unwrap_err(); |
| 90 |
assert_eq!( |
| 91 |
de, |
| 92 |
SpannedError { |
| 93 |
code: Error::SuggestRawIdentifier(String::from("Hello+World")), |
| 94 |
position: Position { line: 1, col: 1 }, |
| 95 |
} |
| 96 |
); |
| 97 |
|
| 98 |
let de = ron::from_str::<RawStruct>( |
| 99 |
"r#Hello+World( |
| 100 |
ab.cd-ef: true, |
| 101 |
)", |
| 102 |
) |
| 103 |
.unwrap_err(); |
| 104 |
assert_eq!( |
| 105 |
de, |
| 106 |
SpannedError { |
| 107 |
code: Error::SuggestRawIdentifier(String::from("ab.cd-ef")), |
| 108 |
position: Position { line: 2, col: 9 }, |
| 109 |
} |
| 110 |
); |
| 111 |
|
| 112 |
let de = ron::from_str::<RawStruct>( |
| 113 |
"r#Hello+World( |
| 114 |
rab.cd-ef: true, |
| 115 |
)", |
| 116 |
) |
| 117 |
.unwrap_err(); |
| 118 |
assert_eq!( |
| 119 |
de, |
| 120 |
SpannedError { |
| 121 |
code: Error::SuggestRawIdentifier(String::from("rab.cd-ef")), |
| 122 |
position: Position { line: 2, col: 9 }, |
| 123 |
} |
| 124 |
); |
| 125 |
|
| 126 |
let de = ron::from_str::<RawStruct>( |
| 127 |
"r#Hello+World( |
| 128 |
r#ab.cd+ef: true, |
| 129 |
)", |
| 130 |
) |
| 131 |
.unwrap_err(); |
| 132 |
assert_eq!( |
| 133 |
de, |
| 134 |
SpannedError { |
| 135 |
code: Error::NoSuchStructField { |
| 136 |
expected: &["ab.cd-ef", "really_not_raw"], |
| 137 |
found: String::from("ab.cd+ef"), |
| 138 |
outer: Some(String::from("Hello+World")), |
| 139 |
}, |
| 140 |
position: Position { line: 2, col: 19 }, |
| 141 |
} |
| 142 |
); |
| 143 |
|
| 144 |
let de = ron::from_str::<RawEnum>("Hello-World").unwrap_err(); |
| 145 |
assert_eq!( |
| 146 |
de, |
| 147 |
SpannedError { |
| 148 |
code: Error::SuggestRawIdentifier(String::from("Hello-World")), |
| 149 |
position: Position { line: 1, col: 1 }, |
| 150 |
} |
| 151 |
); |
| 152 |
|
| 153 |
let de = ron::from_str::<RawEnum>("r#Hello+World").unwrap_err(); |
| 154 |
assert_eq!( |
| 155 |
de, |
| 156 |
SpannedError { |
| 157 |
code: Error::NoSuchEnumVariant { |
| 158 |
expected: &["Hello-World"], |
| 159 |
found: String::from("Hello+World"), |
| 160 |
outer: Some(String::from("RawEnum")), |
| 161 |
}, |
| 162 |
position: Position { line: 1, col: 14 }, |
| 163 |
} |
| 164 |
); |
| 165 |
|
| 166 |
let de = ron::from_str::<EmptyStruct>("r#+").unwrap_err(); |
| 167 |
assert_eq!( |
| 168 |
format!("{}", de), |
| 169 |
r#"1:4: Expected only opening `(`, no name, for un-nameable struct"#, |
| 170 |
); |
| 171 |
} |
| 172 |
|
| 173 |
#[test] |
| 174 |
fn test_raw_identifier_roundtrip() { |
| 175 |
let val = RawStruct { |
| 176 |
field: true, |
| 177 |
really_not_raw: 42, |
| 178 |
}; |
| 179 |
|
| 180 |
let ser = |
| 181 |
ron::ser::to_string_pretty(&val, ron::ser::PrettyConfig::default().struct_names(true)) |
| 182 |
.unwrap(); |
| 183 |
assert_eq!( |
| 184 |
ser, |
| 185 |
"r#Hello+World(\n r#ab.cd-ef: true,\n really_not_raw: 42,\n)" |
| 186 |
); |
| 187 |
|
| 188 |
let de: RawStruct = ron::from_str(&ser).unwrap(); |
| 189 |
assert_eq!(de, val); |
| 190 |
|
| 191 |
let val = RawEnum::RawVariant; |
| 192 |
|
| 193 |
let ser = ron::ser::to_string(&val).unwrap(); |
| 194 |
assert_eq!(ser, "r#Hello-World"); |
| 195 |
|
| 196 |
let de: RawEnum = ron::from_str(&ser).unwrap(); |
| 197 |
assert_eq!(de, val); |
| 198 |
} |