Skip to content

dump!: debugging macro #12426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/libstd/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => (
Expand Down
45 changes: 36 additions & 9 deletions src/test/run-pass/ifmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -238,6 +239,7 @@ pub fn main() {

test_write();
test_print();
test_dump();

// make sure that format! doesn't move out of local variables
let a = ~3;
Expand Down Expand Up @@ -275,18 +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)
}

fn test_dump() {
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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test to make sure the channel is closed here too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The channel c has been moved, correct? So, would I instead verify r.read() returns EOF?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose you would, yes. I didn't realize PortReader didn't have any way to unwrap the Port, like BufferedReader does.

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();
{
Expand Down