Panic by default in serde_test is_human_readable
The serde_test Serializer and Deserializer panic in is_human_readable unless the readableness has been set explicitly through one of the hidden functions. This is to force types that have distinct readable/compact representations to be tested explicitly in one or the other, rather than with a plain assert_tokens which arbitrarily picks one. We need to follow up by designing a better API in serde_test to expose this publicly. For now serde_test cannot be used to test types that rely on is_human_readable. (The hidden functions are meant for our test suite only.)
This commit is contained in:
parent
4cecaf8d02
commit
4ed0362c8e
@ -47,7 +47,7 @@ pub fn assert_tokens<'de, T>(value: &T, tokens: &'de [Token])
|
||||
where
|
||||
T: Serialize + Deserialize<'de> + PartialEq + Debug,
|
||||
{
|
||||
assert_tokens_readable(value, tokens, true);
|
||||
assert_tokens_readable(value, tokens, None);
|
||||
}
|
||||
|
||||
// Not public API
|
||||
@ -55,7 +55,7 @@ where
|
||||
/// Runs both `assert_ser_tokens` and `assert_de_tokens`.
|
||||
///
|
||||
/// See: `assert_tokens`
|
||||
pub fn assert_tokens_readable<'de, T>(value: &T, tokens: &'de [Token], human_readable: bool)
|
||||
pub fn assert_tokens_readable<'de, T>(value: &T, tokens: &'de [Token], human_readable: Option<bool>)
|
||||
where
|
||||
T: Serialize + Deserialize<'de> + PartialEq + Debug,
|
||||
{
|
||||
@ -96,7 +96,7 @@ pub fn assert_ser_tokens<T>(value: &T, tokens: &[Token])
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
assert_ser_tokens_readable(value, tokens, true)
|
||||
assert_ser_tokens_readable(value, tokens, None)
|
||||
}
|
||||
|
||||
// Not public API
|
||||
@ -104,7 +104,7 @@ where
|
||||
/// Asserts that `value` serializes to the given `tokens`.
|
||||
///
|
||||
/// See: `assert_ser_tokens`
|
||||
pub fn assert_ser_tokens_readable<T>(value: &T, tokens: &[Token], human_readable: bool)
|
||||
pub fn assert_ser_tokens_readable<T>(value: &T, tokens: &[Token], human_readable: Option<bool>)
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
@ -207,12 +207,12 @@ pub fn assert_de_tokens<'de, T>(value: &T, tokens: &'de [Token])
|
||||
where
|
||||
T: Deserialize<'de> + PartialEq + Debug,
|
||||
{
|
||||
assert_de_tokens_readable(value, tokens, true)
|
||||
assert_de_tokens_readable(value, tokens, None)
|
||||
}
|
||||
|
||||
// Not public API
|
||||
#[doc(hidden)]
|
||||
pub fn assert_de_tokens_readable<'de, T>(value: &T, tokens: &'de [Token], human_readable: bool)
|
||||
pub fn assert_de_tokens_readable<'de, T>(value: &T, tokens: &'de [Token], human_readable: Option<bool>)
|
||||
where
|
||||
T: Deserialize<'de> + PartialEq + Debug,
|
||||
{
|
||||
|
@ -16,7 +16,7 @@ use token::Token;
|
||||
#[derive(Debug)]
|
||||
pub struct Deserializer<'de> {
|
||||
tokens: &'de [Token],
|
||||
is_human_readable: bool,
|
||||
is_human_readable: Option<bool>,
|
||||
}
|
||||
|
||||
macro_rules! assert_next_token {
|
||||
@ -49,12 +49,12 @@ macro_rules! end_of_tokens {
|
||||
|
||||
impl<'de> Deserializer<'de> {
|
||||
pub fn new(tokens: &'de [Token]) -> Self {
|
||||
Deserializer::readable(tokens, true)
|
||||
Deserializer::readable(tokens, None)
|
||||
}
|
||||
|
||||
// Not public API
|
||||
#[doc(hidden)]
|
||||
pub fn readable(tokens: &'de [Token], is_human_readable: bool) -> Self {
|
||||
pub fn readable(tokens: &'de [Token], is_human_readable: Option<bool>) -> Self {
|
||||
Deserializer { tokens: tokens, is_human_readable: is_human_readable }
|
||||
}
|
||||
|
||||
@ -373,7 +373,15 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
}
|
||||
|
||||
fn is_human_readable(&self) -> bool {
|
||||
self.is_human_readable
|
||||
match self.is_human_readable {
|
||||
Some(is) => is,
|
||||
None => {
|
||||
panic!("There is no serde_test API currently for testing types \
|
||||
that have different human-readable and compact \
|
||||
representation. See \
|
||||
https://github.com/serde-rs/serde/issues/1065.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -15,18 +15,18 @@ use token::Token;
|
||||
#[derive(Debug)]
|
||||
pub struct Serializer<'a> {
|
||||
tokens: &'a [Token],
|
||||
is_human_readable: bool,
|
||||
is_human_readable: Option<bool>,
|
||||
}
|
||||
|
||||
impl<'a> Serializer<'a> {
|
||||
/// Creates the serializer.
|
||||
pub fn new(tokens: &'a [Token]) -> Self {
|
||||
Serializer::readable(tokens, true)
|
||||
Serializer::readable(tokens, None)
|
||||
}
|
||||
|
||||
// Not public API
|
||||
#[doc(hidden)]
|
||||
pub fn readable(tokens: &'a [Token], is_human_readable: bool) -> Self {
|
||||
pub fn readable(tokens: &'a [Token], is_human_readable: Option<bool>) -> Self {
|
||||
Serializer { tokens: tokens, is_human_readable: is_human_readable }
|
||||
}
|
||||
|
||||
@ -291,7 +291,15 @@ impl<'s, 'a> ser::Serializer for &'s mut Serializer<'a> {
|
||||
}
|
||||
|
||||
fn is_human_readable(&self) -> bool {
|
||||
self.is_human_readable
|
||||
match self.is_human_readable {
|
||||
Some(is) => is,
|
||||
None => {
|
||||
panic!("There is no serde_test API currently for testing types \
|
||||
that have different human-readable and compact \
|
||||
representation. See \
|
||||
https://github.com/serde-rs/serde/issues/1065.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -115,7 +115,7 @@ macro_rules! declare_test {
|
||||
fn $name() {
|
||||
$(
|
||||
// Test ser/de roundtripping
|
||||
assert_de_tokens_readable(&$value, $tokens, $readable);
|
||||
assert_de_tokens_readable(&$value, $tokens, Some($readable));
|
||||
|
||||
// Test that the tokens are ignorable
|
||||
assert_de_tokens_ignore($tokens, true);
|
||||
@ -172,7 +172,7 @@ fn assert_de_tokens_ignore(ignorable_tokens: &[Token], readable: bool) {
|
||||
.chain(vec![Token::MapEnd].into_iter())
|
||||
.collect();
|
||||
|
||||
let mut de = serde_test::Deserializer::readable(&concated_tokens, readable);
|
||||
let mut de = serde_test::Deserializer::readable(&concated_tokens, Some(readable));
|
||||
let base = IgnoreBase::deserialize(&mut de).unwrap();
|
||||
assert_eq!(base, IgnoreBase { a: 1 });
|
||||
}
|
||||
@ -912,9 +912,10 @@ fn test_cstr() {
|
||||
#[cfg(feature = "unstable")]
|
||||
#[test]
|
||||
fn test_net_ipaddr() {
|
||||
assert_de_tokens(
|
||||
assert_de_tokens_readable(
|
||||
&"1.2.3.4".parse::<net::IpAddr>().unwrap(),
|
||||
&[Token::Str("1.2.3.4")],
|
||||
Some(true),
|
||||
);
|
||||
}
|
||||
|
||||
@ -1181,7 +1182,7 @@ impl<'de> serde::Deserialize<'de> for CompactBinary {
|
||||
|
||||
#[test]
|
||||
fn test_human_readable() {
|
||||
assert_de_tokens(
|
||||
assert_de_tokens_readable(
|
||||
&CompactBinary((1, 2)),
|
||||
&[
|
||||
Token::Tuple { len: 2},
|
||||
@ -1189,10 +1190,11 @@ fn test_human_readable() {
|
||||
Token::U8(2),
|
||||
Token::TupleEnd,
|
||||
],
|
||||
Some(true),
|
||||
);
|
||||
assert_de_tokens_readable(
|
||||
&CompactBinary((1, 2)),
|
||||
&[Token::BorrowedBytes(&[1, 2])],
|
||||
false,
|
||||
Some(false),
|
||||
);
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ fn ip_addr_roundtrip() {
|
||||
seq b"1234".iter().map(|&b| Token::U8(b)),
|
||||
Token::TupleEnd,
|
||||
],
|
||||
false,
|
||||
Some(false),
|
||||
);
|
||||
}
|
||||
|
||||
@ -40,6 +40,6 @@ fn socked_addr_roundtrip() {
|
||||
Token::U16(1234),
|
||||
Token::TupleEnd,
|
||||
],
|
||||
false,
|
||||
Some(false),
|
||||
);
|
||||
}
|
||||
|
@ -66,6 +66,20 @@ enum Enum {
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
macro_rules! declare_tests {
|
||||
(
|
||||
readable: $readable:tt
|
||||
$($name:ident { $($value:expr => $tokens:expr,)+ })+
|
||||
) => {
|
||||
$(
|
||||
#[test]
|
||||
fn $name() {
|
||||
$(
|
||||
assert_ser_tokens_readable(&$value, $tokens, Some($readable));
|
||||
)+
|
||||
}
|
||||
)+
|
||||
};
|
||||
|
||||
($($name:ident { $($value:expr => $tokens:expr,)+ })+) => {
|
||||
$(
|
||||
#[test]
|
||||
@ -78,19 +92,6 @@ macro_rules! declare_tests {
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! declare_non_human_readable_tests {
|
||||
($($name:ident { $($value:expr => $tokens:expr,)+ })+) => {
|
||||
$(
|
||||
#[test]
|
||||
fn $name() {
|
||||
$(
|
||||
assert_ser_tokens_readable(&$value, $tokens, false);
|
||||
)+
|
||||
}
|
||||
)+
|
||||
}
|
||||
}
|
||||
|
||||
declare_tests! {
|
||||
test_unit {
|
||||
() => &[Token::Unit],
|
||||
@ -368,17 +369,6 @@ declare_tests! {
|
||||
Token::StructEnd,
|
||||
],
|
||||
}
|
||||
test_net_ipv4addr {
|
||||
"1.2.3.4".parse::<net::Ipv4Addr>().unwrap() => &[Token::Str("1.2.3.4")],
|
||||
}
|
||||
test_net_ipv6addr {
|
||||
"::1".parse::<net::Ipv6Addr>().unwrap() => &[Token::Str("::1")],
|
||||
}
|
||||
test_net_socketaddr {
|
||||
"1.2.3.4:1234".parse::<net::SocketAddr>().unwrap() => &[Token::Str("1.2.3.4:1234")],
|
||||
"1.2.3.4:1234".parse::<net::SocketAddrV4>().unwrap() => &[Token::Str("1.2.3.4:1234")],
|
||||
"[::1]:1234".parse::<net::SocketAddrV6>().unwrap() => &[Token::Str("[::1]:1234")],
|
||||
}
|
||||
test_path {
|
||||
Path::new("/usr/local/lib") => &[
|
||||
Token::Str("/usr/local/lib"),
|
||||
@ -411,22 +401,41 @@ declare_tests! {
|
||||
}
|
||||
}
|
||||
|
||||
declare_non_human_readable_tests!{
|
||||
test_non_human_readable_net_ipv4addr {
|
||||
declare_tests! {
|
||||
readable: true
|
||||
test_net_ipv4addr_readable {
|
||||
"1.2.3.4".parse::<net::Ipv4Addr>().unwrap() => &[Token::Str("1.2.3.4")],
|
||||
}
|
||||
test_net_ipv6addr_readable {
|
||||
"::1".parse::<net::Ipv6Addr>().unwrap() => &[Token::Str("::1")],
|
||||
}
|
||||
test_net_ipaddr_readable {
|
||||
"1.2.3.4".parse::<net::IpAddr>().unwrap() => &[Token::Str("1.2.3.4")],
|
||||
}
|
||||
test_net_socketaddr_readable {
|
||||
"1.2.3.4:1234".parse::<net::SocketAddr>().unwrap() => &[Token::Str("1.2.3.4:1234")],
|
||||
"1.2.3.4:1234".parse::<net::SocketAddrV4>().unwrap() => &[Token::Str("1.2.3.4:1234")],
|
||||
"[::1]:1234".parse::<net::SocketAddrV6>().unwrap() => &[Token::Str("[::1]:1234")],
|
||||
}
|
||||
}
|
||||
|
||||
declare_tests! {
|
||||
readable: false
|
||||
test_net_ipv4addr_compact {
|
||||
net::Ipv4Addr::from(*b"1234") => &seq![
|
||||
Token::Tuple { len: 4 },
|
||||
seq b"1234".iter().map(|&b| Token::U8(b)),
|
||||
Token::TupleEnd,
|
||||
],
|
||||
}
|
||||
test_non_human_readable_net_ipv6addr {
|
||||
test_net_ipv6addr_compact {
|
||||
net::Ipv6Addr::from(*b"1234567890123456") => &seq![
|
||||
Token::Tuple { len: 16 },
|
||||
seq b"1234567890123456".iter().map(|&b| Token::U8(b)),
|
||||
Token::TupleEnd,
|
||||
],
|
||||
}
|
||||
test_non_human_readable_net_ipaddr {
|
||||
test_net_ipaddr_compact {
|
||||
net::IpAddr::from(*b"1234") => &seq![
|
||||
Token::NewtypeVariant { name: "IpAddr", variant: "V4" },
|
||||
|
||||
@ -435,7 +444,7 @@ declare_non_human_readable_tests!{
|
||||
Token::TupleEnd,
|
||||
],
|
||||
}
|
||||
test_non_human_readable_net_socketaddr {
|
||||
test_net_socketaddr_compact {
|
||||
net::SocketAddr::from((*b"1234567890123456", 1234)) => &seq![
|
||||
Token::NewtypeVariant { name: "SocketAddr", variant: "V6" },
|
||||
|
||||
@ -496,15 +505,6 @@ declare_tests! {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
#[test]
|
||||
fn test_net_ipaddr() {
|
||||
assert_ser_tokens(
|
||||
&"1.2.3.4".parse::<net::IpAddr>().unwrap(),
|
||||
&[Token::Str("1.2.3.4")],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn test_cannot_serialize_paths() {
|
||||
@ -567,7 +567,7 @@ impl serde::Serialize for CompactBinary {
|
||||
#[test]
|
||||
fn test_human_readable() {
|
||||
let value = CompactBinary("test".to_string());
|
||||
assert_ser_tokens(&value, &[Token::String("test")]);
|
||||
assert_ser_tokens_readable(&value, &[Token::String("test")], Some(true));
|
||||
|
||||
assert_ser_tokens_readable(&value, &[Token::Bytes(b"test")], false);
|
||||
assert_ser_tokens_readable(&value, &[Token::Bytes(b"test")], Some(false));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user