Skip to content

Commit 6b7568c

Browse files
authored
Merge pull request #237 from artichoke/lopopolo/internal-fmt-debug-code-coverage
Add tests and coverage for `fmt::Debug` impls of slice internals
2 parents 70ccd8b + 6157e78 commit 6b7568c

File tree

5 files changed

+203
-5
lines changed

5 files changed

+203
-5
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ jobs:
109109
- name: Install Rust toolchain
110110
uses: artichoke/setup-rust/[email protected]
111111
with:
112-
toolchain: "1.56.0"
112+
toolchain: "1.58.0"
113113

114114
- name: Compile
115115
run: cargo build --verbose

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "1.8.0" # remember to set `html_root_url` in `src/lib.rs`.
44
authors = ["Ryan Lopopolo <[email protected]>"]
55
license = "MIT"
66
edition = "2021"
7-
rust-version = "1.56.0"
7+
rust-version = "1.58.0"
88
readme = "README.md"
99
repository = "https://github.com/artichoke/intaglio"
1010
documentation = "https://docs.rs/intaglio"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ All features are enabled by default.
136136

137137
### Minimum Supported Rust Version
138138

139-
This crate requires at least Rust 1.56.0. This version can be bumped in minor
139+
This crate requires at least Rust 1.58.0. This version can be bumped in minor
140140
releases.
141141

142142
## License

src/internal.rs

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,15 @@ mod tests {
296296
fn error_display_is_not_empty() {
297297
let tc = SymbolOverflowError::new();
298298
let mut buf = String::new();
299-
write!(&mut buf, "{}", tc).unwrap();
299+
write!(&mut buf, "{tc}").unwrap();
300300
assert!(!buf.is_empty());
301301
}
302302

303303
#[test]
304304
fn error_debug_is_not_empty() {
305305
let tc = SymbolOverflowError::new();
306306
let mut buf = String::new();
307-
write!(&mut buf, "{:?}", tc).unwrap();
307+
write!(&mut buf, "{tc:?}").unwrap();
308308
assert!(!buf.is_empty());
309309
}
310310

0 commit comments

Comments
 (0)