No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

129 líneas
4.3 KiB

hace 4 años
  1. 'use strict';
  2. const where = require('ramda/src/where');
  3. const includes = require('ramda/src/includes');
  4. const intersection = require('ramda/src/intersection');
  5. const pipe = require('ramda/src/pipe');
  6. const isEmpty = require('ramda/src/isEmpty');
  7. const complement = require('ramda/src/complement');
  8. const flip = require('ramda/src/flip');
  9. const filter = require('ramda/src/filter');
  10. const over = require('ramda/src/over');
  11. const lensProp = require('ramda/src/lensProp');
  12. const map = require('ramda/src/map');
  13. const view = require('ramda/src/view');
  14. const assoc = require('ramda/src/assoc');
  15. const allPass = require('ramda/src/allPass');
  16. const INTERFACES = {
  17. BDD: 'BDD',
  18. TDD: 'TDD',
  19. QUnit: 'QUnit'
  20. };
  21. const TYPES = {
  22. suite: 'suite',
  23. testCase: 'testCase',
  24. hook: 'hook'
  25. };
  26. const MODIFIERS = {
  27. skip: 'skip',
  28. only: 'only'
  29. };
  30. const baseNames = [
  31. { name: 'describe', interfaces: [ INTERFACES.BDD ], type: TYPES.suite },
  32. { name: 'context', interfaces: [ INTERFACES.BDD ], type: TYPES.suite },
  33. { name: 'suite', interfaces: [ INTERFACES.TDD, INTERFACES.QUnit ], type: TYPES.suite },
  34. { name: 'it', interfaces: [ INTERFACES.BDD ], type: TYPES.testCase },
  35. { name: 'specify', interfaces: [ INTERFACES.BDD ], type: TYPES.testCase },
  36. { name: 'test', interfaces: [ INTERFACES.TDD, INTERFACES.QUnit ], type: TYPES.testCase },
  37. { name: 'before', interfaces: [ INTERFACES.BDD, INTERFACES.QUnit ], type: TYPES.hook },
  38. { name: 'after', interfaces: [ INTERFACES.BDD, INTERFACES.QUnit ], type: TYPES.hook },
  39. { name: 'beforeEach', interfaces: [ INTERFACES.BDD, INTERFACES.QUnit ], type: TYPES.hook },
  40. { name: 'afterEach', interfaces: [ INTERFACES.BDD, INTERFACES.QUnit ], type: TYPES.hook },
  41. { name: 'suiteSetup', interfaces: [ INTERFACES.TDD ], type: TYPES.hook },
  42. { name: 'suiteTeardown', interfaces: [ INTERFACES.TDD ], type: TYPES.hook },
  43. { name: 'setup', interfaces: [ INTERFACES.TDD ], type: TYPES.hook },
  44. { name: 'teardown', interfaces: [ INTERFACES.TDD ], type: TYPES.hook }
  45. ];
  46. const includesSublist = (sublist) => pipe(intersection(sublist), complement(isEmpty));
  47. const isIncludedIn = flip(includes);
  48. const hasMatchingType = (typesToMatch) => where({ type: isIncludedIn(typesToMatch) });
  49. const hasMatchingInterfaces = (interfacesToMatch) => where({ interfaces: includesSublist(interfacesToMatch) });
  50. const hasMatchingModifier = (modifierToMatch) => where({ modifier: isIncludedIn(modifierToMatch) });
  51. const filterTestCasesAndSuites = filter(hasMatchingType([ TYPES.suite, TYPES.testCase ]));
  52. const nameLens = lensProp('name');
  53. const mapNames = (fn) => map(over(nameLens, fn));
  54. const extractNames = map(view(nameLens));
  55. const addModifier = (modifier) => map(assoc('modifier', modifier));
  56. function formatXVariant(name) {
  57. return `x${name}`;
  58. }
  59. function formatSkipVariant(name) {
  60. return `${name}.${MODIFIERS.skip}`;
  61. }
  62. function formatExclusiveVariant(name) {
  63. return `${name}.${MODIFIERS.only}`;
  64. }
  65. const buildXVariants = pipe(
  66. filterTestCasesAndSuites,
  67. filter(hasMatchingInterfaces([ INTERFACES.BDD ])),
  68. mapNames(formatXVariant),
  69. addModifier(MODIFIERS.skip)
  70. );
  71. const buildSkipVariants = pipe(
  72. filterTestCasesAndSuites,
  73. mapNames(formatSkipVariant),
  74. addModifier(MODIFIERS.skip)
  75. );
  76. const buildExclusiveVariants = pipe(
  77. filterTestCasesAndSuites,
  78. mapNames(formatExclusiveVariant),
  79. addModifier(MODIFIERS.only)
  80. );
  81. function buildAllNames(additionalNames) {
  82. const names = addModifier(null)([ ...baseNames, ...additionalNames ]);
  83. return [
  84. ...names,
  85. ...buildSkipVariants(names),
  86. ...buildXVariants(names),
  87. ...buildExclusiveVariants(names)
  88. ];
  89. }
  90. function getNamesByType(type, filterOptions = {}) {
  91. const { modifiers = [], modifiersOnly = false, additionalCustomNames = [] } = filterOptions;
  92. const allNames = buildAllNames(additionalCustomNames);
  93. const predicates = [
  94. hasMatchingType([ type ]),
  95. hasMatchingModifier([ ...modifiers, ...modifiersOnly ? [] : [ null ] ])
  96. ];
  97. const filteredNames = filter(allPass(predicates), allNames);
  98. return extractNames(filteredNames);
  99. }
  100. function getTestCaseNames(options) {
  101. return getNamesByType(TYPES.testCase, options);
  102. }
  103. function getSuiteNames(options) {
  104. return getNamesByType(TYPES.suite, options);
  105. }
  106. module.exports = {
  107. getTestCaseNames,
  108. getSuiteNames
  109. };