| 1 |
use ron::{ |
| 2 |
extensions::Extensions, |
| 3 |
from_str, |
| 4 |
ser::{to_string_pretty, PrettyConfig}, |
| 5 |
Error, Options, |
| 6 |
}; |
| 7 |
use serde_derive::{Deserialize, Serialize}; |
| 8 |
|
| 9 |
#[derive(Debug, PartialEq, Serialize, Deserialize)] |
| 10 |
struct Id(u32); |
| 11 |
|
| 12 |
#[derive(Debug, PartialEq, Serialize, Deserialize)] |
| 13 |
struct Position(f32, f32); |
| 14 |
|
| 15 |
#[derive(Debug, PartialEq, Serialize, Deserialize)] |
| 16 |
enum Query { |
| 17 |
None, |
| 18 |
Creature(Id), |
| 19 |
Location(Position), |
| 20 |
} |
| 21 |
|
| 22 |
#[derive(Debug, PartialEq, Serialize, Deserialize)] |
| 23 |
struct Foo { |
| 24 |
#[allow(unused)] |
| 25 |
pub id: Id, |
| 26 |
#[allow(unused)] |
| 27 |
pub position: Position, |
| 28 |
#[allow(unused)] |
| 29 |
pub query: Query, |
| 30 |
} |
| 31 |
|
| 32 |
const EXPECT_ERROR_MESSAGE: &'static str = |
| 33 |
"expected `Err(Error::ExpectedStructName)`, deserializer returned `Ok`"; |
| 34 |
|
| 35 |
#[test] |
| 36 |
fn explicit_struct_names() { |
| 37 |
let options = Options::default().with_default_extension(Extensions::EXPLICIT_STRUCT_NAMES); |
| 38 |
let foo_ser = Foo { |
| 39 |
id: Id(3), |
| 40 |
position: Position(0.0, 8.72), |
| 41 |
query: Query::Creature(Id(4)), |
| 42 |
}; |
| 43 |
|
| 44 |
|
| 45 |
let content_regular = r#"( |
| 46 |
id: Id(3), |
| 47 |
position: Position(0.0, 8.72), |
| 48 |
query: None, |
| 49 |
)"#; |
| 50 |
let foo = options.from_str::<Foo>(content_regular); |
| 51 |
assert_eq!( |
| 52 |
foo.expect_err(EXPECT_ERROR_MESSAGE).code, |
| 53 |
Error::ExpectedStructName("Foo".to_string()) |
| 54 |
); |
| 55 |
|
| 56 |
|
| 57 |
let content_newtype = r#"Foo( |
| 58 |
id: (3), |
| 59 |
position: Position(0.0, 8.72), |
| 60 |
query: None, |
| 61 |
)"#; |
| 62 |
let foo = options.from_str::<Foo>(content_newtype); |
| 63 |
assert_eq!( |
| 64 |
foo.expect_err(EXPECT_ERROR_MESSAGE).code, |
| 65 |
Error::ExpectedStructName("Id".to_string()) |
| 66 |
); |
| 67 |
|
| 68 |
|
| 69 |
let content_tuple = r#"Foo( |
| 70 |
id: Id(3), |
| 71 |
position: (0.0, 8.72), |
| 72 |
query: None, |
| 73 |
)"#; |
| 74 |
let foo = options.from_str::<Foo>(content_tuple); |
| 75 |
assert_eq!( |
| 76 |
foo.expect_err(EXPECT_ERROR_MESSAGE).code, |
| 77 |
Error::ExpectedStructName("Position".to_string()) |
| 78 |
); |
| 79 |
|
| 80 |
|
| 81 |
let _foo1 = from_str::<Foo>(content_regular).unwrap(); |
| 82 |
let _foo2 = from_str::<Foo>(content_newtype).unwrap(); |
| 83 |
let _foo3 = from_str::<Foo>(content_tuple).unwrap(); |
| 84 |
|
| 85 |
|
| 86 |
let pretty_config = PrettyConfig::new() |
| 87 |
.extensions(Extensions::EXPLICIT_STRUCT_NAMES | Extensions::UNWRAP_VARIANT_NEWTYPES); |
| 88 |
let content = to_string_pretty(&foo_ser, pretty_config).unwrap(); |
| 89 |
assert_eq!( |
| 90 |
content, |
| 91 |
r#"#![enable(unwrap_variant_newtypes)] |
| 92 |
#![enable(explicit_struct_names)] |
| 93 |
Foo( |
| 94 |
id: Id(3), |
| 95 |
position: Position(0.0, 8.72), |
| 96 |
query: Creature(4), |
| 97 |
)"# |
| 98 |
); |
| 99 |
let foo_de = from_str::<Foo>(&content); |
| 100 |
match foo_de { |
| 101 |
|
| 102 |
Err(err) => panic!( |
| 103 |
"failed to deserialize with `explicit_struct_names` and `unwrap_variant_newtypes`: {}", |
| 104 |
err |
| 105 |
), |
| 106 |
|
| 107 |
Ok(foo_de) => assert_eq!(foo_de, foo_ser), |
| 108 |
} |
| 109 |
} |