{"size": 4411, "ext": "js", "max_stars_repo_path": "node_modules/eslint-plugin-mocha/lib/util/names.js", "max_stars_repo_name": "gonnavis/devtools-frontend", "max_stars_repo_stars_event_min_datetime": "2021-04-25T19:26:02.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-13T00:36:50.000Z", "max_issues_repo_path": "node_modules/eslint-plugin-mocha/lib/util/names.js", "max_issues_repo_name": "gonnavis/devtools-frontend", "max_issues_repo_issues_event_min_datetime": "2021-05-12T08:56:20.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-12T08:56:20.000Z", "max_forks_repo_path": "node_modules/eslint-plugin-mocha/lib/util/names.js", "max_forks_repo_name": "emchap/devtools-frontend", "max_forks_repo_forks_event_min_datetime": "2020-11-30T08:06:38.000Z", "max_forks_repo_forks_event_max_datetime": "2020-11-30T08:06:38.000Z"}
{"max_stars_count": 2, "max_issues_count": 1, "max_forks_count": 1, "avg_line_length": 33.9307692308, "max_line_length": 111, "alphanum_fraction": 0.6873724779}
'use strict';

const where = require('ramda/src/where');
const includes = require('ramda/src/includes');
const intersection = require('ramda/src/intersection');
const pipe = require('ramda/src/pipe');
const isEmpty = require('ramda/src/isEmpty');
const complement = require('ramda/src/complement');
const flip = require('ramda/src/flip');
const filter = require('ramda/src/filter');
const over = require('ramda/src/over');
const lensProp = require('ramda/src/lensProp');
const map = require('ramda/src/map');
const view = require('ramda/src/view');
const assoc = require('ramda/src/assoc');
const allPass = require('ramda/src/allPass');

const INTERFACES = {
    BDD: 'BDD',
    TDD: 'TDD',
    QUnit: 'QUnit'
};

const TYPES = {
    suite: 'suite',
    testCase: 'testCase',
    hook: 'hook'
};

const MODIFIERS = {
    skip: 'skip',
    only: 'only'
};

const baseNames = [
    { name: 'describe', interfaces: [ INTERFACES.BDD ], type: TYPES.suite },
    { name: 'context', interfaces: [ INTERFACES.BDD ], type: TYPES.suite },
    { name: 'suite', interfaces: [ INTERFACES.TDD, INTERFACES.QUnit ], type: TYPES.suite },
    { name: 'it', interfaces: [ INTERFACES.BDD ], type: TYPES.testCase },
    { name: 'specify', interfaces: [ INTERFACES.BDD ], type: TYPES.testCase },
    { name: 'test', interfaces: [ INTERFACES.TDD, INTERFACES.QUnit ], type: TYPES.testCase },
    { name: 'before', interfaces: [ INTERFACES.BDD, INTERFACES.QUnit ], type: TYPES.hook },
    { name: 'after', interfaces: [ INTERFACES.BDD, INTERFACES.QUnit ], type: TYPES.hook },
    { name: 'beforeEach', interfaces: [ INTERFACES.BDD, INTERFACES.QUnit ], type: TYPES.hook },
    { name: 'afterEach', interfaces: [ INTERFACES.BDD, INTERFACES.QUnit ], type: TYPES.hook },
    { name: 'suiteSetup', interfaces: [ INTERFACES.TDD ], type: TYPES.hook },
    { name: 'suiteTeardown', interfaces: [ INTERFACES.TDD ], type: TYPES.hook },
    { name: 'setup', interfaces: [ INTERFACES.TDD ], type: TYPES.hook },
    { name: 'teardown', interfaces: [ INTERFACES.TDD ], type: TYPES.hook }
];

const includesSublist = (sublist) => pipe(intersection(sublist), complement(isEmpty));
const isIncludedIn = flip(includes);
const hasMatchingType = (typesToMatch) => where({ type: isIncludedIn(typesToMatch) });
const hasMatchingInterfaces = (interfacesToMatch) => where({ interfaces: includesSublist(interfacesToMatch) });
const hasMatchingModifier = (modifierToMatch) => where({ modifier: isIncludedIn(modifierToMatch) });
const filterTestCasesAndSuites = filter(hasMatchingType([ TYPES.suite, TYPES.testCase ]));

const nameLens = lensProp('name');
const mapNames = (fn) => map(over(nameLens, fn));
const extractNames = map(view(nameLens));
const addModifier = (modifier) => map(assoc('modifier', modifier));

function formatXVariant(name) {
    return `x${name}`;
}

function formatSkipVariant(name) {
    return `${name}.${MODIFIERS.skip}`;
}

function formatExclusiveVariant(name) {
    return `${name}.${MODIFIERS.only}`;
}

const buildXVariants = pipe(
    filterTestCasesAndSuites,
    filter(hasMatchingInterfaces([ INTERFACES.BDD ])),
    mapNames(formatXVariant),
    addModifier(MODIFIERS.skip)
);

const buildSkipVariants = pipe(
    filterTestCasesAndSuites,
    mapNames(formatSkipVariant),
    addModifier(MODIFIERS.skip)
);

const buildExclusiveVariants = pipe(
    filterTestCasesAndSuites,
    mapNames(formatExclusiveVariant),
    addModifier(MODIFIERS.only)
);

function buildAllNames(additionalNames) {
    const names = addModifier(null)([ ...baseNames, ...additionalNames ]);

    return [
        ...names,
        ...buildSkipVariants(names),
        ...buildXVariants(names),
        ...buildExclusiveVariants(names)
    ];
}

function getNamesByType(type, filterOptions = {}) {
    const { modifiers = [], modifiersOnly = false, additionalCustomNames = [] } = filterOptions;
    const allNames = buildAllNames(additionalCustomNames);
    const predicates = [
        hasMatchingType([ type ]),
        hasMatchingModifier([ ...modifiers, ...modifiersOnly ? [] : [ null ] ])
    ];
    const filteredNames = filter(allPass(predicates), allNames);

    return extractNames(filteredNames);
}

function getTestCaseNames(options) {
    return getNamesByType(TYPES.testCase, options);
}

function getSuiteNames(options) {
    return getNamesByType(TYPES.suite, options);
}

module.exports = {
    getTestCaseNames,
    getSuiteNames
};
