From 50f2b83443858fdd8f47c9697a940866d4a624d3 Mon Sep 17 00:00:00 2001 From: Willem Date: Fri, 27 Feb 2015 03:31:44 +0100 Subject: [PATCH] add some tests --- .gitignore | 3 ++ lib/assets-builder.js | 3 ++ lib/factory.js | 4 +- package.json | 3 +- test/test-asset-assembler.js | 88 ++++++++++++++++++++++++++++++++++++ test/test-factory.js | 52 +++++++++++++++++++++ test/test-node-ff-assets.js | 30 ++++++++---- 7 files changed, 173 insertions(+), 10 deletions(-) create mode 100644 test/test-asset-assembler.js create mode 100644 test/test-factory.js diff --git a/.gitignore b/.gitignore index 6965bd0..b227694 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,9 @@ node_modules npm-debug.log +# Ignore mocha test data +test/data + # Ignore binary files *.o *.class diff --git a/lib/assets-builder.js b/lib/assets-builder.js index 4452934..03e1c8a 100644 --- a/lib/assets-builder.js +++ b/lib/assets-builder.js @@ -58,6 +58,9 @@ function buildStepConfigCreate(builder, buildConfig, callback) { } function AssetsBuilder(config) { + if (config === undefined) { + throw new Error('no config'); + } this.config = config; this.startTime = 0; this.currentAssetType = ''; diff --git a/lib/factory.js b/lib/factory.js index b5ee974..beca8d8 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -122,7 +122,9 @@ var builderAssemblerCreate = { }, }; -var builderEmptyCallback = function () {}; +var builderEmptyCallback = function () { + return function () {}; +}; module.exports = { assembler: { diff --git a/package.json b/package.json index 5ab269f..7cc07e5 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "dependencies": { "fetch": "^0.3.6", "fs-extra": "^0.16.3", - "minify": "^1.4.8" + "minify": "^1.4.8", + "underscore": "^1.8.2" }, "devDependencies": { "mocha": "^2.1.0", diff --git a/test/test-asset-assembler.js b/test/test-asset-assembler.js new file mode 100644 index 0000000..c4ccf7c --- /dev/null +++ b/test/test-asset-assembler.js @@ -0,0 +1,88 @@ +'use strict'; + +var u = require('underscore'); +var fs = require('fs-extra'); +var assert = require("assert"); +var assets = require("../lib/node-ff-assets"); + +var testDataCss = function (testName) { + var outputFile = 'test/data/'+testName+'/output.css'; + var dataCss0 = 'test/data/'+testName+'/input0.css'; + var dataCss1 = 'test/data/'+testName+'/input1.css'; + fs.ensureFileSync(dataCss0); + fs.writeFileSync(dataCss0,''); + fs.appendFile(dataCss0,'input__0'); + fs.ensureFileSync(dataCss1); + fs.writeFileSync(dataCss1,''); + fs.appendFile(dataCss1,'input__1'); + if (fs.existsSync(outputFile)) { + fs.writeFileSync(outputFile,''); + } + return outputFile; +} + +var testName = ''; +describe('lib/asset-assembler.js', function() { + testName = 'test-aggregate-two'; + describe(testName, function() { + this.timeout(10000); + var outputFile = testDataCss(testName); + var assetAssembler = new assets.AssetAssembler({ + linkMapping: {'/static-test/': 'test/data/'+testName+'/'}, + linkTarget: '/static-test/output.css', + linkSources: [ + '/static-test/input0.css', + '/static-test/input1.css', + ], + }); + it('run builder and check output', function (done) { + assetAssembler.run(function (err) { + try { + assert.ok(true); + assert.equal(true, fs.existsSync(outputFile)); + + var outputData = ''+fs.readFileSync(outputFile); + assert.equal(true, outputData.indexOf('input__0') > 0); + assert.equal(true, outputData.indexOf('input__1') > 0); + + done(); + } catch (err) { + done(err); + } + }); + }); + }); + testName = 'test-aggregate-two-and-remote'; + describe(testName, function() { + this.timeout(10000); + var outputFile = testDataCss(testName); + var assetAssembler = new assets.AssetAssembler({ + linkMapping: {'/static-test/': 'test/data/'+testName+'/'}, + linkTarget: '/static-test/output.css', + linkSources: [ + '/static-test/input0.css', + '/static-test/input1.css', + '/static-test/fonts-sans-pro.css@http://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700', + ], + }); + it('run builder and check output', function (done) { + assetAssembler.run(function (err) { + try { + assert.ok(true); + assert.equal(true, fs.existsSync(outputFile)); + assert.equal(true, fs.existsSync('test/data/'+testName+'/fonts-sans-pro.css')); + + var outputData = ''+fs.readFileSync(outputFile); + assert.equal(true, outputData.indexOf('input__0') > 0); + assert.equal(true, outputData.indexOf('input__1') > 0); + assert.equal(true, outputData.indexOf('Source Sans Pro') > 0); + + done(); + } catch (err) { + done(err); + } + }); + }); + }); +}); + diff --git a/test/test-factory.js b/test/test-factory.js new file mode 100644 index 0000000..ec1719d --- /dev/null +++ b/test/test-factory.js @@ -0,0 +1,52 @@ +'use strict'; + +var u = require('underscore'); +var assert = require('assert'); +var asserts = require('../lib/node-ff-assets'); + +var checkFunctionResult = function(node,prefixParent) { + var nodeKeys = Object.keys(node); + for (var i = 0; i < nodeKeys.length; i++) { + var nodeKey = nodeKeys[i]; + var nodeValue = node[nodeKey]; + var prefix = prefixParent + '.'+nodeKey; + if (u.isFunction(nodeValue)) { + var result = null; + try { + result = nodeValue(); + } catch (err) { + result = function() {}; + } + it(prefix+' should return function', function() { + assert.equal(true, u.isFunction(result)); + }); + + } else if (u.isObject(nodeValue)) { + checkFunctionResult(nodeValue,prefix); + } + } +} + +describe('lib/factory.js', function() { + describe('check function paths', function() { + it('asserts.factory.assembler should not be undefined', function() { + assert.equal(false, asserts.factory.assembler === undefined); + }); + it('asserts.factory.assembler.constructor should not be undefined', function() { + assert.equal(false, asserts.factory.assembler.constructor === undefined); + }); + it('asserts.factory.assembler.event should not be undefined', function() { + assert.equal(false, asserts.factory.assembler.event === undefined); + }); + it('asserts.factory.builder should not be undefined', function() { + assert.equal(false, asserts.factory.builder === undefined); + }); + it('asserts.factory.lib should not be undefined', function() { + assert.equal(false, asserts.factory.lib === undefined); + }); + }); + describe('check function results', function() { + checkFunctionResult(asserts.factory,'asserts.factory'); + }); +}); + diff --git a/test/test-node-ff-assets.js b/test/test-node-ff-assets.js index c4522a0..7f8d39f 100644 --- a/test/test-node-ff-assets.js +++ b/test/test-node-ff-assets.js @@ -1,24 +1,38 @@ 'use strict'; var assert = require("assert"); -var asserts = require("../lib/node-ff-assets"); +var assets = require("../lib/node-ff-assets"); -describe('node-ff-assets', function() { - describe('export.* check undefined', function() { +describe('lib/node-ff-assets.js', function() { + describe('check exports', function() { it('asserts.factory should not be undefined', function() { - assert.equal(false, asserts.factory === undefined); + assert.equal(false, assets.factory === undefined); }); it('asserts.AssetAssembler should not be undefined', function() { - assert.equal(false, asserts.AssetAssembler === undefined); + assert.equal(false, assets.AssetAssembler === undefined); }); it('asserts.checkAssemblerConfig should not be undefined', function() { - assert.equal(false, asserts.checkAssemblerConfig === undefined); + assert.equal(false, assets.checkAssemblerConfig === undefined); }); it('asserts.AssetsBuilder should not be undefined', function() { - assert.equal(false, asserts.AssetsBuilder === undefined); + assert.equal(false, assets.AssetsBuilder === undefined); }); it('asserts.checkBuilderConfig should not be undefined', function() { - assert.equal(false, asserts.checkBuilderConfig === undefined); + assert.equal(false, assets.checkBuilderConfig === undefined); + }); + it('asserts.build should not be undefined', function() { + assert.equal(false, assets.build === undefined); + }); + }); + describe('asserts.build', function() { + it('check build missing config error', function() { + var err = null; + try { + assets.build(); + } catch (error) { + err = error; + } + assert.equal(false, err === undefined); }); }); });