diff --git a/tests/508_value_adjacently_tagged_bug.rs b/tests/508_value_adjacently_tagged_bug.rs new file mode 100644 index 00000000..c237c2ca --- /dev/null +++ b/tests/508_value_adjacently_tagged_bug.rs @@ -0,0 +1,59 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +#[serde(tag = "type", content = "value")] +enum TheEnum { + Variant([f32; 3]), +} + +#[test] +fn roundtrip_through_value() { + let value = TheEnum::Variant([0.1, 0.1, 0.1]); + + let ron = ron::to_string(&value).unwrap(); + assert_eq!(ron, "(type:Variant,value:(0.1,0.1,0.1))"); + + let de = ron::from_str::(&ron).unwrap(); + assert_eq!(de, value); + + let ron_value = ron::from_str::(&ron).unwrap(); + + // Known bug: ron::Value only stores a unit, cannot find a variant + let err = ron_value.into_rust::().unwrap_err(); + assert_eq!( + err, + ron::Error::InvalidValueForType { + expected: String::from("variant of enum TheEnum"), + found: String::from("a unit value") + } + ); + + let old_serde_ron: &str = "(type:\"Variant\",value:(0.1,0.1,0.1))"; + + // Known bug: serde no longer uses strings in > v1.0.180 to deserialize the variant + let err = ron::from_str::(&old_serde_ron).unwrap_err(); + assert_eq!( + err, + ron::error::SpannedError { + code: ron::Error::ExpectedIdentifier, + position: ron::error::Position { line: 1, col: 7 }, + } + ); + + let ron_value = ron::from_str::(&old_serde_ron).unwrap(); + + // Known bug: ron::Value is asked for an enum but has no special handling for it (yet) + let err = ron_value.into_rust::().unwrap_err(); + assert_eq!( + err, + ron::Error::InvalidValueForType { + expected: String::from("variant of enum TheEnum"), + found: String::from("the string \"Variant\"") + } + ); + + // This still works, but is a bug as well + let ron_value = ron::from_str::("(\"Variant\",(0.1,0.1,0.1))").unwrap(); + let de: TheEnum = ron_value.into_rust::().unwrap(); + assert_eq!(de, value); +}