Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests and coverage for fmt::Debug impls of slice internals #237

Merged
merged 2 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ jobs:
- name: Install Rust toolchain
uses: artichoke/setup-rust/[email protected]
with:
toolchain: "1.56.0"
toolchain: "1.58.0"

- name: Compile
run: cargo build --verbose
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "1.8.0" # remember to set `html_root_url` in `src/lib.rs`.
authors = ["Ryan Lopopolo <[email protected]>"]
license = "MIT"
edition = "2021"
rust-version = "1.56.0"
rust-version = "1.58.0"
readme = "README.md"
repository = "https://github.com/artichoke/intaglio"
documentation = "https://docs.rs/intaglio"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ All features are enabled by default.

### Minimum Supported Rust Version

This crate requires at least Rust 1.56.0. This version can be bumped in minor
This crate requires at least Rust 1.58.0. This version can be bumped in minor
releases.

## License
Expand Down
198 changes: 198 additions & 0 deletions src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,201 @@ impl fmt::Debug for Slice<Path> {
}
}
}

#[cfg(test)]
mod tests {
use core::fmt::Write;
use std::borrow::Cow;
#[cfg(feature = "cstr")]
use std::ffi::CStr;
#[cfg(feature = "osstr")]
use std::ffi::OsStr;
#[cfg(feature = "path")]
use std::path::Path;

use super::Interned;

#[test]
fn test_interned_static_str_debug_format() {
let s = Interned::from(Cow::Borrowed("abc"));
let mut buf = String::new();
write!(&mut buf, "{s:?}").unwrap();
assert_eq!(buf, "\"abc\"");
}

#[test]
fn test_interned_owned_str_debug_format() {
let s = Interned::<str>::from(Cow::Owned("abc".to_string()));
let mut buf = String::new();
write!(&mut buf, "{s:?}").unwrap();
assert_eq!(buf, "\"abc\"");
}

#[test]
#[cfg(feature = "bytes")]
fn test_interned_static_bytes_debug_format() {
let s = Interned::from(Cow::Borrowed(&b"abc"[..]));
let mut buf = String::new();
write!(&mut buf, "{s:?}").unwrap();
assert_eq!(buf, "[97, 98, 99]");

let s = Interned::from(Cow::Borrowed(&b"\xFF"[..]));
let mut buf = String::new();
write!(&mut buf, "{s:?}").unwrap();
assert_eq!(buf, "[255]");

let s = Interned::from(Cow::Borrowed(&b"abc"[..]));
let mut buf = String::new();
write!(&mut buf, "{s:#?}").unwrap();
assert_eq!(buf, "\"abc\"");

let s = Interned::from(Cow::Borrowed(&b"\xFF"[..]));
let mut buf = String::new();
write!(&mut buf, "{s:#?}").unwrap();
assert_eq!(buf, "\"\u{FFFD}\"");
}

#[test]
#[cfg(feature = "bytes")]
fn test_interned_owned_bytes_debug_format() {
let s = Interned::<[u8]>::from(Cow::Owned(b"abc".to_vec()));
let mut buf = String::new();
write!(&mut buf, "{s:?}").unwrap();
assert_eq!(buf, "[97, 98, 99]");

let s = Interned::<[u8]>::from(Cow::Owned(b"\xFF".to_vec()));
let mut buf = String::new();
write!(&mut buf, "{s:?}").unwrap();
assert_eq!(buf, "[255]");

let s = Interned::<[u8]>::from(Cow::Owned(b"abc".to_vec()));
let mut buf = String::new();
write!(&mut buf, "{s:#?}").unwrap();
assert_eq!(buf, "\"abc\"");

let s = Interned::<[u8]>::from(Cow::Owned(b"\xFF".to_vec()));
let mut buf = String::new();
write!(&mut buf, "{s:#?}").unwrap();
assert_eq!(buf, "\"\u{FFFD}\"");
}

#[test]
#[cfg(feature = "cstr")]
fn test_interned_static_cstr_debug_format() {
let s = Interned::from(Cow::Borrowed(
CStr::from_bytes_with_nul(b"abc\x00").unwrap(),
));
let mut buf = String::new();
write!(&mut buf, "{s:?}").unwrap();
assert_eq!(buf, "\"abc\"");

let s = Interned::from(Cow::Borrowed(
CStr::from_bytes_with_nul(b"\xFF\x00").unwrap(),
));
let mut buf = String::new();
write!(&mut buf, "{s:?}").unwrap();
assert_eq!(buf, r#""\xff""#);

let s = Interned::from(Cow::Borrowed(
CStr::from_bytes_with_nul(b"abc\x00").unwrap(),
));
let mut buf = String::new();
write!(&mut buf, "{s:#?}").unwrap();
assert_eq!(buf, "\"abc\"");

let s = Interned::from(Cow::Borrowed(
CStr::from_bytes_with_nul(b"\xFF\x00").unwrap(),
));
let mut buf = String::new();
write!(&mut buf, "{s:#?}").unwrap();
assert_eq!(buf, "\"\u{FFFD}\"");
}

#[test]
#[cfg(feature = "cstr")]
fn test_interned_owned_cstring_debug_format() {
let s = Interned::<CStr>::from(Cow::Owned(
CStr::from_bytes_with_nul(b"abc\x00").unwrap().to_owned(),
));
let mut buf = String::new();
write!(&mut buf, "{s:?}").unwrap();
assert_eq!(buf, "\"abc\"");

let s = Interned::<CStr>::from(Cow::Owned(
CStr::from_bytes_with_nul(b"\xFF\x00").unwrap().to_owned(),
));
let mut buf = String::new();
write!(&mut buf, "{s:?}").unwrap();
assert_eq!(buf, r#""\xff""#);

let s = Interned::<CStr>::from(Cow::Owned(
CStr::from_bytes_with_nul(b"abc\x00").unwrap().to_owned(),
));
let mut buf = String::new();
write!(&mut buf, "{s:#?}").unwrap();
assert_eq!(buf, "\"abc\"");

let s = Interned::<CStr>::from(Cow::Owned(
CStr::from_bytes_with_nul(b"\xFF\x00").unwrap().to_owned(),
));
let mut buf = String::new();
write!(&mut buf, "{s:#?}").unwrap();
assert_eq!(buf, "\"\u{FFFD}\"");
}

#[test]
#[cfg(feature = "osstr")]
fn test_interned_static_osstr_debug_format() {
let s = Interned::from(Cow::Borrowed(OsStr::new("abc")));
let mut buf = String::new();
write!(&mut buf, "{s:?}").unwrap();
assert_eq!(buf, "\"abc\"");

let s = Interned::from(Cow::Borrowed(OsStr::new("abc")));
let mut buf = String::new();
write!(&mut buf, "{s:#?}").unwrap();
assert_eq!(buf, "\"abc\"");
}

#[test]
#[cfg(feature = "osstr")]
fn test_interned_owned_osstring_debug_format() {
let s = Interned::<OsStr>::from(Cow::Owned(OsStr::new("abc").to_owned()));
let mut buf = String::new();
write!(&mut buf, "{s:?}").unwrap();
assert_eq!(buf, "\"abc\"");

let s = Interned::<OsStr>::from(Cow::Owned(OsStr::new("abc").to_owned()));
let mut buf = String::new();
write!(&mut buf, "{s:#?}").unwrap();
assert_eq!(buf, "\"abc\"");
}

#[test]
#[cfg(feature = "path")]
fn test_interned_static_path_debug_format() {
let s = Interned::from(Cow::Borrowed(Path::new("abc")));
let mut buf = String::new();
write!(&mut buf, "{s:?}").unwrap();
assert_eq!(buf, "\"abc\"");

let s = Interned::from(Cow::Borrowed(Path::new("abc")));
let mut buf = String::new();
write!(&mut buf, "{s:#?}").unwrap();
assert_eq!(buf, "\"abc\"");
}

#[test]
#[cfg(feature = "path")]
fn test_interned_owned_pathbuf_debug_format() {
let s = Interned::<Path>::from(Cow::Owned(Path::new("abc").to_owned()));
let mut buf = String::new();
write!(&mut buf, "{s:?}").unwrap();
assert_eq!(buf, "\"abc\"");

let s = Interned::<Path>::from(Cow::Owned(Path::new("abc").to_owned()));
let mut buf = String::new();
write!(&mut buf, "{s:#?}").unwrap();
assert_eq!(buf, "\"abc\"");
}
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,15 @@ mod tests {
fn error_display_is_not_empty() {
let tc = SymbolOverflowError::new();
let mut buf = String::new();
write!(&mut buf, "{}", tc).unwrap();
write!(&mut buf, "{tc}").unwrap();
assert!(!buf.is_empty());
}

#[test]
fn error_debug_is_not_empty() {
let tc = SymbolOverflowError::new();
let mut buf = String::new();
write!(&mut buf, "{:?}", tc).unwrap();
write!(&mut buf, "{tc:?}").unwrap();
assert!(!buf.is_empty());
}

Expand Down
Loading