Skip to content

Commit

Permalink
Clean up libsyntax/test using quasiquote.
Browse files Browse the repository at this point in the history
  • Loading branch information
goffrie committed Mar 13, 2015
1 parent 1c7bbb0 commit c4933fc
Showing 1 changed file with 16 additions and 63 deletions.
79 changes: 16 additions & 63 deletions src/libsyntax/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,26 +549,8 @@ fn path_node(ids: Vec<ast::Ident> ) -> ast::Path {
fn mk_tests(cx: &TestCtxt) -> P<ast::Item> {
// The vector of test_descs for this crate
let test_descs = mk_test_descs(cx);

// FIXME #15962: should be using quote_item, but that stringifies
// __test_reexports, causing it to be reinterned, losing the
// gensym information.
let sp = DUMMY_SP;
let ecx = &cx.ext_cx;
let struct_type = ecx.ty_path(ecx.path(sp, vec![ecx.ident_of("self"),
ecx.ident_of("test"),
ecx.ident_of("TestDescAndFn")]));
let static_lt = ecx.lifetime(sp, token::special_idents::static_lifetime.name);
// &'static [self::test::TestDescAndFn]
let static_type = ecx.ty_rptr(sp,
ecx.ty(sp, ast::TyVec(struct_type)),
Some(static_lt),
ast::MutImmutable);
// static TESTS: $static_type = &[...];
ecx.item_const(sp,
ecx.ident_of("TESTS"),
static_type,
test_descs)
quote_item!(&cx.ext_cx, const TESTS: &'static [self::test::TestDescAndFn] = $test_descs;)
.unwrap()
}

fn is_test_crate(krate: &ast::Crate) -> bool {
Expand Down Expand Up @@ -596,57 +578,31 @@ fn mk_test_descs(cx: &TestCtxt) -> P<ast::Expr> {
}

fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> P<ast::Expr> {
// FIXME #15962: should be using quote_expr, but that stringifies
// __test_reexports, causing it to be reinterned, losing the
// gensym information.

let span = ignored_span(cx, test.span);
let path = test.path.clone();
let ecx = &cx.ext_cx;
let self_id = ecx.ident_of("self");
let test_id = ecx.ident_of("test");

// creates self::test::$name
let test_path = |name| {
ecx.path(span, vec![self_id, test_id, ecx.ident_of(name)])
};
// creates $name: $expr
let field = |name, expr| ecx.field_imm(span, ecx.ident_of(name), expr);

debug!("encoding {}", ast_util::path_name_i(&path[..]));

// path to the #[test] function: "foo::bar::baz"
let path_string = ast_util::path_name_i(&path[..]);
let name_expr = ecx.expr_str(span, token::intern_and_get_ident(&path_string[..]));

// self::test::StaticTestName($name_expr)
let name_expr = ecx.expr_call(span,
ecx.expr_path(test_path("StaticTestName")),
vec![name_expr]);
let path_string = ast_util::path_name_i(&path);
debug!("encoding {}", path_string);

let ignore_expr = ecx.expr_bool(span, test.ignore);
let should_panic_path = |name| {
ecx.path(span, vec![self_id, test_id, ecx.ident_of("ShouldPanic"), ecx.ident_of(name)])
};
let fail_expr = match test.should_panic {
ShouldPanic::No => ecx.expr_path(should_panic_path("No")),
ShouldPanic::No => quote_expr!(ecx, self::test::ShouldPanic::No),
ShouldPanic::Yes(ref msg) => {
let path = should_panic_path("Yes");
let arg = match *msg {
Some(ref msg) => ecx.expr_some(span, ecx.expr_str(span, msg.clone())),
None => ecx.expr_none(span),
};
ecx.expr_call(span, ecx.expr_path(path), vec![arg])
quote_expr!(ecx, self::test::ShouldPanic::Yes($arg))
}
};

// self::test::TestDesc { ... }
let desc_expr = ecx.expr_struct(
span,
test_path("TestDesc"),
vec![field("name", name_expr),
field("ignore", ignore_expr),
field("should_panic", fail_expr)]);
let desc_expr = quote_expr!(ecx, self::test::TestDesc {
name: self::test::StaticTestName($path_string),
ignore: $ignore_expr,
should_panic: $fail_expr,
});


let mut visible_path = match cx.toplevel_reexport {
Expand All @@ -660,13 +616,10 @@ fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> P<ast::Expr> {

let fn_expr = ecx.expr_path(ecx.path_global(span, visible_path));

let variant_name = if test.bench { "StaticBenchFn" } else { "StaticTestFn" };
// self::test::$variant_name($fn_expr)
let testfn_expr = ecx.expr_call(span, ecx.expr_path(test_path(variant_name)), vec![fn_expr]);
let variant_name = ecx.ident_of(if test.bench { "StaticBenchFn" } else { "StaticTestFn" });

// self::test::TestDescAndFn { ... }
ecx.expr_struct(span,
test_path("TestDescAndFn"),
vec![field("desc", desc_expr),
field("testfn", testfn_expr)])
quote_expr!(ecx, self::test::TestDescAndFn {
desc: $desc_expr,
testfn: self::test::$variant_name($fn_expr),
})
}

0 comments on commit c4933fc

Please sign in to comment.