| 1 | #![allow(dead_code)] |
| 2 | |
| 3 | use ron::error::{Error, Position, Span, SpannedError}; |
| 4 | |
| 5 | #[cfg(feature = "internal-span-substring-test")] |
| 6 | use ron::util::span_substring::check_error_span_inclusive; |
| 7 | |
| 8 | #[derive(Debug, serde::Deserialize)] |
| 9 | struct Test { |
| 10 | a: i32, |
| 11 | b: i32, |
| 12 | } |
| 13 | |
| 14 | #[test] |
| 15 | fn test_missing_comma_error() { |
| 16 | let tuple_string = r#"( |
| 17 | 1 // <-- forgotten comma here |
| 18 | 2 |
| 19 | )"#; |
| 20 | |
| 21 | assert_eq!( |
| 22 | ron::from_str::<(i32, i32)>(tuple_string).unwrap_err(), |
| 23 | SpannedError { |
| 24 | code: Error::ExpectedComma, |
| 25 | span: Span { |
| 26 | start: Position { line: 3, col: 9 }, |
| 27 | end: Position { line: 3, col: 9 } |
| 28 | } |
| 29 | } |
| 30 | ); |
| 31 | |
| 32 | let list_string = r#"[ |
| 33 | 0, |
| 34 | 1 // <-- forgotten comma here |
| 35 | 2 |
| 36 | ]"#; |
| 37 | |
| 38 | assert_eq!( |
| 39 | ron::from_str::<Vec<i32>>(list_string).unwrap_err(), |
| 40 | SpannedError { |
| 41 | code: Error::ExpectedComma, |
| 42 | span: Span { |
| 43 | start: Position { line: 4, col: 9 }, |
| 44 | end: Position { line: 4, col: 9 } |
| 45 | } |
| 46 | } |
| 47 | ); |
| 48 | |
| 49 | let struct_string = r#"Test( |
| 50 | a: 1 // <-- forgotten comma here |
| 51 | b: 2 |
| 52 | )"#; |
| 53 | |
| 54 | assert_eq!( |
| 55 | ron::from_str::<Test>(struct_string).unwrap_err(), |
| 56 | SpannedError { |
| 57 | code: Error::ExpectedComma, |
| 58 | span: Span { |
| 59 | start: Position { line: 3, col: 9 }, |
| 60 | end: Position { line: 3, col: 9 } |
| 61 | } |
| 62 | } |
| 63 | ); |
| 64 | |
| 65 | let map_string = r#"{ |
| 66 | "a": 1 // <-- forgotten comma here |
| 67 | "b": 2 |
| 68 | }"#; |
| 69 | |
| 70 | assert_eq!( |
| 71 | ron::from_str::<std::collections::HashMap<String, i32>>(map_string).unwrap_err(), |
| 72 | SpannedError { |
| 73 | code: Error::ExpectedComma, |
| 74 | span: Span { |
| 75 | start: Position { line: 3, col: 9 }, |
| 76 | end: Position { line: 3, col: 9 } |
| 77 | } |
| 78 | } |
| 79 | ); |
| 80 | |
| 81 | let extensions_string = r#"#![enable( |
| 82 | implicit_some // <-- forgotten comma here |
| 83 | unwrap_newtypes |
| 84 | ]) 42"#; |
| 85 | |
| 86 | assert_eq!( |
| 87 | ron::from_str::<u8>(extensions_string).unwrap_err(), |
| 88 | SpannedError { |
| 89 | code: Error::ExpectedComma, |
| 90 | span: Span { |
| 91 | start: Position { line: 2, col: 50 }, |
| 92 | end: Position { line: 3, col: 9 } |
| 93 | } |
| 94 | } |
| 95 | ); |
| 96 | |
| 97 | #[cfg(feature = "internal-span-substring-test")] |
| 98 | check_error_span_inclusive::<u8>( |
| 99 | extensions_string, |
| 100 | Err(SpannedError { |
| 101 | code: Error::ExpectedComma, |
| 102 | span: Span { |
| 103 | start: Position { line: 2, col: 50 }, |
| 104 | end: Position { line: 3, col: 9 }, |
| 105 | }, |
| 106 | }), |
| 107 | "\n u", |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | #[test] |
| 112 | fn test_comma_end() { |
| 113 | assert_eq!(ron::from_str::<(i32, i32)>("(0, 1)").unwrap(), (0, 1)); |
| 114 | assert_eq!(ron::from_str::<(i32, i32)>("(0, 1,)").unwrap(), (0, 1)); |
| 115 | assert_eq!(ron::from_str::<()>("()"), Ok(())); |
| 116 | } |