From 40dad96b51f8449499d9d42b53cf35a70d5aba0b Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Sun, 21 Jun 2015 10:28:46 +0100 Subject: [PATCH] Extracts test helpers from tests. --- test/selectors/advanced-test.js | 25 +-------- test/selectors/simple-test.js | 66 ++--------------------- test/test-helper.js | 92 +++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 87 deletions(-) create mode 100644 test/test-helper.js diff --git a/test/selectors/advanced-test.js b/test/selectors/advanced-test.js index d1f8ed97..c26463b6 100644 --- a/test/selectors/advanced-test.js +++ b/test/selectors/advanced-test.js @@ -1,28 +1,5 @@ var vows = require('vows'); -var assert = require('assert'); -var CleanCSS = require('../../lib/clean'); - -function optimizerContext(group, specs, options) { - var context = {}; - options = options || {}; - options.shorthandCompacting = true; - options.restructuring = true; - - function optimized(target) { - return function (source) { - assert.equal(new CleanCSS(options).minify(source).styles, target); - }; - } - - for (var name in specs) { - context[group + ' - ' + name] = { - topic: specs[name][0], - optimized: optimized(specs[name][1]) - }; - } - - return context; -} +var optimizerContext = require('../test-helper').optimizerContext; vows.describe('advanced optimizer') .addBatch( diff --git a/test/selectors/simple-test.js b/test/selectors/simple-test.js index ee399fd6..a601d71a 100644 --- a/test/selectors/simple-test.js +++ b/test/selectors/simple-test.js @@ -1,69 +1,9 @@ var vows = require('vows'); -var assert = require('assert'); -var tokenize = require('../../lib/tokenizer/tokenize'); -var SimpleOptimizer = require('../../lib/selectors/simple'); -var Compatibility = require('../../lib/utils/compatibility'); -var addOptimizationMetadata = require('../../lib/selectors/optimization-metadata'); +var selectorContext = require('../test-helper').selectorContext; +var propertyContext = require('../test-helper').propertyContext; -function selectorContext(group, specs, options) { - var context = {}; - options = options || {}; - options.compatibility = new Compatibility(options.compatibility).toOptions(); - - function optimized(selectors) { - return function (source) { - var tokens = tokenize(source, { options: {} }); - new SimpleOptimizer(options).optimize(tokens); - - assert.deepEqual(tokens[0] ? tokens[0][1] : null, selectors); - }; - } - - for (var name in specs) { - context['selector - ' + group + ' - ' + name] = { - topic: specs[name][0], - optimized: optimized(specs[name][1]) - }; - } - - return context; -} - -function propertyContext(group, specs, options) { - var context = {}; - options = options || {}; - options.compatibility = new Compatibility(options.compatibility).toOptions(); - - function optimized(selectors) { - return function (source) { - var tokens = tokenize(source, { options: {} }); - addOptimizationMetadata(tokens); - new SimpleOptimizer(options).optimize(tokens); - - var value = tokens[0] ? - tokens[0][2].map(function (property) { - return typeof property == 'string' ? - property : - property.map(function (t) { return t[0]; }); - }) : - null; - - assert.deepEqual(value, selectors); - }; - } - - for (var name in specs) { - context['property - ' + group + ' - ' + name] = { - topic: specs[name][0], - optimized: optimized(specs[name][1]) - }; - } - - return context; -} - -vows.describe(SimpleOptimizer) +vows.describe('simple optimizations') .addBatch( selectorContext('default', { 'optimized': [ diff --git a/test/test-helper.js b/test/test-helper.js new file mode 100644 index 00000000..0f3376bc --- /dev/null +++ b/test/test-helper.js @@ -0,0 +1,92 @@ +var assert = require('assert'); + +var CleanCSS = require('../lib/clean'); +var tokenize = require('../lib/tokenizer/tokenize'); +var SimpleOptimizer = require('../lib/selectors/simple'); +var Compatibility = require('../lib/utils/compatibility'); +var addOptimizationMetadata = require('../lib/selectors/optimization-metadata'); + +function optimizerContext(group, specs, options) { + var context = {}; + options = options || {}; + options.shorthandCompacting = true; + options.restructuring = true; + + function optimized(target) { + return function (source) { + assert.equal(new CleanCSS(options).minify(source).styles, target); + }; + } + + for (var name in specs) { + context[group + ' - ' + name] = { + topic: specs[name][0], + optimized: optimized(specs[name][1]) + }; + } + + return context; +} + +function selectorContext(group, specs, options) { + var context = {}; + options = options || {}; + options.compatibility = new Compatibility(options.compatibility).toOptions(); + + function optimized(selectors) { + return function (source) { + var tokens = tokenize(source, { options: {} }); + new SimpleOptimizer(options).optimize(tokens); + + assert.deepEqual(tokens[0] ? tokens[0][1] : null, selectors); + }; + } + + for (var name in specs) { + context['selector - ' + group + ' - ' + name] = { + topic: specs[name][0], + optimized: optimized(specs[name][1]) + }; + } + + return context; +} + +function propertyContext(group, specs, options) { + var context = {}; + options = options || {}; + options.compatibility = new Compatibility(options.compatibility).toOptions(); + + function optimized(selectors) { + return function (source) { + var tokens = tokenize(source, { options: {} }); + addOptimizationMetadata(tokens); + new SimpleOptimizer(options).optimize(tokens); + + var value = tokens[0] ? + tokens[0][2].map(function (property) { + return typeof property == 'string' ? + property : + property.map(function (t) { return t[0]; }); + }) : + null; + + assert.deepEqual(value, selectors); + }; + } + + for (var name in specs) { + context['property - ' + group + ' - ' + name] = { + topic: specs[name][0], + optimized: optimized(specs[name][1]) + }; + } + + return context; +} + +module.exports = { + optimizerContext: optimizerContext, + selectorContext: selectorContext, + propertyContext: propertyContext +}; -- 2.34.1