test-build.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. #!/usr/bin/env node
  2. ;(function(undefined) {
  3. 'use strict';
  4. /** Load modules */
  5. var fs = require('fs'),
  6. path = require('path'),
  7. vm = require('vm'),
  8. build = require('../build.js'),
  9. minify = require('../build/minify'),
  10. _ = require('../lodash.js');
  11. /** The unit testing framework */
  12. var QUnit = global.QUnit = require('../vendor/qunit/qunit/qunit.js');
  13. require('../vendor/qunit-clib/qunit-clib.js');
  14. /** Used to associate aliases with their real names */
  15. var aliasToRealMap = {
  16. 'all': 'every',
  17. 'any': 'some',
  18. 'collect': 'map',
  19. 'detect': 'find',
  20. 'drop': 'rest',
  21. 'each': 'forEach',
  22. 'foldl': 'reduce',
  23. 'foldr': 'reduceRight',
  24. 'head': 'first',
  25. 'include': 'contains',
  26. 'inject': 'reduce',
  27. 'methods': 'functions',
  28. 'select': 'filter',
  29. 'tail': 'rest',
  30. 'take': 'first',
  31. 'unique': 'uniq'
  32. };
  33. /** Used to associate real names with their aliases */
  34. var realToAliasMap = {
  35. 'contains': ['include'],
  36. 'every': ['all'],
  37. 'filter': ['select'],
  38. 'find': ['detect'],
  39. 'first': ['head', 'take'],
  40. 'forEach': ['each'],
  41. 'functions': ['methods'],
  42. 'map': ['collect'],
  43. 'reduce': ['foldl', 'inject'],
  44. 'reduceRight': ['foldr'],
  45. 'rest': ['drop', 'tail'],
  46. 'some': ['any'],
  47. 'uniq': ['unique']
  48. };
  49. /** List of all Lo-Dash methods */
  50. var allMethods = _.functions(_).filter(function(methodName) {
  51. return !/^_/.test(methodName);
  52. });
  53. /** List of "Arrays" category methods */
  54. var arraysMethods = [
  55. 'compact',
  56. 'difference',
  57. 'drop',
  58. 'first',
  59. 'flatten',
  60. 'head',
  61. 'indexOf',
  62. 'initial',
  63. 'intersection',
  64. 'last',
  65. 'lastIndexOf',
  66. 'max',
  67. 'min',
  68. 'object',
  69. 'range',
  70. 'rest',
  71. 'shuffle',
  72. 'sortedIndex',
  73. 'tail',
  74. 'take',
  75. 'union',
  76. 'uniq',
  77. 'unique',
  78. 'without',
  79. 'zip'
  80. ];
  81. /** List of "Chaining" category methods */
  82. var chainingMethods = [
  83. 'chain',
  84. 'mixin',
  85. 'tap',
  86. 'value'
  87. ];
  88. /** List of "Collections" category methods */
  89. var collectionsMethods = [
  90. 'all',
  91. 'any',
  92. 'collect',
  93. 'contains',
  94. 'countBy',
  95. 'detect',
  96. 'each',
  97. 'every',
  98. 'filter',
  99. 'find',
  100. 'foldl',
  101. 'foldr',
  102. 'forEach',
  103. 'groupBy',
  104. 'include',
  105. 'inject',
  106. 'invoke',
  107. 'map',
  108. 'pluck',
  109. 'reduce',
  110. 'reduceRight',
  111. 'reject',
  112. 'select',
  113. 'size',
  114. 'some',
  115. 'sortBy',
  116. 'toArray',
  117. 'where'
  118. ];
  119. /** List of "Functions" category methods */
  120. var functionsMethods = [
  121. 'after',
  122. 'bind',
  123. 'bindAll',
  124. 'compose',
  125. 'debounce',
  126. 'defer',
  127. 'delay',
  128. 'memoize',
  129. 'once',
  130. 'partial',
  131. 'throttle',
  132. 'wrap'
  133. ];
  134. /** List of "Objects" category methods */
  135. var objectsMethods = [
  136. 'clone',
  137. 'defaults',
  138. 'extend',
  139. 'forIn',
  140. 'forOwn',
  141. 'functions',
  142. 'has',
  143. 'invert',
  144. 'isArguments',
  145. 'isArray',
  146. 'isBoolean',
  147. 'isDate',
  148. 'isElement',
  149. 'isEmpty',
  150. 'isEqual',
  151. 'isFinite',
  152. 'isFunction',
  153. 'isNaN',
  154. 'isNull',
  155. 'isNumber',
  156. 'isObject',
  157. 'isRegExp',
  158. 'isString',
  159. 'isUndefined',
  160. 'keys',
  161. 'methods',
  162. 'merge',
  163. 'omit',
  164. 'pairs',
  165. 'pick',
  166. 'values'
  167. ];
  168. /** List of "Utilities" category methods */
  169. var utilityMethods = [
  170. 'escape',
  171. 'identity',
  172. 'noConflict',
  173. 'random',
  174. 'result',
  175. 'template',
  176. 'times',
  177. 'unescape',
  178. 'uniqueId'
  179. ];
  180. /** List of Backbone's Lo-Dash dependencies */
  181. var backboneDependencies = [
  182. 'bind',
  183. 'bindAll',
  184. 'clone',
  185. 'contains',
  186. 'escape',
  187. 'every',
  188. 'extend',
  189. 'filter',
  190. 'find',
  191. 'first',
  192. 'forEach',
  193. 'groupBy',
  194. 'has',
  195. 'indexOf',
  196. 'initial',
  197. 'invoke',
  198. 'isArray',
  199. 'isEmpty',
  200. 'isEqual',
  201. 'isFunction',
  202. 'isObject',
  203. 'isRegExp',
  204. 'keys',
  205. 'last',
  206. 'lastIndexOf',
  207. 'map',
  208. 'max',
  209. 'min',
  210. 'mixin',
  211. 'reduce',
  212. 'reduceRight',
  213. 'reject',
  214. 'rest',
  215. 'result',
  216. 'shuffle',
  217. 'size',
  218. 'some',
  219. 'sortBy',
  220. 'sortedIndex',
  221. 'toArray',
  222. 'uniqueId',
  223. 'without'
  224. ];
  225. /** List of methods used by Underscore */
  226. var underscoreMethods = _.without.apply(_, [allMethods].concat([
  227. 'countBy',
  228. 'forIn',
  229. 'forOwn',
  230. 'invert',
  231. 'merge',
  232. 'object',
  233. 'omit',
  234. 'pairs',
  235. 'partial',
  236. 'random',
  237. 'unescape',
  238. 'where'
  239. ]));
  240. /*--------------------------------------------------------------------------*/
  241. /**
  242. * Creates a context object to use with `vm.runInContext`.
  243. *
  244. * @private
  245. * @returns {Object} Returns a new context object.
  246. */
  247. function createContext() {
  248. return vm.createContext({
  249. 'clearTimeout': clearTimeout,
  250. 'setTimeout': setTimeout
  251. });
  252. }
  253. /**
  254. * Expands a list of method names to include real and alias names.
  255. *
  256. * @private
  257. * @param {Array} methodNames The array of method names to expand.
  258. * @returns {Array} Returns a new array of expanded method names.
  259. */
  260. function expandMethodNames(methodNames) {
  261. return methodNames.reduce(function(result, methodName) {
  262. var realName = getRealName(methodName);
  263. result.push.apply(result, [realName].concat(getAliases(realName)));
  264. return result;
  265. }, []);
  266. }
  267. /**
  268. * Gets the aliases associated with a given function name.
  269. *
  270. * @private
  271. * @param {String} funcName The name of the function to get aliases for.
  272. * @returns {Array} Returns an array of aliases.
  273. */
  274. function getAliases(funcName) {
  275. return realToAliasMap[funcName] || [];
  276. }
  277. /**
  278. * Gets the real name, not alias, of a given function name.
  279. *
  280. * @private
  281. * @param {String} funcName The name of the function to resolve.
  282. * @returns {String} Returns the real name.
  283. */
  284. function getRealName(funcName) {
  285. return aliasToRealMap[funcName] || funcName;
  286. }
  287. /**
  288. * Tests if a given method on the `lodash` object can be called successfully.
  289. *
  290. * @private
  291. * @param {Object} lodash The built Lo-Dash object.
  292. * @param {String} methodName The name of the Lo-Dash method to test.
  293. * @param {String} message The unit test message.
  294. */
  295. function testMethod(lodash, methodName, message) {
  296. var pass = true,
  297. array = [['a', 1], ['b', 2], ['c', 3]],
  298. object = { 'a': 1, 'b': 2, 'c': 3 },
  299. noop = function() {},
  300. string = 'abc',
  301. func = lodash[methodName];
  302. try {
  303. if (arraysMethods.indexOf(methodName) > -1) {
  304. if (/(?:indexOf|sortedIndex|without)$/i.test(methodName)) {
  305. func(array, string);
  306. } else if (/^(?:difference|intersection|union|uniq|zip)/.test(methodName)) {
  307. func(array, array);
  308. } else if (methodName == 'range') {
  309. func(2, 4);
  310. } else {
  311. func(array);
  312. }
  313. }
  314. else if (chainingMethods.indexOf(methodName) > -1) {
  315. if (methodName == 'chain') {
  316. lodash.chain(array);
  317. lodash(array).chain();
  318. }
  319. else if (methodName == 'mixin') {
  320. lodash.mixin({});
  321. }
  322. else {
  323. lodash(array)[methodName](noop);
  324. }
  325. }
  326. else if (collectionsMethods.indexOf(methodName) > -1) {
  327. if (/^(?:count|group|sort)By$/.test(methodName)) {
  328. func(array, noop);
  329. func(array, string);
  330. func(object, noop);
  331. func(object, string);
  332. }
  333. else if (/^(?:size|toArray)$/.test(methodName)) {
  334. func(array);
  335. func(object);
  336. }
  337. else if (methodName == 'invoke') {
  338. func(array, 'slice');
  339. func(object, 'toFixed');
  340. }
  341. else if (methodName == 'where') {
  342. func(array, object);
  343. func(object, object);
  344. }
  345. else {
  346. func(array, noop, object);
  347. func(object, noop, object);
  348. }
  349. }
  350. else if (functionsMethods.indexOf(methodName) > -1) {
  351. if (methodName == 'after') {
  352. func(1, noop);
  353. } else if (/^(?:bind|partial)$/.test(methodName)) {
  354. func(noop, object, array, string);
  355. } else if (/^(?:compose|memoize|wrap)$/.test(methodName)) {
  356. func(noop, noop);
  357. } else if (/^(?:debounce|throttle)$/.test(methodName)) {
  358. func(noop, 100);
  359. } else if (methodName == 'bindAll') {
  360. func({ 'noop': noop });
  361. } else {
  362. func(noop);
  363. }
  364. }
  365. else if (objectsMethods.indexOf(methodName) > -1) {
  366. if (methodName == 'clone') {
  367. func(object);
  368. func(object, true);
  369. }
  370. else if (/^(?:defaults|extend|merge)$/.test(methodName)) {
  371. func({}, object);
  372. } else if (/^(?:forIn|forOwn)$/.test(methodName)) {
  373. func(object, noop);
  374. } else if (/^(?:omit|pick)$/.test(methodName)) {
  375. func(object, 'b');
  376. } else if (methodName == 'has') {
  377. func(object, string);
  378. } else {
  379. func(object);
  380. }
  381. }
  382. else if (utilityMethods.indexOf(methodName) > -1) {
  383. if (methodName == 'result') {
  384. func(object, 'b');
  385. } else if (methodName == 'times') {
  386. func(2, noop, object);
  387. } else {
  388. func(string, object);
  389. }
  390. }
  391. }
  392. catch(e) {
  393. console.log(e);
  394. pass = false;
  395. }
  396. equal(pass, true, methodName + ': ' + message);
  397. }
  398. /*--------------------------------------------------------------------------*/
  399. QUnit.module('lodash build');
  400. (function() {
  401. var commands = [
  402. 'backbone',
  403. 'csp',
  404. 'legacy',
  405. 'mobile',
  406. 'strict',
  407. 'underscore',
  408. 'category=arrays',
  409. 'category=chaining',
  410. 'category=collections',
  411. 'category=functions',
  412. 'category=objects',
  413. 'category=utilities',
  414. 'exclude=union,uniq,zip',
  415. 'include=each,filter,map',
  416. 'category=collections,functions',
  417. 'underscore backbone',
  418. 'backbone legacy category=utilities exclude=first,last',
  419. 'underscore mobile strict category=functions exports=amd,global include=pick,uniq',
  420. ]
  421. .concat(
  422. allMethods.map(function(methodName) {
  423. return 'include=' + methodName;
  424. })
  425. );
  426. commands.forEach(function(command) {
  427. var start = _.after(2, _.once(QUnit.start));
  428. asyncTest('`lodash ' + command +'`', function() {
  429. build(['--silent'].concat(command.split(' ')), function(source, filepath) {
  430. var basename = path.basename(filepath, '.js'),
  431. context = createContext(),
  432. methodNames = [];
  433. try {
  434. vm.runInContext(source, context);
  435. } catch(e) { }
  436. if (/underscore/.test(command)) {
  437. methodNames = underscoreMethods;
  438. }
  439. if (/backbone/.test(command)) {
  440. methodNames = backboneDependencies;
  441. }
  442. if (/include/.test(command)) {
  443. methodNames = methodNames.concat(command.match(/include=(\S*)/)[1].split(/, */));
  444. }
  445. if (/category/.test(command)) {
  446. methodNames = command.match(/category=(\S*)/)[1].split(/, */).reduce(function(result, category) {
  447. switch (category) {
  448. case 'arrays':
  449. return result.concat(arraysMethods);
  450. case 'chaining':
  451. return result.concat(chainingMethods);
  452. case 'collections':
  453. return result.concat(collectionsMethods);
  454. case 'functions':
  455. return result.concat(functionsMethods);
  456. case 'objects':
  457. return result.concat(objectsMethods);
  458. case 'utilities':
  459. return result.concat(utilityMethods);
  460. }
  461. return result;
  462. }, methodNames);
  463. }
  464. if (!methodNames.length) {
  465. methodNames = allMethods;
  466. }
  467. if (/exclude/.test(command)) {
  468. methodNames = _.without.apply(_, [methodNames].concat(
  469. expandMethodNames(command.match(/exclude=(\S*)/)[1].split(/, */))
  470. ));
  471. } else {
  472. methodNames = expandMethodNames(methodNames);
  473. }
  474. var lodash = context._ || {};
  475. methodNames = _.unique(methodNames);
  476. methodNames.forEach(function(methodName) {
  477. testMethod(lodash, methodName, basename);
  478. });
  479. start();
  480. });
  481. });
  482. });
  483. }());
  484. /*--------------------------------------------------------------------------*/
  485. QUnit.module('strict modifier');
  486. (function() {
  487. var object = Object.create(Object.prototype, {
  488. 'a': { 'value': _.identify },
  489. 'b': { 'value': null }
  490. });
  491. ['non-strict', 'strict'].forEach(function(strictMode, index) {
  492. var start = _.after(2, _.once(QUnit.start));
  493. asyncTest(strictMode + ' should ' + (index ? 'error': 'silently fail') + ' attempting to overwrite read-only properties', function() {
  494. var commands = ['-s', 'include=bindAll,defaults,extend'];
  495. if (index) {
  496. commands.push('strict');
  497. }
  498. build(commands, function(source, filepath) {
  499. var basename = path.basename(filepath, '.js'),
  500. context = createContext(),
  501. pass = !index;
  502. vm.runInContext(source, context);
  503. var lodash = context._;
  504. try {
  505. lodash.bindAll(object);
  506. lodash.extend(object, { 'a': 1 });
  507. lodash.defaults(object, { 'b': 2 });
  508. } catch(e) {
  509. pass = !!index;
  510. }
  511. equal(pass, true, basename);
  512. start();
  513. });
  514. });
  515. });
  516. }());
  517. /*--------------------------------------------------------------------------*/
  518. QUnit.module('underscore modifier');
  519. (function() {
  520. var start = _.once(QUnit.start);
  521. asyncTest('should not have deep clone', function() {
  522. build(['-s', 'underscore'], function(source, filepath) {
  523. var array = [{ 'a': 1 }],
  524. basename = path.basename(filepath, '.js'),
  525. context = createContext();
  526. vm.runInContext(source, context);
  527. var lodash = context._;
  528. ok(lodash.clone(array, true)[0] === array[0], basename);
  529. start();
  530. });
  531. });
  532. }());
  533. /*--------------------------------------------------------------------------*/
  534. QUnit.module('exports command');
  535. (function() {
  536. var commands = [
  537. 'exports=amd',
  538. 'exports=commonjs',
  539. 'exports=global',
  540. 'exports=node',
  541. 'exports=none'
  542. ];
  543. commands.forEach(function(command, index) {
  544. var start = _.after(2, _.once(QUnit.start));
  545. asyncTest('`lodash ' + command +'`', function() {
  546. build(['-s', command], function(source, filepath) {
  547. var basename = path.basename(filepath, '.js'),
  548. context = createContext(),
  549. pass = false;
  550. switch(index) {
  551. case 0:
  552. context.define = function(fn) {
  553. pass = true;
  554. context._ = fn();
  555. };
  556. context.define.amd = {};
  557. vm.runInContext(source, context);
  558. ok(pass, basename);
  559. break;
  560. case 1:
  561. context.exports = {};
  562. vm.runInContext(source, context);
  563. ok(context._ === undefined, basename);
  564. ok(_.isFunction(context.exports._), basename)
  565. break;
  566. case 2:
  567. vm.runInContext(source, context);
  568. ok(_.isFunction(context._), basename);
  569. break;
  570. case 3:
  571. context.exports = {};
  572. context.module = { 'exports': context.exports };
  573. vm.runInContext(source, context);
  574. ok(context._ === undefined, basename);
  575. ok(_.isFunction(context.module.exports), basename);
  576. break;
  577. case 4:
  578. vm.runInContext(source, context);
  579. ok(context._ === undefined, basename);
  580. }
  581. start();
  582. });
  583. });
  584. });
  585. }());
  586. /*--------------------------------------------------------------------------*/
  587. QUnit.module('iife command');
  588. (function() {
  589. var start = _.after(2, _.once(QUnit.start));
  590. asyncTest('`lodash iife=...`', function() {
  591. build(['-s', 'iife=!function(window,undefined){%output%}(this)'], function(source, filepath) {
  592. var basename = path.basename(filepath, '.js'),
  593. context = createContext();
  594. try {
  595. vm.runInContext(source, context);
  596. } catch(e) { }
  597. var lodash = context._ || {};
  598. ok(_.isString(lodash.VERSION), basename);
  599. ok(/!function/.test(source), basename);
  600. start();
  601. });
  602. });
  603. }());
  604. /*--------------------------------------------------------------------------*/
  605. QUnit.module('output options');
  606. (function() {
  607. ['-o a.js', '--output a.js'].forEach(function(command, index) {
  608. var start = _.once(QUnit.start);
  609. asyncTest('`lodash ' + command +'`', function() {
  610. build(['-s'].concat(command.split(' ')), function(source, filepath) {
  611. equal(filepath, 'a.js', command);
  612. start();
  613. });
  614. });
  615. });
  616. }());
  617. /*--------------------------------------------------------------------------*/
  618. QUnit.module('stdout options');
  619. (function() {
  620. ['-c', '--stdout'].forEach(function(command, index) {
  621. var descriptor = Object.getOwnPropertyDescriptor(global, 'console'),
  622. start = _.once(QUnit.start);
  623. asyncTest('`lodash ' + command +'`', function() {
  624. build([command, 'exports=', 'include='], function(source, filepath) {
  625. equal(source, '');
  626. equal(arguments.length, 1);
  627. start();
  628. });
  629. });
  630. });
  631. }());
  632. /*--------------------------------------------------------------------------*/
  633. QUnit.module('minify underscore');
  634. (function() {
  635. var start = _.once(QUnit.start);
  636. asyncTest('`node minify underscore.js`', function() {
  637. var source = fs.readFileSync(path.join(__dirname, '..', 'vendor', 'underscore', 'underscore.js'), 'utf8');
  638. minify(source, {
  639. 'silent': true,
  640. 'workingName': 'underscore.min',
  641. 'onComplete': function(result) {
  642. var context = createContext();
  643. try {
  644. vm.runInContext(result, context);
  645. } catch(e) { }
  646. var underscore = context._ || {};
  647. ok(_.isString(underscore.VERSION));
  648. ok(result.match(/\n/g).length < source.match(/\n/g).length);
  649. start();
  650. }
  651. });
  652. });
  653. }());
  654. }());