Skip to content

Commit

Permalink
Add pull-stream tests to validate pull-stream use cases
Browse files Browse the repository at this point in the history
  • Loading branch information
DamonOehlman committed May 5, 2014
1 parent bdad187 commit 31efcfb
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 13 deletions.
5 changes: 5 additions & 0 deletions test/all.js
Expand Up @@ -15,3 +15,8 @@ require('./validity');

// test our exec function
require('./exec-sync');
require('./exec-async');

// test pull streams integration
require('./pull-sync');
require('./pull-async');
82 changes: 82 additions & 0 deletions test/exec-async.js
@@ -0,0 +1,82 @@
var test = require('tape');
var task = require('..')();
var executed = [];
var a;
var b;
var c;

function trackTask() {
executed.push(this.name);
setTimeout(this.async(), 50);
}

test('specify a dependency', function(t) {
a = task('a', { deps: ['b'] }, trackTask);
executed = [];

// run a
t.plan(2);
task.exec(['a'], function(err) {
t.ok(err, 'captured error');
t.equal(err.message, 'Task "b" not found');
});
});

test('specify dependency (jake style)', function(t) {
a = task('a', ['b'], trackTask);
executed = [];

// run a
t.plan(2);
task.exec(['a'], function(err) {
t.ok(err, 'captured error');
t.equal(err.message, 'Task "b" not found');
});
});

test('register task b', function(t) {
b = task('b', trackTask);
executed = [];

t.plan(2);
task.exec(['a'], function(err) {
t.ifError(err, 'no error');
t.deepEqual(executed, ['b', 'a']);
});
})

test('register additional dependency', function(t) {
c = task('c', trackTask);
b.depends('c');
executed = [];

t.plan(2);
task.exec(['a'], function(err) {
t.ifError(err, 'no error');
t.deepEqual(executed, ['c', 'b', 'a']);
});
});

test('reject cyclic dependency (c --> c)', function(t) {
c.depends('c');
executed = [];

t.plan(2);
task.exec(['a'], function(err) {
t.ifError(err, 'no error');
t.deepEqual(executed, ['c', 'b', 'a']);
});
});

/*
// TODO
test('reject cyclic dependency (c --> a)', function(t) {
c.depends('a');
executed = [];
t.plan(2);
taskify.run('a').once('complete', function(err) {
t.ifError(err, 'no error');
t.deepEqual(executed, ['c', 'b', 'a']);
});
});*/
26 changes: 13 additions & 13 deletions test/exec-sync.js
Expand Up @@ -21,18 +21,18 @@ test('catch dependency error', function(t) {
});
});

// test('register b', function(t) {
// t.plan(1);
// b = task('b', trackTask);
// t.ok(task.get('b'), 'registered');
// });
test('register b', function(t) {
t.plan(1);
b = task('b', trackTask);
t.ok(task.get('b'), 'registered');
});

// test('run a + b', function(t) {
// executed = [];
test('run a + b', function(t) {
executed = [];

// t.plan(2);
// task.run('a').once('complete', function(err) {
// t.ifError(err);
// t.deepEqual(executed, ['b', 'a']);
// });
// });
t.plan(2);
task.exec(['a'], function(err) {
t.ifError(err);
t.deepEqual(executed, ['b', 'a']);
});
});
109 changes: 109 additions & 0 deletions test/pull-async.js
@@ -0,0 +1,109 @@
var test = require('tape');
var task = require('..')();
var pull = require('pull-stream');
var executed = [];
var a;
var b;
var c;

function trackTask() {
var callback = this.async();

executed.push(this.name);
setTimeout(function() {
callback(null, true);
}, 50);
}

test('specify a dependency', function(t) {
a = task('a', { deps: ['b'] }, trackTask);
executed = [];

// run a
t.plan(2);
pull(
pull.values(['a', 'a', 'a']),
pull.asyncMap(task.exec),
pull.collect(function(err, values) {
t.ok(err instanceof Error);
t.equal(err.message, 'Task "b" not found');
})
);
});

test('specify dependency (jake style)', function(t) {
a = task('a', ['b'], trackTask);
executed = [];

// run a
t.plan(2);
pull(
pull.values(['a', 'a', 'a']),
pull.asyncMap(task.exec),
pull.collect(function(err, values) {
t.ok(err instanceof Error);
t.equal(err.message, 'Task "b" not found');
})
);
});

test('register task b', function(t) {
b = task('b', trackTask);
executed = [];

t.plan(2);
pull(
pull.values(['a', 'a', 'a']),
pull.asyncMap(task.exec),
pull.collect(function(err, values) {
t.ifError(err);
t.deepEqual(values, [true, true, true], 'executed tasks');
})
);
})

test('register additional dependency', function(t) {
c = task('c', trackTask);
b.depends('c');
executed = [];

t.plan(3);
pull(
pull.values(['a', 'a', 'a']),
pull.asyncMap(task.exec),
pull.collect(function(err, values) {
t.ifError(err);
t.deepEqual(values, [true, true, true], 'executed tasks');
t.deepEqual(executed, ['c', 'b', 'a', 'c', 'b', 'a', 'c', 'b', 'a'], 'executed in expected order');
})
);
});

test('reject cyclic dependency (c --> c)', function(t) {
c.depends('c');
executed = [];

t.plan(3);
pull(
pull.values(['a', 'a', 'a']),
pull.asyncMap(task.exec),
pull.collect(function(err, values) {
t.ifError(err);
t.deepEqual(values, [true, true, true], 'executed tasks');
t.deepEqual(executed, ['c', 'b', 'a', 'c', 'b', 'a', 'c', 'b', 'a'], 'executed in expected order');
})
);
});

/*
// TODO
test('reject cyclic dependency (c --> a)', function(t) {
c.depends('a');
executed = [];
t.plan(2);
taskify.run('a').once('complete', function(err) {
t.ifError(err, 'no error');
t.deepEqual(executed, ['c', 'b', 'a']);
});
});*/
50 changes: 50 additions & 0 deletions test/pull-sync.js
@@ -0,0 +1,50 @@
var test = require('tape');
var task = require('..')();
var executed = [];
var pull = require('pull-stream');
var a, b, c;

function trackTask() {
executed.push(this.name);

return true;
}

test('register a', function(t) {
t.plan(1);
a = task('a', { deps: ['b'] }, trackTask);
t.ok(task.get('a'), 'registered');
});

test('catch dependency error', function(t) {
t.plan(2);

pull(
pull.values(['a', 'a', 'a']),
pull.asyncMap(task.exec),
pull.collect(function(err, values) {
t.ok(err instanceof Error);
t.equal(err.message, 'Task "b" not found');
})
);
});

test('register b', function(t) {
t.plan(1);
b = task('b', trackTask);
t.ok(task.get('b'), 'registered');
});

test('run a + b', function(t) {
executed = [];

t.plan(2);
pull(
pull.values(['a', 'a', 'a']),
pull.asyncMap(task.exec),
pull.collect(function(err, values) {
t.ifError(err);
t.deepEqual(values, [true, true, true], 'executed tasks');
})
);
});

0 comments on commit 31efcfb

Please sign in to comment.