about summary refs log tree commit diff
diff options
context:
space:
mode:
authorWang Xuerui <idontknw.wang@gmail.com>2016-05-21 23:08:30 +0800
committerWang Xuerui <idontknw.wang@gmail.com>2016-07-14 03:10:46 +0800
commit03563b12e8705f7d8fedf6e1bc48f2bcf22c20bf (patch)
treeefa8d6be077b0f3a5586dca196b7ff3280dd599d
parent718099435bfc1b76650f3cbdc78d334a16d99176 (diff)
downloadrust-03563b12e8705f7d8fedf6e1bc48f2bcf22c20bf.tar.gz
rust-03563b12e8705f7d8fedf6e1bc48f2bcf22c20bf.zip
format: add tests for ergonomic format_args!
format: workaround pretty-printer to pass tests
-rw-r--r--src/test/run-pass/ifmt.rs33
1 files changed, 33 insertions, 0 deletions
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());
+}