From e3cfce22fcdc217a38a53d561cda7f7cf2f04578 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Fri, 30 May 2014 06:36:26 -0600 Subject: [PATCH] Add util tests --- package.json | 3 +- test/helper.js | 12 ++++++ test/lib/util.spec.js | 97 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 test/helper.js create mode 100644 test/lib/util.spec.js diff --git a/package.json b/package.json index bf0d5a1..028b5bc 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "devDependencies": { "glob": "~3.2.9", "mocha": "~1.18.2", - "jshint": "~2.4.4" + "jshint": "~2.4.4", + "chai": "~1.9.1" } } diff --git a/test/helper.js b/test/helper.js new file mode 100644 index 0000000..de6adf3 --- /dev/null +++ b/test/helper.js @@ -0,0 +1,12 @@ +var chai = require('chai'); + + +/** @type {boolean} */ +chai.config.includeStack = true; + + +/** + * Chai's assert function configured to include stacks on failure. + * @type {function} + */ +exports.assert = chai.assert; diff --git a/test/lib/util.spec.js b/test/lib/util.spec.js new file mode 100644 index 0000000..2ec1b8d --- /dev/null +++ b/test/lib/util.spec.js @@ -0,0 +1,97 @@ +var path = require('path'); + +var assert = require('../helper').assert; + +var util = require('../../lib/util'); + +describe('util', function() { + + var files; + beforeEach(function() { + files = [ + path.join('a1', 'b1', 'c2', 'd2.txt'), + path.join('a1', 'b2', 'c2', 'd1.txt'), + path.join('a2.txt'), + path.join('a1', 'b1', 'c1', 'd1.txt'), + path.join('a1', 'b1', 'c2', 'd1.txt'), + path.join('a1', 'b1.txt'), + path.join('a2', 'b1', 'c2.txt'), + path.join('a1', 'b1', 'c2', 'd3.txt'), + path.join('a1', 'b2', 'c1', 'd1.txt'), + path.join('a1.txt'), + path.join('a2', 'b1', 'c1.txt'), + path.join('a2', 'b1.txt') + ].slice(); + }); + + describe('byShortPath', function() { + it('sorts an array of filepaths, shortest first', function() { + files.sort(util.byShortPath); + + var expected = [ + path.join('a1.txt'), + path.join('a2.txt'), + path.join('a1', 'b1.txt'), + path.join('a2', 'b1.txt'), + path.join('a2', 'b1', 'c1.txt'), + path.join('a2', 'b1', 'c2.txt'), + path.join('a1', 'b1', 'c1', 'd1.txt'), + path.join('a1', 'b1', 'c2', 'd1.txt'), + path.join('a1', 'b1', 'c2', 'd2.txt'), + path.join('a1', 'b1', 'c2', 'd3.txt'), + path.join('a1', 'b2', 'c1', 'd1.txt'), + path.join('a1', 'b2', 'c2', 'd1.txt') + ]; + + assert.deepEqual(files, expected); + }); + }); + + describe('uniqueDirs', function() { + + it('gets a list of unique directory paths', function() { + // not comparing order here, so we sort both + var got = util.uniqueDirs(files).sort(); + + var expected = [ + '.', + 'a1', + 'a2', + path.join('a1', 'b1'), + path.join('a1', 'b1', 'c1'), + path.join('a1', 'b1', 'c2'), + path.join('a1', 'b2'), + path.join('a1', 'b2', 'c1'), + path.join('a1', 'b2', 'c2'), + path.join('a2', 'b1') + ].sort(); + + assert.deepEqual(got, expected); + }); + + }); + + describe('dirsToCreate', function() { + + it('gets a sorted list of directories to create', function() { + var got = util.dirsToCreate(files); + + var expected = [ + '.', + 'a1', + 'a2', + path.join('a1', 'b1'), + path.join('a1', 'b2'), + path.join('a2', 'b1'), + path.join('a1', 'b1', 'c1'), + path.join('a1', 'b1', 'c2'), + path.join('a1', 'b2', 'c1'), + path.join('a1', 'b2', 'c2') + ]; + + assert.deepEqual(got, expected); + }); + + }); + +});