Newtype: add tests for deserialization from sequence

(review this commit with "ignore whitespace changes" option on)
This commit is contained in:
Mingun 2024-08-16 22:10:26 +05:00
parent d5a9c11b5c
commit b0d651be40

View File

@ -302,11 +302,14 @@ mod unit {
} }
} }
mod newtype {
use super::*;
#[test] #[test]
fn newtype() { fn map_tag_content() {
let value = AdjacentlyTagged::Newtype::<u8>(1); let value = AdjacentlyTagged::Newtype::<u8>(1);
// newtype with tag first // Map: tag + content
assert_tokens( assert_tokens(
&value, &value,
&[ &[
@ -325,7 +328,7 @@ fn newtype() {
], ],
); );
// newtype with content first // Map: content + tag
assert_de_tokens( assert_de_tokens(
&value, &value,
&[ &[
@ -343,7 +346,10 @@ fn newtype() {
Token::StructEnd, Token::StructEnd,
], ],
); );
}
#[test]
fn map_tag_only() {
// optional newtype with no content field // optional newtype with no content field
assert_de_tokens( assert_de_tokens(
&AdjacentlyTagged::Newtype::<Option<u8>>(None), &AdjacentlyTagged::Newtype::<Option<u8>>(None),
@ -362,6 +368,48 @@ fn newtype() {
); );
} }
#[test]
fn seq() {
let value = AdjacentlyTagged::Newtype::<u8>(1);
// Seq: tag and content
assert_de_tokens(
&value,
&[
Token::Seq { len: Some(2) },
Token::UnitVariant {
name: "AdjacentlyTagged",
variant: "Newtype",
},
Token::U8(1),
Token::SeqEnd,
],
);
// Seq: tag (as string) and content
assert_de_tokens(
&value,
&[
Token::Seq { len: None },
Token::Str("Newtype"), // tag
Token::U8(1), // content
Token::SeqEnd,
],
);
// Seq: tag (as borrowed string) and content
assert_de_tokens(
&value,
&[
Token::Seq { len: None },
Token::BorrowedStr("Newtype"), // tag
Token::U8(1), // content
Token::SeqEnd,
],
);
}
}
#[test] #[test]
fn newtype_with_newtype() { fn newtype_with_newtype() {
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]