rust/src/test/ui/rfc-2361-dbg-macro/dbg-macro-expected-behavior.rs

120 lines
3.0 KiB
Rust
Raw Normal View History

2018-09-18 00:00:09 -05:00
// run-pass
2018-09-23 08:50:11 -05:00
// ignore-cloudabi no processes
// ignore-emscripten no processes
2018-09-18 00:00:09 -05:00
// Tests ensuring that `dbg!(expr)` has the expected run-time behavior.
// as well as some compile time properties we expect.
#[derive(Copy, Clone, Debug)]
struct Unit;
#[derive(Copy, Clone, Debug, PartialEq)]
struct Point<T> {
x: T,
y: T,
}
#[derive(Debug, PartialEq)]
struct NoCopy(usize);
fn test() {
let a: Unit = dbg!(Unit);
let _: Unit = dbg!(a);
// We can move `a` because it's Copy.
drop(a);
// `Point<T>` will be faithfully formatted according to `{:#?}`.
let a = Point { x: 42, y: 24 };
let b: Point<u8> = dbg!(Point { x: 42, y: 24 }); // test stringify!(..)
let c: Point<u8> = dbg!(b);
// Identity conversion:
assert_eq!(a, b);
assert_eq!(a, c);
// We can move `b` because it's Copy.
drop(b);
// Test that we can borrow and that successive applications is still identity.
let a = NoCopy(1337);
let b: &NoCopy = dbg!(dbg!(&a));
assert_eq!(&a, b);
// Test involving lifetimes of temporaries:
fn f<'a>(x: &'a u8) -> &'a u8 { x }
let a: &u8 = dbg!(f(&42));
assert_eq!(a, &42);
// Test side effects:
let mut foo = 41;
assert_eq!(7331, dbg!({
foo += 1;
eprintln!("before");
7331
}));
assert_eq!(foo, 42);
}
fn validate_stderr(stderr: Vec<String>) {
assert_eq!(stderr, &[
2018-11-30 19:54:09 -06:00
":21] Unit = Unit",
2018-09-18 00:00:09 -05:00
2018-11-30 19:54:09 -06:00
":22] a = Unit",
2018-09-18 00:00:09 -05:00
2018-11-30 19:54:09 -06:00
":28] Point{x: 42, y: 24,} = Point {",
2018-09-18 00:00:09 -05:00
" x: 42,",
" y: 24",
"}",
2018-11-30 19:54:09 -06:00
":29] b = Point {",
2018-09-18 00:00:09 -05:00
" x: 42,",
" y: 24",
"}",
2018-11-30 19:54:09 -06:00
":38] &a = NoCopy(",
2018-09-18 00:00:09 -05:00
" 1337",
")",
2018-11-30 19:54:09 -06:00
":38] dbg!(& a) = NoCopy(",
2018-09-18 00:00:09 -05:00
" 1337",
")",
2018-11-30 19:54:09 -06:00
":43] f(&42) = 42",
2018-09-18 00:00:09 -05:00
"before",
2018-11-30 19:54:09 -06:00
":48] { foo += 1; eprintln!(\"before\"); 7331 } = 7331",
2018-09-18 00:00:09 -05:00
]);
}
fn main() {
// The following is a hack to deal with compiletest's inability
// to check the output (to stdout) of run-pass tests.
use std::env;
use std::process::Command;
let mut args = env::args();
let prog = args.next().unwrap();
let child = args.next();
if let Some("child") = child.as_ref().map(|s| &**s) {
// Only run the test if we've been spawned as 'child'
test()
} else {
// This essentially spawns as 'child' to run the tests
// and then it collects output of stderr and checks the output
// against what we expect.
let out = Command::new(&prog).arg("child").output().unwrap();
assert!(out.status.success());
assert!(out.stdout.is_empty());
let stderr = String::from_utf8(out.stderr).unwrap();
let stderr = stderr.lines().map(|mut s| {
if s.starts_with("[") {
// Strip `[` and file path:
s = s.trim_start_matches("[");
assert!(s.starts_with(file!()));
s = s.trim_start_matches(file!());
}
s.to_owned()
}).collect();
validate_stderr(stderr);
}
}