about summary refs log tree commit diff
path: root/src/libcoretest
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-03-12 07:30:44 +0000
committerbors <bors@rust-lang.org>2015-03-12 07:30:44 +0000
commit49f7550a25722fbdb779eef80afa85f6d802f7e8 (patch)
treeea61e689eb6a17ae4766f873f095c66b2bdfa5f5 /src/libcoretest
parent8715a65496b557798a9ff346194991aea3581f4d (diff)
parent905a611b94d1fd50f15cd06f27cd44fd4dacb131 (diff)
downloadrust-49f7550a25722fbdb779eef80afa85f6d802f7e8.tar.gz
rust-49f7550a25722fbdb779eef80afa85f6d802f7e8.zip
Auto merge of #23162 - sfackler:debug-builders, r=alexcrichton
I've made some minor changes from the implementation attached to the RFC to try to minimize codegen. The methods now take `&Debug` trait objects rather than being parameterized and there are inlined stub methods that call to non-inlined methods to do the work.

r? @alexcrichton 

cc @huonw for the `derive(Debug)` changes.
Diffstat (limited to 'src/libcoretest')
-rw-r--r--src/libcoretest/fmt/builders.rs401
-rw-r--r--src/libcoretest/fmt/mod.rs1
-rw-r--r--src/libcoretest/lib.rs1
3 files changed, 403 insertions, 0 deletions
diff --git a/src/libcoretest/fmt/builders.rs b/src/libcoretest/fmt/builders.rs
new file mode 100644
index 00000000000..b2fbc90be59
--- /dev/null
+++ b/src/libcoretest/fmt/builders.rs
@@ -0,0 +1,401 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+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() {
diff --git a/src/libcoretest/lib.rs b/src/libcoretest/lib.rs
index 1dbbb845d46..536bce0d05f 100644
--- a/src/libcoretest/lib.rs
+++ b/src/libcoretest/lib.rs
@@ -23,6 +23,7 @@
 #![feature(hash)]
 #![feature(io)]
 #![feature(collections)]
+#![feature(debug_builders)]
 #![allow(deprecated)] // rand
 
 extern crate core;