From 94e1b362f06cba2f2cc894cff44d0fdde4cb204a Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 11 Jul 2011 15:57:11 -0700 Subject: [PATCH] Pass command-line args to the test runner. Issue #428 This will let the test runner filter the tests it runs. --- src/comp/front/test.rs | 33 ++++++++++++++++++++++++++++----- src/lib/test.rs | 2 +- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/comp/front/test.rs b/src/comp/front/test.rs index ae047b01a77f..ca7f3b69d7a9 100644 --- a/src/comp/front/test.rs +++ b/src/comp/front/test.rs @@ -249,11 +249,20 @@ fn mk_test_desc_rec(&test_ctxt cx, ast::ident[] path) -> @ast::expr { } fn mk_main(&test_ctxt cx) -> @ast::item { - auto ret_ty = @rec(node=ast::ty_int, - span=rec(lo=0u, hi=0u)); - let ast::fn_decl decl = rec(inputs = ~[], - output = ret_ty, + let ast::mt args_mt = rec(ty = @nospan(ast::ty_str), + mut = ast::imm); + let ast::ty args_ty = nospan(ast::ty_vec(args_mt)); + + let ast::arg args_arg = rec(mode = ast::val, + ty = @args_ty, + ident = "args", + id = cx.next_node_id()); + + auto ret_ty = nospan(ast::ty_int); + + let ast::fn_decl decl = rec(inputs = ~[args_arg], + output = @ret_ty, purity = ast::impure_fn, cf = ast::return, constraints = ~[]); @@ -281,6 +290,18 @@ fn mk_main(&test_ctxt cx) -> @ast::item { fn mk_test_main_call(&test_ctxt cx) -> @ast::expr { + // Get the args passed to main so we can pass the to test_main + let ast::path args_path = nospan(rec(global = false, + idents = ~["args"], + types = ~[])); + + let ast::expr_ args_path_expr_ = ast::expr_path(args_path); + + let ast::expr args_path_expr = rec(id = cx.next_node_id(), + node = args_path_expr_, + span = rec(lo=0u, hi=0u)); + + // Call __test::test to generate the vector of test_descs let ast::path test_path = nospan(rec(global = false, idents = ~["tests"], types = ~[])); @@ -297,6 +318,7 @@ fn mk_test_main_call(&test_ctxt cx) -> @ast::expr { node = test_call_expr_, span = rec(lo=0u, hi=0u)); + // Call std::test::test_main let ast::path test_main_path = nospan(rec(global = false, idents = ~["std", "test", @@ -311,7 +333,8 @@ fn mk_test_main_call(&test_ctxt cx) -> @ast::expr { span = rec(lo=0u, hi=0u)); let ast::expr_ test_main_call_expr_ - = ast::expr_call(@test_main_path_expr, ~[@test_call_expr]); + = ast::expr_call(@test_main_path_expr, ~[@args_path_expr, + @test_call_expr]); let ast::expr test_main_call_expr = rec(id = cx.next_node_id(), node = test_main_call_expr_, diff --git a/src/lib/test.rs b/src/lib/test.rs index fbd9c27df22f..446902432e2e 100644 --- a/src/lib/test.rs +++ b/src/lib/test.rs @@ -27,7 +27,7 @@ type test_desc = rec(test_name name, // The default console test runner. It accepts the command line // arguments and a vector of test_descs (generated at compile time). -fn test_main(&test_desc[] tests) -> int { +fn test_main(&vec[str] args, &test_desc[] tests) -> int { if (run_tests(tests)) { ret 0; } else {