2015-03-06 23:16:44 -06:00
|
|
|
mod debug_struct {
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-03-06 23:16:44 -06:00
|
|
|
fmt.debug_struct("Foo").finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!("Foo", format!("{:?}", Foo));
|
|
|
|
assert_eq!("Foo", format!("{:#?}", Foo));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_single() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-12-22 16:42:04 -06:00
|
|
|
fmt.debug_struct("Foo").field("bar", &true).finish()
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!("Foo { bar: true }", format!("{:?}", Foo));
|
|
|
|
assert_eq!(
|
2019-12-22 16:42:04 -06:00
|
|
|
"Foo {
|
2019-04-03 09:30:08 -05:00
|
|
|
bar: true,
|
2015-03-06 23:16:44 -06:00
|
|
|
}",
|
2019-12-22 16:42:04 -06:00
|
|
|
format!("{:#?}", Foo)
|
|
|
|
);
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_multiple() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-03-06 23:16:44 -06:00
|
|
|
fmt.debug_struct("Foo")
|
|
|
|
.field("bar", &true)
|
2016-03-10 21:06:36 -06:00
|
|
|
.field("baz", &format_args!("{}/{}", 10, 20))
|
2015-03-06 23:16:44 -06:00
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!("Foo { bar: true, baz: 10/20 }", format!("{:?}", Foo));
|
|
|
|
assert_eq!(
|
2019-12-22 16:42:04 -06:00
|
|
|
"Foo {
|
2015-03-06 23:16:44 -06:00
|
|
|
bar: true,
|
2019-04-03 09:30:08 -05:00
|
|
|
baz: 10/20,
|
2015-03-06 23:16:44 -06:00
|
|
|
}",
|
2019-12-22 16:42:04 -06:00
|
|
|
format!("{:#?}", Foo)
|
|
|
|
);
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nested() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-03-06 23:16:44 -06:00
|
|
|
fmt.debug_struct("Foo")
|
|
|
|
.field("bar", &true)
|
2016-03-10 21:06:36 -06:00
|
|
|
.field("baz", &format_args!("{}/{}", 10, 20))
|
2015-03-06 23:16:44 -06:00
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl fmt::Debug for Bar {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-12-22 16:42:04 -06:00
|
|
|
fmt.debug_struct("Bar").field("foo", &Foo).field("hello", &"world").finish()
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
2019-12-22 16:42:04 -06:00
|
|
|
"Bar { foo: Foo { bar: true, baz: 10/20 }, hello: \"world\" }",
|
|
|
|
format!("{:?}", Bar)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
"Bar {
|
2015-03-06 23:16:44 -06:00
|
|
|
foo: Foo {
|
|
|
|
bar: true,
|
2019-04-03 09:30:08 -05:00
|
|
|
baz: 10/20,
|
2015-03-06 23:16:44 -06:00
|
|
|
},
|
2019-04-03 09:30:08 -05:00
|
|
|
hello: \"world\",
|
2015-03-06 23:16:44 -06:00
|
|
|
}",
|
2019-12-22 16:42:04 -06:00
|
|
|
format!("{:#?}", Bar)
|
|
|
|
);
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
2019-11-24 14:44:19 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_only_non_exhaustive() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2020-01-16 14:11:16 -06:00
|
|
|
fmt.debug_struct("Foo").finish_non_exhaustive()
|
2019-11-24 14:44:19 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!("Foo { .. }", format!("{:?}", Foo));
|
2021-04-21 06:50:56 -05:00
|
|
|
assert_eq!("Foo { .. }", format!("{:#?}", Foo));
|
2019-11-24 14:44:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_multiple_and_non_exhaustive() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
fmt.debug_struct("Foo")
|
|
|
|
.field("bar", &true)
|
|
|
|
.field("baz", &format_args!("{}/{}", 10, 20))
|
|
|
|
.finish_non_exhaustive()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!("Foo { bar: true, baz: 10/20, .. }", format!("{:?}", Foo));
|
|
|
|
assert_eq!(
|
2020-01-16 14:11:16 -06:00
|
|
|
"Foo {
|
2019-11-24 14:44:19 -06:00
|
|
|
bar: true,
|
|
|
|
baz: 10/20,
|
|
|
|
..
|
|
|
|
}",
|
2020-01-16 14:11:16 -06:00
|
|
|
format!("{:#?}", Foo)
|
|
|
|
);
|
2019-11-24 14:44:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nested_non_exhaustive() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
fmt.debug_struct("Foo")
|
|
|
|
.field("bar", &true)
|
|
|
|
.field("baz", &format_args!("{}/{}", 10, 20))
|
|
|
|
.finish_non_exhaustive()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl fmt::Debug for Bar {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
fmt.debug_struct("Bar")
|
|
|
|
.field("foo", &Foo)
|
|
|
|
.field("hello", &"world")
|
|
|
|
.finish_non_exhaustive()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
2020-01-16 14:11:16 -06:00
|
|
|
"Bar { foo: Foo { bar: true, baz: 10/20, .. }, hello: \"world\", .. }",
|
|
|
|
format!("{:?}", Bar)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
"Bar {
|
2019-11-24 14:44:19 -06:00
|
|
|
foo: Foo {
|
|
|
|
bar: true,
|
|
|
|
baz: 10/20,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
hello: \"world\",
|
|
|
|
..
|
|
|
|
}",
|
2020-01-16 14:11:16 -06:00
|
|
|
format!("{:#?}", Bar)
|
|
|
|
);
|
2019-11-24 14:44:19 -06:00
|
|
|
}
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
mod debug_tuple {
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-03-06 23:16:44 -06:00
|
|
|
fmt.debug_tuple("Foo").finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!("Foo", format!("{:?}", Foo));
|
|
|
|
assert_eq!("Foo", format!("{:#?}", Foo));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_single() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-12-22 16:42:04 -06:00
|
|
|
fmt.debug_tuple("Foo").field(&true).finish()
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!("Foo(true)", format!("{:?}", Foo));
|
|
|
|
assert_eq!(
|
2019-12-22 16:42:04 -06:00
|
|
|
"Foo(
|
2019-04-03 09:30:08 -05:00
|
|
|
true,
|
2015-03-06 23:16:44 -06:00
|
|
|
)",
|
2019-12-22 16:42:04 -06:00
|
|
|
format!("{:#?}", Foo)
|
|
|
|
);
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_multiple() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-12-22 16:42:04 -06:00
|
|
|
fmt.debug_tuple("Foo").field(&true).field(&format_args!("{}/{}", 10, 20)).finish()
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!("Foo(true, 10/20)", format!("{:?}", Foo));
|
|
|
|
assert_eq!(
|
2019-12-22 16:42:04 -06:00
|
|
|
"Foo(
|
2015-03-06 23:16:44 -06:00
|
|
|
true,
|
2019-04-03 09:30:08 -05:00
|
|
|
10/20,
|
2015-03-06 23:16:44 -06:00
|
|
|
)",
|
2019-12-22 16:42:04 -06:00
|
|
|
format!("{:#?}", Foo)
|
|
|
|
);
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nested() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-12-22 16:42:04 -06:00
|
|
|
fmt.debug_tuple("Foo").field(&true).field(&format_args!("{}/{}", 10, 20)).finish()
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl fmt::Debug for Bar {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-12-22 16:42:04 -06:00
|
|
|
fmt.debug_tuple("Bar").field(&Foo).field(&"world").finish()
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
assert_eq!("Bar(Foo(true, 10/20), \"world\")", format!("{:?}", Bar));
|
2015-03-06 23:16:44 -06:00
|
|
|
assert_eq!(
|
2019-12-22 16:42:04 -06:00
|
|
|
"Bar(
|
2015-03-06 23:16:44 -06:00
|
|
|
Foo(
|
|
|
|
true,
|
2019-04-03 09:30:08 -05:00
|
|
|
10/20,
|
2015-03-06 23:16:44 -06:00
|
|
|
),
|
2019-04-03 09:30:08 -05:00
|
|
|
\"world\",
|
2015-03-06 23:16:44 -06:00
|
|
|
)",
|
2019-12-22 16:42:04 -06:00
|
|
|
format!("{:#?}", Bar)
|
|
|
|
);
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod debug_map {
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-03-27 00:42:29 -05:00
|
|
|
fmt.debug_map().finish()
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-27 00:42:29 -05:00
|
|
|
assert_eq!("{}", format!("{:?}", Foo));
|
|
|
|
assert_eq!("{}", format!("{:#?}", Foo));
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_single() {
|
2019-05-01 02:19:41 -05:00
|
|
|
struct Entry;
|
2015-03-06 23:16:44 -06:00
|
|
|
|
2019-05-01 02:19:41 -05:00
|
|
|
impl fmt::Debug for Entry {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-12-22 16:42:04 -06:00
|
|
|
fmt.debug_map().entry(&"bar", &true).finish()
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 02:19:41 -05:00
|
|
|
struct KeyValue;
|
|
|
|
|
|
|
|
impl fmt::Debug for KeyValue {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-12-22 16:42:04 -06:00
|
|
|
fmt.debug_map().key(&"bar").value(&true).finish()
|
2019-05-01 02:19:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(format!("{:?}", Entry), format!("{:?}", KeyValue));
|
|
|
|
assert_eq!(format!("{:#?}", Entry), format!("{:#?}", KeyValue));
|
|
|
|
|
|
|
|
assert_eq!("{\"bar\": true}", format!("{:?}", Entry));
|
2015-03-06 23:16:44 -06:00
|
|
|
assert_eq!(
|
2019-12-22 16:42:04 -06:00
|
|
|
"{
|
2019-04-03 09:30:08 -05:00
|
|
|
\"bar\": true,
|
2015-03-06 23:16:44 -06:00
|
|
|
}",
|
2019-12-22 16:42:04 -06:00
|
|
|
format!("{:#?}", Entry)
|
|
|
|
);
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_multiple() {
|
2019-05-01 02:19:41 -05:00
|
|
|
struct Entry;
|
2015-03-06 23:16:44 -06:00
|
|
|
|
2019-05-01 02:19:41 -05:00
|
|
|
impl fmt::Debug for Entry {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-03-27 00:42:29 -05:00
|
|
|
fmt.debug_map()
|
2015-03-06 23:16:44 -06:00
|
|
|
.entry(&"bar", &true)
|
2016-03-10 21:06:36 -06:00
|
|
|
.entry(&10, &format_args!("{}/{}", 10, 20))
|
2015-03-06 23:16:44 -06:00
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 02:19:41 -05:00
|
|
|
struct KeyValue;
|
|
|
|
|
|
|
|
impl fmt::Debug for KeyValue {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
fmt.debug_map()
|
2019-12-22 16:42:04 -06:00
|
|
|
.key(&"bar")
|
|
|
|
.value(&true)
|
|
|
|
.key(&10)
|
|
|
|
.value(&format_args!("{}/{}", 10, 20))
|
2019-05-01 02:19:41 -05:00
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(format!("{:?}", Entry), format!("{:?}", KeyValue));
|
|
|
|
assert_eq!(format!("{:#?}", Entry), format!("{:#?}", KeyValue));
|
|
|
|
|
|
|
|
assert_eq!("{\"bar\": true, 10: 10/20}", format!("{:?}", Entry));
|
2015-03-06 23:16:44 -06:00
|
|
|
assert_eq!(
|
2019-12-22 16:42:04 -06:00
|
|
|
"{
|
2015-03-06 23:16:44 -06:00
|
|
|
\"bar\": true,
|
2019-04-03 09:30:08 -05:00
|
|
|
10: 10/20,
|
2015-03-06 23:16:44 -06:00
|
|
|
}",
|
2019-12-22 16:42:04 -06:00
|
|
|
format!("{:#?}", Entry)
|
|
|
|
);
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nested() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-03-27 00:42:29 -05:00
|
|
|
fmt.debug_map()
|
2015-03-06 23:16:44 -06:00
|
|
|
.entry(&"bar", &true)
|
2016-03-10 21:06:36 -06:00
|
|
|
.entry(&10, &format_args!("{}/{}", 10, 20))
|
2015-03-06 23:16:44 -06:00
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl fmt::Debug for Bar {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-12-22 16:42:04 -06:00
|
|
|
fmt.debug_map().entry(&"foo", &Foo).entry(&Foo, &"world").finish()
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
assert_eq!(
|
|
|
|
"{\"foo\": {\"bar\": true, 10: 10/20}, \
|
2015-03-27 00:42:29 -05:00
|
|
|
{\"bar\": true, 10: 10/20}: \"world\"}",
|
2019-12-22 16:42:04 -06:00
|
|
|
format!("{:?}", Bar)
|
|
|
|
);
|
2015-03-06 23:16:44 -06:00
|
|
|
assert_eq!(
|
2019-12-22 16:42:04 -06:00
|
|
|
"{
|
2015-03-27 00:42:29 -05:00
|
|
|
\"foo\": {
|
2015-03-06 23:16:44 -06:00
|
|
|
\"bar\": true,
|
2019-04-03 09:30:08 -05:00
|
|
|
10: 10/20,
|
2015-03-06 23:16:44 -06:00
|
|
|
},
|
2015-03-27 00:42:29 -05:00
|
|
|
{
|
2015-03-06 23:16:44 -06:00
|
|
|
\"bar\": true,
|
2019-04-03 09:30:08 -05:00
|
|
|
10: 10/20,
|
|
|
|
}: \"world\",
|
2015-03-06 23:16:44 -06:00
|
|
|
}",
|
2019-12-22 16:42:04 -06:00
|
|
|
format!("{:#?}", Bar)
|
|
|
|
);
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
2019-05-01 02:19:41 -05:00
|
|
|
|
2019-10-09 09:24:45 -05:00
|
|
|
#[test]
|
|
|
|
fn test_entry_err() {
|
|
|
|
// Ensure errors in a map entry don't trigger panics (#65231)
|
|
|
|
use std::fmt::Write;
|
|
|
|
|
|
|
|
struct ErrorFmt;
|
|
|
|
|
|
|
|
impl fmt::Debug for ErrorFmt {
|
|
|
|
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
Err(fmt::Error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct KeyValue<K, V>(usize, K, V);
|
|
|
|
|
|
|
|
impl<K, V> fmt::Debug for KeyValue<K, V>
|
|
|
|
where
|
|
|
|
K: fmt::Debug,
|
|
|
|
V: fmt::Debug,
|
|
|
|
{
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
let mut map = fmt.debug_map();
|
|
|
|
|
|
|
|
for _ in 0..self.0 {
|
|
|
|
map.entry(&self.1, &self.2);
|
|
|
|
}
|
|
|
|
|
|
|
|
map.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut buf = String::new();
|
|
|
|
|
|
|
|
assert!(write!(&mut buf, "{:?}", KeyValue(1, ErrorFmt, "bar")).is_err());
|
|
|
|
assert!(write!(&mut buf, "{:?}", KeyValue(1, "foo", ErrorFmt)).is_err());
|
|
|
|
|
|
|
|
assert!(write!(&mut buf, "{:?}", KeyValue(2, ErrorFmt, "bar")).is_err());
|
|
|
|
assert!(write!(&mut buf, "{:?}", KeyValue(2, "foo", ErrorFmt)).is_err());
|
|
|
|
}
|
|
|
|
|
2019-05-01 02:19:41 -05:00
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_invalid_key_when_entry_is_incomplete() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-12-22 16:42:04 -06:00
|
|
|
fmt.debug_map().key(&"bar").key(&"invalid").finish()
|
2019-05-01 02:19:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
format!("{:?}", Foo);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_invalid_finish_incomplete_entry() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-12-22 16:42:04 -06:00
|
|
|
fmt.debug_map().key(&"bar").finish()
|
2019-05-01 02:19:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
format!("{:?}", Foo);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_invalid_value_before_key() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-12-22 16:42:04 -06:00
|
|
|
fmt.debug_map().value(&"invalid").key(&"bar").finish()
|
2019-05-01 02:19:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
format!("{:?}", Foo);
|
|
|
|
}
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
mod debug_set {
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-03-27 00:42:29 -05:00
|
|
|
fmt.debug_set().finish()
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-27 00:42:29 -05:00
|
|
|
assert_eq!("{}", format!("{:?}", Foo));
|
|
|
|
assert_eq!("{}", format!("{:#?}", Foo));
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_single() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-12-22 16:42:04 -06:00
|
|
|
fmt.debug_set().entry(&true).finish()
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-27 00:42:29 -05:00
|
|
|
assert_eq!("{true}", format!("{:?}", Foo));
|
2015-03-06 23:16:44 -06:00
|
|
|
assert_eq!(
|
2019-12-22 16:42:04 -06:00
|
|
|
"{
|
2019-04-03 09:30:08 -05:00
|
|
|
true,
|
2015-03-06 23:16:44 -06:00
|
|
|
}",
|
2019-12-22 16:42:04 -06:00
|
|
|
format!("{:#?}", Foo)
|
|
|
|
);
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_multiple() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-12-22 16:42:04 -06:00
|
|
|
fmt.debug_set().entry(&true).entry(&format_args!("{}/{}", 10, 20)).finish()
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-27 00:42:29 -05:00
|
|
|
assert_eq!("{true, 10/20}", format!("{:?}", Foo));
|
2015-03-06 23:16:44 -06:00
|
|
|
assert_eq!(
|
2019-12-22 16:42:04 -06:00
|
|
|
"{
|
2015-03-06 23:16:44 -06:00
|
|
|
true,
|
2019-04-03 09:30:08 -05:00
|
|
|
10/20,
|
2015-03-06 23:16:44 -06:00
|
|
|
}",
|
2019-12-22 16:42:04 -06:00
|
|
|
format!("{:#?}", Foo)
|
|
|
|
);
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nested() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-12-22 16:42:04 -06:00
|
|
|
fmt.debug_set().entry(&true).entry(&format_args!("{}/{}", 10, 20)).finish()
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl fmt::Debug for Bar {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-12-22 16:42:04 -06:00
|
|
|
fmt.debug_set().entry(&Foo).entry(&"world").finish()
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
assert_eq!("{{true, 10/20}, \"world\"}", format!("{:?}", Bar));
|
2015-03-06 23:16:44 -06:00
|
|
|
assert_eq!(
|
2019-12-22 16:42:04 -06:00
|
|
|
"{
|
2015-03-27 00:42:29 -05:00
|
|
|
{
|
2015-03-06 23:16:44 -06:00
|
|
|
true,
|
2019-04-03 09:30:08 -05:00
|
|
|
10/20,
|
2015-03-06 23:16:44 -06:00
|
|
|
},
|
2019-04-03 09:30:08 -05:00
|
|
|
\"world\",
|
2015-03-06 23:16:44 -06:00
|
|
|
}",
|
2019-12-22 16:42:04 -06:00
|
|
|
format!("{:#?}", Bar)
|
|
|
|
);
|
2015-03-06 23:16:44 -06:00
|
|
|
}
|
|
|
|
}
|
2015-03-27 00:42:29 -05:00
|
|
|
|
|
|
|
mod debug_list {
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-03-27 00:42:29 -05:00
|
|
|
fmt.debug_list().finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!("[]", format!("{:?}", Foo));
|
|
|
|
assert_eq!("[]", format!("{:#?}", Foo));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_single() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-12-22 16:42:04 -06:00
|
|
|
fmt.debug_list().entry(&true).finish()
|
2015-03-27 00:42:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!("[true]", format!("{:?}", Foo));
|
|
|
|
assert_eq!(
|
2019-12-22 16:42:04 -06:00
|
|
|
"[
|
2019-04-03 09:30:08 -05:00
|
|
|
true,
|
2015-03-27 00:42:29 -05:00
|
|
|
]",
|
2019-12-22 16:42:04 -06:00
|
|
|
format!("{:#?}", Foo)
|
|
|
|
);
|
2015-03-27 00:42:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_multiple() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-12-22 16:42:04 -06:00
|
|
|
fmt.debug_list().entry(&true).entry(&format_args!("{}/{}", 10, 20)).finish()
|
2015-03-27 00:42:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!("[true, 10/20]", format!("{:?}", Foo));
|
|
|
|
assert_eq!(
|
2019-12-22 16:42:04 -06:00
|
|
|
"[
|
2015-03-27 00:42:29 -05:00
|
|
|
true,
|
2019-04-03 09:30:08 -05:00
|
|
|
10/20,
|
2015-03-27 00:42:29 -05:00
|
|
|
]",
|
2019-12-22 16:42:04 -06:00
|
|
|
format!("{:#?}", Foo)
|
|
|
|
);
|
2015-03-27 00:42:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nested() {
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-12-22 16:42:04 -06:00
|
|
|
fmt.debug_list().entry(&true).entry(&format_args!("{}/{}", 10, 20)).finish()
|
2015-03-27 00:42:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl fmt::Debug for Bar {
|
2019-04-20 11:44:29 -05:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-12-22 16:42:04 -06:00
|
|
|
fmt.debug_list().entry(&Foo).entry(&"world").finish()
|
2015-03-27 00:42:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
assert_eq!("[[true, 10/20], \"world\"]", format!("{:?}", Bar));
|
2015-03-27 00:42:29 -05:00
|
|
|
assert_eq!(
|
2019-12-22 16:42:04 -06:00
|
|
|
"[
|
2015-03-27 00:42:29 -05:00
|
|
|
[
|
|
|
|
true,
|
2019-04-03 09:30:08 -05:00
|
|
|
10/20,
|
2015-03-27 00:42:29 -05:00
|
|
|
],
|
2019-04-03 09:30:08 -05:00
|
|
|
\"world\",
|
2015-03-27 00:42:29 -05:00
|
|
|
]",
|
2019-12-22 16:42:04 -06:00
|
|
|
format!("{:#?}", Bar)
|
|
|
|
);
|
2015-03-27 00:42:29 -05:00
|
|
|
}
|
|
|
|
}
|
2017-11-24 02:56:19 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_formatting_parameters_are_forwarded() {
|
|
|
|
use std::collections::{BTreeMap, BTreeSet};
|
|
|
|
#[derive(Debug)]
|
2021-05-21 12:35:49 -05:00
|
|
|
#[allow(dead_code)]
|
2017-11-24 02:56:19 -06:00
|
|
|
struct Foo {
|
|
|
|
bar: u32,
|
|
|
|
baz: u32,
|
|
|
|
}
|
|
|
|
let struct_ = Foo { bar: 1024, baz: 7 };
|
|
|
|
let tuple = (1024, 7);
|
|
|
|
let list = [1024, 7];
|
|
|
|
let mut map = BTreeMap::new();
|
|
|
|
map.insert("bar", 1024);
|
|
|
|
map.insert("baz", 7);
|
|
|
|
let mut set = BTreeSet::new();
|
|
|
|
set.insert(1024);
|
|
|
|
set.insert(7);
|
|
|
|
|
|
|
|
assert_eq!(format!("{:03?}", struct_), "Foo { bar: 1024, baz: 007 }");
|
|
|
|
assert_eq!(format!("{:03?}", tuple), "(1024, 007)");
|
|
|
|
assert_eq!(format!("{:03?}", list), "[1024, 007]");
|
|
|
|
assert_eq!(format!("{:03?}", map), r#"{"bar": 1024, "baz": 007}"#);
|
|
|
|
assert_eq!(format!("{:03?}", set), "{007, 1024}");
|
2019-12-22 16:42:04 -06:00
|
|
|
assert_eq!(
|
|
|
|
format!("{:#03?}", struct_),
|
|
|
|
"
|
2017-11-24 02:56:19 -06:00
|
|
|
Foo {
|
|
|
|
bar: 1024,
|
2019-04-03 09:30:08 -05:00
|
|
|
baz: 007,
|
2017-11-24 02:56:19 -06:00
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
"
|
|
|
|
.trim()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
format!("{:#03?}", tuple),
|
|
|
|
"
|
2017-11-24 02:56:19 -06:00
|
|
|
(
|
|
|
|
1024,
|
2019-04-03 09:30:08 -05:00
|
|
|
007,
|
2017-11-24 02:56:19 -06:00
|
|
|
)
|
2019-12-22 16:42:04 -06:00
|
|
|
"
|
|
|
|
.trim()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
format!("{:#03?}", list),
|
|
|
|
"
|
2017-11-24 02:56:19 -06:00
|
|
|
[
|
|
|
|
1024,
|
2019-04-03 09:30:08 -05:00
|
|
|
007,
|
2017-11-24 02:56:19 -06:00
|
|
|
]
|
2019-12-22 16:42:04 -06:00
|
|
|
"
|
|
|
|
.trim()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
format!("{:#03?}", map),
|
|
|
|
r#"
|
2017-11-24 02:56:19 -06:00
|
|
|
{
|
|
|
|
"bar": 1024,
|
2019-04-03 09:30:08 -05:00
|
|
|
"baz": 007,
|
2017-11-24 02:56:19 -06:00
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
"#
|
|
|
|
.trim()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
format!("{:#03?}", set),
|
|
|
|
"
|
2017-11-24 02:56:19 -06:00
|
|
|
{
|
|
|
|
007,
|
2019-04-03 09:30:08 -05:00
|
|
|
1024,
|
2017-11-24 02:56:19 -06:00
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
"
|
|
|
|
.trim()
|
|
|
|
);
|
2017-11-24 02:56:19 -06:00
|
|
|
}
|