subset-tests.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. (function() {
  2. var subTestStart = 0;
  3. var subTestEnd = Infinity;
  4. var match;
  5. if (location.search) {
  6. match = /(?:^\?|&)(\d+)-(\d+|last)(?:&|$)/.exec(location.search);
  7. if (match) {
  8. subTestStart = parseInt(match[1], 10);
  9. if (match[2] !== "last") {
  10. subTestEnd = parseInt(match[2], 10);
  11. }
  12. }
  13. // Below is utility code to generate <meta> for copy/paste into tests.
  14. // Sample usage:
  15. // test.html?split=1000
  16. match = /(?:^\?|&)split=(\d+)(?:&|$)/.exec(location.search);
  17. if (match) {
  18. var testsPerVariant = parseInt(match[1], 10);
  19. add_completion_callback(tests => {
  20. var total = tests.length;
  21. var template = '<meta name="variant" content="?%s-%s">';
  22. var metas = [];
  23. for (var i = 1; i < total - testsPerVariant; i = i + testsPerVariant) {
  24. metas.push(template.replace("%s", i).replace("%s", i + testsPerVariant - 1));
  25. }
  26. metas.push(template.replace("%s", i).replace("%s", "last"));
  27. var pre = document.createElement('pre');
  28. pre.textContent = metas.join('\n');
  29. document.body.insertBefore(pre, document.body.firstChild);
  30. document.getSelection().selectAllChildren(pre);
  31. });
  32. }
  33. }
  34. /**
  35. * Check if `currentSubTest` is in the subset specified in the URL.
  36. * @param {number} currentSubTest
  37. * @returns {boolean}
  38. */
  39. function shouldRunSubTest(currentSubTest) {
  40. return currentSubTest >= subTestStart && currentSubTest <= subTestEnd;
  41. }
  42. var currentSubTest = 0;
  43. /**
  44. * Only test a subset of tests with, e.g., `?1-10` in the URL.
  45. * Can be used together with `<meta name="variant" content="...">`
  46. * Sample usage:
  47. * for (const test of tests) {
  48. * subsetTest(async_test, test.fn, test.name);
  49. * }
  50. */
  51. function subsetTest(testFunc, ...args) {
  52. currentSubTest++;
  53. if (shouldRunSubTest(currentSubTest)) {
  54. return testFunc(...args);
  55. }
  56. return null;
  57. }
  58. self.shouldRunSubTest = shouldRunSubTest;
  59. self.subsetTest = subsetTest;
  60. })();