Skip to content

Commit

Permalink
Improve test specs and parallel logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpfs committed Mar 19, 2016
1 parent f4ae11a commit e96731b
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 65 deletions.
80 changes: 74 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,26 @@ For the command line interface install [mkdoc][] globally (`npm i -g mkdoc`).

## API

### task
### mk

```javascript
task()
mk()
```

Creates a task collection.

Returns a Task.

### #task

```javascript
static task()
```

Adds a task to the default task collection.

Returns a Task.

### Task

```javascript
Expand All @@ -47,14 +57,37 @@ Adds task function(s) to the list of known tasks.
### .run

```javascript
Task.prototype.run([opts])
Task.prototype.run()
```

Gets a task runner for this collection of tasks.

Returns a task Runner.

* `opts` processing options.
### #src

```javascript
static src()
```

Parses a markdown string into a stream.

Returns the output stream.

### #dest

```javascript
static dest([file])
```

Get a destination output stream.

If the file option is not given a destination stream that prints to
stdout is returned.

Returns an output stream.

* `file` String path to the output file.

### runner

Expand All @@ -80,8 +113,7 @@ Execute task functions.

#### Options

* `list` Array of tasks.
* `scope` Object task execution scope.
* `task` Object collection of tasks.

### .get

Expand All @@ -95,6 +127,29 @@ Returns a task or undefined.

* `id` Function|String task identifier.

### .series

```javascript
Runner.prototype.series(list, cb)
```

Execute task functions in series.

* `list` Array of task functions.
* `cb` Function callback function.

### .parallel

```javascript
Runner.prototype.parallel(list[, concurrent], cb)
```

Execute task functions in parallel.

* `list` Array of task functions.
* `concurrent` Number number of concurrent calls.
* `cb` Function callback function.

### .exec

```javascript
Expand All @@ -103,11 +158,24 @@ Runner.prototype.exec(id, cb)

Execute a task by name identifier.

Dependencies are run in parallel before task execution.

Returns a task or undefined.

* `id` Function|String task identifier.
* `cb` Function callback function.

### .each

```javascript
Runner.prototype.each([names], cb)
```

Execute a list of tasks to by string identifiers.

* `names` Array list of task names.
* `cb` Function callback function.

## License

MIT
Expand Down
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function add() {
}

if(!args.length) {
throw new TypeError('task function(s) expected');
throw new TypeError('not enough arguments for task()');
}

function gather(args) {
Expand Down Expand Up @@ -126,10 +126,13 @@ Task.prototype.run = run;
mk.src = ast.src;

/**
* Get a file write output stream.
* Get a destination output stream.
*
* If the file option is not given a destination stream that prints to
* stdout is returned.
*
* @static {function} dest
* @param {String} file path to the output file.
* @param {String} [file] path to the output file.
*
* @returns an output stream.
*/
Expand Down
4 changes: 3 additions & 1 deletion runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,12 @@ function parallel(list, concurrent, cb) {
concurrent = concurrent || items.length;

function run() {
if(complete) {
items = items.slice(concurrent);
}
for(var i = 0;i < concurrent;i++) {
items[i].call(scope, next);
}
items = items.slice(concurrent);
}

function next(err) {
Expand Down
13 changes: 13 additions & 0 deletions test/spec/defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var expect = require('chai').expect
, mktask = require('../../index');

describe('mktask:', function() {

it('should get default task collection', function(done) {
// static task() method
var res = mktask.task();
expect(res).to.be.an('object');
done();
});

});
18 changes: 18 additions & 0 deletions test/spec/dest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var expect = require('chai').expect
, mktask = require('../../index');

describe('mktask:', function() {

it('should get file write destination stream', function(done) {
var res = mktask.dest('target/foo.txt');
expect(res).to.be.an('object');
done();
});

it('should get default destination stream', function(done) {
var res = mktask.dest();
expect(res).to.be.an('object');
done();
});

});
48 changes: 48 additions & 0 deletions test/spec/each.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var expect = require('chai').expect
, mktask = require('../../index');

describe('mktask:', function() {

it('should iterate task function (each)', function(done) {
var mk = mktask()
, called = 0;

function readme(cb) {
called++;
cb();
}

mk.task(readme);

var runner = mk.run();
runner.each([readme.name], function() {
expect(called).to.eql(1);
done();
});
});

it('should iterate all task functions (each)', function(done) {
var mk = mktask()
, called = 0;

function api(cb) {
called++;
cb();
}

function readme(cb) {
called++;
cb();
}

mk.task(api);
mk.task(readme);

var runner = mk.run();
runner.each(function() {
expect(called).to.eql(2);
done();
});
});

});
54 changes: 0 additions & 54 deletions test/spec/run.js → test/spec/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,6 @@ var expect = require('chai').expect

describe('mktask:', function() {

it('should get task from runner', function(done) {
var mk = mktask()
, readme = function readme(){}
, res = mk.task(readme)
, runner = mk.run();

expect(res).to.be.an('object');
expect(runner).to.be.an('object');

expect(runner.get(readme)).to.eql(res);
expect(runner.get(readme.name)).to.eql(res);

expect(runner.get('foo')).to.eql(undefined);

done();
});

it('should return task from runner (exec)', function(done) {
var mk = mktask()
, readme = function readme(){}
Expand Down Expand Up @@ -87,43 +70,6 @@ describe('mktask:', function() {
});
});

it('should exec task functions dependencies in parallel', function(done) {
var mk = mktask()
, called = 0
, docsCalled
, apiCalled
, readmeCalled;

function docs(cb) {
called++;
docsCalled = 1;
cb();
}

function api(cb) {
called++;
apiCalled = 1;
cb();
}

function readme(cb) {
called++;
readmeCalled = 1;
cb();
}

mk.task([docs, api], readme);

var runner = mk.run();
runner.exec(readme, function() {
expect(called).to.eql(3);
expect(docsCalled).to.eql(1);
expect(apiCalled).to.eql(1);
expect(readmeCalled).to.eql(1);
done();
});
});

it('should callback with error on task function exec', function(done) {
var mk = mktask()

Expand Down
Loading

0 comments on commit e96731b

Please sign in to comment.