Skip to content

Commit

Permalink
Add tests for RawValue inside untagged*
Browse files Browse the repository at this point in the history
  • Loading branch information
juntyr committed Oct 4, 2023
1 parent 32f0401 commit 9557e02
Show file tree
Hide file tree
Showing 2 changed files with 226 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ While data structures with any of these attributes should generally roundtrip th
- an untagged unit variant
- untagged tuple / struct variants with no fields are not supported
- untagged tuple variants with just one field (that are not newtype variants) are not supported when the `#![enable(unwrap_variant_newtypes)]` extension is enabled
- serializing a `ron::value::RawValue` using a `PrettyConfig` may add leading and trailing whitespace and comments, which the `ron::value::RawValue` absorbs upon deserialization

Furthermore, serde imposes the following restrictions for data to roundtrip:

Expand Down
225 changes: 225 additions & 0 deletions tests/502_known_bugs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,25 @@ fn one_tuple_variant_inside_adjacently_tagged() {
),
Ok(())
);
assert_eq!(
ron::from_str::<AdjacentlyTagged>("(tag: B, content: (ho: 24, a: (hi: OneTuple(42))))"),
Ok(AdjacentlyTagged::B {
ho: 24,
a: A {
hi: OneEnum::OneTuple(42, ())
}
}),
);
assert_eq!(
ron::from_str::<AdjacentlyTagged>("(content: (ho: 24, a: (hi: OneTuple(42))), tag: B)"),
Err(SpannedError {
code: Error::InvalidValueForType {
expected: String::from("tuple variant"),
found: String::from("the unsigned integer `42`")
},
position: Position { line: 1, col: 50 }
})
);
}

#[test]
Expand Down Expand Up @@ -1060,6 +1079,212 @@ fn one_tuple_variant_inside_flatten_struct_variant() {
);
}

#[test]
fn raw_value_inside_internally_tagged() {
#[derive(PartialEq, Debug, Serialize, Deserialize)]
struct A {
hi: Box<ron::value::RawValue>,
}

#[derive(PartialEq, Debug, Serialize, Deserialize)]
#[serde(tag = "tag")]
enum InternallyTagged {
B { ho: i32, a: A },
}

assert_eq!(
check_roundtrip(
&InternallyTagged::B {
ho: 24,
a: A {
hi: ron::value::RawValue::from_boxed_ron(String::from("42").into_boxed_str())
.unwrap(),
}
},
PrettyConfig::default()
),
Err(Err(SpannedError {
code: Error::InvalidValueForType {
expected: String::from("any valid RON-value-string"),
found: String::from("the unsigned integer `42`")
},
position: Position { line: 7, col: 2 }
}))
);
}

#[test]
fn raw_value_inside_adjacently_tagged() {
#[derive(PartialEq, Debug, Serialize, Deserialize)]
struct A {
hi: Box<ron::value::RawValue>,
}

#[derive(PartialEq, Debug, Serialize, Deserialize)]
#[serde(tag = "tag", content = "content")]
enum AdjacentlyTagged {
B { ho: i32, a: A },
}

assert_eq!(
check_roundtrip(
&AdjacentlyTagged::B {
ho: 24,
a: A {
hi: ron::value::RawValue::from_boxed_ron(String::from("42").into_boxed_str())
.unwrap(),
}
},
PrettyConfig::default()
),
// adds an extra space
Err(Ok(Error::Message(String::from("ROUNDTRIP error: B { ho: 24, a: A { hi: RawValue(42) } } != B { ho: 24, a: A { hi: RawValue( 42) } }"))))
);
assert_eq!(
ron::from_str::<AdjacentlyTagged>("(tag: B, content: (ho: 24, a: (hi:42)))"),
Ok(AdjacentlyTagged::B {
ho: 24,
a: A {
hi: ron::value::RawValue::from_boxed_ron(String::from("42").into_boxed_str())
.unwrap(),
}
}),
);
assert_eq!(
ron::from_str::<AdjacentlyTagged>("(content: (ho: 24, a: (hi:42)), tag: B)"),
Err(SpannedError {
code: Error::InvalidValueForType {
expected: String::from("any valid RON-value-string"),
found: String::from("the unsigned integer `42`")
},
position: Position { line: 1, col: 39 }
})
);
}

#[test]
fn raw_value_inside_untagged() {
#[derive(PartialEq, Debug, Serialize, Deserialize)]
struct A {
hi: Box<ron::value::RawValue>,
}

#[derive(PartialEq, Debug, Serialize, Deserialize)]
#[serde(untagged)]
enum Untagged {
B { ho: i32, a: A },
}

assert_eq!(
check_roundtrip(
&Untagged::B {
ho: 24,
a: A {
hi: ron::value::RawValue::from_boxed_ron(String::from("42").into_boxed_str())
.unwrap(),
}
},
PrettyConfig::default()
),
Err(Err(SpannedError {
code: Error::Message(String::from(
"data did not match any variant of untagged enum Untagged"
)),
position: Position { line: 6, col: 2 }
}))
);
}

#[test]
fn raw_value_inside_flatten_struct() {
#[derive(PartialEq, Debug, Serialize, Deserialize)]
struct A {
hi: Box<ron::value::RawValue>,
}

#[derive(PartialEq, Debug, Serialize, Deserialize)]
struct B {
a: A,
}

#[derive(PartialEq, Debug, Serialize, Deserialize)]
struct FlattenedStruct {
ho: i32,
#[serde(flatten)]
a: B,
}

assert_eq!(
check_roundtrip(
&FlattenedStruct {
ho: 24,
a: B {
a: A {
hi: ron::value::RawValue::from_boxed_ron(
String::from("42").into_boxed_str()
)
.unwrap(),
}
}
},
PrettyConfig::default()
),
Err(Err(SpannedError {
code: Error::InvalidValueForType {
expected: String::from("any valid RON-value-string"),
found: String::from("the unsigned integer `42`")
},
position: Position { line: 6, col: 1 }
}))
);
}

#[test]
fn raw_value_inside_flatten_struct_variant() {
#[derive(PartialEq, Debug, Serialize, Deserialize)]
struct A {
hi: Box<ron::value::RawValue>,
}

#[derive(PartialEq, Debug, Serialize, Deserialize)]
struct B {
a: A,
}

#[derive(PartialEq, Debug, Serialize, Deserialize)]
enum FlattenedStructVariant {
C {
ho: i32,
#[serde(flatten)]
a: B,
},
}

assert_eq!(
check_roundtrip(
&FlattenedStructVariant::C {
ho: 24,
a: B {
a: A {
hi: ron::value::RawValue::from_boxed_ron(
String::from("42").into_boxed_str()
)
.unwrap(),
}
}
},
PrettyConfig::default()
),
Err(Err(SpannedError {
code: Error::InvalidValueForType {
expected: String::from("any valid RON-value-string"),
found: String::from("the unsigned integer `42`")
},
position: Position { line: 6, col: 1 }
}))
);
}

#[test]
fn i128_inside_internally_tagged() {
#[derive(PartialEq, Debug, Serialize, Deserialize)]
Expand Down

0 comments on commit 9557e02

Please sign in to comment.