| 1 |
#![allow(dead_code)] |
| 2 |
|
| 3 |
use ron::error::{Error, Position, SpannedError}; |
| 4 |
|
| 5 |
#[derive(Debug, serde::Deserialize)] |
| 6 |
struct Test { |
| 7 |
a: i32, |
| 8 |
b: i32, |
| 9 |
} |
| 10 |
|
| 11 |
#[test] |
| 12 |
fn test_missing_comma_error() { |
| 13 |
let tuple_string = r#"( |
| 14 |
1 // <-- forgotten comma here |
| 15 |
2 |
| 16 |
)"#; |
| 17 |
|
| 18 |
assert_eq!( |
| 19 |
ron::from_str::<(i32, i32)>(tuple_string).unwrap_err(), |
| 20 |
SpannedError { |
| 21 |
code: Error::ExpectedComma, |
| 22 |
position: Position { line: 3, col: 9 } |
| 23 |
} |
| 24 |
); |
| 25 |
|
| 26 |
let list_string = r#"[ |
| 27 |
0, |
| 28 |
1 // <-- forgotten comma here |
| 29 |
2 |
| 30 |
]"#; |
| 31 |
|
| 32 |
assert_eq!( |
| 33 |
ron::from_str::<Vec<i32>>(list_string).unwrap_err(), |
| 34 |
SpannedError { |
| 35 |
code: Error::ExpectedComma, |
| 36 |
position: Position { line: 4, col: 9 } |
| 37 |
} |
| 38 |
); |
| 39 |
|
| 40 |
let struct_string = r#"Test( |
| 41 |
a: 1 // <-- forgotten comma here |
| 42 |
b: 2 |
| 43 |
)"#; |
| 44 |
|
| 45 |
assert_eq!( |
| 46 |
ron::from_str::<Test>(struct_string).unwrap_err(), |
| 47 |
SpannedError { |
| 48 |
code: Error::ExpectedComma, |
| 49 |
position: Position { line: 3, col: 9 } |
| 50 |
} |
| 51 |
); |
| 52 |
|
| 53 |
let map_string = r#"{ |
| 54 |
"a": 1 // <-- forgotten comma here |
| 55 |
"b": 2 |
| 56 |
}"#; |
| 57 |
|
| 58 |
assert_eq!( |
| 59 |
ron::from_str::<std::collections::HashMap<String, i32>>(map_string).unwrap_err(), |
| 60 |
SpannedError { |
| 61 |
code: Error::ExpectedComma, |
| 62 |
position: Position { line: 3, col: 9 } |
| 63 |
} |
| 64 |
); |
| 65 |
|
| 66 |
let extensions_string = r#"#![enable( |
| 67 |
implicit_some // <-- forgotten comma here |
| 68 |
unwrap_newtypes |
| 69 |
]) 42"#; |
| 70 |
|
| 71 |
assert_eq!( |
| 72 |
ron::from_str::<u8>(extensions_string).unwrap_err(), |
| 73 |
SpannedError { |
| 74 |
code: Error::ExpectedComma, |
| 75 |
position: Position { line: 3, col: 9 } |
| 76 |
} |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
#[test] |
| 81 |
fn test_comma_end() { |
| 82 |
assert_eq!(ron::from_str::<(i32, i32)>("(0, 1)").unwrap(), (0, 1)); |
| 83 |
assert_eq!(ron::from_str::<(i32, i32)>("(0, 1,)").unwrap(), (0, 1)); |
| 84 |
assert_eq!(ron::from_str::<()>("()"), Ok(())); |
| 85 |
} |