Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Doctests for JavaScript and CoffeeScript
CoffeeScript JavaScript Makefile HTML Shell

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
README.markdown
doctest.coffee
doctest.js

README.markdown

doctest

A quick and (very) dirty implementation of JavaScript doctests, for those rare occasions when tests of this nature are actually appropriate.

Math.product = function () {
  // > Math.product(3, 4, 5)
  // 60
  // > Math.product(2, "ten")
  // NaN
  // > Math.product(6)
  // undefined
  var idx = arguments.length
  if (idx) {
    var product = arguments[0]
    while (--idx) product *= arguments[idx]
    return product
  }
}

To run doctests, pass doctest paths to one or more "modules" to be tested. Each path should be one of the following:

This can easily be done from a browser console:

> doctest("./math-extensions.js")
retrieving /scripts/./math-extensions.js...
running doctests in /scripts/./math-extensions.js...
..x
expected undefined on line 7 (got 6)

Oops. Looks like we have a bug.

Errors

It's easy to indicate that an error (of a particular kind) is expected:

// > (var x = 5)
// SyntaxError
// > null.length
// TypeError

Scoping

All expressions are eval'd in the global scope, so the following will leave a user property attached to the global object:

// > user = {first_name: "Sheldon", last_name: "Cooper"}
// > user.first_name + " " + user.last_name
// "Sheldon Cooper"

This shouldn't be a problem in practice (and it's actually rather useful in some cases), but it's worth bearing in mind that variables are available to all subsequent tests.

Dependencies

Something went wrong with that request. Please try again.