| 1 |
use ron::{ |
| 2 |
de::from_bytes, |
| 3 |
error::{Error, Position, SpannedError}, |
| 4 |
from_str, to_string, |
| 5 |
value::RawValue, |
| 6 |
}; |
| 7 |
use serde::{Deserialize, Serialize}; |
| 8 |
|
| 9 |
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)] |
| 10 |
struct WithRawValue { |
| 11 |
a: bool, |
| 12 |
b: Box<RawValue>, |
| 13 |
} |
| 14 |
|
| 15 |
#[test] |
| 16 |
fn test_raw_value_simple() { |
| 17 |
let raw: &RawValue = from_str("true").unwrap(); |
| 18 |
assert_eq!(raw.get_ron(), "true"); |
| 19 |
let ser = to_string(raw).unwrap(); |
| 20 |
assert_eq!(ser, "true"); |
| 21 |
} |
| 22 |
|
| 23 |
#[test] |
| 24 |
fn test_raw_value_inner() { |
| 25 |
let raw: WithRawValue = from_str("(a: false, b: [1, /* lol */ 2, 3])").unwrap(); |
| 26 |
assert_eq!(raw.b.get_ron(), " [1, /* lol */ 2, 3]"); |
| 27 |
let ser = to_string(&raw).unwrap(); |
| 28 |
assert_eq!(ser, "(a:false,b: [1, /* lol */ 2, 3])"); |
| 29 |
} |
| 30 |
|
| 31 |
#[test] |
| 32 |
fn test_raw_value_comment() { |
| 33 |
let raw: WithRawValue = from_str("(a: false, b: /* yeah */ 4)").unwrap(); |
| 34 |
assert_eq!(raw.b.get_ron(), " /* yeah */ 4"); |
| 35 |
|
| 36 |
let raw: WithRawValue = from_str("(a: false, b: 4 /* yes */)").unwrap(); |
| 37 |
assert_eq!(raw.b.get_ron(), " 4 /* yes */"); |
| 38 |
|
| 39 |
let raw: WithRawValue = from_str("(a: false, b: (/* this */ 4 /* too */))").unwrap(); |
| 40 |
assert_eq!(raw.b.get_ron(), " (/* this */ 4 /* too */)"); |
| 41 |
} |
| 42 |
|
| 43 |
#[test] |
| 44 |
fn test_raw_value_invalid() { |
| 45 |
let err = from_str::<&RawValue>("4.d").unwrap_err(); |
| 46 |
assert_eq!( |
| 47 |
err, |
| 48 |
SpannedError { |
| 49 |
code: Error::TrailingCharacters, |
| 50 |
position: Position { line: 1, col: 3 } |
| 51 |
} |
| 52 |
); |
| 53 |
|
| 54 |
let err = from_bytes::<&RawValue>(b"\0").unwrap_err(); |
| 55 |
assert_eq!( |
| 56 |
err, |
| 57 |
SpannedError { |
| 58 |
code: Error::UnexpectedChar('\0'), |
| 59 |
position: Position { line: 1, col: 1 } |
| 60 |
} |
| 61 |
) |
| 62 |
} |
| 63 |
|
| 64 |
#[test] |
| 65 |
fn test_raw_value_from_ron() { |
| 66 |
let raw = RawValue::from_ron("/* hi */ (None, 4.2) /* bye */").unwrap(); |
| 67 |
assert_eq!(raw.get_ron(), "/* hi */ (None, 4.2) /* bye */"); |
| 68 |
|
| 69 |
let err = RawValue::from_ron("4.d").unwrap_err(); |
| 70 |
assert_eq!( |
| 71 |
err, |
| 72 |
SpannedError { |
| 73 |
code: Error::TrailingCharacters, |
| 74 |
position: Position { line: 1, col: 3 } |
| 75 |
} |
| 76 |
); |
| 77 |
|
| 78 |
let raw = |
| 79 |
RawValue::from_boxed_ron(String::from("/* hi */ (None, 4.2) /* bye */").into_boxed_str()) |
| 80 |
.unwrap(); |
| 81 |
assert_eq!(raw.get_ron(), "/* hi */ (None, 4.2) /* bye */"); |
| 82 |
|
| 83 |
let err = RawValue::from_boxed_ron(String::from("(").into_boxed_str()).unwrap_err(); |
| 84 |
assert_eq!( |
| 85 |
err, |
| 86 |
SpannedError { |
| 87 |
code: Error::Eof, |
| 88 |
position: Position { line: 1, col: 2 }, |
| 89 |
} |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
#[test] |
| 94 |
fn test_raw_value_into_rust() { |
| 95 |
let raw = RawValue::from_ron("/* hi */ (a: false, b: None) /* bye */").unwrap(); |
| 96 |
|
| 97 |
let with: WithRawValue = raw.into_rust().unwrap(); |
| 98 |
assert_eq!( |
| 99 |
with, |
| 100 |
WithRawValue { |
| 101 |
a: false, |
| 102 |
b: from_str(" None").unwrap(), |
| 103 |
} |
| 104 |
); |
| 105 |
|
| 106 |
let err = raw.into_rust::<i32>().unwrap_err(); |
| 107 |
assert_eq!( |
| 108 |
err, |
| 109 |
SpannedError { |
| 110 |
code: Error::ExpectedInteger, |
| 111 |
position: Position { line: 1, col: 10 }, |
| 112 |
} |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
#[test] |
| 117 |
fn test_raw_value_from_rust() { |
| 118 |
let raw = RawValue::from_rust(&42).unwrap(); |
| 119 |
assert_eq!(raw.get_ron(), "42"); |
| 120 |
|
| 121 |
let raw = RawValue::from_rust(&WithRawValue { |
| 122 |
a: true, |
| 123 |
b: from_str("4.2").unwrap(), |
| 124 |
}) |
| 125 |
.unwrap(); |
| 126 |
assert_eq!(raw.get_ron(), "(a:true,b:4.2)"); |
| 127 |
} |
| 128 |
|
| 129 |
#[test] |
| 130 |
fn test_raw_value_serde_json() { |
| 131 |
let raw = RawValue::from_ron("/* hi */ (None, 4.2) /* bye */").unwrap(); |
| 132 |
|
| 133 |
let ser = serde_json::to_string(&WithRawValue { |
| 134 |
a: true, |
| 135 |
b: raw.to_owned(), |
| 136 |
}) |
| 137 |
.unwrap(); |
| 138 |
assert_eq!(ser, "{\"a\":true,\"b\":\"/* hi */ (None, 4.2) /* bye */\"}"); |
| 139 |
|
| 140 |
let with: WithRawValue = serde_json::from_str(&ser).unwrap(); |
| 141 |
assert_eq!(raw, &*with.b); |
| 142 |
|
| 143 |
let err = |
| 144 |
serde_json::from_str::<WithRawValue>("{\"a\":true,\"b\":\"/* hi */ (a:) /* bye */\"}") |
| 145 |
.unwrap_err(); |
| 146 |
assert_eq!( |
| 147 |
err.to_string(), |
| 148 |
"invalid RON value at 1:13: Unexpected char ')' at line 1 column 39" |
| 149 |
); |
| 150 |
|
| 151 |
let err = serde_json::from_str::<WithRawValue>("{\"a\":true,\"b\":42}").unwrap_err(); |
| 152 |
assert_eq!( |
| 153 |
err.to_string(), |
| 154 |
"invalid type: integer `42`, expected any valid RON-value-string at line 1 column 16" |
| 155 |
); |
| 156 |
|
| 157 |
let err = serde_json::from_str::<&RawValue>("\"/* hi */ (a:) /* bye */\"").unwrap_err(); |
| 158 |
assert_eq!( |
| 159 |
err.to_string(), |
| 160 |
"invalid RON value at 1:13: Unexpected char ')' at line 1 column 25" |
| 161 |
); |
| 162 |
|
| 163 |
let err = serde_json::from_str::<&RawValue>("42").unwrap_err(); |
| 164 |
assert_eq!( |
| 165 |
err.to_string(), |
| 166 |
"invalid type: integer `42`, expected any valid borrowed RON-value-string at line 1 column 2" |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
#[test] |
| 171 |
fn test_raw_value_clone_into() { |
| 172 |
let raw = RawValue::from_boxed_ron(String::from("(None, 4.2)").into_boxed_str()).unwrap(); |
| 173 |
let raw2 = raw.clone(); |
| 174 |
assert_eq!(raw, raw2); |
| 175 |
|
| 176 |
let boxed_str: Box<str> = raw2.into(); |
| 177 |
assert_eq!(&*boxed_str, "(None, 4.2)"); |
| 178 |
} |
| 179 |
|
| 180 |
#[test] |
| 181 |
fn test_raw_value_debug_display() { |
| 182 |
let raw = RawValue::from_ron("/* hi */ (None, 4.2) /* bye */").unwrap(); |
| 183 |
|
| 184 |
assert_eq!(format!("{}", raw), "/* hi */ (None, 4.2) /* bye */"); |
| 185 |
assert_eq!( |
| 186 |
format!("{:#?}", raw), |
| 187 |
"\ |
| 188 |
RawValue( |
| 189 |
/* hi */ (None, 4.2) /* bye */, |
| 190 |
)\ |
| 191 |
" |
| 192 |
); |
| 193 |
} |
| 194 |
|
| 195 |
#[test] |
| 196 |
fn test_boxed_raw_value_deserialise_from_string() { |
| 197 |
let string = serde::de::value::StringDeserializer::<Error>::new(String::from("4.2")); |
| 198 |
|
| 199 |
let err = <&RawValue>::deserialize(string.clone()).unwrap_err(); |
| 200 |
assert_eq!( |
| 201 |
err, |
| 202 |
Error::InvalidValueForType { |
| 203 |
expected: String::from("any valid borrowed RON-value-string"), |
| 204 |
found: String::from("the string \"4.2\""), |
| 205 |
} |
| 206 |
); |
| 207 |
|
| 208 |
let boxed_raw = Box::<RawValue>::deserialize(string).unwrap(); |
| 209 |
assert_eq!(boxed_raw.get_ron(), "4.2"); |
| 210 |
|
| 211 |
let string = serde::de::value::StringDeserializer::<Error>::new(String::from("[")); |
| 212 |
|
| 213 |
let err = Box::<RawValue>::deserialize(string).unwrap_err(); |
| 214 |
assert_eq!( |
| 215 |
err, |
| 216 |
Error::Message(String::from( |
| 217 |
"invalid RON value at 1:2: Unexpected end of RON" |
| 218 |
)) |
| 219 |
); |
| 220 |
} |
| 221 |
|
| 222 |
#[test] |
| 223 |
fn test_whitespace_rountrip_issues_and_trim() { |
| 224 |
let mut v = WithRawValue { |
| 225 |
a: true, |
| 226 |
b: RawValue::from_ron("42 ").unwrap().to_owned(), |
| 227 |
}; |
| 228 |
v = ron::from_str(&ron::to_string(&v).unwrap()).unwrap(); |
| 229 |
assert_eq!(v.b.get_ron(), "42 "); |
| 230 |
assert_eq!(v.b.trim().get_ron(), "42"); |
| 231 |
assert_eq!(v.b.to_owned().trim_boxed().get_ron(), "42"); |
| 232 |
|
| 233 |
for i in 1..=10 { |
| 234 |
v = ron::from_str( |
| 235 |
&ron::ser::to_string_pretty(&v, ron::ser::PrettyConfig::default()).unwrap(), |
| 236 |
) |
| 237 |
.unwrap(); |
| 238 |
assert_eq!(v.b.get_ron(), &format!("{: <1$}42 ", "", i)); |
| 239 |
assert_eq!(v.b.trim().get_ron(), "42"); |
| 240 |
assert_eq!(v.b.to_owned().trim_boxed().get_ron(), "42"); |
| 241 |
} |
| 242 |
|
| 243 |
assert_eq!(RawValue::from_ron("42").unwrap().trim().get_ron(), "42"); |
| 244 |
assert_eq!( |
| 245 |
RawValue::from_ron(" /* start */ 42 // end\n ") |
| 246 |
.unwrap() |
| 247 |
.trim() |
| 248 |
.get_ron(), |
| 249 |
"42" |
| 250 |
); |
| 251 |
|
| 252 |
assert_eq!( |
| 253 |
RawValue::from_ron("42") |
| 254 |
.unwrap() |
| 255 |
.to_owned() |
| 256 |
.trim_boxed() |
| 257 |
.get_ron(), |
| 258 |
"42" |
| 259 |
); |
| 260 |
assert_eq!( |
| 261 |
RawValue::from_ron(" /* start */ 42 // end\n ") |
| 262 |
.unwrap() |
| 263 |
.to_owned() |
| 264 |
.trim_boxed() |
| 265 |
.get_ron(), |
| 266 |
"42" |
| 267 |
); |
| 268 |
} |
| 269 |
|
| 270 |
#[test] |
| 271 |
fn test_fuzzer_found_issue() { |
| 272 |
assert_eq!( |
| 273 |
RawValue::from_ron(""), |
| 274 |
Err(SpannedError { |
| 275 |
code: Error::Eof, |
| 276 |
position: Position { line: 1, col: 1 }, |
| 277 |
}) |
| 278 |
); |
| 279 |
|
| 280 |
assert_eq!(ron::from_str("C"), RawValue::from_ron("C")); |
| 281 |
|
| 282 |
assert_eq!(ron::from_str(" t "), RawValue::from_ron(" t ")); |
| 283 |
|
| 284 |
assert_eq!( |
| 285 |
RawValue::from_ron("#![enable(implicit_some)] 42"), |
| 286 |
Err(SpannedError { |
| 287 |
code: Error::Message(String::from( |
| 288 |
"ron::value::RawValue cannot enable extensions" |
| 289 |
)), |
| 290 |
position: Position { line: 1, col: 27 }, |
| 291 |
}) |
| 292 |
); |
| 293 |
|
| 294 |
assert_eq!( |
| 295 |
RawValue::from_boxed_ron(String::from("#![enable(implicit_some)] 42").into_boxed_str()), |
| 296 |
Err(SpannedError { |
| 297 |
code: Error::Message(String::from( |
| 298 |
"ron::value::RawValue cannot enable extensions" |
| 299 |
)), |
| 300 |
position: Position { line: 1, col: 27 }, |
| 301 |
}) |
| 302 |
); |
| 303 |
|
| 304 |
assert_eq!( |
| 305 |
RawValue::from_ron("42 //"), |
| 306 |
Err(SpannedError { |
| 307 |
code: Error::UnclosedLineComment, |
| 308 |
position: Position { line: 1, col: 6 }, |
| 309 |
}) |
| 310 |
); |
| 311 |
assert_eq!( |
| 312 |
ron::from_str::<&RawValue>("42 //"), |
| 313 |
Err(SpannedError { |
| 314 |
code: Error::UnclosedLineComment, |
| 315 |
position: Position { line: 1, col: 6 }, |
| 316 |
}) |
| 317 |
); |
| 318 |
assert_eq!( |
| 319 |
ron::from_str::<&RawValue>("42 //\n").unwrap(), |
| 320 |
RawValue::from_ron("42 //\n").unwrap() |
| 321 |
); |
| 322 |
assert_eq!( |
| 323 |
ron::from_str::<&RawValue>("42 /**/").unwrap(), |
| 324 |
RawValue::from_ron("42 /**/").unwrap() |
| 325 |
); |
| 326 |
assert_eq!( |
| 327 |
ron::from_str::<&RawValue>("42 ").unwrap(), |
| 328 |
RawValue::from_ron("42 ").unwrap() |
| 329 |
); |
| 330 |
|
| 331 |
assert_eq!( |
| 332 |
RawValue::from_ron("a//"), |
| 333 |
Err(SpannedError { |
| 334 |
code: Error::UnclosedLineComment, |
| 335 |
position: Position { line: 1, col: 4 }, |
| 336 |
}) |
| 337 |
); |
| 338 |
assert_eq!( |
| 339 |
ron::from_str::<&RawValue>("a//"), |
| 340 |
Err(SpannedError { |
| 341 |
code: Error::UnclosedLineComment, |
| 342 |
position: Position { line: 1, col: 4 }, |
| 343 |
}) |
| 344 |
); |
| 345 |
assert_eq!( |
| 346 |
ron::from_str::<&RawValue>("a//\n").unwrap(), |
| 347 |
RawValue::from_ron("a//\n").unwrap() |
| 348 |
); |
| 349 |
assert_eq!( |
| 350 |
ron::from_str::<&RawValue>("a/**/").unwrap(), |
| 351 |
RawValue::from_ron("a/**/").unwrap() |
| 352 |
); |
| 353 |
assert_eq!( |
| 354 |
ron::from_str::<&RawValue>("a").unwrap(), |
| 355 |
RawValue::from_ron("a").unwrap() |
| 356 |
); |
| 357 |
|
| 358 |
assert_eq!( |
| 359 |
ron::to_string(&Some(Some(RawValue::from_ron("None").unwrap()))).unwrap(), |
| 360 |
"Some(Some(None))" |
| 361 |
); |
| 362 |
// Since a RawValue can contain anything, no implicit Some are allowed around it |
| 363 |
assert_eq!( |
| 364 |
ron::Options::default() |
| 365 |
.with_default_extension(ron::extensions::Extensions::IMPLICIT_SOME) |
| 366 |
.to_string(&Some(Some(RawValue::from_ron("None").unwrap()))) |
| 367 |
.unwrap(), |
| 368 |
"Some(Some(None))" |
| 369 |
); |
| 370 |
assert_eq!( |
| 371 |
ron::from_str::<Option<Option<&RawValue>>>("Some(Some(None))").unwrap(), |
| 372 |
Some(Some(RawValue::from_ron("None").unwrap())) |
| 373 |
); |
| 374 |
} |