diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /third_party/rust/serde_bytes/tests | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/serde_bytes/tests')
-rw-r--r-- | third_party/rust/serde_bytes/tests/test_derive.rs | 101 | ||||
-rw-r--r-- | third_party/rust/serde_bytes/tests/test_partialeq.rs | 15 | ||||
-rw-r--r-- | third_party/rust/serde_bytes/tests/test_serde.rs | 59 |
3 files changed, 175 insertions, 0 deletions
diff --git a/third_party/rust/serde_bytes/tests/test_derive.rs b/third_party/rust/serde_bytes/tests/test_derive.rs new file mode 100644 index 0000000000..c7e97f1ec4 --- /dev/null +++ b/third_party/rust/serde_bytes/tests/test_derive.rs @@ -0,0 +1,101 @@ +#![allow(clippy::derive_partial_eq_without_eq, clippy::ref_option_ref)] + +use serde_bytes::{ByteBuf, Bytes}; +use serde_derive::{Deserialize, Serialize}; +use serde_test::{assert_tokens, Token}; +use std::borrow::Cow; + +#[derive(Serialize, Deserialize, PartialEq, Debug)] +struct Test<'a> { + #[serde(with = "serde_bytes")] + slice: &'a [u8], + + #[serde(with = "serde_bytes")] + vec: Vec<u8>, + + #[serde(with = "serde_bytes")] + bytes: &'a Bytes, + + #[serde(with = "serde_bytes")] + byte_buf: ByteBuf, + + #[serde(with = "serde_bytes")] + cow_slice: Cow<'a, [u8]>, + + #[serde(with = "serde_bytes")] + cow_bytes: Cow<'a, Bytes>, + + #[serde(with = "serde_bytes")] + boxed_slice: Box<[u8]>, + + #[serde(with = "serde_bytes")] + boxed_bytes: Box<Bytes>, + + #[serde(with = "serde_bytes")] + opt_slice: Option<&'a [u8]>, + + #[serde(with = "serde_bytes")] + opt_vec: Option<Vec<u8>>, + + #[serde(with = "serde_bytes")] + opt_cow_slice: Option<Cow<'a, [u8]>>, +} + +#[derive(Serialize)] +struct Dst { + #[serde(with = "serde_bytes")] + bytes: [u8], +} + +#[test] +fn test() { + let test = Test { + slice: b"...", + vec: b"...".to_vec(), + bytes: Bytes::new(b"..."), + byte_buf: ByteBuf::from(b"...".as_ref()), + cow_slice: Cow::Borrowed(b"..."), + cow_bytes: Cow::Borrowed(Bytes::new(b"...")), + boxed_slice: b"...".to_vec().into_boxed_slice(), + boxed_bytes: ByteBuf::from(b"...".as_ref()).into_boxed_bytes(), + opt_slice: Some(b"..."), + opt_vec: Some(b"...".to_vec()), + opt_cow_slice: Some(Cow::Borrowed(b"...")), + }; + + assert_tokens( + &test, + &[ + Token::Struct { + name: "Test", + len: 11, + }, + Token::Str("slice"), + Token::BorrowedBytes(b"..."), + Token::Str("vec"), + Token::Bytes(b"..."), + Token::Str("bytes"), + Token::BorrowedBytes(b"..."), + Token::Str("byte_buf"), + Token::Bytes(b"..."), + Token::Str("cow_slice"), + Token::BorrowedBytes(b"..."), + Token::Str("cow_bytes"), + Token::BorrowedBytes(b"..."), + Token::Str("boxed_slice"), + Token::Bytes(b"..."), + Token::Str("boxed_bytes"), + Token::Bytes(b"..."), + Token::Str("opt_slice"), + Token::Some, + Token::BorrowedBytes(b"..."), + Token::Str("opt_vec"), + Token::Some, + Token::Bytes(b"..."), + Token::Str("opt_cow_slice"), + Token::Some, + Token::BorrowedBytes(b"..."), + Token::StructEnd, + ], + ); +} diff --git a/third_party/rust/serde_bytes/tests/test_partialeq.rs b/third_party/rust/serde_bytes/tests/test_partialeq.rs new file mode 100644 index 0000000000..a57d194882 --- /dev/null +++ b/third_party/rust/serde_bytes/tests/test_partialeq.rs @@ -0,0 +1,15 @@ +#![allow(clippy::needless_pass_by_value)] + +use serde_bytes::{ByteBuf, Bytes}; + +fn _bytes_eq_slice(bytes: &Bytes, slice: &[u8]) -> bool { + bytes == slice +} + +fn _bytebuf_eq_vec(bytebuf: ByteBuf, vec: Vec<u8>) -> bool { + bytebuf == vec +} + +fn _bytes_eq_bytestring(bytes: &Bytes) -> bool { + bytes == b"..." +} diff --git a/third_party/rust/serde_bytes/tests/test_serde.rs b/third_party/rust/serde_bytes/tests/test_serde.rs new file mode 100644 index 0000000000..d48e3fd434 --- /dev/null +++ b/third_party/rust/serde_bytes/tests/test_serde.rs @@ -0,0 +1,59 @@ +use serde_bytes::{ByteBuf, Bytes}; +use serde_test::{assert_de_tokens, assert_ser_tokens, assert_tokens, Token}; + +#[test] +fn test_bytes() { + let empty = Bytes::new(&[]); + assert_tokens(&empty, &[Token::BorrowedBytes(b"")]); + assert_ser_tokens(&empty, &[Token::Bytes(b"")]); + assert_ser_tokens(&empty, &[Token::ByteBuf(b"")]); + assert_de_tokens(&empty, &[Token::BorrowedStr("")]); + + let buf = vec![65, 66, 67]; + let bytes = Bytes::new(&buf); + assert_tokens(&bytes, &[Token::BorrowedBytes(b"ABC")]); + assert_ser_tokens(&bytes, &[Token::Bytes(b"ABC")]); + assert_ser_tokens(&bytes, &[Token::ByteBuf(b"ABC")]); + assert_de_tokens(&bytes, &[Token::BorrowedStr("ABC")]); +} + +#[test] +fn test_byte_buf() { + let empty = ByteBuf::new(); + assert_tokens(&empty, &[Token::BorrowedBytes(b"")]); + assert_tokens(&empty, &[Token::Bytes(b"")]); + assert_tokens(&empty, &[Token::ByteBuf(b"")]); + assert_de_tokens(&empty, &[Token::BorrowedStr("")]); + assert_de_tokens(&empty, &[Token::Str("")]); + assert_de_tokens(&empty, &[Token::String("")]); + assert_de_tokens(&empty, &[Token::Seq { len: None }, Token::SeqEnd]); + assert_de_tokens(&empty, &[Token::Seq { len: Some(0) }, Token::SeqEnd]); + + let buf = ByteBuf::from(vec![65, 66, 67]); + assert_tokens(&buf, &[Token::BorrowedBytes(b"ABC")]); + assert_tokens(&buf, &[Token::Bytes(b"ABC")]); + assert_tokens(&buf, &[Token::ByteBuf(b"ABC")]); + assert_de_tokens(&buf, &[Token::BorrowedStr("ABC")]); + assert_de_tokens(&buf, &[Token::Str("ABC")]); + assert_de_tokens(&buf, &[Token::String("ABC")]); + assert_de_tokens( + &buf, + &[ + Token::Seq { len: None }, + Token::U8(65), + Token::U8(66), + Token::U8(67), + Token::SeqEnd, + ], + ); + assert_de_tokens( + &buf, + &[ + Token::Seq { len: Some(3) }, + Token::U8(65), + Token::U8(66), + Token::U8(67), + Token::SeqEnd, + ], + ); +} |