| 1 | use ron::error::{Error, Position, Span, SpannedError}; |
| 2 | use serde::Deserialize; |
| 3 | |
| 4 | #[derive(Debug, Deserialize, PartialEq)] |
| 5 | struct MyUnitStruct; |
| 6 | |
| 7 | #[derive(Debug, Deserialize, PartialEq)] |
| 8 | struct MyTupleStruct(bool, i32); |
| 9 | |
| 10 | #[derive(Debug, Deserialize, PartialEq)] |
| 11 | struct MyNewtypeStruct(MyTupleStruct); |
| 12 | |
| 13 | #[derive(Debug, Deserialize, PartialEq)] |
| 14 | struct MyStruct { |
| 15 | a: bool, |
| 16 | b: i32, |
| 17 | } |
| 18 | |
| 19 | #[test] |
| 20 | fn test_unit_struct_name_mismatch() { |
| 21 | assert_eq!(ron::from_str::<MyUnitStruct>("()"), Ok(MyUnitStruct),); |
| 22 | assert_eq!( |
| 23 | ron::from_str::<MyUnitStruct>("MyUnitStruct"), |
| 24 | Ok(MyUnitStruct), |
| 25 | ); |
| 26 | assert_eq!( |
| 27 | ron::from_str::<MyUnitStruct>("MyUnit Struct"), |
| 28 | Err(SpannedError { |
| 29 | code: Error::ExpectedDifferentStructName { |
| 30 | expected: "MyUnitStruct", |
| 31 | found: String::from("MyUnit") |
| 32 | }, |
| 33 | span: Span { |
| 34 | start: ron::error::Position { line: 1, col: 1 }, |
| 35 | end: Position { line: 1, col: 7 } |
| 36 | } |
| 37 | }), |
| 38 | ); |
| 39 | assert_eq!( |
| 40 | ron::from_str::<MyUnitStruct>("42"), |
| 41 | Err(SpannedError { |
| 42 | code: Error::ExpectedNamedStructLike("MyUnitStruct"), |
| 43 | span: Span { |
| 44 | start: ron::error::Position { line: 1, col: 1 }, |
| 45 | end: Position { line: 1, col: 1 } |
| 46 | } |
| 47 | }), |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | #[test] |
| 52 | fn test_tuple_struct_name_mismatch() { |
| 53 | assert_eq!( |
| 54 | ron::from_str::<MyTupleStruct>("(true, 42)"), |
| 55 | Ok(MyTupleStruct(true, 42)), |
| 56 | ); |
| 57 | assert_eq!( |
| 58 | ron::from_str::<MyTupleStruct>("MyTupleStruct(true, 42)"), |
| 59 | Ok(MyTupleStruct(true, 42)), |
| 60 | ); |
| 61 | assert_eq!( |
| 62 | ron::from_str::<MyTupleStruct>("MyTypleStruct(true, 42)"), |
| 63 | Err(SpannedError { |
| 64 | code: Error::ExpectedDifferentStructName { |
| 65 | expected: "MyTupleStruct", |
| 66 | found: String::from("MyTypleStruct") |
| 67 | }, |
| 68 | span: Span { |
| 69 | start: ron::error::Position { line: 1, col: 1 }, |
| 70 | end: Position { line: 1, col: 14 } |
| 71 | } |
| 72 | }), |
| 73 | ); |
| 74 | assert_eq!( |
| 75 | ron::from_str::<MyTupleStruct>("42"), |
| 76 | Err(SpannedError { |
| 77 | code: Error::ExpectedNamedStructLike("MyTupleStruct"), |
| 78 | span: Span { |
| 79 | start: ron::error::Position { line: 1, col: 1 }, |
| 80 | end: Position { line: 1, col: 1 } |
| 81 | } |
| 82 | }), |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | #[test] |
| 87 | fn test_newtype_struct_name_mismatch() { |
| 88 | assert_eq!( |
| 89 | ron::from_str::<MyNewtypeStruct>("((true, 42))"), |
| 90 | Ok(MyNewtypeStruct(MyTupleStruct(true, 42))), |
| 91 | ); |
| 92 | assert_eq!( |
| 93 | ron::from_str::<MyNewtypeStruct>("MyNewtypeStruct((true, 42))"), |
| 94 | Ok(MyNewtypeStruct(MyTupleStruct(true, 42))), |
| 95 | ); |
| 96 | assert_eq!( |
| 97 | ron::from_str::<MyNewtypeStruct>("MyNewtypeStrucl((true, 42))"), |
| 98 | Err(SpannedError { |
| 99 | code: Error::ExpectedDifferentStructName { |
| 100 | expected: "MyNewtypeStruct", |
| 101 | found: String::from("MyNewtypeStrucl") |
| 102 | }, |
| 103 | span: Span { |
| 104 | start: ron::error::Position { line: 1, col: 1 }, |
| 105 | end: Position { line: 1, col: 16 } |
| 106 | } |
| 107 | }), |
| 108 | ); |
| 109 | assert_eq!( |
| 110 | ron::from_str::<MyNewtypeStruct>("42"), |
| 111 | Err(SpannedError { |
| 112 | code: Error::ExpectedNamedStructLike("MyNewtypeStruct"), |
| 113 | span: Span { |
| 114 | start: ron::error::Position { line: 1, col: 1 }, |
| 115 | end: Position { line: 1, col: 1 } |
| 116 | } |
| 117 | }), |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | #[test] |
| 122 | fn test_struct_name_mismatch() { |
| 123 | assert_eq!( |
| 124 | ron::from_str::<MyStruct>("(a: true, b: 42)"), |
| 125 | Ok(MyStruct { a: true, b: 42 }), |
| 126 | ); |
| 127 | assert_eq!( |
| 128 | ron::from_str::<MyStruct>("MyStruct(a: true, b: 42)"), |
| 129 | Ok(MyStruct { a: true, b: 42 }), |
| 130 | ); |
| 131 | assert_eq!( |
| 132 | ron::from_str::<MyStruct>("MuStryct(a: true, b: 42)"), |
| 133 | Err(SpannedError { |
| 134 | code: Error::ExpectedDifferentStructName { |
| 135 | expected: "MyStruct", |
| 136 | found: String::from("MuStryct") |
| 137 | }, |
| 138 | span: Span { |
| 139 | start: ron::error::Position { line: 1, col: 1 }, |
| 140 | end: Position { line: 1, col: 9 } |
| 141 | } |
| 142 | }), |
| 143 | ); |
| 144 | assert_eq!( |
| 145 | ron::from_str::<MyStruct>("42"), |
| 146 | Err(SpannedError { |
| 147 | code: Error::ExpectedNamedStructLike("MyStruct"), |
| 148 | span: Span { |
| 149 | start: ron::error::Position { line: 1, col: 1 }, |
| 150 | end: Position { line: 1, col: 1 } |
| 151 | } |
| 152 | }), |
| 153 | ); |
| 154 | } |