<commit_before>var glob = require('glob')
var path = require('path')
var _ = require('lodash')
var fs = require('fs')
var functionNamesAtLine = require('function-names-at-line')

module.exports = function (locator, cwd) {
  var currentTestOrdinal = 0

  // TODO declaration nested b/c of that stupid counter.
  function buildExampleGroup (groupDeclaration, criteria, cwd, file) {
    var testFuncs = selectedTestFunctionsIn(groupDeclaration, criteria, cwd, file)
    var tests = _.map(testFuncs, function (testEntry, i) {
      currentTestOrdinal++

      return {
        testFunction: testEntry.testFunction,
        testName: testEntry.testName,
        type: 'test',
        context: Object.create(null),
        description: currentTestOrdinal + ' - ' + testSummaryDescription(file, testEntry.testName, i),
        file: file
      }
    })

    return {
      tests: tests,
      file: file,
      name: 'module',
      type: 'exampleGroup',
      beforeAll: groupDeclaration.beforeAll || function () {},
      afterAll: groupDeclaration.afterAll || function () {},
      beforeEach: groupDeclaration.beforeEach || function () {},
      afterEach: groupDeclaration.afterEach || function () {}
    }
  }


  var criteria = criteriaFor(locator)

  var testModules = _.map(glob.sync(criteria.glob), function (file) {
    var testPath = path.resolve(cwd, file)
    var testModule = require(testPath)

    return buildExampleGroup(testModule, criteria, cwd, file)
  })

  return {
    modules: testModules,
    count: currentTestOrdinal
  }
}

function criteriaFor (locator) {
  var parts
  if (_.includes(locator, '#')) {
    parts = locator.split('#')
    return {
      glob: parts[0],
      name: parts[1]
    }
  } else if (_.includes(locator, ':')) {
    parts = locator.split(':')
    return {
      glob: parts[0],
      lineNumber: parts[1]
    }
  } else {
    return {
      glob: locator
    }
  }
}

var selectedTestFunctionsIn = function (testModule, criteria, cwd, file) {
  var testFunctions = testFunctionsIn(testModule)
  if (criteria.name) {
    return filterTestFunctionsByNames(testFunctions, [criteria.name])
  } else if (criteria.lineNumber && _.isPlainObject(testModule)) {
    var source = fs.readFileSync(path.resolve(cwd, file)).toString()
    var names = functionNamesAtLine(source, criteria.lineNumber)
    return filterTestFunctionsByNames(testFunctions, names)
  } else {
    return testFunctions
  }
}

var testFunctionsIn = function (testModule) {
  if (_.isFunction(testModule)) {
    return [{
      testFunction: testModule,
      testName: testModule.name
    }]
  } else {
    return _(testModule).functions()
      .without('beforeEach', 'beforeAll', 'afterEach', 'afterAll')
      .map(function (testName) {
        return {
          testFunction: testModule[testName],
          testName: testName
        }
      })
      .value()
  }
}

var filterTestFunctionsByNames = function (testFunctions, testNames) {
  return _.filter(testFunctions, function (testFunction) {
    return _.includes(testNames, testFunction.testName)
  })<commit_msg>start unmucking up this. no comment<commit_after>var glob = require('glob')
var path = require('path')
var _ = require('lodash')
var fs = require('fs')
var functionNamesAtLine = require('function-names-at-line')

function noOpHook () {}

module.exports = function (locator, cwd) {
  var command = {
    currentTestOrdinal: 0,
    cwd: cwd,
    criteria: criteriaFor(locator)
  }

  var testModules = _.map(glob.sync(command.criteria.glob), function (file) {
    var testPath = path.resolve(command.cwd, file)
    var testModule = require(testPath)

    return buildExampleGroup('module', testModule, command, file)
  })

  return {
    modules: removeTestlessExampleGroups(filterToSelectedTests(testModules)),
    count: command.currentTestOrdinal
  }
}

function buildExampleGroup (name, groupDeclaration, command, file) {
  var testFuncs = selectedTestFunctionsIn(groupDeclaration, command, file)
  var tests = _.map(testFuncs, function (testEntry, i) {
    command.currentTestOrdinal++

    return {
      testFunction: testEntry.testFunction,
      testName: testEntry.testName,
      type: 'test',
      context: Object.create(null),
      description: command.currentTestOrdinal + ' - ' + testSummaryDescription(file, testEntry.testName, i),
      file: file
    }
  })

  return {
    tests: tests,
    file: file,
    name: name,
    type: 'exampleGroup',
    beforeAll: groupDeclaration.beforeAll || noOpHook,
    afterAll: groupDeclaration.afterAll || noOpHook,
    beforeEach: groupDeclaration.beforeEach || noOpHook,
    afterEach: groupDeclaration.afterEach || noOpHook
  }
}

function removeTestlessExampleGroups (exampleGroup) {
  return exampleGroup // TODO
}

function filterToSelectedTests (exampleGroup) {
  return exampleGroup // TODO
}

function findAllTestsUnder (exampleGroup) {
  return exampleGroup // TODO
}

function criteriaFor (locator) {
  var parts
  if (_.includes(locator, '#')) {
    parts = locator.split('#')
    return {
      glob: parts[0],
      name: parts[1]
    }
  } else if (_.includes(locator, ':')) {
    parts = locator.split(':')
    return {
      glob: parts[0],
      lineNumber: parts[1]
    }
  } else {
    return {
      glob: locator
    }
  }
}

var selectedTestFunctionsIn = function (testModule, command, file) {
  var testFunctions = testFunctionsIn(testModule)
  if (command.criteria.name) {
    return filterTestFunctionsByNames(testFunctions, [command.criteria.name])
  } else if (command.criteria.lineNumber && _.isPlainObject(testModule)) {
    var source = fs.readFileSync(path.resolve(command.cwd, file)).toString()
    var names = functionNamesAtLine(source, command.criteria.lineNumber)
    return filterTestFunctionsByNames(testFunctions, names)
  } else {
    return testFunctions
  }
}

var testFunctionsIn = function (testModule) {
  if (_.isFunction(testModule)) {
    return [{
      testFunction: testModule,
      testName: testModule.name
    }]
  } else {
    return _(testModule).functions()
      .without('beforeEach', 'beforeAll', 'afterEach', 'afterAll')
      .map(function (testName) {
        return {
          testFunction: testModule[testName],
          testName: testName
        }
      })
      .value()
  }
}

var filterTestFunctionsByNames = function (testFunctions, testNames) {
  return _.filter(testFunctions, function (testFunction) {
    return _.includes(testNames, testFunction.testName)
  })
{'fraction_non_alphanumeric': 0.1183641975308642, 'fraction_numerical': 0.0015432098765432098, 'mean_word_length': 3.426912568306011, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2818438', 'n_tokens_mistral': 1945, 'n_tokens_neox': 1927, 'n_words': 435}