From bd9e9abf352f8cc31cf69309455aa970ab04275a Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 4 May 2023 17:25:17 -0700 Subject: [PATCH] Reimplement tests that touched serde_test internal API --- serde_test/src/lib.rs | 4 ---- test_suite/tests/test_borrow.rs | 32 ++++++++++++++------------ test_suite/tests/test_de.rs | 40 ++++++++++++++------------------- 3 files changed, 35 insertions(+), 41 deletions(-) diff --git a/serde_test/src/lib.rs b/serde_test/src/lib.rs index 89cce8bcd..95943a4e0 100644 --- a/serde_test/src/lib.rs +++ b/serde_test/src/lib.rs @@ -182,7 +182,3 @@ pub use assert::{ pub use token::Token; pub use configure::{Compact, Configure, Readable}; - -// Not public API. -#[doc(hidden)] -pub use de::Deserializer; diff --git a/test_suite/tests/test_borrow.rs b/test_suite/tests/test_borrow.rs index 002d63cf4..654d1554a 100644 --- a/test_suite/tests/test_borrow.rs +++ b/test_suite/tests/test_borrow.rs @@ -4,6 +4,8 @@ clippy::used_underscore_binding )] +use serde::de::value::{BorrowedStrDeserializer, MapDeserializer}; +use serde::de::IntoDeserializer; use serde::{Deserialize, Deserializer}; use serde_test::{assert_de_tokens, assert_de_tokens_error, Token}; @@ -130,20 +132,22 @@ fn test_cow() { borrowed: Cow<'b, str>, } - let tokens = &[ - Token::Struct { - name: "Cows", - len: 2, - }, - Token::Str("copied"), - Token::BorrowedStr("copied"), - Token::Str("borrowed"), - Token::BorrowedStr("borrowed"), - Token::StructEnd, - ]; - - let mut de = serde_test::Deserializer::new(tokens); - let cows = Cows::deserialize(&mut de).unwrap(); + struct BorrowedStr(&'static str); + + impl<'de> IntoDeserializer<'de> for BorrowedStr { + type Deserializer = BorrowedStrDeserializer<'de, serde::de::value::Error>; + + fn into_deserializer(self) -> Self::Deserializer { + BorrowedStrDeserializer::new(self.0) + } + } + + let de = MapDeserializer::new(IntoIterator::into_iter([ + ("copied", BorrowedStr("copied")), + ("borrowed", BorrowedStr("borrowed")), + ])); + + let cows = Cows::deserialize(de).unwrap(); match cows.copied { Cow::Owned(ref s) if s == "copied" => {} diff --git a/test_suite/tests/test_de.rs b/test_suite/tests/test_de.rs index 226667939..83dc6978e 100644 --- a/test_suite/tests/test_de.rs +++ b/test_suite/tests/test_de.rs @@ -33,7 +33,7 @@ use std::time::{Duration, UNIX_EPOCH}; use std::sync::atomic::{AtomicI64, AtomicU64}; use fnv::FnvHasher; -use serde::de::DeserializeOwned; +use serde::de::{DeserializeOwned, IntoDeserializer}; use serde::{Deserialize, Deserializer}; use serde_test::{assert_de_tokens, Configure, Token}; @@ -202,9 +202,8 @@ fn assert_de_tokens_ignore(ignorable_tokens: &[Token]) { .chain(vec![Token::MapEnd].into_iter()) .collect(); - let mut de = serde_test::Deserializer::new(&concated_tokens); - let base = IgnoreBase::deserialize(&mut de).unwrap(); - assert_eq!(base, IgnoreBase { a: 1 }); + let expected = IgnoreBase { a: 1 }; + assert_de_tokens(&expected, &concated_tokens); } ////////////////////////////////////////////////////////////////////////// @@ -2245,39 +2244,34 @@ fn test_cstr() { #[test] fn test_atomics() { - fn test(load: L, val: T, token: Token) + fn test(load: L, val: T) where L: Fn(&A, Ordering) -> T, A: DeserializeOwned, - T: PartialEq + Debug, + T: PartialEq + Debug + Copy + for<'de> IntoDeserializer<'de>, { - let tokens = &[token]; - let mut de = serde_test::Deserializer::new(tokens); - match A::deserialize(&mut de) { + match A::deserialize(val.into_deserializer()) { Ok(v) => { let loaded = load(&v, Ordering::Relaxed); assert_eq!(val, loaded); } Err(e) => panic!("tokens failed to deserialize: {}", e), - }; - if de.remaining() > 0 { - panic!("{} remaining tokens", de.remaining()); } } - test(AtomicBool::load, true, Token::Bool(true)); - test(AtomicI8::load, -127, Token::I8(-127i8)); - test(AtomicI16::load, -510, Token::I16(-510i16)); - test(AtomicI32::load, -131072, Token::I32(-131072i32)); - test(AtomicIsize::load, -131072isize, Token::I32(-131072)); - test(AtomicU8::load, 127, Token::U8(127u8)); - test(AtomicU16::load, 510u16, Token::U16(510u16)); - test(AtomicU32::load, 131072u32, Token::U32(131072u32)); - test(AtomicUsize::load, 131072usize, Token::U32(131072)); + test(AtomicBool::load, true); + test(AtomicI8::load, -127i8); + test(AtomicI16::load, -510i16); + test(AtomicI32::load, -131072i32); + test(AtomicIsize::load, -131072isize); + test(AtomicU8::load, 127u8); + test(AtomicU16::load, 510u16); + test(AtomicU32::load, 131072u32); + test(AtomicUsize::load, 131072usize); #[cfg(target_arch = "x86_64")] { - test(AtomicI64::load, -8589934592, Token::I64(-8589934592)); - test(AtomicU64::load, 8589934592u64, Token::U64(8589934592)); + test(AtomicI64::load, -8589934592i64); + test(AtomicU64::load, 8589934592u64); } }