1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-24 01:11:07 +01:00

increase bytestring test coverage

This commit is contained in:
Rob Ede 2023-04-01 22:45:18 +01:00
parent 4cbe741230
commit 177590a7d8
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933

View File

@ -11,7 +11,7 @@ use alloc::{
string::{String, ToString}, string::{String, ToString},
vec::Vec, vec::Vec,
}; };
use core::{borrow, convert::TryFrom, fmt, hash, ops, str}; use core::{borrow::Borrow, convert::TryFrom, fmt, hash, ops, str};
use bytes::Bytes; use bytes::Bytes;
@ -132,13 +132,12 @@ impl ops::Deref for ByteString {
#[inline] #[inline]
fn deref(&self) -> &str { fn deref(&self) -> &str {
let bytes = self.0.as_ref(); let bytes = self.0.as_ref();
// SAFETY: // SAFETY: UTF-8 validity is guaranteed during construction.
// UTF-8 validity is guaranteed at during construction.
unsafe { str::from_utf8_unchecked(bytes) } unsafe { str::from_utf8_unchecked(bytes) }
} }
} }
impl borrow::Borrow<str> for ByteString { impl Borrow<str> for ByteString {
fn borrow(&self) -> &str { fn borrow(&self) -> &str {
self self
} }
@ -292,7 +291,7 @@ mod serde {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use alloc::borrow::ToOwned; use alloc::{borrow::ToOwned, format, vec};
use core::{ use core::{
hash::{Hash, Hasher}, hash::{Hash, Hasher},
panic::{RefUnwindSafe, UnwindSafe}, panic::{RefUnwindSafe, UnwindSafe},
@ -309,7 +308,7 @@ mod test {
assert_impl_all!(ByteString: UnwindSafe, RefUnwindSafe); assert_impl_all!(ByteString: UnwindSafe, RefUnwindSafe);
#[test] #[test]
fn test_partial_eq() { fn eq() {
let s: ByteString = ByteString::from_static("test"); let s: ByteString = ByteString::from_static("test");
assert_eq!(s, "test"); assert_eq!(s, "test");
assert_eq!(s, *"test"); assert_eq!(s, *"test");
@ -317,12 +316,45 @@ mod test {
} }
#[test] #[test]
fn test_new() { fn new() {
let _: ByteString = ByteString::new(); let _: ByteString = ByteString::new();
} }
#[test] #[test]
fn test_hash() { fn as_bytes() {
let buf = ByteString::new();
assert!(buf.as_bytes().is_empty());
let buf = ByteString::from("hello");
assert_eq!(buf.as_bytes(), "hello");
}
#[test]
fn from_bytes_unchecked() {
let buf = unsafe { ByteString::from_bytes_unchecked(Bytes::new()) };
assert!(buf.is_empty());
let buf = unsafe { ByteString::from_bytes_unchecked(Bytes::from("hello")) };
assert_eq!(buf, "hello");
}
#[test]
fn as_ref() {
let buf = ByteString::new();
let _: &ByteString = buf.as_ref();
let _: &[u8] = buf.as_ref();
}
#[test]
fn borrow() {
let buf = ByteString::new();
let _: &str = buf.borrow();
}
#[test]
fn hash() {
let mut hasher1 = AHasher::default(); let mut hasher1 = AHasher::default();
"str".hash(&mut hasher1); "str".hash(&mut hasher1);
@ -333,7 +365,7 @@ mod test {
} }
#[test] #[test]
fn test_from_string() { fn from_string() {
let s: ByteString = "hello".to_owned().into(); let s: ByteString = "hello".to_owned().into();
assert_eq!(&s, "hello"); assert_eq!(&s, "hello");
let t: &str = s.as_ref(); let t: &str = s.as_ref();
@ -341,23 +373,30 @@ mod test {
} }
#[test] #[test]
fn test_from_str() { fn from_str() {
let _: ByteString = "str".into(); let _: ByteString = "str".into();
let _: ByteString = "str".to_owned().into_boxed_str().into();
} }
#[test] #[test]
fn test_from_static_str() { fn to_string() {
let buf = ByteString::from("foo");
assert_eq!(String::from(buf), "foo");
}
#[test]
fn from_static_str() {
static _S: ByteString = ByteString::from_static("hello"); static _S: ByteString = ByteString::from_static("hello");
let _ = ByteString::from_static("str"); let _ = ByteString::from_static("str");
} }
#[test] #[test]
fn test_try_from_slice() { fn try_from_slice() {
let _ = ByteString::try_from(b"nice bytes").unwrap(); let _ = ByteString::try_from(b"nice bytes").unwrap();
} }
#[test] #[test]
fn test_try_from_array() { fn try_from_array() {
assert_eq!( assert_eq!(
ByteString::try_from([b'h', b'i']).unwrap(), ByteString::try_from([b'h', b'i']).unwrap(),
ByteString::from_static("hi") ByteString::from_static("hi")
@ -365,25 +404,43 @@ mod test {
} }
#[test] #[test]
fn test_try_from_bytes() { fn try_from_vec() {
let _ = ByteString::try_from(vec![b'f', b'o', b'o']).unwrap();
ByteString::try_from(vec![0, 159, 146, 150]).unwrap_err();
}
#[test]
fn try_from_bytes() {
let _ = ByteString::try_from(Bytes::from_static(b"nice bytes")).unwrap(); let _ = ByteString::try_from(Bytes::from_static(b"nice bytes")).unwrap();
} }
#[test] #[test]
fn test_try_from_bytes_mut() { fn try_from_bytes_mut() {
let _ = ByteString::try_from(bytes::BytesMut::from(&b"nice bytes"[..])).unwrap(); let _ = ByteString::try_from(bytes::BytesMut::from(&b"nice bytes"[..])).unwrap();
} }
#[test]
fn display() {
let buf = ByteString::from("bar");
assert_eq!(format!("{buf}"), "bar");
}
#[test]
fn debug() {
let buf = ByteString::from("baz");
assert_eq!(format!("{buf:?}"), r#""baz""#);
}
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
#[test] #[test]
fn test_serialize() { fn serialize() {
let s: ByteString = serde_json::from_str(r#""nice bytes""#).unwrap(); let s: ByteString = serde_json::from_str(r#""nice bytes""#).unwrap();
assert_eq!(s, "nice bytes"); assert_eq!(s, "nice bytes");
} }
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
#[test] #[test]
fn test_deserialize() { fn deserialize() {
let s = serde_json::to_string(&ByteString::from_static("nice bytes")).unwrap(); let s = serde_json::to_string(&ByteString::from_static("nice bytes")).unwrap();
assert_eq!(s, r#""nice bytes""#); assert_eq!(s, r#""nice bytes""#);
} }
@ -399,7 +456,7 @@ mod test {
#[test] #[test]
#[should_panic] #[should_panic]
fn test_slice_ref_catches_not_a_subset() { fn slice_ref_catches_not_a_subset() {
// panics because the given slice is not derived from the original byte string, despite // panics because the given slice is not derived from the original byte string, despite
// being a logical subset of the string // being a logical subset of the string
ByteString::from_static("foo bar").slice_ref("foo"); ByteString::from_static("foo bar").slice_ref("foo");