Add unit tests to ensure non-underscore builds may include _.chain and _.findWhere methods.

Former-commit-id: 91a4ea5114a45bc5038a37d7ff4ea4b6212182ca
This commit is contained in:
John-David Dalton
2013-04-07 22:55:52 -07:00
parent f1d3df1ec0
commit 1faa5a80e4
3 changed files with 98 additions and 80 deletions

View File

@@ -182,7 +182,7 @@
// method used by the `backbone` and `underscore` builds // method used by the `backbone` and `underscore` builds
'chain': ['value'], 'chain': ['value'],
'findWhere': ['where'] 'findWhere': ['find']
}; };
/** Used to inline `iteratorTemplate` */ /** Used to inline `iteratorTemplate` */
@@ -199,8 +199,11 @@
'useKeys' 'useKeys'
]; ];
/** List of all Lo-Dash methods */ /** List of all methods */
var allMethods = _.without(_.keys(dependencyMap), 'chain', 'findWhere'); var allMethods = _.without(_.keys(dependencyMap));
/** List of Lo-Dash methods */
var lodashMethods = _.without(allMethods, 'chain', 'findWhere');
/** List of Backbone's Lo-Dash dependencies */ /** List of Backbone's Lo-Dash dependencies */
var backboneDependencies = [ var backboneDependencies = [
@@ -277,10 +280,8 @@
'unzip' 'unzip'
]; ];
/** List of methods used by Underscore */ /** List of Underscore methods */
var underscoreMethods = _.without var underscoreMethods = _.without.apply(_, [allMethods].concat(lodashOnlyMethods));
.apply(_, [allMethods].concat(lodashOnlyMethods))
.concat('chain', 'findWhere');
/** List of ways to export the `lodash` function */ /** List of ways to export the `lodash` function */
var exportsAll = [ var exportsAll = [
@@ -1740,6 +1741,7 @@
if (isUnderscore) { if (isUnderscore) {
dependencyMap.contains = _.without(dependencyMap.contains, 'isString'); dependencyMap.contains = _.without(dependencyMap.contains, 'isString');
dependencyMap.createCallback = _.without(dependencyMap.createCallback, 'isEqual'); dependencyMap.createCallback = _.without(dependencyMap.createCallback, 'isEqual');
dependencyMap.findWhere = ['where'];
dependencyMap.flatten = _.without(dependencyMap.flatten, 'createCallback'); dependencyMap.flatten = _.without(dependencyMap.flatten, 'createCallback');
dependencyMap.isEmpty = ['isArray', 'isString']; dependencyMap.isEmpty = ['isArray', 'isString'];
dependencyMap.isEqual = _.without(dependencyMap.isEqual, 'forIn', 'isArguments'); dependencyMap.isEqual = _.without(dependencyMap.isEqual, 'forIn', 'isArguments');
@@ -1815,7 +1817,7 @@
}, []))); }, [])));
} }
if (!result) { if (!result) {
result = allMethods.slice(); result = lodashMethods.slice();
} }
if (plusMethods.length) { if (plusMethods.length) {
result = _.union(result, getDependencies(plusMethods)); result = _.union(result, getDependencies(plusMethods));
@@ -2443,44 +2445,46 @@
}); });
} }
} }
// add `_.findWhere` // add Underscore's `_.findWhere`
if (_.contains(buildMethods, 'findWhere')) { if (_.contains(buildMethods, 'findWhere')) {
source = source.replace(matchFunction(source, 'find'), function(match) { if (isUnderscore) {
var indent = getIndent(match); source = source.replace(matchFunction(source, 'find'), function(match) {
return match && (match + [ var indent = getIndent(match);
'', return match && (match + [
'/**', '',
' * Examines each element in a `collection`, returning the first that', '/**',
' * has the given `properties`. When checking `properties`, this method', ' * Examines each element in a `collection`, returning the first that',
' * performs a deep comparison between values to determine if they are', ' * has the given `properties`. When checking `properties`, this method',
' * equivalent to each other.', ' * performs a deep comparison between values to determine if they are',
' *', ' * equivalent to each other.',
' * @static', ' *',
' * @memberOf _', ' * @static',
' * @category Collections', ' * @memberOf _',
' * @param {Array|Object|String} collection The collection to iterate over.', ' * @category Collections',
' * @param {Object} properties The object of property values to filter by.', ' * @param {Array|Object|String} collection The collection to iterate over.',
' * @returns {Mixed} Returns the found element, else `undefined`.', ' * @param {Object} properties The object of property values to filter by.',
' * @example', ' * @returns {Mixed} Returns the found element, else `undefined`.',
' *', ' * @example',
' * var food = [', ' *',
" * { 'name': 'apple', 'organic': false, 'type': 'fruit' },", ' * var food = [',
" * { 'name': 'banana', 'organic': true, 'type': 'fruit' },", " * { 'name': 'apple', 'organic': false, 'type': 'fruit' },",
" * { 'name': 'beet', 'organic': false, 'type': 'vegetable' }", " * { 'name': 'banana', 'organic': true, 'type': 'fruit' },",
' * ];', " * { 'name': 'beet', 'organic': false, 'type': 'vegetable' }",
' *', ' * ];',
" * _.findWhere(food, { 'type': 'vegetable' });", ' *',
" * // => { 'name': 'beet', 'organic': false, 'type': 'vegetable' }", " * _.findWhere(food, { 'type': 'vegetable' });",
' */', " * // => { 'name': 'beet', 'organic': false, 'type': 'vegetable' }",
'function findWhere(object, properties) {', ' */',
' return where(object, properties, true);', 'function findWhere(object, properties) {',
'}', ' return where(object, properties, true);',
'' '}',
].join('\n' + indent)); ''
}); ].join('\n' + indent));
});
}
source = source.replace(getMethodAssignments(source), function(match) { source = source.replace(getMethodAssignments(source), function(match) {
return match.replace(/^( *)lodash.find *=.+/m, '$&\n$1lodash.findWhere = findWhere;'); var methodName = isUnderscore ? 'findWhere' : 'find';
return match.replace(/^( *)lodash.find *=.+/m, '$&\n$1lodash.findWhere = ' + methodName + ';');
}); });
} }
// add Underscore's chaining methods // add Underscore's chaining methods

View File

@@ -90,10 +90,13 @@
}; };
/** List of all Lo-Dash methods */ /** List of all Lo-Dash methods */
var allMethods = _.functions(_).filter(function(methodName) { var lodashMethods = _.functions(_).filter(function(methodName) {
return !/^_/.test(methodName); return !/^_/.test(methodName);
}); });
/** List of all methods */
var allMethods = lodashMethods.concat('chain', 'findWhere');
/** List of "Arrays" category methods */ /** List of "Arrays" category methods */
var arraysMethods = [ var arraysMethods = [
'compact', 'compact',
@@ -125,6 +128,7 @@
/** List of "Chaining" category methods */ /** List of "Chaining" category methods */
var chainingMethods = [ var chainingMethods = [
'chain',
'tap', 'tap',
'value' 'value'
]; ];
@@ -315,10 +319,8 @@
'unzip' 'unzip'
]; ];
/** List of methods used by Underscore */ /** List of Underscore methods */
var underscoreMethods = _.without var underscoreMethods = _.without.apply(_, [allMethods].concat(lodashOnlyMethods));
.apply(_, [allMethods].concat(lodashOnlyMethods))
.concat('chain', 'findWhere');
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
@@ -804,14 +806,15 @@
(function() { (function() {
var commands = [ var commands = [
'backbone', 'backbone',
'underscore' 'underscore',
'modern plus=chain'
]; ];
commands.forEach(function(command) { commands.forEach(function(command) {
asyncTest('`lodash ' + command +'`', function() { asyncTest('`lodash ' + command +'`', function() {
var start = _.after(2, _.once(QUnit.start)); var start = _.after(2, _.once(QUnit.start));
build(['-s', command], function(data) { build(['-s'].concat(command.split(' ')), function(data) {
var basename = path.basename(data.outputPath, '.js'), var basename = path.basename(data.outputPath, '.js'),
context = createContext(); context = createContext();
@@ -843,6 +846,37 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
QUnit.module('modifiers with findWhere');
(function() {
var commands = [
'underscore include=findWhere',
'modern include=findWhere',
'plus=findWhere'
];
commands.forEach(function(command) {
asyncTest('`lodash ' + command + '`', function() {
var start = _.after(2, _.once(QUnit.start));
build(['-s'].concat(command.split(' ')), function(data) {
var basename = path.basename(data.outputPath, '.js'),
context = createContext();
vm.runInContext(data.source, context);
var lodash = context._;
var collection = [{ 'a': 1 }, { 'a': 1 }];
deepEqual(lodash.findWhere(collection, { 'a': 1 }), collection[0], '_.findWhere: ' + basename);
start();
});
});
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('underscore modifier'); QUnit.module('underscore modifier');
(function() { (function() {
@@ -985,23 +1019,6 @@
}); });
}); });
asyncTest('`lodash underscore include=findWhere`', function() {
var start = _.after(2, _.once(QUnit.start));
build(['-s', 'underscore', 'include=findWhere'], function(data) {
var basename = path.basename(data.outputPath, '.js'),
context = createContext();
vm.runInContext(data.source, context);
var lodash = context._;
var collection = [{ 'a': 1 }, { 'a': 1 }];
deepEqual(lodash.findWhere(collection, { 'a': 1 }), collection[0], '_.findWhere: ' + basename);
start();
});
});
asyncTest('`lodash underscore include=partial`', function() { asyncTest('`lodash underscore include=partial`', function() {
var start = _.after(2, _.once(QUnit.start)); var start = _.after(2, _.once(QUnit.start));
@@ -1412,7 +1429,7 @@
methodNames = (methodNames || []).concat(command.match(/category=(\S*)/)[1].split(/, */).map(capitalize)); methodNames = (methodNames || []).concat(command.match(/category=(\S*)/)[1].split(/, */).map(capitalize));
} }
if (!methodNames) { if (!methodNames) {
methodNames = allMethods.slice(); methodNames = lodashMethods.slice();
} }
if (/plus/.test(command)) { if (/plus/.test(command)) {
methodNames = methodNames.concat(command.match(/plus=(\S*)/)[1].split(/, */)); methodNames = methodNames.concat(command.match(/plus=(\S*)/)[1].split(/, */));
@@ -1429,16 +1446,13 @@
var result = getMethodsByCategory(category); var result = getMethodsByCategory(category);
// limit category methods to those available for specific builds // limit category methods to those available for specific builds
if (isBackbone) { result = result.filter(function(methodName) {
result = result.filter(function(methodName) { return _.contains(
return _.contains(backboneDependencies, methodName); isBackbone ? backboneDependencies :
}); isUnderscore ? underscoreMethods :
} lodashMethods, methodName
else if (isUnderscore) { );
result = result.filter(function(methodName) { });
return _.contains(underscoreMethods, methodName);
});
}
if (result.length) { if (result.length) {
methodNames = _.without(methodNames, category); methodNames = _.without(methodNames, category);
push.apply(methodNames, result); push.apply(methodNames, result);

View File

@@ -3205,7 +3205,7 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
QUnit.module('"Arrays" methods'); QUnit.module('"Arrays" category methods');
(function() { (function() {
var args = arguments; var args = arguments;