| 1 |
use ron::error::{Error, Position, 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 |
position: Position { line: 1, col: 7 } |
| 34 |
}), |
| 35 |
); |
| 36 |
assert_eq!( |
| 37 |
ron::from_str::<MyUnitStruct>("42"), |
| 38 |
Err(SpannedError { |
| 39 |
code: Error::ExpectedNamedStructLike("MyUnitStruct"), |
| 40 |
position: Position { line: 1, col: 1 } |
| 41 |
}), |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
#[test] |
| 46 |
fn test_tuple_struct_name_mismatch() { |
| 47 |
assert_eq!( |
| 48 |
ron::from_str::<MyTupleStruct>("(true, 42)"), |
| 49 |
Ok(MyTupleStruct(true, 42)), |
| 50 |
); |
| 51 |
assert_eq!( |
| 52 |
ron::from_str::<MyTupleStruct>("MyTupleStruct(true, 42)"), |
| 53 |
Ok(MyTupleStruct(true, 42)), |
| 54 |
); |
| 55 |
assert_eq!( |
| 56 |
ron::from_str::<MyTupleStruct>("MyTypleStruct(true, 42)"), |
| 57 |
Err(SpannedError { |
| 58 |
code: Error::ExpectedDifferentStructName { |
| 59 |
expected: "MyTupleStruct", |
| 60 |
found: String::from("MyTypleStruct") |
| 61 |
}, |
| 62 |
position: Position { line: 1, col: 14 } |
| 63 |
}), |
| 64 |
); |
| 65 |
assert_eq!( |
| 66 |
ron::from_str::<MyTupleStruct>("42"), |
| 67 |
Err(SpannedError { |
| 68 |
code: Error::ExpectedNamedStructLike("MyTupleStruct"), |
| 69 |
position: Position { line: 1, col: 1 } |
| 70 |
}), |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
#[test] |
| 75 |
fn test_newtype_struct_name_mismatch() { |
| 76 |
assert_eq!( |
| 77 |
ron::from_str::<MyNewtypeStruct>("((true, 42))"), |
| 78 |
Ok(MyNewtypeStruct(MyTupleStruct(true, 42))), |
| 79 |
); |
| 80 |
assert_eq!( |
| 81 |
ron::from_str::<MyNewtypeStruct>("MyNewtypeStruct((true, 42))"), |
| 82 |
Ok(MyNewtypeStruct(MyTupleStruct(true, 42))), |
| 83 |
); |
| 84 |
assert_eq!( |
| 85 |
ron::from_str::<MyNewtypeStruct>("MyNewtypeStrucl((true, 42))"), |
| 86 |
Err(SpannedError { |
| 87 |
code: Error::ExpectedDifferentStructName { |
| 88 |
expected: "MyNewtypeStruct", |
| 89 |
found: String::from("MyNewtypeStrucl") |
| 90 |
}, |
| 91 |
position: Position { line: 1, col: 16 } |
| 92 |
}), |
| 93 |
); |
| 94 |
assert_eq!( |
| 95 |
ron::from_str::<MyNewtypeStruct>("42"), |
| 96 |
Err(SpannedError { |
| 97 |
code: Error::ExpectedNamedStructLike("MyNewtypeStruct"), |
| 98 |
position: Position { line: 1, col: 1 } |
| 99 |
}), |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
#[test] |
| 104 |
fn test_struct_name_mismatch() { |
| 105 |
assert_eq!( |
| 106 |
ron::from_str::<MyStruct>("(a: true, b: 42)"), |
| 107 |
Ok(MyStruct { a: true, b: 42 }), |
| 108 |
); |
| 109 |
assert_eq!( |
| 110 |
ron::from_str::<MyStruct>("MyStruct(a: true, b: 42)"), |
| 111 |
Ok(MyStruct { a: true, b: 42 }), |
| 112 |
); |
| 113 |
assert_eq!( |
| 114 |
ron::from_str::<MyStruct>("MuStryct(a: true, b: 42)"), |
| 115 |
Err(SpannedError { |
| 116 |
code: Error::ExpectedDifferentStructName { |
| 117 |
expected: "MyStruct", |
| 118 |
found: String::from("MuStryct") |
| 119 |
}, |
| 120 |
position: Position { line: 1, col: 9 } |
| 121 |
}), |
| 122 |
); |
| 123 |
assert_eq!( |
| 124 |
ron::from_str::<MyStruct>("42"), |
| 125 |
Err(SpannedError { |
| 126 |
code: Error::ExpectedNamedStructLike("MyStruct"), |
| 127 |
position: Position { line: 1, col: 1 } |
| 128 |
}), |
| 129 |
); |
| 130 |
} |