Unit: add tests for deserialization from sequence

(review this commit with "ignore whitespace changes" option on)
This commit is contained in:
Mingun
2024-08-12 00:45:29 +05:00
committed by Mingun
parent a7f0bab078
commit 29dc6c3367
@@ -18,9 +18,12 @@ enum AdjacentlyTagged<T> {
Struct { f: u8 }, Struct { f: u8 },
} }
mod unit {
use super::*;
#[test] #[test]
fn unit() { fn map_str_tag_only() {
// unit with no content // Map: tag only
assert_tokens( assert_tokens(
&AdjacentlyTagged::Unit::<u8>, &AdjacentlyTagged::Unit::<u8>,
&[ &[
@@ -37,7 +40,7 @@ fn unit() {
], ],
); );
// unit with no content and incorrect hint for number of elements // Map: tag only and incorrect hint for number of elements
assert_de_tokens( assert_de_tokens(
&AdjacentlyTagged::Unit::<u8>, &AdjacentlyTagged::Unit::<u8>,
&[ &[
@@ -53,8 +56,11 @@ fn unit() {
Token::StructEnd, Token::StructEnd,
], ],
); );
}
// unit with tag first #[test]
fn map_str_tag_content() {
// Map: tag + content
assert_de_tokens( assert_de_tokens(
&AdjacentlyTagged::Unit::<u8>, &AdjacentlyTagged::Unit::<u8>,
&[ &[
@@ -72,8 +78,7 @@ fn unit() {
Token::StructEnd, Token::StructEnd,
], ],
); );
// Map: content + tag
// unit with content first
assert_de_tokens( assert_de_tokens(
&AdjacentlyTagged::Unit::<u8>, &AdjacentlyTagged::Unit::<u8>,
&[ &[
@@ -92,7 +97,7 @@ fn unit() {
], ],
); );
// unit with excess content (f, g, h) // Map: tag + content + excess fields (f, g, h)
assert_de_tokens( assert_de_tokens(
&AdjacentlyTagged::Unit::<u8>, &AdjacentlyTagged::Unit::<u8>,
&[ &[
@@ -118,6 +123,46 @@ fn unit() {
); );
} }
#[test]
fn seq_tag_content() {
// Seq: tag and content
assert_de_tokens(
&AdjacentlyTagged::Unit::<u8>,
&[
Token::Seq { len: Some(2) },
Token::UnitVariant {
name: "AdjacentlyTagged",
variant: "Unit",
},
Token::Unit,
Token::SeqEnd,
],
);
// Seq: tag (as string) and content
assert_de_tokens(
&AdjacentlyTagged::Unit::<u8>,
&[
Token::Seq { len: None },
Token::Str("Unit"), // tag
Token::Unit, // content
Token::SeqEnd,
],
);
// Seq: tag (as borrowed string) and content
assert_de_tokens(
&AdjacentlyTagged::Unit::<u8>,
&[
Token::Seq { len: None },
Token::BorrowedStr("Unit"), // tag
Token::Unit, // content
Token::SeqEnd,
],
);
}
}
#[test] #[test]
fn newtype() { fn newtype() {
let value = AdjacentlyTagged::Newtype::<u8>(1); let value = AdjacentlyTagged::Newtype::<u8>(1);