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,8 +2445,9 @@
}); });
} }
} }
// add `_.findWhere` // add Underscore's `_.findWhere`
if (_.contains(buildMethods, 'findWhere')) { if (_.contains(buildMethods, 'findWhere')) {
if (isUnderscore) {
source = source.replace(matchFunction(source, 'find'), function(match) { source = source.replace(matchFunction(source, 'find'), function(match) {
var indent = getIndent(match); var indent = getIndent(match);
return match && (match + [ return match && (match + [
@@ -2478,9 +2481,10 @@
'' ''
].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(backboneDependencies, methodName); return _.contains(
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;