about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2016-07-13 13:26:57 -0700
committerGitHub <noreply@github.com>2016-07-13 13:26:57 -0700
commitdb71987ee1198be60fef3e361ad1ce59a70cd7f7 (patch)
tree6decc21f8f66747b91a95c21c6816fb2d9b4bac0 /src/test
parent0b7fb80e1c05bee176ea68d21e19a352a106c968 (diff)
parent51e54e57a45d7b956e3e3390db76ea2651f5d3b3 (diff)
downloadrust-db71987ee1198be60fef3e361ad1ce59a70cd7f7.tar.gz
rust-db71987ee1198be60fef3e361ad1ce59a70cd7f7.zip
Auto merge of #33642 - xen0n:ergonomic-format-macro, r=alexcrichton
Ergonomic format_args!

Fixes #9456 (at last).

Not a ground-up rewrite of the existing machinery, but more like an added intermediary layer between macro arguments and format placeholders. This is now implementing Rust RFC 1618!
Diffstat (limited to 'src/test')
-rw-r--r--src/test/compile-fail/ifmt-bad-arg.rs3
-rw-r--r--src/test/run-pass/ifmt.rs33
2 files changed, 33 insertions, 3 deletions
diff --git a/src/test/compile-fail/ifmt-bad-arg.rs b/src/test/compile-fail/ifmt-bad-arg.rs
index 1368702b160..272ad980feb 100644
--- a/src/test/compile-fail/ifmt-bad-arg.rs
+++ b/src/test/compile-fail/ifmt-bad-arg.rs
@@ -23,9 +23,6 @@ fn main() {
     format!("{foo}", 1, foo=2);        //~ ERROR: argument never used
     format!("", foo=2);                //~ ERROR: named argument never used
 
-    format!("{0:x} {0:X}", 1);         //~ ERROR: redeclared with type `X`
-    format!("{foo:x} {foo:X}", foo=1); //~ ERROR: redeclared with type `X`
-
     format!("{foo}", foo=1, foo=2);    //~ ERROR: duplicate argument
     format!("", foo=1, 2);             //~ ERROR: positional arguments cannot follow
 
diff --git a/src/test/run-pass/ifmt.rs b/src/test/run-pass/ifmt.rs
index 27cafeacc20..0a69ccf47dd 100644
--- a/src/test/run-pass/ifmt.rs
+++ b/src/test/run-pass/ifmt.rs
@@ -163,6 +163,24 @@ pub fn main() {
     t!(format!("{:?}", 0.0), "0");
 
 
+    // Ergonomic format_args!
+    t!(format!("{0:x} {0:X}", 15), "f F");
+    t!(format!("{0:x} {0:X} {}", 15), "f F 15");
+    // NOTE: For now the longer test cases must not be followed immediately by
+    // >1 empty lines, or the pretty printer will break. Since no one wants to
+    // touch the current pretty printer (#751), we have no choice but to work
+    // around it. Some of the following test cases are also affected.
+    t!(format!("{:x}{0:X}{a:x}{:X}{1:x}{a:X}", 13, 14, a=15), "dDfEeF");
+    t!(format!("{a:x} {a:X}", a=15), "f F");
+
+    // And its edge cases
+    t!(format!("{a:.0$} {b:.0$} {0:.0$}\n{a:.c$} {b:.c$} {c:.c$}",
+               4, a="abcdefg", b="hijklmn", c=3),
+               "abcd hijk 4\nabc hij 3");
+    t!(format!("{a:.*} {0} {:.*}", 4, 3, "efgh", a="abcdef"), "abcd 4 efg");
+    t!(format!("{:.a$} {a} {a:#x}", "aaaaaa", a=2), "aa 2 0x2");
+
+
     // Test that pointers don't get truncated.
     {
         let val = usize::MAX;
@@ -177,6 +195,7 @@ pub fn main() {
     test_write();
     test_print();
     test_order();
+    test_once();
 
     // make sure that format! doesn't move out of local variables
     let a: Box<_> = box 3;
@@ -260,3 +279,17 @@ fn test_order() {
                        foo(), foo(), foo(), a=foo(), b=foo(), c=foo()),
                "1 2 4 5 3 6".to_string());
 }
+
+fn test_once() {
+    // Make sure each argument are evaluted only once even though it may be
+    // formatted multiple times
+    fn foo() -> isize {
+        static mut FOO: isize = 0;
+        unsafe {
+            FOO += 1;
+            FOO
+        }
+    }
+    assert_eq!(format!("{0} {0} {0} {a} {a} {a}", foo(), a=foo()),
+               "1 1 1 2 2 2".to_string());
+}