1 // Support code for rustc's built in test runner generator. Currently,
2 // none of this is meant for users. It is intended to support the
3 // simplest interface possible for representing and running tests
4 // while providing a base that other test frameworks may build off of.
12 import core::result::{ok, err};
16 export default_test_fn;
24 export run_tests_console;
25 export run_tests_console_;
30 export default_test_to_task;
31 export configure_test_task;
36 fn sched_threads() -> uint;
40 // The name of a test. By convention this follows the rules for rust
41 // paths; i.e. it should be a series of identifiers seperated by double
42 // colons. This way if some test runner wants to arrange the tests
43 // hierarchically it may.
46 // A function that runs a test. If the function returns successfully,
47 // the test succeeds; if the function fails then the test fails. We
48 // may need to come up with a more clever definition of test in order
49 // to support isolation of tests into tasks.
52 type default_test_fn = test_fn<fn()>;
54 // The definition of a single test. A test runner will run a list of
63 // The default console test runner. It accepts the command line
64 // arguments and a vector of test_descs (generated at compile time).
65 fn test_main(args: [str], tests: [test_desc<default_test_fn>]) {
66 check (vec::is_not_empty(args));
68 alt parse_opts(args) {
70 either::right(m) { fail m }
72 if !run_tests_console(opts, tests) { fail "Some tests failed"; }
75 type test_opts = {filter: option::t<str>, run_ignored: bool};
77 type opt_res = either::t<test_opts, str>;
79 // Parses command line arguments into test options
80 fn parse_opts(args: [str]) : vec::is_not_empty(args) -> opt_res {
82 let args_ = vec::tail(args);
83 let opts = [getopts::optflag("ignored")];
85 alt getopts::getopts(args_, opts) {
87 err(f) { ret either::right(getopts::fail_str(f)) }
91 if vec::len(match.free) > 0u {
92 option::some(match.free[0])
93 } else { option::none };
95 let run_ignored = getopts::opt_present(match, "ignored");
97 let test_opts = {filter: filter, run_ignored: run_ignored};
99 ret either::left(test_opts);
102 tag test_result { tr_ok; tr_failed; tr_ignored; }
104 type joinable = (task, comm::port<task::task_notification>);
106 // To get isolation and concurrency tests have to be run in their own tasks.
107 // In cases where test functions are closures it is not ok to just dump them
108 // into a task and run them, so this transformation gives the caller a chance
109 // to create the test task.
110 type test_to_task<T> = fn@(test_fn<T>) -> joinable;
112 // A simple console test runner
113 fn run_tests_console(opts: test_opts,
114 tests: [test_desc<default_test_fn>]) -> bool {
115 run_tests_console_(opts, tests, default_test_to_task)
118 fn run_tests_console_<T: copy>(opts: test_opts, tests: [test_desc<T>],
119 to_task: test_to_task<T>) -> bool {
125 mutable passed: uint,
126 mutable failed: uint,
127 mutable ignored: uint,
128 mutable failures: [test_desc<T>]};
130 fn callback<T: copy>(event: testevent<T>, st: test_state) {
132 te_filtered(filtered_tests) {
133 st.total = vec::len(filtered_tests);
134 st.out.write_line(#fmt["\nrunning %u tests", st.total]);
136 te_wait(test) { st.out.write_str(#fmt["test %s ... ", test.name]); }
137 te_result(test, result) {
141 write_ok(st.out, st.use_color);
142 st.out.write_line("");
146 write_failed(st.out, st.use_color);
147 st.out.write_line("");
148 st.failures += [test];
152 write_ignored(st.out, st.use_color);
153 st.out.write_line("");
162 use_color: use_color(),
167 mutable failures: []};
169 run_tests(opts, tests, to_task, bind callback(_, st));
171 assert (st.passed + st.failed + st.ignored == st.total);
172 let success = st.failed == 0u;
175 st.out.write_line("\nfailures:");
176 for test: test_desc<T> in st.failures {
177 let testname = test.name; // Satisfy alias analysis
178 st.out.write_line(#fmt[" %s", testname]);
182 st.out.write_str(#fmt["\nresult: "]);
184 // There's no parallelism at this point so it's safe to use color
185 write_ok(st.out, true);
186 } else { write_failed(st.out, true); }
187 st.out.write_str(#fmt[". %u passed; %u failed; %u ignored\n\n", st.passed,
188 st.failed, st.ignored]);
192 fn write_ok(out: io::writer, use_color: bool) {
193 write_pretty(out, "ok", term::color_green, use_color);
196 fn write_failed(out: io::writer, use_color: bool) {
197 write_pretty(out, "FAILED", term::color_red, use_color);
200 fn write_ignored(out: io::writer, use_color: bool) {
201 write_pretty(out, "ignored", term::color_yellow, use_color);
204 fn write_pretty(out: io::writer, word: str, color: u8, use_color: bool) {
205 if use_color && term::color_supported() {
206 term::fg(out.get_buf_writer(), color);
209 if use_color && term::color_supported() {
210 term::reset(out.get_buf_writer());
215 fn use_color() -> bool { ret get_concurrency() == 1u; }
218 te_filtered([test_desc<T>]);
219 te_wait(test_desc<T>);
220 te_result(test_desc<T>, test_result);
223 fn run_tests<T: copy>(opts: test_opts, tests: [test_desc<T>],
224 to_task: test_to_task<T>,
225 callback: fn@(testevent<T>)) {
227 let filtered_tests = filter_tests(opts, tests);
228 callback(te_filtered(filtered_tests));
230 // It's tempting to just spawn all the tests at once but that doesn't
231 // provide a great user experience because you might sit waiting for the
232 // result of a particular test for an unusually long amount of time.
233 let concurrency = get_concurrency();
234 #debug("using %u test tasks", concurrency);
235 let total = vec::len(filtered_tests);
240 while wait_idx < total {
241 while vec::len(futures) < concurrency && run_idx < total {
242 futures += [run_test(filtered_tests[run_idx], to_task)];
246 let future = futures[0];
247 callback(te_wait(future.test));
248 let result = future.wait();
249 callback(te_result(future.test, result));
250 futures = vec::slice(futures, 1u, vec::len(futures));
255 fn get_concurrency() -> uint { rustrt::sched_threads() }
257 fn filter_tests<T: copy>(opts: test_opts,
258 tests: [test_desc<T>]) -> [test_desc<T>] {
259 let filtered = tests;
261 // Remove tests that don't match the test filter
262 filtered = if option::is_none(opts.filter) {
267 option::some(f) { f }
271 fn filter_fn<T: copy>(test: test_desc<T>, filter_str: str) ->
272 option::t<test_desc<T>> {
273 if str::find(test.name, filter_str) >= 0 {
274 ret option::some(test);
275 } else { ret option::none; }
278 let filter = bind filter_fn(_, filter_str);
280 vec::filter_map(filtered, filter)
283 // Maybe pull out the ignored test and unignore them
284 filtered = if !opts.run_ignored {
287 fn filter<T: copy>(test: test_desc<T>) -> option::t<test_desc<T>> {
289 ret option::some({name: test.name,
292 should_fail: test.should_fail});
293 } else { ret option::none; }
296 vec::filter_map(filtered, bind filter(_))
299 // Sort the tests alphabetically
302 fn lteq<T>(t1: test_desc<T>, t2: test_desc<T>) -> bool {
303 str::lteq(t1.name, t2.name)
305 sort::merge_sort(bind lteq(_, _), filtered)
311 type test_future<T> = {test: test_desc<T>, wait: fn@() -> test_result};
313 fn run_test<T: copy>(test: test_desc<T>,
314 to_task: test_to_task<T>) -> test_future<T> {
316 ret {test: test, wait: fn () -> test_result { tr_ignored }};
319 let test_task = to_task(test.fn);
322 bind fn (test_task: joinable, should_fail: bool) -> test_result {
323 alt task::join(test_task) {
325 if should_fail { tr_failed }
329 if should_fail { tr_ok }
333 }(test_task, test.should_fail)};
336 // We need to run our tests in another task in order to trap test failures.
337 // This function only works with functions that don't contain closures.
338 fn default_test_to_task(&&f: default_test_fn) -> joinable {
339 fn run_task(f: default_test_fn) {
340 configure_test_task();
343 ret task::spawn_joinable(copy f, run_task);
346 // Call from within a test task to make sure it's set up correctly
347 fn configure_test_task() {
348 // If this task fails we don't want that failure to propagate to the
349 // test runner or else we couldn't keep running tests
356 // indent-tabs-mode: nil
358 // buffer-file-coding-system: utf-8-unix