rust-playground icon indicating copy to clipboard operation
rust-playground copied to clipboard

Expand macros doesn’t work for #[test] code

Open lilyball opened this issue 4 years ago • 4 comments

If I have a playground that uses #[test] I can run it with cargo test, but the “Expand macros” feature doesn’t support tests. I’m pretty sure passing the --test flag to rustc when asking it to print the expanded code will work.

lilyball avatar Mar 19 '21 00:03 lilyball

/cc @Flowneee

shepmaster avatar Mar 19 '21 00:03 shepmaster

Looks like simply add --test to rustc force it to compile not original code, but testing binary (with Rust testing framework, can I call it that?), so it produces unexpected result (below). I'm not sure that can be disabled, but I'll try to look into this next week. For now I suggest you write testing code in normal functions and call them from #[test]-annotated functions.

Command: cargo +nightly rustc -- -Zunstable-options --pretty=expanded --test

main.rs:

#[test]
fn test() {
    println!("{}", 9);
}

fn main() {}
#![feature(prelude_import)]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std;
extern crate test;
#[cfg(test)]
#[rustc_test_marker]
pub const test: test::TestDescAndFn =
    test::TestDescAndFn{desc:
                            test::TestDesc{name: test::StaticTestName("test"),
                                           ignore: false,
                                           allow_fail: false,
                                           should_panic:
                                               test::ShouldPanic::No,
                                           test_type:
                                               test::TestType::UnitTest,},
                        testfn:
                            test::StaticTestFn(||
                                                   test::assert_test_result(test())),};
fn test() {

    {
        ::std::io::_print(::core::fmt::Arguments::new_v1(&["", "\n"],
                                                         &match (&9,) {
                                                              (arg0,) =>
                                                              [::core::fmt::ArgumentV1::new(arg0,
                                                                                            ::core::fmt::Display::fmt)],
                                                          }));
    };
}
#[allow(dead_code)]
fn main() { }
#[main]
pub fn main() -> () {
    extern crate test;
    test::test_main_static(&[&test])
}

Flowneee avatar Mar 19 '21 19:03 Flowneee

What you pasted looks exactly like what I wanted. Yes, it includes the test harness stuff, but that's as it should be.

lilyball avatar Mar 20 '21 08:03 lilyball

But it will always add test stuff when expanding, which is not what everyone want. Maybe we could add ability to pass flags to rustc or add expand with tests checkbox (which will pass --test), but I don't see right now how that could be integrated in interface. Or we might add Expand macro (testing) tool, but it also seems ugly. If we can come up with interface, rest will be easy.

Flowneee avatar Mar 20 '21 12:03 Flowneee