diff options
| author | Steven Fackler <sfackler@gmail.com> | 2015-03-06 21:16:44 -0800 |
|---|---|---|
| committer | Steven Fackler <sfackler@gmail.com> | 2015-03-09 23:24:34 -0700 |
| commit | e3656bd81baa3c2cb5065da04f9debf378f99772 (patch) | |
| tree | d10b07092f56e83dd42e97c1f97fd924fc05b6bc /src/libcoretest | |
| parent | 12b846ab80ca054d2fbfb0320d33badbd5ef0112 (diff) | |
| download | rust-e3656bd81baa3c2cb5065da04f9debf378f99772.tar.gz rust-e3656bd81baa3c2cb5065da04f9debf378f99772.zip | |
Implement RFC 640
Diffstat (limited to 'src/libcoretest')
| -rw-r--r-- | src/libcoretest/fmt/builders.rs | 391 | ||||
| -rw-r--r-- | src/libcoretest/fmt/mod.rs | 1 |
2 files changed, 392 insertions, 0 deletions
diff --git a/src/libcoretest/fmt/builders.rs b/src/libcoretest/fmt/builders.rs new file mode 100644 index 00000000000..84076b349d2 --- /dev/null +++ b/src/libcoretest/fmt/builders.rs @@ -0,0 +1,391 @@ +mod debug_struct { + use std::fmt; + + #[test] + fn test_empty() { + struct Foo; + + impl fmt::Debug for Foo { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + 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 { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("Foo") + .field("bar", &true) + .finish() + } + } + + assert_eq!("Foo { bar: true }", format!("{:?}", Foo)); + assert_eq!( +"Foo { + bar: true +}", + format!("{:#?}", Foo)); + } + + #[test] + fn test_multiple() { + 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!("{}/{}", 10i32, 20i32)) + .finish() + } + } + + assert_eq!("Foo { bar: true, baz: 10/20 }", format!("{:?}", Foo)); + assert_eq!( +"Foo { + bar: true, + baz: 10/20 +}", + format!("{:#?}", Foo)); + } + + #[test] + fn test_nested() { + 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!("{}/{}", 10i32, 20i32)) + .finish() + } + } + + 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() + } + } + + assert_eq!("Bar { foo: Foo { bar: true, baz: 10/20 }, hello: \"world\" }", + format!("{:?}", Bar)); + assert_eq!( +"Bar { + foo: Foo { + bar: true, + baz: 10/20 + }, + hello: \"world\" +}", + format!("{:#?}", Bar)); + } +} + +mod debug_tuple { + use std::fmt; + + #[test] + fn test_empty() { + struct Foo; + + impl fmt::Debug for Foo { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + 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 { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_tuple("Foo") + .field(&true) + .finish() + } + } + + assert_eq!("Foo(true)", format!("{:?}", Foo)); + assert_eq!( +"Foo( + true +)", + format!("{:#?}", Foo)); + } + + #[test] + fn test_multiple() { + struct Foo; + + impl fmt::Debug for Foo { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_tuple("Foo") + .field(&true) + .field(&format_args!("{}/{}", 10i32, 20i32)) + .finish() + } + } + + assert_eq!("Foo(true, 10/20)", format!("{:?}", Foo)); + assert_eq!( +"Foo( + true, + 10/20 +)", + format!("{:#?}", Foo)); + } + + #[test] + fn test_nested() { + struct Foo; + + impl fmt::Debug for Foo { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_tuple("Foo") + .field(&true) + .field(&format_args!("{}/{}", 10i32, 20i32)) + .finish() + } + } + + struct Bar; + + impl fmt::Debug for Bar { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + 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 + ), + \"world\" +)", + format!("{:#?}", Bar)); + } +} + +mod debug_map { + use std::fmt; + + #[test] + fn test_empty() { + struct Foo; + + impl fmt::Debug for Foo { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_map("Foo").finish() + } + } + + assert_eq!("Foo {}", format!("{:?}", Foo)); + assert_eq!("Foo {}", format!("{:#?}", Foo)); + } + + #[test] + fn test_single() { + struct Foo; + + impl fmt::Debug for Foo { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_map("Foo") + .entry(&"bar", &true) + .finish() + } + } + + assert_eq!("Foo { \"bar\": true }", format!("{:?}", Foo)); + assert_eq!( +"Foo { + \"bar\": true +}", + format!("{:#?}", Foo)); + } + + #[test] + fn test_multiple() { + struct Foo; + + impl fmt::Debug for Foo { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_map("Foo") + .entry(&"bar", &true) + .entry(&10i32, &format_args!("{}/{}", 10i32, 20i32)) + .finish() + } + } + + assert_eq!("Foo { \"bar\": true, 10: 10/20 }", format!("{:?}", Foo)); + assert_eq!( +"Foo { + \"bar\": true, + 10: 10/20 +}", + format!("{:#?}", Foo)); + } + + #[test] + fn test_nested() { + struct Foo; + + impl fmt::Debug for Foo { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_map("Foo") + .entry(&"bar", &true) + .entry(&10i32, &format_args!("{}/{}", 10i32, 20i32)) + .finish() + } + } + + struct Bar; + + impl fmt::Debug for Bar { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_map("Bar") + .entry(&"foo", &Foo) + .entry(&Foo, &"world") + .finish() + } + } + + assert_eq!("Bar { \"foo\": Foo { \"bar\": true, 10: 10/20 }, \ + Foo { \"bar\": true, 10: 10/20 }: \"world\" }", + format!("{:?}", Bar)); + assert_eq!( +"Bar { + \"foo\": Foo { + \"bar\": true, + 10: 10/20 + }, + Foo { + \"bar\": true, + 10: 10/20 + }: \"world\" +}", + format!("{:#?}", Bar)); + } +} + +mod debug_set { + use std::fmt; + + #[test] + fn test_empty() { + struct Foo; + + impl fmt::Debug for Foo { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_set("Foo").finish() + } + } + + assert_eq!("Foo {}", format!("{:?}", Foo)); + assert_eq!("Foo {}", format!("{:#?}", Foo)); + } + + #[test] + fn test_single() { + struct Foo; + + impl fmt::Debug for Foo { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_set("Foo") + .entry(&true) + .finish() + } + } + + assert_eq!("Foo { true }", format!("{:?}", Foo)); + assert_eq!( +"Foo { + true +}", + format!("{:#?}", Foo)); + } + + #[test] + fn test_multiple() { + struct Foo; + + impl fmt::Debug for Foo { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_set("Foo") + .entry(&true) + .entry(&format_args!("{}/{}", 10i32, 20i32)) + .finish() + } + } + + assert_eq!("Foo { true, 10/20 }", format!("{:?}", Foo)); + assert_eq!( +"Foo { + true, + 10/20 +}", + format!("{:#?}", Foo)); + } + + #[test] + fn test_nested() { + struct Foo; + + impl fmt::Debug for Foo { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_set("Foo") + .entry(&true) + .entry(&format_args!("{}/{}", 10i32, 20i32)) + .finish() + } + } + + struct Bar; + + impl fmt::Debug for Bar { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_set("Bar") + .entry(&Foo) + .entry(&"world") + .finish() + } + } + + assert_eq!("Bar { Foo { true, 10/20 }, \"world\" }", + format!("{:?}", Bar)); + assert_eq!( +"Bar { + Foo { + true, + 10/20 + }, + \"world\" +}", + format!("{:#?}", Bar)); + } +} diff --git a/src/libcoretest/fmt/mod.rs b/src/libcoretest/fmt/mod.rs index e7792014446..cdb9c38f027 100644 --- a/src/libcoretest/fmt/mod.rs +++ b/src/libcoretest/fmt/mod.rs @@ -9,6 +9,7 @@ // except according to those terms. mod num; +mod builders; #[test] fn test_format_flags() { |
