test-ui.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. ;(function(window) {
  2. 'use strict';
  3. /** The base path of the lodash builds. */
  4. var basePath = '../';
  5. /** The lodash build to load. */
  6. var build = (build = /build=([^&]+)/.exec(location.search)) && decodeURIComponent(build[1]);
  7. /** The module loader to use. */
  8. var loader = (loader = /loader=([^&]+)/.exec(location.search)) && decodeURIComponent(loader[1]);
  9. /** The `ui` object. */
  10. var ui = {};
  11. /*--------------------------------------------------------------------------*/
  12. // Initialize controls.
  13. addEventListener('load', function() {
  14. function eventHandler(event) {
  15. var buildIndex = buildList.selectedIndex,
  16. loaderIndex = loaderList.selectedIndex,
  17. search = location.search.replace(/^\?|&?(?:build|loader)=[^&]*&?/g, '');
  18. if (event.stopPropagation) {
  19. event.stopPropagation();
  20. } else {
  21. event.cancelBubble = true;
  22. }
  23. location.href =
  24. location.href.split('?')[0] + '?' +
  25. (search ? search + '&' : '') +
  26. 'build=' + (buildIndex < 0 ? build : buildList[buildIndex].value) + '&' +
  27. 'loader=' + (loaderIndex < 0 ? loader : loaderList[loaderIndex].value);
  28. }
  29. function init() {
  30. var toolbar = document.getElementById('qunit-testrunner-toolbar');
  31. if (!toolbar) {
  32. setTimeout(init, 15);
  33. return;
  34. }
  35. toolbar.insertBefore(span2, toolbar.lastChild);
  36. toolbar.insertBefore(span1, span2);
  37. buildList.selectedIndex = (function() {
  38. switch (build) {
  39. case 'lodash': return 1;
  40. case 'lodash-core-dev': return 2;
  41. case 'lodash-core': return 3;
  42. case 'lodash-dev':
  43. case null: return 0;
  44. }
  45. return -1;
  46. }());
  47. loaderList.selectedIndex = (function() {
  48. switch (loader) {
  49. case 'curl': return 1;
  50. case 'dojo': return 2;
  51. case 'requirejs': return 3;
  52. case 'none':
  53. case null: return 0;
  54. }
  55. return -1;
  56. }());
  57. buildList.addEventListener('change', eventHandler);
  58. loaderList.addEventListener('change', eventHandler);
  59. }
  60. var span1 = document.createElement('span');
  61. span1.innerHTML =
  62. '<label for="qunit-build">Build: </label>' +
  63. '<select id="qunit-build">' +
  64. '<option value="lodash-dev">lodash (development)</option>' +
  65. '<option value="lodash">lodash (production)</option>' +
  66. '<option value="lodash-core-dev">lodash-core (development)</option>' +
  67. '<option value="lodash-core">lodash-core (production)</option>' +
  68. '</select>';
  69. var span2 = document.createElement('span');
  70. span2.innerHTML =
  71. '<label for="qunit-loader">Loader: </label>' +
  72. '<select id="qunit-loader">' +
  73. '<option value="none">None</option>' +
  74. '<option value="curl">Curl</option>' +
  75. '<option value="dojo">Dojo</option>' +
  76. '<option value="requirejs">RequireJS</option>' +
  77. '</select>';
  78. span1.style.cssText =
  79. span2.style.cssText = 'display:inline-block;float:right;line-height:2.1em;margin-left:1em;margin-top:0;';
  80. span1.firstChild.style.cssText =
  81. span2.firstChild.style.cssText = 'display:inline-block;margin-right:.5em;';
  82. var buildList = span1.lastChild,
  83. loaderList = span2.lastChild;
  84. setTimeout(function() {
  85. ui.timing.loadEventEnd = +new Date;
  86. }, 1);
  87. init();
  88. });
  89. // The lodash build file path.
  90. ui.buildPath = (function() {
  91. var result;
  92. switch (build) {
  93. case 'lodash': result = 'dist/lodash.min.js'; break;
  94. case 'lodash-core-dev': result = 'dist/lodash.core.js'; break;
  95. case 'lodash-core': result = 'dist/lodash.core.min.js'; break;
  96. case null: build = 'lodash-dev';
  97. case 'lodash-dev': result = 'lodash.js'; break;
  98. default: return build;
  99. }
  100. return basePath + result;
  101. }());
  102. // The module loader file path.
  103. ui.loaderPath = (function() {
  104. var result;
  105. switch (loader) {
  106. case 'curl': result = 'node_modules/curl-amd/dist/curl-kitchen-sink/curl.js'; break;
  107. case 'dojo': result = 'node_modules/dojo/dojo.js'; break;
  108. case 'requirejs': result = 'node_modules/requirejs/require.js'; break;
  109. case null: loader = 'none'; return '';
  110. default: return loader;
  111. }
  112. return basePath + result;
  113. }());
  114. // Used to indicate testing a core build.
  115. ui.isCore = /\bcore(\.min)?\.js\b/.test(ui.buildPath);
  116. // Used to indicate testing a foreign file.
  117. ui.isForeign = RegExp('^(\\w+:)?//').test(build);
  118. // Used to indicate testing a modularized build.
  119. ui.isModularize = /\b(?:amd|commonjs|es|node|npm|(index|main)\.js)\b/.test([location.pathname, location.search]);
  120. // Used to indicate testing in Sauce Labs' automated test cloud.
  121. ui.isSauceLabs = location.port == '9001';
  122. // Used to indicate that lodash is in strict mode.
  123. ui.isStrict = /\bes\b/.test([location.pathname, location.search]);
  124. ui.urlParams = { 'build': build, 'loader': loader };
  125. ui.timing = { 'loadEventEnd': 0 };
  126. window.ui = ui;
  127. }(this));