rust/src/libcore/tests/fmt/builders.rs

544 lines
12 KiB
Rust
Raw Normal View History

2015-03-07 05:16:44 +00:00
mod debug_struct {
use std::fmt;
#[test]
fn test_empty() {
struct Foo;
impl fmt::Debug for Foo {
2019-04-20 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2015-03-07 05:16:44 +00: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 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2015-03-07 05:16:44 +00:00
fmt.debug_struct("Foo")
.field("bar", &true)
.finish()
}
}
assert_eq!("Foo { bar: true }", format!("{:?}", Foo));
assert_eq!(
"Foo {
bar: true,
2015-03-07 05:16:44 +00:00
}",
format!("{:#?}", Foo));
}
#[test]
fn test_multiple() {
struct Foo;
impl fmt::Debug for Foo {
2019-04-20 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2015-03-07 05:16:44 +00:00
fmt.debug_struct("Foo")
.field("bar", &true)
2016-03-11 03:06:36 +00:00
.field("baz", &format_args!("{}/{}", 10, 20))
2015-03-07 05:16:44 +00:00
.finish()
}
}
assert_eq!("Foo { bar: true, baz: 10/20 }", format!("{:?}", Foo));
assert_eq!(
"Foo {
bar: true,
baz: 10/20,
2015-03-07 05:16:44 +00:00
}",
format!("{:#?}", Foo));
}
#[test]
fn test_nested() {
struct Foo;
impl fmt::Debug for Foo {
2019-04-20 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2015-03-07 05:16:44 +00:00
fmt.debug_struct("Foo")
.field("bar", &true)
2016-03-11 03:06:36 +00:00
.field("baz", &format_args!("{}/{}", 10, 20))
2015-03-07 05:16:44 +00:00
.finish()
}
}
struct Bar;
impl fmt::Debug for Bar {
2019-04-20 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2015-03-07 05:16:44 +00:00
fmt.debug_struct("Bar")
.field("foo", &Foo)
.field("hello", &"world")
.finish()
}
}
assert_eq!("Bar { foo: Foo { bar: true, baz: 10/20 }, hello: \"world\" }",
format!("{:?}", Bar));
assert_eq!(
"Bar {
foo: Foo {
bar: true,
baz: 10/20,
2015-03-07 05:16:44 +00:00
},
hello: \"world\",
2015-03-07 05:16:44 +00:00
}",
format!("{:#?}", Bar));
}
}
mod debug_tuple {
use std::fmt;
#[test]
fn test_empty() {
struct Foo;
impl fmt::Debug for Foo {
2019-04-20 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2015-03-07 05:16:44 +00: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 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2015-03-07 05:16:44 +00:00
fmt.debug_tuple("Foo")
.field(&true)
.finish()
}
}
assert_eq!("Foo(true)", format!("{:?}", Foo));
assert_eq!(
"Foo(
true,
2015-03-07 05:16:44 +00:00
)",
format!("{:#?}", Foo));
}
#[test]
fn test_multiple() {
struct Foo;
impl fmt::Debug for Foo {
2019-04-20 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2015-03-07 05:16:44 +00:00
fmt.debug_tuple("Foo")
.field(&true)
2016-03-11 03:06:36 +00:00
.field(&format_args!("{}/{}", 10, 20))
2015-03-07 05:16:44 +00:00
.finish()
}
}
assert_eq!("Foo(true, 10/20)", format!("{:?}", Foo));
assert_eq!(
"Foo(
true,
10/20,
2015-03-07 05:16:44 +00:00
)",
format!("{:#?}", Foo));
}
#[test]
fn test_nested() {
struct Foo;
impl fmt::Debug for Foo {
2019-04-20 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2015-03-07 05:16:44 +00:00
fmt.debug_tuple("Foo")
.field(&true)
2016-03-11 03:06:36 +00:00
.field(&format_args!("{}/{}", 10, 20))
2015-03-07 05:16:44 +00:00
.finish()
}
}
struct Bar;
impl fmt::Debug for Bar {
2019-04-20 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2015-03-07 05:16:44 +00:00
fmt.debug_tuple("Bar")
.field(&Foo)
.field(&"world")
.finish()
}
}
assert_eq!("Bar(Foo(true, 10/20), \"world\")",
format!("{:?}", Bar));
assert_eq!(
"Bar(
Foo(
true,
10/20,
2015-03-07 05:16:44 +00:00
),
\"world\",
2015-03-07 05:16:44 +00:00
)",
format!("{:#?}", Bar));
}
}
mod debug_map {
use std::fmt;
#[test]
fn test_empty() {
struct Foo;
impl fmt::Debug for Foo {
2019-04-20 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_map().finish()
2015-03-07 05:16:44 +00:00
}
}
assert_eq!("{}", format!("{:?}", Foo));
assert_eq!("{}", format!("{:#?}", Foo));
2015-03-07 05:16:44 +00:00
}
#[test]
fn test_single() {
struct Foo;
impl fmt::Debug for Foo {
2019-04-20 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_map()
2015-03-07 05:16:44 +00:00
.entry(&"bar", &true)
.finish()
}
}
assert_eq!("{\"bar\": true}", format!("{:?}", Foo));
2015-03-07 05:16:44 +00:00
assert_eq!(
"{
\"bar\": true,
2015-03-07 05:16:44 +00:00
}",
format!("{:#?}", Foo));
}
#[test]
fn test_multiple() {
struct Foo;
impl fmt::Debug for Foo {
2019-04-20 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_map()
2015-03-07 05:16:44 +00:00
.entry(&"bar", &true)
2016-03-11 03:06:36 +00:00
.entry(&10, &format_args!("{}/{}", 10, 20))
2015-03-07 05:16:44 +00:00
.finish()
}
}
assert_eq!("{\"bar\": true, 10: 10/20}", format!("{:?}", Foo));
2015-03-07 05:16:44 +00:00
assert_eq!(
"{
2015-03-07 05:16:44 +00:00
\"bar\": true,
10: 10/20,
2015-03-07 05:16:44 +00:00
}",
format!("{:#?}", Foo));
}
#[test]
fn test_nested() {
struct Foo;
impl fmt::Debug for Foo {
2019-04-20 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_map()
2015-03-07 05:16:44 +00:00
.entry(&"bar", &true)
2016-03-11 03:06:36 +00:00
.entry(&10, &format_args!("{}/{}", 10, 20))
2015-03-07 05:16:44 +00:00
.finish()
}
}
struct Bar;
impl fmt::Debug for Bar {
2019-04-20 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_map()
2015-03-07 05:16:44 +00:00
.entry(&"foo", &Foo)
.entry(&Foo, &"world")
.finish()
}
}
assert_eq!("{\"foo\": {\"bar\": true, 10: 10/20}, \
{\"bar\": true, 10: 10/20}: \"world\"}",
2015-03-07 05:16:44 +00:00
format!("{:?}", Bar));
assert_eq!(
"{
\"foo\": {
2015-03-07 05:16:44 +00:00
\"bar\": true,
10: 10/20,
2015-03-07 05:16:44 +00:00
},
{
2015-03-07 05:16:44 +00:00
\"bar\": true,
10: 10/20,
}: \"world\",
2015-03-07 05:16:44 +00:00
}",
format!("{:#?}", Bar));
}
}
mod debug_set {
use std::fmt;
#[test]
fn test_empty() {
struct Foo;
impl fmt::Debug for Foo {
2019-04-20 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_set().finish()
2015-03-07 05:16:44 +00:00
}
}
assert_eq!("{}", format!("{:?}", Foo));
assert_eq!("{}", format!("{:#?}", Foo));
2015-03-07 05:16:44 +00:00
}
#[test]
fn test_single() {
struct Foo;
impl fmt::Debug for Foo {
2019-04-20 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_set()
2015-03-07 05:16:44 +00:00
.entry(&true)
.finish()
}
}
assert_eq!("{true}", format!("{:?}", Foo));
2015-03-07 05:16:44 +00:00
assert_eq!(
"{
true,
2015-03-07 05:16:44 +00:00
}",
format!("{:#?}", Foo));
}
#[test]
fn test_multiple() {
struct Foo;
impl fmt::Debug for Foo {
2019-04-20 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_set()
2015-03-07 05:16:44 +00:00
.entry(&true)
2016-03-11 03:06:36 +00:00
.entry(&format_args!("{}/{}", 10, 20))
2015-03-07 05:16:44 +00:00
.finish()
}
}
assert_eq!("{true, 10/20}", format!("{:?}", Foo));
2015-03-07 05:16:44 +00:00
assert_eq!(
"{
2015-03-07 05:16:44 +00:00
true,
10/20,
2015-03-07 05:16:44 +00:00
}",
format!("{:#?}", Foo));
}
#[test]
fn test_nested() {
struct Foo;
impl fmt::Debug for Foo {
2019-04-20 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_set()
2015-03-07 05:16:44 +00:00
.entry(&true)
2016-03-11 03:06:36 +00:00
.entry(&format_args!("{}/{}", 10, 20))
2015-03-07 05:16:44 +00:00
.finish()
}
}
struct Bar;
impl fmt::Debug for Bar {
2019-04-20 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_set()
2015-03-07 05:16:44 +00:00
.entry(&Foo)
.entry(&"world")
.finish()
}
}
assert_eq!("{{true, 10/20}, \"world\"}",
2015-03-07 05:16:44 +00:00
format!("{:?}", Bar));
assert_eq!(
"{
{
2015-03-07 05:16:44 +00:00
true,
10/20,
2015-03-07 05:16:44 +00:00
},
\"world\",
2015-03-07 05:16:44 +00:00
}",
format!("{:#?}", Bar));
}
}
mod debug_list {
use std::fmt;
#[test]
fn test_empty() {
struct Foo;
impl fmt::Debug for Foo {
2019-04-20 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
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 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_list()
.entry(&true)
.finish()
}
}
assert_eq!("[true]", format!("{:?}", Foo));
assert_eq!(
"[
true,
]",
format!("{:#?}", Foo));
}
#[test]
fn test_multiple() {
struct Foo;
impl fmt::Debug for Foo {
2019-04-20 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_list()
.entry(&true)
2016-03-11 03:06:36 +00:00
.entry(&format_args!("{}/{}", 10, 20))
.finish()
}
}
assert_eq!("[true, 10/20]", format!("{:?}", Foo));
assert_eq!(
"[
true,
10/20,
]",
format!("{:#?}", Foo));
}
#[test]
fn test_nested() {
struct Foo;
impl fmt::Debug for Foo {
2019-04-20 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_list()
.entry(&true)
2016-03-11 03:06:36 +00:00
.entry(&format_args!("{}/{}", 10, 20))
.finish()
}
}
struct Bar;
impl fmt::Debug for Bar {
2019-04-20 16:44:29 +00:00
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_list()
.entry(&Foo)
.entry(&"world")
.finish()
}
}
assert_eq!("[[true, 10/20], \"world\"]",
format!("{:?}", Bar));
assert_eq!(
"[
[
true,
10/20,
],
\"world\",
]",
format!("{:#?}", Bar));
}
}
#[test]
fn test_formatting_parameters_are_forwarded() {
use std::collections::{BTreeMap, BTreeSet};
#[derive(Debug)]
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}");
assert_eq!(format!("{:#03?}", struct_), "
Foo {
bar: 1024,
baz: 007,
}
".trim());
assert_eq!(format!("{:#03?}", tuple), "
(
1024,
007,
)
".trim());
assert_eq!(format!("{:#03?}", list), "
[
1024,
007,
]
".trim());
assert_eq!(format!("{:#03?}", map), r#"
{
"bar": 1024,
"baz": 007,
}
"#.trim());
assert_eq!(format!("{:#03?}", set), "
{
007,
1024,
}
".trim());
}