davidchambers/doctest — pull request #30 by davidchambers (2014-04-13 17:14:18+00:00)
Title: handle empty substrings correctly

In the process of investigating #29 I stumbled upon this nasty bug.

Given the following file:

```javascript
// > identity("foobar")
// "foobar"
function identity(x) {
return x;
}

// > constant(42)()
// 42
function constant(x) {
return function() {
return x;
};
}
```

The rewriter produces:

```javascript
// > identity("foobar")
// "foobar"
function identity(x) {
return x;
}

// > constant(42)()
// 42
function constant(x) {
return function() {
return x;
};
}
__doctest.input(function () {
return identity('foobar');
});
__doctest.output(2, function () {
return 'foobar';
});
function identity(x) {
return x;
}

__doctest.input(function () {
return constant(42)();
});
__doctest.output(8, function () {
return 42;
});
function constant(x) {
return function() {
return x;
};
}
__doctest.input(function () {
return;
});
```

Note that the entire source appears verbatim at the beginning of the rewritten source, followed by the correctly rewritten source. This resulted from `substring` failing to handle empty substrings correctly, triggered here by a doctest appearing at the very beginning of the file.

Fortunately, the fix is straightforward. :)