Merge pull request #2441 from dtolnay/test

Reimplement tests that touched serde_test internal API
This commit is contained in:
David Tolnay 2023-05-04 17:42:07 -07:00 committed by GitHub
commit 624879c4c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 40 deletions

View File

@ -182,7 +182,3 @@ pub use assert::{
pub use token::Token; pub use token::Token;
pub use configure::{Compact, Configure, Readable}; pub use configure::{Compact, Configure, Readable};
// Not public API.
#[doc(hidden)]
pub use de::Deserializer;

View File

@ -4,6 +4,8 @@
clippy::used_underscore_binding clippy::used_underscore_binding
)] )]
use serde::de::value::{BorrowedStrDeserializer, MapDeserializer};
use serde::de::IntoDeserializer;
use serde::{Deserialize, Deserializer}; use serde::{Deserialize, Deserializer};
use serde_test::{assert_de_tokens, assert_de_tokens_error, Token}; use serde_test::{assert_de_tokens, assert_de_tokens_error, Token};
@ -130,20 +132,22 @@ fn test_cow() {
borrowed: Cow<'b, str>, borrowed: Cow<'b, str>,
} }
let tokens = &[ struct BorrowedStr(&'static str);
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); impl<'de> IntoDeserializer<'de> for BorrowedStr {
let cows = Cows::deserialize(&mut de).unwrap(); 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 { match cows.copied {
Cow::Owned(ref s) if s == "copied" => {} Cow::Owned(ref s) if s == "copied" => {}

View File

@ -33,7 +33,7 @@ use std::time::{Duration, UNIX_EPOCH};
use std::sync::atomic::{AtomicI64, AtomicU64}; use std::sync::atomic::{AtomicI64, AtomicU64};
use fnv::FnvHasher; use fnv::FnvHasher;
use serde::de::DeserializeOwned; use serde::de::{DeserializeOwned, IntoDeserializer};
use serde::{Deserialize, Deserializer}; use serde::{Deserialize, Deserializer};
use serde_test::{assert_de_tokens, Configure, Token}; 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()) .chain(vec![Token::MapEnd].into_iter())
.collect(); .collect();
let mut de = serde_test::Deserializer::new(&concated_tokens); let expected = IgnoreBase { a: 1 };
let base = IgnoreBase::deserialize(&mut de).unwrap(); assert_de_tokens(&expected, &concated_tokens);
assert_eq!(base, IgnoreBase { a: 1 });
} }
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
@ -2245,39 +2244,34 @@ fn test_cstr() {
#[test] #[test]
fn test_atomics() { fn test_atomics() {
fn test<L, A, T>(load: L, val: T, token: Token) fn test<L, A, T>(load: L, val: T)
where where
L: Fn(&A, Ordering) -> T, L: Fn(&A, Ordering) -> T,
A: DeserializeOwned, A: DeserializeOwned,
T: PartialEq + Debug, T: PartialEq + Debug + Copy + for<'de> IntoDeserializer<'de>,
{ {
let tokens = &[token]; match A::deserialize(val.into_deserializer()) {
let mut de = serde_test::Deserializer::new(tokens);
match A::deserialize(&mut de) {
Ok(v) => { Ok(v) => {
let loaded = load(&v, Ordering::Relaxed); let loaded = load(&v, Ordering::Relaxed);
assert_eq!(val, loaded); assert_eq!(val, loaded);
} }
Err(e) => panic!("tokens failed to deserialize: {}", e), 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(AtomicBool::load, true);
test(AtomicI8::load, -127, Token::I8(-127i8)); test(AtomicI8::load, -127i8);
test(AtomicI16::load, -510, Token::I16(-510i16)); test(AtomicI16::load, -510i16);
test(AtomicI32::load, -131072, Token::I32(-131072i32)); test(AtomicI32::load, -131072i32);
test(AtomicIsize::load, -131072isize, Token::I32(-131072)); test(AtomicIsize::load, -131072isize);
test(AtomicU8::load, 127, Token::U8(127u8)); test(AtomicU8::load, 127u8);
test(AtomicU16::load, 510u16, Token::U16(510u16)); test(AtomicU16::load, 510u16);
test(AtomicU32::load, 131072u32, Token::U32(131072u32)); test(AtomicU32::load, 131072u32);
test(AtomicUsize::load, 131072usize, Token::U32(131072)); test(AtomicUsize::load, 131072usize);
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
{ {
test(AtomicI64::load, -8589934592, Token::I64(-8589934592)); test(AtomicI64::load, -8589934592i64);
test(AtomicU64::load, 8589934592u64, Token::U64(8589934592)); test(AtomicU64::load, 8589934592u64);
} }
} }