Skip to content

Commit 4012eb6

Browse files
committed
add some tests
1 parent f8c0337 commit 4012eb6

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

src/string.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ impl<LenT: ValidLength> serde::Serialize for FixedString<LenT> {
482482
#[cfg(test)]
483483
mod test {
484484
use super::*;
485+
use core::fmt::Debug;
485486

486487
fn check_u8_roundtrip_generic(to_fixed: fn(String) -> FixedString<u8>) {
487488
for i in 0..=u8::MAX {
@@ -692,4 +693,111 @@ mod test {
692693
assert_eq!(s.len(), 4);
693694
assert!(s.is_inline());
694695
}
696+
697+
fn try_from_rountrip<LenT, S>(value: S)
698+
where
699+
LenT: ValidLength,
700+
FixedString<LenT>: TryFrom<S>,
701+
<FixedString<LenT> as TryFrom<S>>::Error: Debug,
702+
S: AsRef<str>,
703+
S: From<FixedString<LenT>>,
704+
Box<str>: From<S>,
705+
{
706+
let string = value.as_ref().to_string();
707+
708+
let fixed_str: FixedString<LenT> = value.try_into().expect("Try into should work");
709+
710+
assert_eq!(fixed_str, string);
711+
712+
let value: S = fixed_str.into();
713+
714+
assert_eq!(value.as_ref(), string);
715+
716+
let fixed_str = FixedString::<LenT>::from_string_trunc(value);
717+
718+
assert_eq!(fixed_str, string);
719+
720+
let fixed_str = FixedString::<LenT>::from_string_trunc::<FixedString<LenT>>(fixed_str);
721+
722+
let value: S = fixed_str.into();
723+
724+
let fixed_str = FixedString::<LenT>::try_from_string(value).expect("try_from_string works");
725+
726+
assert_eq!(fixed_str, string);
727+
728+
let fixed_str = FixedString::<LenT>::try_from_string::<FixedString<LenT>>(fixed_str)
729+
.expect("try_from_string works");
730+
731+
assert_eq!(fixed_str, string);
732+
}
733+
734+
#[test]
735+
fn test_try_from_string() {
736+
let value = "Hello, world!";
737+
738+
try_from_rountrip::<u8, String>(value.into());
739+
try_from_rountrip::<u16, String>(value.into());
740+
#[cfg(any(target_pointer_width = "64", target_pointer_width = "32"))]
741+
try_from_rountrip::<u32, String>(value.into());
742+
}
743+
744+
#[test]
745+
fn test_try_from_boxed_str() {
746+
let value = "Hello, world!";
747+
748+
try_from_rountrip::<u8, Box<str>>(value.into());
749+
try_from_rountrip::<u16, Box<str>>(value.into());
750+
#[cfg(any(target_pointer_width = "64", target_pointer_width = "32"))]
751+
try_from_rountrip::<u32, Box<str>>(value.into());
752+
}
753+
754+
#[test]
755+
fn test_try_from_owned_cow_string() {
756+
let owned_cow: Cow<'static, str> = Cow::Owned("Hello, world!".into());
757+
758+
#[cfg(any(target_pointer_width = "64", target_pointer_width = "32"))]
759+
try_from_rountrip::<u32, Cow<'static, str>>(owned_cow.clone());
760+
try_from_rountrip::<u16, Cow<'static, str>>(owned_cow.clone());
761+
try_from_rountrip::<u8, Cow<'static, str>>(owned_cow);
762+
}
763+
764+
#[test]
765+
fn test_try_from_cow_string() {
766+
let owned_cow: Cow<'_, str> = Cow::Borrowed("Hello, world!");
767+
768+
#[cfg(any(target_pointer_width = "64", target_pointer_width = "32"))]
769+
try_from_rountrip::<u32, Cow<'_, str>>(owned_cow.clone());
770+
try_from_rountrip::<u16, Cow<'_, str>>(owned_cow.clone());
771+
try_from_rountrip::<u8, Cow<'_, str>>(owned_cow);
772+
}
773+
774+
#[test]
775+
fn test_try_from_rc_str() {
776+
let static_string = "Test string";
777+
778+
let rc_string: Rc<str> = static_string.into();
779+
780+
let value: FixedString::<u8> = rc_string.try_into().unwrap();
781+
782+
assert_eq!(static_string, value);
783+
784+
let rc_string: Rc<str> = value.into();
785+
786+
assert_eq!(static_string, rc_string.as_ref());
787+
}
788+
789+
#[test]
790+
fn test_try_from_arc_str() {
791+
let static_string = "Test string";
792+
793+
let arc_string: Arc<str> = static_string.into();
794+
795+
let value: FixedString::<u8> = arc_string.try_into().unwrap();
796+
797+
assert_eq!(static_string, value);
798+
799+
let arc_string: Arc<str> = value.into();
800+
801+
assert_eq!(static_string, arc_string.as_ref());
802+
}
695803
}

0 commit comments

Comments
 (0)