Skip to content

Commit 2e652d7

Browse files
committed
Improve core::fmt coverage
1 parent 408eacf commit 2e652d7

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

library/coretests/tests/fmt/builders.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,21 @@ mod debug_struct {
173173
format!("{Bar:#?}")
174174
);
175175
}
176+
177+
#[test]
178+
fn test_field_with() {
179+
struct Foo;
180+
impl fmt::Debug for Foo {
181+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
182+
fmt.debug_struct("Foo")
183+
.field_with("bar", |f| f.write_str("true"))
184+
.field_with("baz", |f| f.write_str("false"))
185+
.finish()
186+
}
187+
}
188+
189+
assert_eq!("Foo {\n bar: true,\n baz: false,\n}", format!("{Foo:#?}"))
190+
}
176191
}
177192

178193
mod debug_tuple {

library/coretests/tests/fmt/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,36 @@ fn test_fmt_debug_of_raw_pointers() {
5757
check_fmt(vtable as *const dyn Debug, "Pointer { addr: ", ", metadata: DynMetadata(");
5858
}
5959

60+
#[test]
61+
fn test_fmt_debug_of_mut_reference() {
62+
let mut x: u32 = 0;
63+
64+
assert_eq!(format!("{:?}", &mut x), "0");
65+
}
66+
67+
#[test]
68+
fn test_default_write_impls() {
69+
use core::fmt::Write;
70+
71+
struct Buf(String);
72+
73+
impl Write for Buf {
74+
fn write_str(&mut self, s: &str) -> core::fmt::Result {
75+
self.0.write_str(s)
76+
}
77+
}
78+
79+
let mut buf = Buf(String::new());
80+
buf.write_char('a').unwrap();
81+
82+
assert_eq!(buf.0, "a");
83+
84+
let mut buf = Buf(String::new());
85+
buf.write_fmt(format_args!("a")).unwrap();
86+
87+
assert_eq!(buf.0, "a");
88+
}
89+
6090
#[test]
6191
fn test_estimated_capacity() {
6292
assert_eq!(format_args!("").estimated_capacity(), 0);

library/coretests/tests/fmt/num.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,9 @@ fn test_format_debug_hex() {
323323
assert_eq!(format!("{:02x?}", b"Foo\0"), "[46, 6f, 6f, 00]");
324324
assert_eq!(format!("{:02X?}", b"Foo\0"), "[46, 6F, 6F, 00]");
325325
}
326+
327+
#[test]
328+
#[should_panic = "Formatting argument out of range"]
329+
fn test_rt_width_too_long() {
330+
let _ = format!("Hello {:width$}!", "x", width = u16::MAX as usize + 1);
331+
}

library/coretests/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#![feature(core_private_bignum)]
3232
#![feature(core_private_diy_float)]
3333
#![feature(cstr_display)]
34+
#![feature(debug_closure_helpers)]
3435
#![feature(dec2flt)]
3536
#![feature(drop_guard)]
3637
#![feature(duration_constants)]

0 commit comments

Comments
 (0)