Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

    power-doctestpublic

    power-doctest Build Status

    Doctest + power-assert.

    Installation

    npm install -g power-doctest

    Task

    Usage

    Usage: power-doctest /path/to/file.js
    

    Test code :

    function sum(ary) {
        return ary.reduce(function (current, next) {
            return current + next
        }, 0);
    }
     
    var total = sum([1, 2, 3, 4, 5]);
    total; // => 5

    This code expect total to be 5.

    Result :

    $ power-doctest example/example.js
    var assert = require('power-assert');
    function sum(ary) {
        return ary.reduce(function (current, next) {
            return current + next;
        }, 0);
    }
    var total = sum([
        1,
        2,
        3,
        4,
        5
    ]);
    assert.equal(assert._expr(assert._capt(total, 'arguments/0'){
        content: 'assert.equal(total, 5)',
        line: 14
    }), 5);
    //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiIsInNvdXJjZXNDb250ZW50IjpbXX0

    And execute this transformed code:

    $ power-doctest example/example.js | node -p
    AssertionError:   # at line: 14 
     
      assert.equal(total, 5)
                   |
                   15
     

    assert-test

    Exception Test

    Look like => Error is assert.throw().

    throw new Error(); // => Error
    var object = {};
    obj.not.found;// => Error

    Covert this case to:

    var assert = require('power-assert');
    assert.throws(function () {
        throw new Error();
    }, Error);
    var object = {};
    assert.throws(function () {
        object.not.found;
    }, Error);

    UseCase

    It will be useful for writing document or book. Example code should be tested, but it is difficult. power-doctest help to introduce automatic testing for example code.

    Contributing

    1. Fork it!
    2. Create your feature branch: git checkout -b my-new-feature
    3. Commit your changes: git commit -am 'Add some feature'
    4. Push to the branch: git push origin my-new-feature
    5. Submit a pull request :D

    License

    MIT

    Keywords

    none

    install

    npm i power-doctest

    Downloadsweekly downloads

    32

    version

    1.3.2

    license

    MIT

    repository

    githubgithub

    last publish

    collaborators

    • avatar