Skip to content

Commit 8f85b3a

Browse files
committed
---
yaml --- r: 6082 b: refs/heads/master c: 76077a9 h: refs/heads/master v: v3
1 parent 9e3611e commit 8f85b3a

File tree

8 files changed

+127
-24
lines changed

8 files changed

+127
-24
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: eabc9f229574a906437dd63359caec1c15fb75ad
2+
refs/heads/master: 76077a9fb7f67a8af1b2eb16c2814ca703ad6c97

trunk/src/comp/front/test.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export modify_for_testing;
1414

1515
type node_id_gen = fn@() -> ast::node_id;
1616

17-
type test = {span: span, path: [ast::ident], ignore: bool};
17+
type test = {span: span, path: [ast::ident], ignore: bool, should_fail: bool};
1818

1919
type test_ctxt =
2020
@{sess: session::session,
@@ -105,7 +105,8 @@ fn fold_item(cx: test_ctxt, &&i: @ast::item, fld: fold::ast_fold) ->
105105
_ {
106106
log "this is a test function";
107107
let test = {span: i.span,
108-
path: cx.path, ignore: is_ignored(cx, i)};
108+
path: cx.path, ignore: is_ignored(cx, i),
109+
should_fail: should_fail(i)};
109110
cx.testfns += [test];
110111
log #fmt["have %u test functions", vec::len(cx.testfns)];
111112
}
@@ -148,6 +149,10 @@ fn is_ignored(cx: test_ctxt, i: @ast::item) -> bool {
148149
}
149150
}
150151

152+
fn should_fail(i: @ast::item) -> bool {
153+
vec::len(attr::find_attrs_by_name(i.attrs, "should_fail")) > 0u
154+
}
155+
151156
fn add_test_module(cx: test_ctxt, m: ast::_mod) -> ast::_mod {
152157
let testmod = mk_test_module(cx);
153158
ret {items: m.items + [testmod] with m};
@@ -299,8 +304,19 @@ fn mk_test_desc_rec(cx: test_ctxt, test: test) -> @ast::expr {
299304
let ignore_field: ast::field =
300305
nospan({mut: ast::imm, ident: "ignore", expr: @ignore_expr});
301306

307+
let fail_lit: ast::lit = nospan(ast::lit_bool(test.should_fail));
308+
309+
let fail_expr: ast::expr =
310+
{id: cx.next_node_id(),
311+
node: ast::expr_lit(@fail_lit),
312+
span: span};
313+
314+
let fail_field: ast::field =
315+
nospan({mut: ast::imm, ident: "should_fail", expr: @fail_expr});
316+
302317
let desc_rec_: ast::expr_ =
303-
ast::expr_rec([name_field, fn_field, ignore_field], option::none);
318+
ast::expr_rec([name_field, fn_field, ignore_field, fail_field],
319+
option::none);
304320
let desc_rec: ast::expr =
305321
{id: cx.next_node_id(), node: desc_rec_, span: span};
306322
ret @desc_rec;

trunk/src/compiletest/compiletest.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ fn make_test(cx: cx, testfile: str, configport: port<[u8]>) ->
170170
test::test_desc<fn@()> {
171171
{name: make_test_name(cx.config, testfile),
172172
fn: make_test_closure(testfile, chan(configport)),
173-
ignore: header::is_test_ignored(cx.config, testfile)}
173+
ignore: header::is_test_ignored(cx.config, testfile),
174+
should_fail: false}
174175
}
175176

176177
fn make_test_name(config: config, testfile: str) -> str {

trunk/src/lib/test.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ type default_test_fn = test_fn<fn()>;
5050
type test_desc<T> = {
5151
name: test_name,
5252
fn: test_fn<T>,
53-
ignore: bool
53+
ignore: bool,
54+
should_fail: bool
5455
};
5556

5657
// The default console test runner. It accepts the command line
@@ -218,7 +219,6 @@ fn run_tests<T>(opts: test_opts, tests: [test_desc<T>],
218219
callback: fn@(testevent<T>)) {
219220

220221
let filtered_tests = filter_tests(opts, tests);
221-
222222
callback(te_filtered(filtered_tests));
223223

224224
// It's tempting to just spawn all the tests at once but that doesn't
@@ -282,7 +282,8 @@ fn filter_tests<T>(opts: test_opts,
282282
if test.ignore {
283283
ret option::some({name: test.name,
284284
fn: test.fn,
285-
ignore: false});
285+
ignore: false,
286+
should_fail: test.should_fail});
286287
} else { ret option::none; }
287288
};
288289

@@ -305,17 +306,25 @@ type test_future<T> = {test: test_desc<T>, wait: fn@() -> test_result};
305306

306307
fn run_test<T>(test: test_desc<T>,
307308
to_task: test_to_task<T>) -> test_future<T> {
308-
if !test.ignore {
309-
let test_task = to_task(test.fn);
310-
ret {test: test,
311-
wait:
312-
bind fn (test_task: joinable) -> test_result {
313-
alt task::join(test_task) {
314-
task::tr_success. { tr_ok }
315-
task::tr_failure. { tr_failed }
316-
}
317-
}(test_task)};
318-
} else { ret {test: test, wait: fn () -> test_result { tr_ignored }}; }
309+
if test.ignore {
310+
ret {test: test, wait: fn () -> test_result { tr_ignored }};
311+
}
312+
313+
let test_task = to_task(test.fn);
314+
ret {test: test,
315+
wait:
316+
bind fn (test_task: joinable, should_fail: bool) -> test_result {
317+
alt task::join(test_task) {
318+
task::tr_success. {
319+
if should_fail { tr_failed }
320+
else { tr_ok }
321+
}
322+
task::tr_failure. {
323+
if should_fail { tr_ok }
324+
else { tr_failed }
325+
}
326+
}
327+
}(test_task, test.should_fail)};
319328
}
320329

321330
// We need to run our tests in another task in order to trap test failures.

trunk/src/test/stdtest/char.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,15 @@ fn test_to_digit() {
2626
assert (char::to_digit('z') == 35u8);
2727
assert (char::to_digit('Z') == 35u8);
2828
}
29+
30+
#[test]
31+
#[should_fail]
32+
fn test_to_digit_fail_1() {
33+
char::to_digit(' ');
34+
}
35+
36+
#[test]
37+
#[should_fail]
38+
fn test_to_digit_fail_2() {
39+
char::to_digit('$');
40+
}

trunk/src/test/stdtest/int.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ fn test_from_str() {
1919
assert(int::from_str("-00100") == -100);
2020
}
2121

22+
#[test]
23+
#[should_fail]
24+
fn test_from_str_fail_1() {
25+
int::from_str(" ");
26+
}
27+
28+
#[test]
29+
#[should_fail]
30+
fn test_from_str_fail_2() {
31+
int::from_str("x");
32+
}
33+
2234
#[test]
2335
fn test_parse_buf() {
2436
assert (int::parse_buf(bytes("123"), 10u) == 123);
@@ -40,6 +52,18 @@ fn test_parse_buf() {
4052
assert (int::parse_buf(bytes("-Z"), 36u) == -35);
4153
}
4254

55+
#[test]
56+
#[should_fail]
57+
fn test_parse_buf_fail_1() {
58+
int::parse_buf(bytes("Z"), 35u);
59+
}
60+
61+
#[test]
62+
#[should_fail]
63+
fn test_parse_buf_fail_2() {
64+
int::parse_buf(bytes("-9"), 2u);
65+
}
66+
4367
#[test]
4468
fn test_to_str() {
4569
assert (eq(int::to_str(0, 10u), "0"));

trunk/src/test/stdtest/test.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import std::vec;
77
#[test]
88
fn do_not_run_ignored_tests() {
99
fn f() { fail; }
10-
let desc = {name: "whatever", fn: f, ignore: true};
10+
let desc = {name: "whatever", fn: f, ignore: true, should_fail: false};
1111
let future = test::run_test(desc, test::default_test_to_task);
1212
let result = future.wait();
1313
assert result != test::tr_ok;
@@ -16,11 +16,27 @@ fn do_not_run_ignored_tests() {
1616
#[test]
1717
fn ignored_tests_result_in_ignored() {
1818
fn f() { }
19-
let desc = {name: "whatever", fn: f, ignore: true};
19+
let desc = {name: "whatever", fn: f, ignore: true, should_fail: false};
2020
let res = test::run_test(desc, test::default_test_to_task).wait();
2121
assert (res == test::tr_ignored);
2222
}
2323

24+
#[test]
25+
fn test_should_fail() {
26+
fn f() { fail; }
27+
let desc = {name: "whatever", fn: f, ignore: false, should_fail: true};
28+
let res = test::run_test(desc, test::default_test_to_task).wait();
29+
assert res == test::tr_ok;
30+
}
31+
32+
#[test]
33+
fn test_should_fail_but_succeeds() {
34+
fn f() { }
35+
let desc = {name: "whatever", fn: f, ignore: false, should_fail: true};
36+
let res = test::run_test(desc, test::default_test_to_task).wait();
37+
assert res == test::tr_failed;
38+
}
39+
2440
#[test]
2541
fn first_free_arg_should_be_a_filter() {
2642
let args = ["progname", "filter"];
@@ -44,8 +60,8 @@ fn filter_for_ignored_option() {
4460

4561
let opts = {filter: option::none, run_ignored: true};
4662
let tests =
47-
[{name: "1", fn: fn () { }, ignore: true},
48-
{name: "2", fn: fn () { }, ignore: false}];
63+
[{name: "1", fn: fn () { }, ignore: true, should_fail: false},
64+
{name: "2", fn: fn () { }, ignore: false, should_fail: false}];
4965
let filtered = test::filter_tests(opts, tests);
5066

5167
assert (vec::len(filtered) == 1u);
@@ -69,7 +85,8 @@ fn sort_tests() {
6985
let testfn = fn () { };
7086
let tests = [];
7187
for name: str in names {
72-
let test = {name: name, fn: testfn, ignore: false};
88+
let test = {name: name, fn: testfn, ignore: false,
89+
should_fail: false};
7390
tests += [test];
7491
}
7592
tests

trunk/src/test/stdtest/uint.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ fn test_from_str() {
1414
assert (uint::from_str("00100") == 100u);
1515
}
1616

17+
#[test]
18+
#[should_fail]
19+
fn test_from_str_fail_1() {
20+
uint::from_str(" ");
21+
}
22+
23+
#[test]
24+
#[should_fail]
25+
fn test_from_str_fail_2() {
26+
uint::from_str("x");
27+
}
28+
1729
#[test]
1830
fn test_parse_buf() {
1931
assert (uint::parse_buf(bytes("123"), 10u) == 123u);
@@ -24,6 +36,18 @@ fn test_parse_buf() {
2436
assert (uint::parse_buf(bytes("z"), 36u) == 35u);
2537
}
2638

39+
#[test]
40+
#[should_fail]
41+
fn test_parse_buf_fail_1() {
42+
uint::parse_buf(bytes("Z"), 10u);
43+
}
44+
45+
#[test]
46+
#[should_fail]
47+
fn test_parse_buf_fail_2() {
48+
uint::parse_buf(bytes("_"), 2u);
49+
}
50+
2751
#[test]
2852
fn test_next_power_of_two() {
2953
assert (uint::next_power_of_two(0u) == 0u);

0 commit comments

Comments
 (0)