Skip to content

Commit

Permalink
Add tests for ByteArray
Browse files Browse the repository at this point in the history
  • Loading branch information
sgued committed Feb 17, 2023
1 parent 82e8a3c commit e903fbd
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ pub use crate::bytebuf::ByteBuf;
///
/// #[serde(with = "serde_bytes")]
/// byte_buf: Vec<u8>,
///
/// #[serde(with = "serde_bytes")]
/// byte_array: [u8; 314],
/// }
/// ```
pub fn serialize<T, S>(bytes: &T, serializer: S) -> Result<S::Ok, S::Error>
Expand All @@ -106,6 +109,9 @@ where
/// struct Packet {
/// #[serde(with = "serde_bytes")]
/// payload: Vec<u8>,
///
/// #[serde(with = "serde_bytes")]
/// byte_array: [u8; 314],
/// }
/// ```
#[cfg(any(feature = "std", feature = "alloc"))]
Expand Down
30 changes: 28 additions & 2 deletions tests/test_derive.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::derive_partial_eq_without_eq, clippy::ref_option_ref)]

use serde_bytes::{ByteBuf, Bytes};
use serde_bytes::{ByteArray, ByteBuf, Bytes};
use serde_derive::{Deserialize, Serialize};
use serde_test::{assert_tokens, Token};
use std::borrow::Cow;
Expand All @@ -10,12 +10,18 @@ struct Test<'a> {
#[serde(with = "serde_bytes")]
slice: &'a [u8],

#[serde(with = "serde_bytes")]
array: [u8; 314],

#[serde(with = "serde_bytes")]
vec: Vec<u8>,

#[serde(with = "serde_bytes")]
bytes: &'a Bytes,

#[serde(with = "serde_bytes")]
byte_array: ByteArray<314>,

#[serde(with = "serde_bytes")]
byte_buf: ByteBuf,

Expand All @@ -37,6 +43,12 @@ struct Test<'a> {
#[serde(with = "serde_bytes")]
opt_vec: Option<Vec<u8>>,

#[serde(with = "serde_bytes")]
opt_array: Option<[u8; 314]>,

#[serde(with = "serde_bytes")]
opt_bytearray: Option<ByteArray<314>>,

#[serde(with = "serde_bytes")]
opt_cow_slice: Option<Cow<'a, [u8]>>,
}
Expand All @@ -51,15 +63,19 @@ struct Dst {
fn test() {
let test = Test {
slice: b"...",
array: [0; 314],
vec: b"...".to_vec(),
bytes: Bytes::new(b"..."),
byte_array: ByteArray::new([0; 314]),
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_array: Some([0; 314]),
opt_bytearray: Some(ByteArray::new([0; 314])),
opt_cow_slice: Some(Cow::Borrowed(b"...")),
};

Expand All @@ -68,14 +84,18 @@ fn test() {
&[
Token::Struct {
name: "Test",
len: 11,
len: 15,
},
Token::Str("slice"),
Token::BorrowedBytes(b"..."),
Token::Str("array"),
Token::Bytes(&[0; 314]),
Token::Str("vec"),
Token::Bytes(b"..."),
Token::Str("bytes"),
Token::BorrowedBytes(b"..."),
Token::Str("byte_array"),
Token::Bytes(&[0; 314]),
Token::Str("byte_buf"),
Token::Bytes(b"..."),
Token::Str("cow_slice"),
Expand All @@ -92,6 +112,12 @@ fn test() {
Token::Str("opt_vec"),
Token::Some,
Token::Bytes(b"..."),
Token::Str("opt_array"),
Token::Some,
Token::Bytes(&[0; 314]),
Token::Str("opt_bytearray"),
Token::Some,
Token::Bytes(&[0; 314]),
Token::Str("opt_cow_slice"),
Token::Some,
Token::BorrowedBytes(b"..."),
Expand Down
10 changes: 9 additions & 1 deletion tests/test_partialeq.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::needless_pass_by_value)]

use serde_bytes::{ByteBuf, Bytes};
use serde_bytes::{ByteArray, ByteBuf, Bytes};

fn _bytes_eq_slice(bytes: &Bytes, slice: &[u8]) -> bool {
bytes == slice
Expand All @@ -13,3 +13,11 @@ fn _bytebuf_eq_vec(bytebuf: ByteBuf, vec: Vec<u8>) -> bool {
fn _bytes_eq_bytestring(bytes: &Bytes) -> bool {
bytes == b"..."
}

fn _bytearray_eq_bytestring<const N: usize>(bytes: &ByteArray<N>) -> bool {
bytes == &[0u8; N]
}

fn _bytearray_eq_bytearray<const N: usize>(bytes: &ByteArray<N>, other: &ByteArray<N>) -> bool {
bytes == other
}
18 changes: 17 additions & 1 deletion tests/test_serde.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use serde_bytes::{ByteBuf, Bytes};
use serde_bytes::{ByteArray, ByteBuf, Bytes};
use serde_test::{assert_de_tokens, assert_ser_tokens, assert_tokens, Token};

#[test]
Expand Down Expand Up @@ -57,3 +57,19 @@ fn test_byte_buf() {
],
);
}

#[test]
fn test_bytearray() {
let empty = ByteArray::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 = [65, 66, 67];
let bytes = ByteArray::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")]);
}

0 comments on commit e903fbd

Please sign in to comment.