Skip to content
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
15 changes: 15 additions & 0 deletions library/coretests/tests/fmt/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 30 additions & 0 deletions library/coretests/tests/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,36 @@ fn test_fmt_debug_of_raw_pointers() {
check_fmt(vtable as *const dyn Debug, "Pointer { addr: ", ", metadata: DynMetadata(");
}

#[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);
Expand Down
6 changes: 6 additions & 0 deletions library/coretests/tests/fmt/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
1 change: 1 addition & 0 deletions library/coretests/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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)]
Expand Down
Loading