Wednesday, February 29, 2012

Dart Code Coverage (not quite implemented)

‹prev | My Chain | next›

I need to finish off some work for the beta release of Dart for Hipsters, but first must honor the gods of my chain, who have been so very good to me, with a (hopefully) brief link in the chain...

Because I love testing, have included a chapter on testing in Dart for Hipsters. This would not normally be that much of a stretch, but in a bleeding edge language like Dart, the testing harness is only available in the "bleeding_edge" branch. Now that's bleeding edge.

Surprisingly, this bleeding edge bleeding edge testing harness works fairly well and includes some worthwhile features. What it does not include is a test coverage tool even though it has a tab for it:

Checking the Dart console, I see:
Exception: NoSuchMethodException : method not found: 'getCoverageSummary'
Receiver: Instance of 'DARTest'
Arguments: []
Stack Trace:  0. Function: 'Object.noSuchMethod' url: 'bootstrap' line:669 col:3
 1. Function: 'DARTest._showCoverageControls@15eceda' url: 'file:///home/cstrom/repos/dart-comics/tests/lib/dartest/dartest.dart' line:436 col:61
 2. Function: 'DARTest.function' url: 'file:///home/cstrom/repos/dart-comics/tests/lib/dartest/dartest.dart' line:285 col:28
And indeed, the getCoverageSummary method is nowhere to be found in either the darttest or unittest paths. I eventually find it, not it bleeding_edge/dart/client/testing, but in bleeding_edge/dart/compiler/lib.

I export this path into my tests/lib directory (after installing subversion again):
svn export lib/compiler
I have to edit bleeding_edge/dart/client/testing/darttest/darttest.dart to source the file defining getCoverageSummary():
// ...


// ...
With that, I get something in the tab, but it is zero test coverage despite the fact that I do have some coverage:

After looking through some of the code in compiler/coverage.dart, I begin to suspect that this code simply holds coverage information, but does not actually perform the tracking (perhaps the compiler does this). In fact, back in my test code, I can use a couple of the methods to set the expected number of function calls, and pretend that I made that call:
// ...
main() {
  setCoverageTotals('foo', 1, 0, 0);
  coverFunction('foo', 'funcBar');

  test('HipsterCollection has multiple models', (){
  // ...
Now when I check coverage, I suddenly have 100% function coverage:

I have already warned readers that the testing chapter is bleeding edge, so I don't think it will come as a shock to them that the Coverage tab is not ready yet. Nevertheless, I am still excited enough by Dart testing to retain the chapter.

Day #311


  1. Replies
    1. Wow. That's some old Dart code!

      I have not investigated this any further. I believe that the coverage code is still available in, but I have no idea how well it works :-\