Skip to content

Commit 9070e95

Browse files
authored
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`
2 parents d316cfd + 2e652d7 commit 9070e95

4 files changed

Lines changed: 52 additions & 0 deletions

File tree

‎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+
fntest_field_with(){
179+
structFoo;
180+
impl fmt::DebugforFoo{
181+
fnfmt(&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*constdynDebug,"Pointer { addr: ",", metadata: DynMetadata(");
5858
}
5959

60+
#[test]
61+
fntest_fmt_debug_of_mut_reference(){
62+
letmut x:u32 = 0;
63+
64+
assert_eq!(format!("{:?}",&mut x),"0");
65+
}
66+
67+
#[test]
68+
fntest_default_write_impls(){
69+
use core::fmt::Write;
70+
71+
structBuf(String);
72+
73+
implWriteforBuf{
74+
fnwrite_str(&mutself,s:&str) -> core::fmt::Result{
75+
self.0.write_str(s)
76+
}
77+
}
78+
79+
letmut buf = Buf(String::new());
80+
buf.write_char('a').unwrap();
81+
82+
assert_eq!(buf.0,"a");
83+
84+
letmut buf = Buf(String::new());
85+
buf.write_fmt(format_args!("a")).unwrap();
86+
87+
assert_eq!(buf.0,"a");
88+
}
89+
6090
#[test]
6191
fntest_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+
fntest_rt_width_too_long(){
330+
let _ = format!("Hello {:width$}!","x", width = u16::MAXasusize + 1);
331+
}

‎library/coretests/tests/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#![feature(core_private_bignum)]
3434
#![feature(core_private_diy_float)]
3535
#![feature(cstr_display)]
36+
#![feature(debug_closure_helpers)]
3637
#![feature(dec2flt)]
3738
#![feature(drop_guard)]
3839
#![feature(duration_constants)]

0 commit comments

Comments
 (0)