Skip to content

Commit 64d43be

Browse files
committed
Sort tests before running them. Issue #428
1 parent f010f79 commit 64d43be

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/lib/test.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// simplest interface possible for representing and running tests
44
// while providing a base that other test frameworks may build off of.
55

6+
import sort = sort::ivector;
7+
68
export test_name;
79
export test_fn;
810
export test_desc;
@@ -165,6 +167,7 @@ fn run_tests(&test_opts opts, &test_desc[] tests) -> bool {
165167
fn filter_tests(&test_opts opts, &test_desc[] tests) -> test_desc[] {
166168
auto filtered = tests;
167169

170+
// Remove tests that don't match the test filter
168171
filtered = if (option::is_none(opts.filter)) {
169172
filtered
170173
} else {
@@ -183,6 +186,7 @@ fn filter_tests(&test_opts opts, &test_desc[] tests) -> test_desc[] {
183186
ivec::filter_map(filter, filtered)
184187
};
185188

189+
// Maybe pull out the ignored test and unignore them
186190
filtered = if (!opts.run_ignored) {
187191
filtered
188192
} else {
@@ -199,6 +203,14 @@ fn filter_tests(&test_opts opts, &test_desc[] tests) -> test_desc[] {
199203
ivec::filter_map(filter, filtered)
200204
};
201205

206+
// Sort the tests alphabetically
207+
filtered = {
208+
fn lteq(&test_desc t1, &test_desc t2) -> bool {
209+
str::lteq(t1.name, t2.name)
210+
}
211+
sort::merge_sort(lteq, filtered)
212+
};
213+
202214
ret filtered;
203215
}
204216

src/test/stdtest/test.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,51 @@ fn filter_for_ignored_option() {
6666
assert filtered.(0).ignore == false;
6767
}
6868

69+
#[test]
70+
fn sort_tests() {
71+
auto opts = rec(filter = option::none,
72+
run_ignored = false);
73+
74+
auto names = ~["sha1::test",
75+
"int::test_to_str",
76+
"int::test_pow",
77+
"test::do_not_run_ignored_tests",
78+
"test::ignored_tests_result_in_ignored",
79+
"test::first_free_arg_should_be_a_filter",
80+
"test::parse_ignored_flag",
81+
"test::filter_for_ignored_option",
82+
"test::sort_tests"];
83+
auto tests = {
84+
auto testfn = fn() {};
85+
auto tests = ~[];
86+
for (str name in names) {
87+
auto test = rec(name = name,
88+
fn = testfn,
89+
ignore = false);
90+
tests += ~[test];
91+
}
92+
tests
93+
};
94+
auto filtered = test::filter_tests(opts, tests);
95+
96+
auto expected = ~["int::test_pow",
97+
"int::test_to_str",
98+
"sha1::test",
99+
"test::do_not_run_ignored_tests",
100+
"test::filter_for_ignored_option",
101+
"test::first_free_arg_should_be_a_filter",
102+
"test::ignored_tests_result_in_ignored",
103+
"test::parse_ignored_flag",
104+
"test::sort_tests"];
105+
106+
auto pairs = ivec::zip(expected, filtered);
107+
108+
for (tup(str, test::test_desc) p in pairs) {
109+
log_err #fmt("e: %s a: %s", p._0, p._1.name);
110+
assert p._0 == p._1.name;
111+
}
112+
}
113+
69114
// Local Variables:
70115
// mode: rust;
71116
// fill-column: 78;

0 commit comments

Comments
 (0)