@@ -353,3 +353,201 @@ impl fmt::Debug for Slice<Path> {
353353 }
354354 }
355355}
356+
357+ #[ cfg( test) ]
358+ mod tests {
359+ use core:: fmt:: Write ;
360+ use std:: borrow:: Cow ;
361+ #[ cfg( feature = "cstr" ) ]
362+ use std:: ffi:: CStr ;
363+ #[ cfg( feature = "osstr" ) ]
364+ use std:: ffi:: OsStr ;
365+ #[ cfg( feature = "path" ) ]
366+ use std:: path:: Path ;
367+
368+ use super :: Interned ;
369+
370+ #[ test]
371+ fn test_interned_static_str_debug_format ( ) {
372+ let s = Interned :: from ( Cow :: Borrowed ( "abc" ) ) ;
373+ let mut buf = String :: new ( ) ;
374+ write ! ( & mut buf, "{s:?}" ) . unwrap ( ) ;
375+ assert_eq ! ( buf, "\" abc\" " ) ;
376+ }
377+
378+ #[ test]
379+ fn test_interned_owned_str_debug_format ( ) {
380+ let s = Interned :: < str > :: from ( Cow :: Owned ( "abc" . to_string ( ) ) ) ;
381+ let mut buf = String :: new ( ) ;
382+ write ! ( & mut buf, "{s:?}" ) . unwrap ( ) ;
383+ assert_eq ! ( buf, "\" abc\" " ) ;
384+ }
385+
386+ #[ test]
387+ #[ cfg( feature = "bytes" ) ]
388+ fn test_interned_static_bytes_debug_format ( ) {
389+ let s = Interned :: from ( Cow :: Borrowed ( & b"abc" [ ..] ) ) ;
390+ let mut buf = String :: new ( ) ;
391+ write ! ( & mut buf, "{s:?}" ) . unwrap ( ) ;
392+ assert_eq ! ( buf, "[97, 98, 99]" ) ;
393+
394+ let s = Interned :: from ( Cow :: Borrowed ( & b"\xFF " [ ..] ) ) ;
395+ let mut buf = String :: new ( ) ;
396+ write ! ( & mut buf, "{s:?}" ) . unwrap ( ) ;
397+ assert_eq ! ( buf, "[255]" ) ;
398+
399+ let s = Interned :: from ( Cow :: Borrowed ( & b"abc" [ ..] ) ) ;
400+ let mut buf = String :: new ( ) ;
401+ write ! ( & mut buf, "{s:#?}" ) . unwrap ( ) ;
402+ assert_eq ! ( buf, "\" abc\" " ) ;
403+
404+ let s = Interned :: from ( Cow :: Borrowed ( & b"\xFF " [ ..] ) ) ;
405+ let mut buf = String :: new ( ) ;
406+ write ! ( & mut buf, "{s:#?}" ) . unwrap ( ) ;
407+ assert_eq ! ( buf, "\" \u{FFFD} \" " ) ;
408+ }
409+
410+ #[ test]
411+ #[ cfg( feature = "bytes" ) ]
412+ fn test_interned_owned_bytes_debug_format ( ) {
413+ let s = Interned :: < [ u8 ] > :: from ( Cow :: Owned ( b"abc" . to_vec ( ) ) ) ;
414+ let mut buf = String :: new ( ) ;
415+ write ! ( & mut buf, "{s:?}" ) . unwrap ( ) ;
416+ assert_eq ! ( buf, "[97, 98, 99]" ) ;
417+
418+ let s = Interned :: < [ u8 ] > :: from ( Cow :: Owned ( b"\xFF " . to_vec ( ) ) ) ;
419+ let mut buf = String :: new ( ) ;
420+ write ! ( & mut buf, "{s:?}" ) . unwrap ( ) ;
421+ assert_eq ! ( buf, "[255]" ) ;
422+
423+ let s = Interned :: < [ u8 ] > :: from ( Cow :: Owned ( b"abc" . to_vec ( ) ) ) ;
424+ let mut buf = String :: new ( ) ;
425+ write ! ( & mut buf, "{s:#?}" ) . unwrap ( ) ;
426+ assert_eq ! ( buf, "\" abc\" " ) ;
427+
428+ let s = Interned :: < [ u8 ] > :: from ( Cow :: Owned ( b"\xFF " . to_vec ( ) ) ) ;
429+ let mut buf = String :: new ( ) ;
430+ write ! ( & mut buf, "{s:#?}" ) . unwrap ( ) ;
431+ assert_eq ! ( buf, "\" \u{FFFD} \" " ) ;
432+ }
433+
434+ #[ test]
435+ #[ cfg( feature = "cstr" ) ]
436+ fn test_interned_static_cstr_debug_format ( ) {
437+ let s = Interned :: from ( Cow :: Borrowed (
438+ CStr :: from_bytes_with_nul ( b"abc\x00 " ) . unwrap ( ) ,
439+ ) ) ;
440+ let mut buf = String :: new ( ) ;
441+ write ! ( & mut buf, "{s:?}" ) . unwrap ( ) ;
442+ assert_eq ! ( buf, "\" abc\" " ) ;
443+
444+ let s = Interned :: from ( Cow :: Borrowed (
445+ CStr :: from_bytes_with_nul ( b"\xFF \x00 " ) . unwrap ( ) ,
446+ ) ) ;
447+ let mut buf = String :: new ( ) ;
448+ write ! ( & mut buf, "{s:?}" ) . unwrap ( ) ;
449+ assert_eq ! ( buf, r#""\xff""# ) ;
450+
451+ let s = Interned :: from ( Cow :: Borrowed (
452+ CStr :: from_bytes_with_nul ( b"abc\x00 " ) . unwrap ( ) ,
453+ ) ) ;
454+ let mut buf = String :: new ( ) ;
455+ write ! ( & mut buf, "{s:#?}" ) . unwrap ( ) ;
456+ assert_eq ! ( buf, "\" abc\" " ) ;
457+
458+ let s = Interned :: from ( Cow :: Borrowed (
459+ CStr :: from_bytes_with_nul ( b"\xFF \x00 " ) . unwrap ( ) ,
460+ ) ) ;
461+ let mut buf = String :: new ( ) ;
462+ write ! ( & mut buf, "{s:#?}" ) . unwrap ( ) ;
463+ assert_eq ! ( buf, "\" \u{FFFD} \" " ) ;
464+ }
465+
466+ #[ test]
467+ #[ cfg( feature = "cstr" ) ]
468+ fn test_interned_owned_cstring_debug_format ( ) {
469+ let s = Interned :: < CStr > :: from ( Cow :: Owned (
470+ CStr :: from_bytes_with_nul ( b"abc\x00 " ) . unwrap ( ) . to_owned ( ) ,
471+ ) ) ;
472+ let mut buf = String :: new ( ) ;
473+ write ! ( & mut buf, "{s:?}" ) . unwrap ( ) ;
474+ assert_eq ! ( buf, "\" abc\" " ) ;
475+
476+ let s = Interned :: < CStr > :: from ( Cow :: Owned (
477+ CStr :: from_bytes_with_nul ( b"\xFF \x00 " ) . unwrap ( ) . to_owned ( ) ,
478+ ) ) ;
479+ let mut buf = String :: new ( ) ;
480+ write ! ( & mut buf, "{s:?}" ) . unwrap ( ) ;
481+ assert_eq ! ( buf, r#""\xff""# ) ;
482+
483+ let s = Interned :: < CStr > :: from ( Cow :: Owned (
484+ CStr :: from_bytes_with_nul ( b"abc\x00 " ) . unwrap ( ) . to_owned ( ) ,
485+ ) ) ;
486+ let mut buf = String :: new ( ) ;
487+ write ! ( & mut buf, "{s:#?}" ) . unwrap ( ) ;
488+ assert_eq ! ( buf, "\" abc\" " ) ;
489+
490+ let s = Interned :: < CStr > :: from ( Cow :: Owned (
491+ CStr :: from_bytes_with_nul ( b"\xFF \x00 " ) . unwrap ( ) . to_owned ( ) ,
492+ ) ) ;
493+ let mut buf = String :: new ( ) ;
494+ write ! ( & mut buf, "{s:#?}" ) . unwrap ( ) ;
495+ assert_eq ! ( buf, "\" \u{FFFD} \" " ) ;
496+ }
497+
498+ #[ test]
499+ #[ cfg( feature = "osstr" ) ]
500+ fn test_interned_static_osstr_debug_format ( ) {
501+ let s = Interned :: from ( Cow :: Borrowed ( OsStr :: new ( "abc" ) ) ) ;
502+ let mut buf = String :: new ( ) ;
503+ write ! ( & mut buf, "{s:?}" ) . unwrap ( ) ;
504+ assert_eq ! ( buf, "\" abc\" " ) ;
505+
506+ let s = Interned :: from ( Cow :: Borrowed ( OsStr :: new ( "abc" ) ) ) ;
507+ let mut buf = String :: new ( ) ;
508+ write ! ( & mut buf, "{s:#?}" ) . unwrap ( ) ;
509+ assert_eq ! ( buf, "\" abc\" " ) ;
510+ }
511+
512+ #[ test]
513+ #[ cfg( feature = "osstr" ) ]
514+ fn test_interned_owned_osstring_debug_format ( ) {
515+ let s = Interned :: < OsStr > :: from ( Cow :: Owned ( OsStr :: new ( "abc" ) . to_owned ( ) ) ) ;
516+ let mut buf = String :: new ( ) ;
517+ write ! ( & mut buf, "{s:?}" ) . unwrap ( ) ;
518+ assert_eq ! ( buf, "\" abc\" " ) ;
519+
520+ let s = Interned :: < OsStr > :: from ( Cow :: Owned ( OsStr :: new ( "abc" ) . to_owned ( ) ) ) ;
521+ let mut buf = String :: new ( ) ;
522+ write ! ( & mut buf, "{s:#?}" ) . unwrap ( ) ;
523+ assert_eq ! ( buf, "\" abc\" " ) ;
524+ }
525+
526+ #[ test]
527+ #[ cfg( feature = "path" ) ]
528+ fn test_interned_static_path_debug_format ( ) {
529+ let s = Interned :: from ( Cow :: Borrowed ( Path :: new ( "abc" ) ) ) ;
530+ let mut buf = String :: new ( ) ;
531+ write ! ( & mut buf, "{s:?}" ) . unwrap ( ) ;
532+ assert_eq ! ( buf, "\" abc\" " ) ;
533+
534+ let s = Interned :: from ( Cow :: Borrowed ( Path :: new ( "abc" ) ) ) ;
535+ let mut buf = String :: new ( ) ;
536+ write ! ( & mut buf, "{s:#?}" ) . unwrap ( ) ;
537+ assert_eq ! ( buf, "\" abc\" " ) ;
538+ }
539+
540+ #[ test]
541+ #[ cfg( feature = "path" ) ]
542+ fn test_interned_owned_pathbuf_debug_format ( ) {
543+ let s = Interned :: < Path > :: from ( Cow :: Owned ( Path :: new ( "abc" ) . to_owned ( ) ) ) ;
544+ let mut buf = String :: new ( ) ;
545+ write ! ( & mut buf, "{s:?}" ) . unwrap ( ) ;
546+ assert_eq ! ( buf, "\" abc\" " ) ;
547+
548+ let s = Interned :: < Path > :: from ( Cow :: Owned ( Path :: new ( "abc" ) . to_owned ( ) ) ) ;
549+ let mut buf = String :: new ( ) ;
550+ write ! ( & mut buf, "{s:#?}" ) . unwrap ( ) ;
551+ assert_eq ! ( buf, "\" abc\" " ) ;
552+ }
553+ }
0 commit comments