diff options
| author | Matthias Krüger <476013+matthiaskrgr@users.noreply.github.com> | 2025-09-15 06:03:46 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-15 06:03:46 +0200 |
| commit | 9070e95dda1a5ca675ae82a70d2f3061d21bb235 (patch) | |
| tree | 125ea75c0f3cc6beca60a6a606efbafc87d45c56 /library/coretests | |
| parent | d316cfdbf94608ebbead09635f3c1cb0129e5c96 (diff) | |
| parent | 2e652d7d13ff9f1944b92a4f20248a61a96fe71c (diff) | |
| download | rust-9070e95dda1a5ca675ae82a70d2f3061d21bb235.tar.gz rust-9070e95dda1a5ca675ae82a70d2f3061d21bb235.zip | |
Rollup merge of #146478 - ferrocene:pvdrz/improve-fmt-coverage, r=Mark-Simulacrum
Improve `core::fmt` coverage This PR improves the `core::fmt` coverage by adding new tests to `coretests`
Diffstat (limited to 'library/coretests')
| -rw-r--r-- | library/coretests/tests/fmt/builders.rs | 15 | ||||
| -rw-r--r-- | library/coretests/tests/fmt/mod.rs | 30 | ||||
| -rw-r--r-- | library/coretests/tests/fmt/num.rs | 6 | ||||
| -rw-r--r-- | library/coretests/tests/lib.rs | 1 |
4 files changed, 52 insertions, 0 deletions
diff --git a/library/coretests/tests/fmt/builders.rs b/library/coretests/tests/fmt/builders.rs index ba4801f5912..156eebb1e9d 100644 --- a/library/coretests/tests/fmt/builders.rs +++ b/library/coretests/tests/fmt/builders.rs @@ -173,6 +173,21 @@ mod debug_struct { format!("{Bar:#?}") ); } + + #[test] + fn test_field_with() { + struct Foo; + impl fmt::Debug for Foo { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt.debug_struct("Foo") + .field_with("bar", |f| f.write_str("true")) + .field_with("baz", |f| f.write_str("false")) + .finish() + } + } + + assert_eq!("Foo {\n bar: true,\n baz: false,\n}", format!("{Foo:#?}")) + } } mod debug_tuple { diff --git a/library/coretests/tests/fmt/mod.rs b/library/coretests/tests/fmt/mod.rs index 16f116d2590..586e890befe 100644 --- a/library/coretests/tests/fmt/mod.rs +++ b/library/coretests/tests/fmt/mod.rs @@ -58,6 +58,36 @@ fn test_fmt_debug_of_raw_pointers() { } #[test] +fn test_fmt_debug_of_mut_reference() { + let mut x: u32 = 0; + + assert_eq!(format!("{:?}", &mut x), "0"); +} + +#[test] +fn test_default_write_impls() { + use core::fmt::Write; + + struct Buf(String); + + impl Write for Buf { + fn write_str(&mut self, s: &str) -> core::fmt::Result { + self.0.write_str(s) + } + } + + let mut buf = Buf(String::new()); + buf.write_char('a').unwrap(); + + assert_eq!(buf.0, "a"); + + let mut buf = Buf(String::new()); + buf.write_fmt(format_args!("a")).unwrap(); + + assert_eq!(buf.0, "a"); +} + +#[test] fn test_estimated_capacity() { assert_eq!(format_args!("").estimated_capacity(), 0); assert_eq!(format_args!("{}", { "" }).estimated_capacity(), 0); diff --git a/library/coretests/tests/fmt/num.rs b/library/coretests/tests/fmt/num.rs index 4c145b484dd..052b16db521 100644 --- a/library/coretests/tests/fmt/num.rs +++ b/library/coretests/tests/fmt/num.rs @@ -323,3 +323,9 @@ fn test_format_debug_hex() { assert_eq!(format!("{:02x?}", b"Foo\0"), "[46, 6f, 6f, 00]"); assert_eq!(format!("{:02X?}", b"Foo\0"), "[46, 6F, 6F, 00]"); } + +#[test] +#[should_panic = "Formatting argument out of range"] +fn test_rt_width_too_long() { + let _ = format!("Hello {:width$}!", "x", width = u16::MAX as usize + 1); +} diff --git a/library/coretests/tests/lib.rs b/library/coretests/tests/lib.rs index a246c806140..5c519f3a499 100644 --- a/library/coretests/tests/lib.rs +++ b/library/coretests/tests/lib.rs @@ -33,6 +33,7 @@ #![feature(core_private_bignum)] #![feature(core_private_diy_float)] #![feature(cstr_display)] +#![feature(debug_closure_helpers)] #![feature(dec2flt)] #![feature(drop_guard)] #![feature(duration_constants)] |
