/*global describe, it, before, after, beforeEach, afterEach*/


'use strict';


// stdlib
var path    = require('path');
var format  = require('util').format;
var assert  = require('assert');


// internal
var Puncher = require('..');


////////////////////////////////////////////////////////////////////////////////


describe('Puncher', function () {
  var puncher;


  beforeEach(function () {
    puncher = new Puncher();
  });


  it("should correctly calculate start/stops", function (done) {
    var result;

    puncher.start('Foo');
    setTimeout(function () {
      result = puncher.stop().result();

      var foo = result[0];

      assert.ok(105 > foo.elapsed.total && foo.elapsed.total >= 100,
        format('Expect total elapsed time %d to be about 100ms',
          foo.elapsed.total));

      assert.equal(foo.elapsed.total, foo.stop - foo.start,
        format('Expect start/stop difference %d equal total elapsed time %d',
          foo.stop - foo.start, foo.elapsed.total));

      assert.equal(foo.elapsed.total, foo.offset.stop - foo.offset.start,
        format('Expect offset start/stop difference %d equal total elapsed time %d',
          foo.stop - foo.start, foo.elapsed.total));

      done();
    }, 100);
  });


  it("should properly calculate star/atops of nested scopes", function (done) {
    var result;

    puncher.start('Foo');
    setTimeout(function () {
      puncher.start('Bar');
      setTimeout(function () {
        result = puncher.stop(true).result();

        var foo = result[0], bar = foo.childs[0];

        assert.ok(205 > foo.elapsed.total && foo.elapsed.total >= 200,
          format('Expect overal time %d to be about 100ms',
            foo.elapsed.total));

        assert.ok(105 > bar.elapsed.total && bar.elapsed.total >= 100,
          format('Expect overal time of nested scope %d to be about 100ms',
            foo.elapsed.total));

        done();
      }, 100);
    }, 100);
  });


  it("should calculate missed coverage", function (done) {
    var result;

    puncher.start('Foo');
    setTimeout(function () {
      puncher.start('Bar');
      setTimeout(function () {
        result = puncher.stop(true).result();

        var foo = result[0];

        assert.ok(105 > foo.elapsed.missed && foo.elapsed.missed >= 100,
          format('Expect missed coverage time %d to be about 100ms',
            foo.elapsed.missed));

        done();
      }, 100);
    }, 100);
  });


  it("should tell if there are non-closed scopes", function () {
    puncher.start('Foo').start('Bar').stop();

    assert.ok(!puncher.stopped(), 'Should be non-stopped');

    puncher.stop();

    assert.ok(puncher.stopped(), 'Should be stopped');
  });


  it("should allow stop all non-closed scopes", function () {
    puncher.start('Foo').start('Bar').stop(true);
    assert.ok(puncher.stopped(), 'Should be stopped');
  });
});

@@ -2207,26 +2207,47 @@
  = result%5B0%5D
+, bar = foo.childs%5B0%5D
 ;%0A
-
 %0A        ass
@@ -2349,21 +2349,16 @@
 overage 
-time 
 %25d to be
@@ -2403,24 +2403,183 @@
 .missed));%0A%0A
+        assert.equal(0, bar.elapsed.missed,%0A          format('Expect missed coverage %25d of scope without childs to be 0ms',%0A            bar.elapsed.missed));%0A%0A
         done

Fix test specs