Jared K. Smith 16d7a4b
/*jshint node:true */
Jared K. Smith 16d7a4b
/*global describe:false, it:false */
Jared K. Smith 16d7a4b
Jared K. Smith 16d7a4b
"use strict";
Jared K. Smith 16d7a4b
Jared K. Smith 16d7a4b
var sequence = require('../');
Jared K. Smith 16d7a4b
var should = require('should');
Jared K. Smith 16d7a4b
require('mocha');
Jared K. Smith 16d7a4b
Jared K. Smith 16d7a4b
describe('task sequencing', function() {
Jared K. Smith 16d7a4b
	describe('sequence()', function() {
Jared K. Smith 16d7a4b
Jared K. Smith 16d7a4b
		var dependencyTree = {
Jared K. Smith 16d7a4b
			a: [],
Jared K. Smith 16d7a4b
			b: ['a'],
Jared K. Smith 16d7a4b
			c: ['a'],
Jared K. Smith 16d7a4b
			d: ['b','c'],
Jared K. Smith 16d7a4b
			e: ['f'],
Jared K. Smith 16d7a4b
			f: ['e'],
Jared K. Smith 16d7a4b
			g: ['g']
Jared K. Smith 16d7a4b
		};
Jared K. Smith 16d7a4b
		var noop = function () {};
Jared K. Smith 16d7a4b
Jared K. Smith 16d7a4b
		var makeTasks = function (tree) {
Jared K. Smith 16d7a4b
			var tasks = {}, p;
Jared K. Smith 16d7a4b
			for (p in tree) {
Jared K. Smith 16d7a4b
				if (tree.hasOwnProperty(p)) {
Jared K. Smith 16d7a4b
					tasks[p] = {
Jared K. Smith 16d7a4b
						name: p,
Jared K. Smith 16d7a4b
						dep: tree[p],
Jared K. Smith 16d7a4b
						fn: noop
Jared K. Smith 16d7a4b
					};
Jared K. Smith 16d7a4b
				}
Jared K. Smith 16d7a4b
			}
Jared K. Smith 16d7a4b
			return tasks;
Jared K. Smith 16d7a4b
		};
Jared K. Smith 16d7a4b
Jared K. Smith 16d7a4b
		var theTest = function (source,expected) {
Jared K. Smith 16d7a4b
			// Arrange
Jared K. Smith 16d7a4b
			var tasks = makeTasks(dependencyTree);
Jared K. Smith 16d7a4b
			var actual = [];
Jared K. Smith 16d7a4b
Jared K. Smith 16d7a4b
			// Act
Jared K. Smith 16d7a4b
			sequence(tasks, source.split(','), actual);
Jared K. Smith 16d7a4b
Jared K. Smith 16d7a4b
			// Assert
Jared K. Smith 16d7a4b
			actual.join(',').should.equal(expected);
Jared K. Smith 16d7a4b
		};
Jared K. Smith 16d7a4b
Jared K. Smith 16d7a4b
		it('a -> a', function() {
Jared K. Smith 16d7a4b
			theTest('a', 'a');
Jared K. Smith 16d7a4b
		});
Jared K. Smith 16d7a4b
		it('a,a -> a', function() {
Jared K. Smith 16d7a4b
			theTest('a,a', 'a');
Jared K. Smith 16d7a4b
		});
Jared K. Smith 16d7a4b
		it('c -> a,c', function() {
Jared K. Smith 16d7a4b
			theTest('c', 'a,c');
Jared K. Smith 16d7a4b
		});
Jared K. Smith 16d7a4b
		it('b -> a,b', function() {
Jared K. Smith 16d7a4b
			theTest('b', 'a,b');
Jared K. Smith 16d7a4b
		});
Jared K. Smith 16d7a4b
		it('c,b -> a,c,b', function() {
Jared K. Smith 16d7a4b
			theTest('c,b', 'a,c,b');
Jared K. Smith 16d7a4b
		});
Jared K. Smith 16d7a4b
		it('b,c -> a,b,c', function() {
Jared K. Smith 16d7a4b
			theTest('b,c', 'a,b,c');
Jared K. Smith 16d7a4b
		});
Jared K. Smith 16d7a4b
		it('b,a -> a,b', function() {
Jared K. Smith 16d7a4b
			theTest('b,a', 'a,b');
Jared K. Smith 16d7a4b
		});
Jared K. Smith 16d7a4b
		it('d -> a,b,c,d', function() {
Jared K. Smith 16d7a4b
			theTest('d', 'a,b,c,d');
Jared K. Smith 16d7a4b
		});
Jared K. Smith 16d7a4b
		it('c,d -> a,c,b,d', function() {
Jared K. Smith 16d7a4b
			theTest('c,d', 'a,c,b,d');
Jared K. Smith 16d7a4b
		});
Jared K. Smith 16d7a4b
		it('b,d -> a,b,c,d', function() {
Jared K. Smith 16d7a4b
			theTest('b,d', 'a,b,c,d');
Jared K. Smith 16d7a4b
		});
Jared K. Smith 16d7a4b
		it('e -> throw', function() {
Jared K. Smith 16d7a4b
			var failed = false, i;
Jared K. Smith 16d7a4b
			var expectedRecursionList = ['e','f','e'];
Jared K. Smith 16d7a4b
			var expectedTaskList = ['a','b','c','d','e','f','g'];
Jared K. Smith 16d7a4b
			try {
Jared K. Smith 16d7a4b
				theTest('e', 'throw');
Jared K. Smith 16d7a4b
				failed = true;
Jared K. Smith 16d7a4b
			} catch (err) {
Jared K. Smith 16d7a4b
				should.exist(err);
Jared K. Smith 16d7a4b
				err.message.should.match(/recursive/i, err.message+' should include recursive');
Jared K. Smith 16d7a4b
				err.recursiveTasks.length.should.equal(expectedRecursionList.length);
Jared K. Smith 16d7a4b
				for (i = 0; i < expectedRecursionList.length; i++) {
Jared K. Smith 16d7a4b
					err.recursiveTasks[i].should.equal(expectedRecursionList[i]);
Jared K. Smith 16d7a4b
				}
Jared K. Smith 16d7a4b
				err.taskList.length.should.equal(expectedTaskList.length);
Jared K. Smith 16d7a4b
				expectedTaskList.forEach(function (item) {
Jared K. Smith 16d7a4b
					err.taskList.should.include(item);
Jared K. Smith 16d7a4b
				});
Jared K. Smith 16d7a4b
			}
Jared K. Smith 16d7a4b
			failed.should.equal(false);
Jared K. Smith 16d7a4b
		});
Jared K. Smith 16d7a4b
		it('g -> throw', function() {
Jared K. Smith 16d7a4b
			var failed = false, i;
Jared K. Smith 16d7a4b
			var expectedRecursionList = ['g','g'];
Jared K. Smith 16d7a4b
			var expectedTaskList = ['a','b','c','d','e','f','g'];
Jared K. Smith 16d7a4b
			try {
Jared K. Smith 16d7a4b
				theTest('g', 'throw');
Jared K. Smith 16d7a4b
				failed = true;
Jared K. Smith 16d7a4b
			} catch (err) {
Jared K. Smith 16d7a4b
				should.exist(err);
Jared K. Smith 16d7a4b
				err.message.should.match(/recursive/i, err.message+' should include recursive');
Jared K. Smith 16d7a4b
				err.recursiveTasks.length.should.equal(expectedRecursionList.length);
Jared K. Smith 16d7a4b
				for (i = 0; i < expectedRecursionList.length; i++) {
Jared K. Smith 16d7a4b
					err.recursiveTasks[i].should.equal(expectedRecursionList[i]);
Jared K. Smith 16d7a4b
				}
Jared K. Smith 16d7a4b
				err.taskList.length.should.equal(expectedTaskList.length);
Jared K. Smith 16d7a4b
				expectedTaskList.forEach(function (item) {
Jared K. Smith 16d7a4b
					err.taskList.should.include(item);
Jared K. Smith 16d7a4b
				});
Jared K. Smith 16d7a4b
			}
Jared K. Smith 16d7a4b
			failed.should.equal(false);
Jared K. Smith 16d7a4b
		});
Jared K. Smith 16d7a4b
		it('h -> throw', function() {
Jared K. Smith 16d7a4b
			var failed = false;
Jared K. Smith 16d7a4b
			var expectedTaskList = ['a','b','c','d','e','f','g'];
Jared K. Smith 16d7a4b
			try {
Jared K. Smith 16d7a4b
				theTest('h', 'throw');
Jared K. Smith 16d7a4b
				failed = true;
Jared K. Smith 16d7a4b
			} catch (err) {
Jared K. Smith 16d7a4b
				should.exist(err);
Jared K. Smith 16d7a4b
				err.message.should.match(/not defined/i, err.message+' should include not defined');
Jared K. Smith 16d7a4b
				err.missingTask.should.equal('h');
Jared K. Smith 16d7a4b
				err.taskList.length.should.equal(expectedTaskList.length);
Jared K. Smith 16d7a4b
				expectedTaskList.forEach(function (item) {
Jared K. Smith 16d7a4b
					err.taskList.should.include(item);
Jared K. Smith 16d7a4b
				});
Jared K. Smith 16d7a4b
			}
Jared K. Smith 16d7a4b
			failed.should.equal(false);
Jared K. Smith 16d7a4b
		});
Jared K. Smith 16d7a4b
Jared K. Smith 16d7a4b
	});
Jared K. Smith 16d7a4b
});