Add "category" options unit tests to test-build.js.

Former-commit-id: 0499317babeb422e88700edd0f1e46c1fa6196fd
This commit is contained in:
John-David Dalton
2012-09-18 01:22:10 -07:00
parent 483bc9ad87
commit f85287a444
2 changed files with 59 additions and 33 deletions

View File

@@ -303,8 +303,11 @@
' (i.e. “amd”, “commonjs”, “global”, “node”, and “none”)', ' (i.e. “amd”, “commonjs”, “global”, “node”, and “none”)',
' lodash iife=... Code to replace the immediately-invoked function expression that wraps Lo-Dash', ' lodash iife=... Code to replace the immediately-invoked function expression that wraps Lo-Dash',
' (e.g. “!function(window,undefined){%output%}(this)”)', ' (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:', ' Options:',
'', '',
@@ -420,7 +423,7 @@
*/ */
function getMethodsByCategory(source, category) { function getMethodsByCategory(source, category) {
return allMethods.filter(function(methodName) { 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. * only real method names.
* *
* @private * @private
* @param {String} source The source to inspect.
* @param {String} value The option to convert. * @param {String} value The option to convert.
* @returns {Array} Returns the new converted array. * @returns {Array} Returns the new converted array.
*/ */
function optionToMethodsArray(value) { function optionToMethodsArray(source, value) {
var methodNames = optionToArray(value); var methodNames = optionToArray(value);
// convert categories to method names // convert categories to method names
methodNames.forEach(function(category) { methodNames.forEach(function(category) {
push.apply(methodNames, getMethodsByCategory(category)); push.apply(methodNames, getMethodsByCategory(source, category));
}); });
// convert aliases to real method names // convert aliases to real method names
methodNames = methodNames.map(getRealName); methodNames = methodNames.map(getRealName);
// remove nonexistent method names and duplicates // remove nonexistent and duplicate method names
return _.uniq(_.intersection(allMethods, methodNames)); return _.uniq(_.intersection(allMethods, methodNames));
} }
@@ -739,7 +743,7 @@
// used to report invalid command-line arguments // used to report invalid command-line arguments
var invalidArgs = _.reject(options.slice(options[0] == 'node' ? 2 : 0), function(value, index, options) { var invalidArgs = _.reject(options.slice(options[0] == 'node' ? 2 : 0), function(value, index, options) {
if (/^(?:-o|--output)$/.test(options[index - 1]) || 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 true;
} }
return [ return [
@@ -839,23 +843,23 @@
var minusMethods = options.reduce(function(result, value) { var minusMethods = options.reduce(function(result, value) {
return /exclude|minus/.test(value) return /exclude|minus/.test(value)
? _.union(result, optionToMethodsArray(value)) ? _.union(result, optionToMethodsArray(source, value))
: result; : result;
}, []); }, []);
var plusMethods = options.reduce(function(result, value) { var plusMethods = options.reduce(function(result, value) {
return /plus/.test(value) return /plus/.test(value)
? _.union(result, optionToMethodsArray(value)) ? _.union(result, optionToMethodsArray(source, value))
: result; : result;
}, []); }, []);
// add methods explicitly // add method names explicitly
options.some(function(value) { options.some(function(value) {
return /include/.test(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) { if (isBackbone && !buildMethods) {
buildMethods = getDependencies(backboneDependencies); buildMethods = getDependencies(backboneDependencies);
} }
@@ -863,7 +867,7 @@
buildMethods = getDependencies(underscoreMethods); buildMethods = getDependencies(underscoreMethods);
} }
// add methods explicitly by category // add method names by category
options.some(function(value) { options.some(function(value) {
if (!/category/.test(value)) { if (!/category/.test(value)) {
return false; return false;

View File

@@ -294,6 +294,31 @@
return realToAliasMap[funcName] || []; 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. * Gets the real name, not alias, of a given function name.
* *
@@ -659,10 +684,10 @@
QUnit.module('minify underscore'); QUnit.module('minify underscore');
(function() { (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() { asyncTest('`node minify underscore.js`', function() {
var source = fs.readFileSync(path.join(__dirname, '..', 'vendor', 'underscore', 'underscore.js'), 'utf8');
minify(source, { minify(source, {
'silent': true, 'silent': true,
'workingName': 'underscore.min', 'workingName': 'underscore.min',
@@ -677,7 +702,7 @@
var underscore = context._ || {}; var underscore = context._ || {};
ok(_.isString(underscore.VERSION)); 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(); start();
} }
}); });
@@ -744,6 +769,7 @@
'category=utilities', 'category=utilities',
'exclude=union,uniq,zip', 'exclude=union,uniq,zip',
'include=each,filter,map', 'include=each,filter,map',
'include=once plus=bind,Chaining',
'category=collections,functions', 'category=collections,functions',
'underscore backbone', 'underscore backbone',
'backbone legacy category=utilities minus=first,last', 'backbone legacy category=utilities minus=first,last',
@@ -770,36 +796,26 @@
} catch(e) { } catch(e) {
console.log(e); console.log(e);
} }
// add method names explicitly
if (/include/.test(command)) { if (/include/.test(command)) {
methodNames = command.match(/include=(\S*)/)[1].split(/, */); methodNames = command.match(/include=(\S*)/)[1].split(/, */);
} }
// add method names required by Backbone and Underscore builds
if (/backbone/.test(command) && !methodNames) { if (/backbone/.test(command) && !methodNames) {
methodNames = backboneDependencies.slice(); methodNames = backboneDependencies.slice();
} }
if (/underscore/.test(command) && !methodNames) { if (/underscore/.test(command) && !methodNames) {
methodNames = underscoreMethods.slice(); methodNames = underscoreMethods.slice();
} }
// add method names explicitly by category
if (/category/.test(command)) { if (/category/.test(command)) {
// resolve method names belonging to each category (case-insensitive)
methodNames = command.match(/category=(\S*)/)[1].split(/, */).reduce(function(result, category) { methodNames = command.match(/category=(\S*)/)[1].split(/, */).reduce(function(result, category) {
switch (category) { var capitalized = category[0].toUpperCase() + category.toLowerCase().slice(1);
case 'arrays': return result.concat(getMethodsByCategory(capitalized));
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;
}, methodNames || []); }, methodNames || []);
} }
// init `methodNames` if it hasn't been inited
if (!methodNames) { if (!methodNames) {
methodNames = allMethods.slice(); methodNames = allMethods.slice();
} }
@@ -815,7 +831,13 @@
.concat(expandMethodNames(command.match(/exclude=(\S*)/)[1].split(/, */)))); .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._ || {}; var lodash = context._ || {};
methodNames.forEach(function(methodName) { methodNames.forEach(function(methodName) {