hadley/test_that — pull request #74 by trestletech (2013-04-14 20:35:31+00:00)
Title: Add notion of "provided" contexts which are provided for each tested file.

I encountered a bug in the printing of the summary reporter which ended up being a little more involved that I had initially hoped, but I think I have it all working now.

### Bug

I found that, if a context isn't specified at the top of a file, you can start to get some strange behavior. For instance, if I specify my first context mid-way through my first tested file, I'd get an output like this from the summary reporter:

..12...get_file : ..
get_file_part_2 : ...3...............
incorporate_foreign_keys : ..............
read_data_package : ..............
read_json_table : .....................................
write_json_table : .......

As you can see, I have some "orphaned" tests before my first context. This would get really difficult to parse automatically, as the test results (alpha-numeric chars) blend in to the context name.

### Quick Fix

I moved the newlines to print before each context, rather than after. This solves the issue above.

..12...
get_file : ..
get_file_part_2 : ...3...............
incorporate_foreign_keys : ..............
read_data_package : ..............
read_json_table : .....................................
write_json_table : .......

### Better Fix

Unfortunately, I found that if my files were tested in a different order (i.e. the file with the first context specified mid-way through were not the first file tested), that the above fix didn't solve the problem, as the test results would just be appended to the previous context.

The solution I propose here is to provide a default context for every file tested (named to match the testing file name). These contexts are annotated as "provided" contexts so that they can be dealt with in special ways by each reporter. Here I patched the summary reporter to delay the printing of a provided context until if/when a test result was obtained that had no parent context -- at which point the provided context (the file name) would be printed and used as the context for this test. If a context were specified at the top of each testing file, these would be ignored completely. The results render as follows:

test-get-file.R : ..12...
get_file : ..
get_file_part_2 : ...................
test-incorporate-foreign-keys.R : ..............
read_data_package : ..............
read_json_table : .....................................
write_json_table : .......

As you can see, the tests run in `test-get-file.R` before a context was specified are now using the provided context based on the file name. Most other files are just ignoring the provided context since they specify their context explicitly. But because I removed the explicit context from `test-incorporate-foreign-keys.R`, it too uses the provided context for the entire file.