From 167830077bafed272f0276cd76cc5451428f5047 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Thu, 20 Feb 2014 10:22:15 -0800 Subject: [PATCH 1/2] dump!: debugging macro dump! is a shorter way of quickly outputing some values to debug programs. Example: dump!(val_a, 2 + 2); => "val_a = true, 2 + 2 = 4" fixes #12015 --- src/libstd/macros.rs | 17 +++++++++++++++++ src/test/run-pass/ifmt.rs | 13 +++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index 14ae7c9900c46..314661ab5e378 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -169,6 +169,23 @@ macro_rules! println( ($($arg:tt)*) => (format_args!(::std::io::stdio::println_args, $($arg)*)) ) +#[macro_export] +macro_rules! dump( + ($a:expr) => ( + println!(concat!(stringify!($a), " = {:?}"), $a); + ); + ($a:expr, $($b:expr),+) => ( + println!( + concat!( + stringify!($a), " = {:?}", + $(", ", stringify!($b), " = {:?}"),+ + ), + $a, + $($b),+ + ); + ); +) + #[macro_export] macro_rules! local_data_key( ($name:ident: $ty:ty) => ( diff --git a/src/test/run-pass/ifmt.rs b/src/test/run-pass/ifmt.rs index 09188b77ad8ae..2888e15644dba 100644 --- a/src/test/run-pass/ifmt.rs +++ b/src/test/run-pass/ifmt.rs @@ -238,6 +238,7 @@ pub fn main() { test_write(); test_print(); + test_dump(); // make sure that format! doesn't move out of local variables let a = ~3; @@ -285,6 +286,18 @@ fn test_print() { println!("{foo}", foo="bar"); } +// Just make sure that the macros are defined, there's not really a lot that we +// can do with them just yet (to test the output) +fn test_dump() { + dump!("hello"); + dump!(2u8, 2 + 2); + let val_a = ~[0u8]; + let val_b = Some(3); + let val_c = true; + dump!(val_a, val_b, val_c); + dump!(bytes!("bye")); +} + // Just make sure that the macros are defined, there's not really a lot that we // can do with them just yet (to test the output) fn test_format_args() { From 2097352f2ee501707f813f1929aefa709c75a63d Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Thu, 20 Feb 2014 12:36:59 -0800 Subject: [PATCH 2/2] format: add formatting tests for print! and dump! --- src/test/run-pass/ifmt.rs | 50 +++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/src/test/run-pass/ifmt.rs b/src/test/run-pass/ifmt.rs index 2888e15644dba..a2cf84db511fc 100644 --- a/src/test/run-pass/ifmt.rs +++ b/src/test/run-pass/ifmt.rs @@ -17,6 +17,7 @@ use std::fmt; use std::io::MemWriter; use std::io; +use std::io::{stdio, BufferedReader, PortReader, ChanWriter}; use std::str; struct A; @@ -276,30 +277,43 @@ fn test_write() { t!(s, "34helloline\nbar\n"); } -// Just make sure that the macros are defined, there's not really a lot that we -// can do with them just yet (to test the output) fn test_print() { - print!("hi"); - print!("{:?}", ~[0u8]); - println!("hello"); - println!("this is a {}", "test"); - println!("{foo}", foo="bar"); + let (p, c) = Chan::new(); + let (mut r, w) = (BufferedReader::new(PortReader::new(p)), ChanWriter::new(c)); + spawn(proc() { + stdio::set_stdout(~w as ~Writer); + print!("hi"); + print!("{:?}", ~[0u8]); + println!("hello"); + println!("this is a {}", "test"); + println!("{foo}", foo="bar"); + }); + t!(r.read_line().unwrap(), "hi~[0u8]hello\n"); + t!(r.read_line().unwrap(), "this is a test\n"); + t!(r.read_line().unwrap(), "bar\n"); + assert_eq!(r.read_byte().unwrap_err().kind, io::EndOfFile) } -// Just make sure that the macros are defined, there's not really a lot that we -// can do with them just yet (to test the output) fn test_dump() { - dump!("hello"); - dump!(2u8, 2 + 2); - let val_a = ~[0u8]; - let val_b = Some(3); - let val_c = true; - dump!(val_a, val_b, val_c); - dump!(bytes!("bye")); + let (p, c) = Chan::new(); + let (mut r, w) = (BufferedReader::new(PortReader::new(p)), ChanWriter::new(c)); + spawn(proc() { + stdio::set_stdout(~w as ~Writer); + dump!("hello"); + dump!(2u8, 2 + 2); + let val_a = ~[0u8]; + let val_b = Some(3); + let val_c = true; + dump!(val_a, val_b, val_c); + dump!(bytes!("bye")); + }); + t!(r.read_line().unwrap(), "\"hello\" = \"hello\"\n"); + t!(r.read_line().unwrap(), "2u8 = 2u8, 2 + 2 = 4\n"); + t!(r.read_line().unwrap(), "val_a = ~[0u8], val_b = Some(3), val_c = true\n"); + t!(r.read_line().unwrap(), "bytes!(\"bye\") = &[98u8, 121u8, 101u8]\n"); + assert_eq!(r.read_byte().unwrap_err().kind, io::EndOfFile) } -// Just make sure that the macros are defined, there's not really a lot that we -// can do with them just yet (to test the output) fn test_format_args() { let mut buf = MemWriter::new(); {