diff options
| author | Stefan Lankes <stlankes@users.noreply.github.com> | 2019-10-20 10:48:58 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-10-20 10:48:58 +0200 |
| commit | b6801b7dcd56a272dda2fbd88ecbc5b1476d8b83 (patch) | |
| tree | c77cecd79f214f1a3a5cbdee0dd8eb934ea86c3b /src/libsyntax_ext/test.rs | |
| parent | 5ebd4d9c27bf8fee4f7d664d76c41832745dff43 (diff) | |
| parent | e66a6282275802fcb0a29ba58ddc445fc64ac8ef (diff) | |
| download | rust-b6801b7dcd56a272dda2fbd88ecbc5b1476d8b83.tar.gz rust-b6801b7dcd56a272dda2fbd88ecbc5b1476d8b83.zip | |
Merge branch 'master' into rusty-hermit
Diffstat (limited to 'src/libsyntax_ext/test.rs')
| -rw-r--r-- | src/libsyntax_ext/test.rs | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/src/libsyntax_ext/test.rs b/src/libsyntax_ext/test.rs index 5d68a92579f..b0da413d63a 100644 --- a/src/libsyntax_ext/test.rs +++ b/src/libsyntax_ext/test.rs @@ -1,9 +1,11 @@ /// The expansion from a test function to the appropriate test struct for libtest /// Ideally, this code would be in libtest but for efficiency and error messages it lives here. +use crate::util::check_builtin_macro_attribute; + use syntax::ast; -use syntax::attr::{self, check_builtin_macro_attribute}; -use syntax::ext::base::*; +use syntax::attr; +use syntax_expand::base::*; use syntax::print::pprust; use syntax::source_map::respan; use syntax::symbol::{Symbol, sym}; @@ -106,6 +108,11 @@ pub fn expand_test_or_bench( cx.path(sp, vec![test_id, cx.ident_of("ShouldPanic", sp), cx.ident_of(name, sp)]) }; + // creates test::TestType::$name + let test_type_path = |name| { + cx.path(sp, vec![test_id, cx.ident_of("TestType", sp), cx.ident_of(name, sp)]) + }; + // creates $name: $expr let field = |name, expr| cx.field_imm(sp, cx.ident_of(name, sp), expr); @@ -181,6 +188,17 @@ pub fn expand_test_or_bench( cx.expr_path(should_panic_path("YesWithMessage")), vec![cx.expr_str(sp, sym)]), }), + // test_type: ... + field("test_type", match test_type(cx) { + // test::TestType::UnitTest + TestType::UnitTest => cx.expr_path(test_type_path("UnitTest")), + // test::TestType::IntegrationTest + TestType::IntegrationTest => cx.expr_path( + test_type_path("IntegrationTest") + ), + // test::TestPath::Unknown + TestType::Unknown => cx.expr_path(test_type_path("Unknown")), + }), // }, ])), // testfn: test::StaticTestFn(...) | test::StaticBenchFn(...) @@ -261,6 +279,34 @@ fn should_panic(cx: &ExtCtxt<'_>, i: &ast::Item) -> ShouldPanic { } } +enum TestType { + UnitTest, + IntegrationTest, + Unknown, +} + +/// Attempts to determine the type of test. +/// Since doctests are created without macro expanding, only possible variants here +/// are `UnitTest`, `IntegrationTest` or `Unknown`. +fn test_type(cx: &ExtCtxt<'_>) -> TestType { + // Root path from context contains the topmost sources directory of the crate. + // I.e., for `project` with sources in `src` and tests in `tests` folders + // (no matter how many nested folders lie inside), + // there will be two different root paths: `/project/src` and `/project/tests`. + let crate_path = cx.root_path.as_path(); + + if crate_path.ends_with("src") { + // `/src` folder contains unit-tests. + TestType::UnitTest + } else if crate_path.ends_with("tests") { + // `/tests` folder contains integration tests. + TestType::IntegrationTest + } else { + // Crate layout doesn't match expected one, test type is unknown. + TestType::Unknown + } +} + fn has_test_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool { let has_should_panic_attr = attr::contains_name(&i.attrs, sym::should_panic); let ref sd = cx.parse_sess.span_diagnostic; |
