GithubHelp home page GithubHelp logo

ericlanduyt / doctest Goto Github PK

View Code? Open in Web Editor NEW

This project forked from davidchambers/doctest

0.0 1.0 0.0 426 KB

Doctests for JavaScript (and CoffeeScript)

License: Do What The F*ck You Want To Public License

JavaScript 90.73% CoffeeScript 8.44% Shell 0.84%

doctest's Introduction

doctest

Doctests are executable usage examples sometimes found in "docstrings". JavaScript doesn't have docstrings, but inline documentation can be included in code comments. doctest finds and evaluates usage examples in code comments and reports any inaccuracies. doctest works with JavaScript and CoffeeScript modules.

Example

// toFahrenheit :: Number -> Number
//
// Convert degrees Celsius to degrees Fahrenheit.
//
// > toFahrenheit(0)
// 32
// > toFahrenheit(100)
// 212
function toFahrenheit(degreesCelsius) {
  return degreesCelsius * 9 / 5 + 32;
}

Doctest will execute toFahrenheit(0) and verify that its output is 32.

Installation

$ npm install doctest

Running doctests

Test a module via JavaScript API:

> doctest ({}) ('lib/temperature.js')

Test a module via command-line interface:

$ doctest lib/temperature.js

The exit code is 0 if all tests pass, 1 otherwise.

Supported module systems

Module system Option Node.js Dependencies
AMD amd ✔︎
CommonJS commonjs ✔︎ ✔︎
ECMAScript modules esm ✔︎ ✔︎

Specify module system via JavaScript API:

> doctest ({module: 'amd'}) ('path/to/amd/module.js')

Specify module system via command-line interface:

$ doctest --module commonjs path/to/commonjs/module.js

Line wrapping

Input lines may be wrapped by beginning each continuation with FULL STOP (.):

// > reverse([
// .   'foo',
// .   'bar',
// .   'baz',
// . ])
// ['baz', 'bar', 'foo']

Output lines may be wrapped in the same way:

// > reverse([
// .   'foo',
// .   'bar',
// .   'baz',
// . ])
// [ 'baz',
// . 'bar',
// . 'foo' ]

Exceptions

An output line beginning with EXCLAMATION MARK (!) indicates that the preceding expression is expected to throw. The exclamation mark must be followed by SPACE ( ) and the name of an Error constructor. For example:

// > null.length
// ! TypeError

The constructor name may be followed by COLON (:), SPACE ( ), and the expected error message. For example:

// > null.length
// ! TypeError: Cannot read property 'length' of null

Scoping

Each doctest has access to variables in its scope chain.

Integrations

Running the test suite

$ npm install
$ npm test

doctest's People

Contributors

davidchambers avatar danse avatar avaq avatar cclauss avatar eush77 avatar mkllnk avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.