mirror of
https://github.com/fafhrd91/actix-net
synced 2024-11-27 15:42:57 +01:00
increase bytestring test coverage
This commit is contained in:
parent
4cbe741230
commit
177590a7d8
@ -11,7 +11,7 @@ use alloc::{
|
||||
string::{String, ToString},
|
||||
vec::Vec,
|
||||
};
|
||||
use core::{borrow, convert::TryFrom, fmt, hash, ops, str};
|
||||
use core::{borrow::Borrow, convert::TryFrom, fmt, hash, ops, str};
|
||||
|
||||
use bytes::Bytes;
|
||||
|
||||
@ -132,13 +132,12 @@ impl ops::Deref for ByteString {
|
||||
#[inline]
|
||||
fn deref(&self) -> &str {
|
||||
let bytes = self.0.as_ref();
|
||||
// SAFETY:
|
||||
// UTF-8 validity is guaranteed at during construction.
|
||||
// SAFETY: UTF-8 validity is guaranteed during construction.
|
||||
unsafe { str::from_utf8_unchecked(bytes) }
|
||||
}
|
||||
}
|
||||
|
||||
impl borrow::Borrow<str> for ByteString {
|
||||
impl Borrow<str> for ByteString {
|
||||
fn borrow(&self) -> &str {
|
||||
self
|
||||
}
|
||||
@ -292,7 +291,7 @@ mod serde {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use alloc::borrow::ToOwned;
|
||||
use alloc::{borrow::ToOwned, format, vec};
|
||||
use core::{
|
||||
hash::{Hash, Hasher},
|
||||
panic::{RefUnwindSafe, UnwindSafe},
|
||||
@ -309,7 +308,7 @@ mod test {
|
||||
assert_impl_all!(ByteString: UnwindSafe, RefUnwindSafe);
|
||||
|
||||
#[test]
|
||||
fn test_partial_eq() {
|
||||
fn eq() {
|
||||
let s: ByteString = ByteString::from_static("test");
|
||||
assert_eq!(s, "test");
|
||||
assert_eq!(s, *"test");
|
||||
@ -317,12 +316,45 @@ mod test {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_new() {
|
||||
fn new() {
|
||||
let _: ByteString = ByteString::new();
|
||||
}
|
||||
|
||||
#[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();
|
||||
"str".hash(&mut hasher1);
|
||||
|
||||
@ -333,7 +365,7 @@ mod test {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_string() {
|
||||
fn from_string() {
|
||||
let s: ByteString = "hello".to_owned().into();
|
||||
assert_eq!(&s, "hello");
|
||||
let t: &str = s.as_ref();
|
||||
@ -341,23 +373,30 @@ mod test {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_str() {
|
||||
fn from_str() {
|
||||
let _: ByteString = "str".into();
|
||||
let _: ByteString = "str".to_owned().into_boxed_str().into();
|
||||
}
|
||||
|
||||
#[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");
|
||||
let _ = ByteString::from_static("str");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_try_from_slice() {
|
||||
fn try_from_slice() {
|
||||
let _ = ByteString::try_from(b"nice bytes").unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_try_from_array() {
|
||||
fn try_from_array() {
|
||||
assert_eq!(
|
||||
ByteString::try_from([b'h', b'i']).unwrap(),
|
||||
ByteString::from_static("hi")
|
||||
@ -365,25 +404,43 @@ mod 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();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_try_from_bytes_mut() {
|
||||
fn try_from_bytes_mut() {
|
||||
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")]
|
||||
#[test]
|
||||
fn test_serialize() {
|
||||
fn serialize() {
|
||||
let s: ByteString = serde_json::from_str(r#""nice bytes""#).unwrap();
|
||||
assert_eq!(s, "nice bytes");
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
#[test]
|
||||
fn test_deserialize() {
|
||||
fn deserialize() {
|
||||
let s = serde_json::to_string(&ByteString::from_static("nice bytes")).unwrap();
|
||||
assert_eq!(s, r#""nice bytes""#);
|
||||
}
|
||||
@ -399,7 +456,7 @@ mod test {
|
||||
|
||||
#[test]
|
||||
#[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
|
||||
// being a logical subset of the string
|
||||
ByteString::from_static("foo bar").slice_ref("foo");
|
||||
|
Loading…
Reference in New Issue
Block a user