Skip to content

Commit 03563b1

Browse files
committed
format: add tests for ergonomic format_args!
format: workaround pretty-printer to pass tests
1 parent 7180994 commit 03563b1

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/test/run-pass/ifmt.rs

+33
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,24 @@ pub fn main() {
163163
t!(format!("{:?}", 0.0), "0");
164164

165165

166+
// Ergonomic format_args!
167+
t!(format!("{0:x} {0:X}", 15), "f F");
168+
t!(format!("{0:x} {0:X} {}", 15), "f F 15");
169+
// NOTE: For now the longer test cases must not be followed immediately by
170+
// >1 empty lines, or the pretty printer will break. Since no one wants to
171+
// touch the current pretty printer (#751), we have no choice but to work
172+
// around it. Some of the following test cases are also affected.
173+
t!(format!("{:x}{0:X}{a:x}{:X}{1:x}{a:X}", 13, 14, a=15), "dDfEeF");
174+
t!(format!("{a:x} {a:X}", a=15), "f F");
175+
176+
// And its edge cases
177+
t!(format!("{a:.0$} {b:.0$} {0:.0$}\n{a:.c$} {b:.c$} {c:.c$}",
178+
4, a="abcdefg", b="hijklmn", c=3),
179+
"abcd hijk 4\nabc hij 3");
180+
t!(format!("{a:.*} {0} {:.*}", 4, 3, "efgh", a="abcdef"), "abcd 4 efg");
181+
t!(format!("{:.a$} {a} {a:#x}", "aaaaaa", a=2), "aa 2 0x2");
182+
183+
166184
// Test that pointers don't get truncated.
167185
{
168186
let val = usize::MAX;
@@ -177,6 +195,7 @@ pub fn main() {
177195
test_write();
178196
test_print();
179197
test_order();
198+
test_once();
180199

181200
// make sure that format! doesn't move out of local variables
182201
let a: Box<_> = box 3;
@@ -260,3 +279,17 @@ fn test_order() {
260279
foo(), foo(), foo(), a=foo(), b=foo(), c=foo()),
261280
"1 2 4 5 3 6".to_string());
262281
}
282+
283+
fn test_once() {
284+
// Make sure each argument are evaluted only once even though it may be
285+
// formatted multiple times
286+
fn foo() -> isize {
287+
static mut FOO: isize = 0;
288+
unsafe {
289+
FOO += 1;
290+
FOO
291+
}
292+
}
293+
assert_eq!(format!("{0} {0} {0} {a} {a} {a}", foo(), a=foo()),
294+
"1 1 1 2 2 2".to_string());
295+
}

0 commit comments

Comments
 (0)