Skip to content

Commit 52ebcbc

Browse files
committed
---
yaml --- r: 3919 b: refs/heads/master c: b3dee95 h: refs/heads/master i: 3917: 35879a9 3915: 366e7ce 3911: 3e1bbc9 3903: 6c687cb v: v3
1 parent 4ab1d63 commit 52ebcbc

File tree

5 files changed

+121
-26
lines changed

5 files changed

+121
-26
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 49da7da441716da2ae99b893907f0fbd1655d813
2+
refs/heads/master: b3dee955144a722da50e17dde62cb36cbcccf73f

trunk/src/comp/front/test.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ export modify_for_testing;
1111

1212
type node_id_gen = @fn() -> ast::node_id;
1313

14+
type test = rec(ast::ident[] path,
15+
bool ignore);
16+
1417
type test_ctxt = @rec(node_id_gen next_node_id,
1518
mutable ast::ident[] path,
16-
mutable ast::ident[][] testfns);
19+
mutable test[] testfns);
1720

1821
// Traverse the crate, collecting all the test functions, eliding any
1922
// existing main functions, and synthesizing a main test harness
@@ -88,7 +91,9 @@ fn fold_item(&test_ctxt cx, &@ast::item i,
8891

8992
if (is_test_fn(i)) {
9093
log "this is a test function";
91-
cx.testfns += ~[cx.path];
94+
auto test = rec(path = cx.path,
95+
ignore = is_ignored(i));
96+
cx.testfns += ~[test];
9297
log #fmt("have %u test functions", ivec::len(cx.testfns));
9398
}
9499

@@ -116,6 +121,10 @@ fn is_test_fn(&@ast::item i) -> bool {
116121
ret has_test_attr && has_test_signature(i);
117122
}
118123

124+
fn is_ignored(&@ast::item i) -> bool {
125+
attr::contains_name(attr::attr_metas(i.attrs), "ignore")
126+
}
127+
119128
fn add_test_module(&test_ctxt cx, &ast::_mod m) -> ast::_mod {
120129
auto testmod = mk_test_module(cx);
121130
ret rec(items=m.items + ~[testmod] with m);
@@ -225,18 +234,20 @@ fn mk_test_desc_vec(&test_ctxt cx) -> @ast::expr {
225234
log #fmt("building test vector from %u tests",
226235
ivec::len(cx.testfns));
227236
auto descs = ~[];
228-
for (ast::ident[] testpath in cx.testfns) {
229-
log #fmt("encoding %s", ast::path_name_i(testpath));
230-
auto path = testpath;
231-
descs += ~[mk_test_desc_rec(cx, path)];
237+
for (test test in cx.testfns) {
238+
auto test_ = test; // Satisfy alias analysis
239+
descs += ~[mk_test_desc_rec(cx, test_)];
232240
}
233241

234242
ret @rec(id = cx.next_node_id(),
235243
node = ast::expr_vec(descs, ast::imm, ast::sk_unique),
236244
span = rec(lo=0u,hi=0u));
237245
}
238246

239-
fn mk_test_desc_rec(&test_ctxt cx, ast::ident[] path) -> @ast::expr {
247+
fn mk_test_desc_rec(&test_ctxt cx, test test) -> @ast::expr {
248+
auto path = test.path;
249+
250+
log #fmt("encoding %s", ast::path_name_i(path));
240251

241252
let ast::lit name_lit = nospan(ast::lit_str(ast::path_name_i(path),
242253
ast::sk_rc));
@@ -260,7 +271,19 @@ fn mk_test_desc_rec(&test_ctxt cx, ast::ident[] path) -> @ast::expr {
260271
ident = "fn",
261272
expr = @fn_expr));
262273

263-
let ast::expr_ desc_rec_ = ast::expr_rec(~[name_field, fn_field],
274+
let ast::lit ignore_lit = nospan(ast::lit_bool(test.ignore));
275+
276+
let ast::expr ignore_expr = rec(id = cx.next_node_id(),
277+
node = ast::expr_lit(@ignore_lit),
278+
span = rec(lo=0u, hi=0u));
279+
280+
let ast::field ignore_field = nospan(rec(mut = ast::imm,
281+
ident = "ignore",
282+
expr = @ignore_expr));
283+
284+
let ast::expr_ desc_rec_ = ast::expr_rec(~[name_field,
285+
fn_field,
286+
ignore_field],
264287
option::none);
265288
let ast::expr desc_rec = rec(id = cx.next_node_id(),
266289
node = desc_rec_,

trunk/src/lib/test.rs

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ export test_name;
77
export test_fn;
88
export test_desc;
99
export test_main;
10+
export test_result;
11+
export tr_ok;
12+
export tr_failed;
13+
export tr_ignored;
14+
export run_test;
1015

1116
// The name of a test. By convention this follows the rules for rust
1217
// paths, i.e it should be a series of identifiers seperated by double
@@ -23,7 +28,8 @@ type test_fn = fn();
2328
// The definition of a single test. A test runner will run a list of
2429
// these.
2530
type test_desc = rec(test_name name,
26-
test_fn fn);
31+
test_fn fn,
32+
bool ignore);
2733

2834
// The default console test runner. It accepts the command line
2935
// arguments and a vector of test_descs (generated at compile time).
@@ -43,6 +49,12 @@ fn parse_opts(&vec[str] args) -> test_opts {
4349
})
4450
}
4551

52+
tag test_result {
53+
tr_ok;
54+
tr_failed;
55+
tr_ignored;
56+
}
57+
4658
// A simple console test runner
4759
fn run_tests(&test_opts opts, &test_desc[] tests) -> bool {
4860

@@ -55,38 +67,42 @@ fn run_tests(&test_opts opts, &test_desc[] tests) -> bool {
5567

5668
auto passed = 0u;
5769
auto failed = 0u;
70+
auto ignored = 0u;
5871

5972
for (test_desc test in filtered_tests) {
6073
out.write_str(#fmt("running %s ... ", test.name));
61-
if (run_test(test)) {
62-
passed += 1u;
63-
write_ok(out);
64-
out.write_line("");
65-
} else {
66-
failed += 1u;
67-
write_failed(out);
68-
out.write_line("");
74+
alt (run_test(test)) {
75+
tr_ok {
76+
passed += 1u;
77+
write_ok(out);
78+
out.write_line("");
79+
}
80+
tr_failed {
81+
failed += 1u;
82+
write_failed(out);
83+
out.write_line("");
84+
}
85+
tr_ignored {
86+
ignored += 1u;
87+
write_ignored(out);
88+
out.write_line("");
89+
}
6990
}
7091
}
7192

72-
assert passed + failed == total;
93+
assert passed + failed + ignored == total;
7394

7495
out.write_str(#fmt("\nresult: "));
7596
if (failed == 0u) {
7697
write_ok(out);
7798
} else {
7899
write_failed(out);
79100
}
80-
out.write_str(#fmt(". %u passed; %u failed\n\n",
81-
passed, failed));
101+
out.write_str(#fmt(". %u passed; %u failed; %u ignored\n\n",
102+
passed, failed, ignored));
82103

83104
ret true;
84105

85-
fn run_test(&test_desc test) -> bool {
86-
test.fn();
87-
ret true;
88-
}
89-
90106
fn write_ok(&io::writer out) {
91107
if (term::color_supported()) {
92108
term::fg(out.get_buf_writer(), term::color_green);
@@ -106,6 +122,16 @@ fn run_tests(&test_opts opts, &test_desc[] tests) -> bool {
106122
term::reset(out.get_buf_writer());
107123
}
108124
}
125+
126+
fn write_ignored(&io::writer out) {
127+
if (term::color_supported()) {
128+
term::fg(out.get_buf_writer(), term::color_yellow);
129+
}
130+
out.write_str("ignored");
131+
if (term::color_supported()) {
132+
term::reset(out.get_buf_writer());
133+
}
134+
}
109135
}
110136

111137
fn filter_tests(&test_opts opts, &test_desc[] tests) -> test_desc[] {
@@ -128,6 +154,15 @@ fn filter_tests(&test_opts opts, &test_desc[] tests) -> test_desc[] {
128154
ret ivec::filter_map(filter, tests);
129155
}
130156

157+
fn run_test(&test_desc test) -> test_result {
158+
if (!test.ignore) {
159+
test.fn();
160+
ret tr_ok;
161+
} else {
162+
ret tr_ignored;
163+
}
164+
}
165+
131166

132167
// Local Variables:
133168
// mode: rust;

trunk/src/test/stdtest/stdtest.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std;
22

33
mod sha1;
44
mod int;
5+
mod test;
56
// Local Variables:
67
// mode: rust
78
// fill-column: 78;

trunk/src/test/stdtest/test.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import std::test;
2+
3+
#[test]
4+
fn do_not_run_ignored_tests() {
5+
auto ran = @mutable false;
6+
auto f = bind fn(@mutable bool ran) {
7+
*ran = true;
8+
} (ran);
9+
10+
auto desc = rec(name = "whatever",
11+
fn = f,
12+
ignore = true);
13+
14+
auto res = test::run_test(desc);
15+
16+
assert ran == false;
17+
}
18+
19+
#[test]
20+
fn ignored_tests_result_in_ignored() {
21+
fn f() { }
22+
auto desc = rec(name = "whatever",
23+
fn = f,
24+
ignore = true);
25+
auto res = test::run_test(desc);
26+
assert res == test::tr_ignored;
27+
}
28+
29+
// Local Variables:
30+
// mode: rust;
31+
// fill-column: 78;
32+
// indent-tabs-mode: nil
33+
// c-basic-offset: 4
34+
// buffer-file-coding-system: utf-8-unix
35+
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
36+
// End:

0 commit comments

Comments
 (0)