From f85287a4443d52068fcc0cdb3754d2d775e91a4c Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 18 Sep 2012 01:22:10 -0700 Subject: [PATCH] Add "category" options unit tests to test-build.js. Former-commit-id: 0499317babeb422e88700edd0f1e46c1fa6196fd --- build.js | 28 +++++++++++--------- test/test-build.js | 64 +++++++++++++++++++++++++++++++--------------- 2 files changed, 59 insertions(+), 33 deletions(-) diff --git a/build.js b/build.js index bbaaa6d73..53a3736a7 100755 --- a/build.js +++ b/build.js @@ -303,8 +303,11 @@ ' (i.e. “amd”, “commonjs”, “global”, “node”, and “none”)', ' lodash iife=... Code to replace the immediately-invoked function expression that wraps Lo-Dash', ' (e.g. “!function(window,undefined){%output%}(this)”)', + ' lodash template=... The file path pattern used for matching template files to compile', + ' (e.g. `lodash template=path/to/templates/*.tmpl`)', '', - ' All arguments, except `legacy` with `csp`/`mobile`, may be combined.', + ' All arguments, except `legacy` with `csp` or `mobile`, may be combined.', + ' Unless specified by `-o` or `--output`, all files created are saved to the current working directory.', '', ' Options:', '', @@ -420,7 +423,7 @@ */ function getMethodsByCategory(source, category) { return allMethods.filter(function(methodName) { - return RegExp('@category ' + category + '\\b').test(matchFunction(source, methodName)); + return category && RegExp('@category ' + category + '\\b').test(matchFunction(source, methodName)); }); } @@ -493,21 +496,22 @@ * only real method names. * * @private + * @param {String} source The source to inspect. * @param {String} value The option to convert. * @returns {Array} Returns the new converted array. */ - function optionToMethodsArray(value) { + function optionToMethodsArray(source, value) { var methodNames = optionToArray(value); // convert categories to method names methodNames.forEach(function(category) { - push.apply(methodNames, getMethodsByCategory(category)); + push.apply(methodNames, getMethodsByCategory(source, category)); }); // convert aliases to real method names methodNames = methodNames.map(getRealName); - // remove nonexistent method names and duplicates + // remove nonexistent and duplicate method names return _.uniq(_.intersection(allMethods, methodNames)); } @@ -739,7 +743,7 @@ // used to report invalid command-line arguments var invalidArgs = _.reject(options.slice(options[0] == 'node' ? 2 : 0), function(value, index, options) { if (/^(?:-o|--output)$/.test(options[index - 1]) || - /^(?:category|exclude|exports|iife|include|minus|plus)=.*$/.test(value)) { + /^(?:category|exclude|exports|iife|include|minus|plus|template)=.*$/.test(value)) { return true; } return [ @@ -839,23 +843,23 @@ var minusMethods = options.reduce(function(result, value) { return /exclude|minus/.test(value) - ? _.union(result, optionToMethodsArray(value)) + ? _.union(result, optionToMethodsArray(source, value)) : result; }, []); var plusMethods = options.reduce(function(result, value) { return /plus/.test(value) - ? _.union(result, optionToMethodsArray(value)) + ? _.union(result, optionToMethodsArray(source, value)) : result; }, []); - // add methods explicitly + // add method names explicitly options.some(function(value) { return /include/.test(value) && - (buildMethods = getDependencies(optionToMethodsArray(value))); + (buildMethods = getDependencies(optionToMethodsArray(source, value))); }); - // add methods required by Backbone and Underscore builds + // add method names required by Backbone and Underscore builds if (isBackbone && !buildMethods) { buildMethods = getDependencies(backboneDependencies); } @@ -863,7 +867,7 @@ buildMethods = getDependencies(underscoreMethods); } - // add methods explicitly by category + // add method names by category options.some(function(value) { if (!/category/.test(value)) { return false; diff --git a/test/test-build.js b/test/test-build.js index 7fafca77f..1b54c5947 100644 --- a/test/test-build.js +++ b/test/test-build.js @@ -294,6 +294,31 @@ return realToAliasMap[funcName] || []; } + /** + * Gets the names of methods belonging to the given `category`. + * + * @private + * @param {String} category The category to filter by. + * @returns {Array} Returns a new array of method names belonging to the given category. + */ + function getMethodsByCategory(category) { + switch (category) { + case 'Arrays': + return arraysMethods.slice(); + case 'Chaining': + return chainingMethods.slice(); + case 'Collections': + return collectionsMethods.slice(); + case 'Functions': + return functionsMethods.slice(); + case 'Objects': + return objectsMethods.slice(); + case 'Utilities': + return utilityMethods.slice(); + } + return []; + } + /** * Gets the real name, not alias, of a given function name. * @@ -659,10 +684,10 @@ QUnit.module('minify underscore'); (function() { - var start = _.once(QUnit.start); + var source = fs.readFileSync(path.join(__dirname, '..', 'vendor', 'underscore', 'underscore.js'), 'utf8'), + start = _.once(QUnit.start); asyncTest('`node minify underscore.js`', function() { - var source = fs.readFileSync(path.join(__dirname, '..', 'vendor', 'underscore', 'underscore.js'), 'utf8'); minify(source, { 'silent': true, 'workingName': 'underscore.min', @@ -677,7 +702,7 @@ var underscore = context._ || {}; ok(_.isString(underscore.VERSION)); - ok(result.match(/\n/g).length < source.match(/\n/g).length); + ok(!/Lo-Dash/.test(result) && result.match(/\n/g).length < source.match(/\n/g).length); start(); } }); @@ -744,6 +769,7 @@ 'category=utilities', 'exclude=union,uniq,zip', 'include=each,filter,map', + 'include=once plus=bind,Chaining', 'category=collections,functions', 'underscore backbone', 'backbone legacy category=utilities minus=first,last', @@ -770,36 +796,26 @@ } catch(e) { console.log(e); } - + // add method names explicitly if (/include/.test(command)) { methodNames = command.match(/include=(\S*)/)[1].split(/, */); } + // add method names required by Backbone and Underscore builds if (/backbone/.test(command) && !methodNames) { methodNames = backboneDependencies.slice(); } if (/underscore/.test(command) && !methodNames) { methodNames = underscoreMethods.slice(); } - + // add method names explicitly by category if (/category/.test(command)) { + // resolve method names belonging to each category (case-insensitive) methodNames = command.match(/category=(\S*)/)[1].split(/, */).reduce(function(result, category) { - switch (category) { - case 'arrays': - return result.concat(arraysMethods); - case 'chaining': - return result.concat(chainingMethods); - case 'collections': - return result.concat(collectionsMethods); - case 'functions': - return result.concat(functionsMethods); - case 'objects': - return result.concat(objectsMethods); - case 'utilities': - return result.concat(utilityMethods); - } - return result; + var capitalized = category[0].toUpperCase() + category.toLowerCase().slice(1); + return result.concat(getMethodsByCategory(capitalized)); }, methodNames || []); } + // init `methodNames` if it hasn't been inited if (!methodNames) { methodNames = allMethods.slice(); } @@ -815,7 +831,13 @@ .concat(expandMethodNames(command.match(/exclude=(\S*)/)[1].split(/, */)))); } - methodNames = _.uniq(expandMethodNames(methodNames)); + // expand aliases and categories to real method names + methodNames = expandMethodNames(methodNames).reduce(function(result, methodName) { + return result.concat(methodName, getMethodsByCategory(methodName)); + }, []); + + // remove nonexistent and duplicate method names + methodNames = _.uniq(_.intersection(allMethods, expandMethodNames(methodNames))); var lodash = context._ || {}; methodNames.forEach(function(methodName) {