<commit_before>/**
 * Adapts Jasmine-Node tests to work better with WebDriverJS. Borrows
 * heavily from the mocha WebDriverJS adapter at
 * https://code.google.com/p/selenium/source/browse/javascript/node/selenium-webdriver/testing/index.js
 */

var WebElement; // Equal to webdriver.WebElement
var idleEventName = 'idle'; // Equal to webdriver.promise.ControlFlow.EventType.IDLE
var maybePromise = require('./maybePromise');

/**
 * Validates that the parameter is a function.
 * @param {Object} functionToValidate The function to validate.
 * @throws {Error}
 * @return {Object} The original parameter.
 */
function validateFunction(functionToValidate) {
  if (functionToValidate && typeof functionToValidate === 'function') {
    return functionToValidate;
  } else {
    throw Error(functionToValidate + ' is not a function');
  }
}

/**
 * Validates that the parameter is a number.
 * @param {Object} numberToValidate The number to validate.
 * @throws {Error}
 * @return {Object} The original number.
 */
function validateNumber(numberToValidate) {
  if (!isNaN(numberToValidate)) {
    return numberToValidate;
  } else {
    throw Error(numberToValidate + ' is not a number');
  }
}

/**
 * Validates that the parameter is a string.
 * @param {Object} stringToValidate The string to validate.
 * @throws {Error}
 * @return {Object} The original string.
 */
function validateString(stringtoValidate) {
  if (typeof stringtoValidate == 'string' || stringtoValidate instanceof String) {
    return stringtoValidate;
  } else {
    throw Error(stringtoValidate + ' is not a string');
  }
}

/**
 * Calls a function once the scheduler is idle.  If the scheduler does not support the idle API,
 * calls the function immediately.  See scheduler.md#idle-api for details.
 *
 * @param {Object} scheduler The scheduler to wait for.
 * @param {!Function} fn The function to call.
 */
function callWhenIdle(scheduler, fn) {
  if (!scheduler.once || !scheduler.isIdle || scheduler.isIdle()) {
    fn();
  } else {
    scheduler.once(idleEventName, function() { fn(); });
  }
}


/**
 * Wraps a function so it runs inside a scheduler's `execute()` block.
 *
 * In the most common case, this means wrapping in a `webdriver.promise.ControlFlow` instance
 * to wait for the control flow to complete one task before starting the next.  See scheduler.md
 * for details.
 *
 * @param {!Object} scheduler See scheduler.md for details.
 * @param {!Function} newPromise Makes a new promise using whatever implementation the scheduler
 *   prefers.
 * @param {!Function} globalFn The function to wrap.
 * @param {!string} fnName The name of the function being wrapped (e.g. `'it'`).
 * @return {!Function} The new function.
 */
function wrapInScheduler(scheduler, newPromise, globalFn, fnName) {
  return function() {
    var driverError = new Error();
    driverError.stack = driverError.stack.replace(/ +at.+jasminewd.+\n/, '');

    function asyncTestFn(fn, description) {
      description = description ? ('("' + description + '")') : '';
      return function(done) {
        var async = fn.length > 0;
        var testFn = fn.bind(this);

        scheduler.execute(function schedulerExecute() {
          return newPromise(function(fulfill, reject) {
            function wrappedReject(err) {
              var wrappedErr = new Error(err);
              reject(wrappedErr);
            }
            if (async) {
              // If testFn is async (it expects a done callback), resolve the promise of this
              // test whenever that callback says to.  Any promises returned from testFn are
              // ignored.
              var proxyDone = fulfill;
              proxyDone.fail = wrappedReject;
              testFn(proxyDone);
            } else {
              // Without a callback, testFn can return a promise, or it will
              // be assumed to have completed synchronously.
              var ret = testFn();
              if (maybePromise.isPromise(ret)) {
                ret.then(fulfill, wrappedReject);
              } else {
                fulfill(ret);
              }<commit_msg>minor fix to keep stack from original error (#86)<commit_after>/**
 * Adapts Jasmine-Node tests to work better with WebDriverJS. Borrows
 * heavily from the mocha WebDriverJS adapter at
 * https://code.google.com/p/selenium/source/browse/javascript/node/selenium-webdriver/testing/index.js
 */

var WebElement; // Equal to webdriver.WebElement
var idleEventName = 'idle'; // Equal to webdriver.promise.ControlFlow.EventType.IDLE
var maybePromise = require('./maybePromise');

/**
 * Validates that the parameter is a function.
 * @param {Object} functionToValidate The function to validate.
 * @throws {Error}
 * @return {Object} The original parameter.
 */
function validateFunction(functionToValidate) {
  if (functionToValidate && typeof functionToValidate === 'function') {
    return functionToValidate;
  } else {
    throw Error(functionToValidate + ' is not a function');
  }
}

/**
 * Validates that the parameter is a number.
 * @param {Object} numberToValidate The number to validate.
 * @throws {Error}
 * @return {Object} The original number.
 */
function validateNumber(numberToValidate) {
  if (!isNaN(numberToValidate)) {
    return numberToValidate;
  } else {
    throw Error(numberToValidate + ' is not a number');
  }
}

/**
 * Validates that the parameter is a string.
 * @param {Object} stringToValidate The string to validate.
 * @throws {Error}
 * @return {Object} The original string.
 */
function validateString(stringtoValidate) {
  if (typeof stringtoValidate == 'string' || stringtoValidate instanceof String) {
    return stringtoValidate;
  } else {
    throw Error(stringtoValidate + ' is not a string');
  }
}

/**
 * Calls a function once the scheduler is idle.  If the scheduler does not support the idle API,
 * calls the function immediately.  See scheduler.md#idle-api for details.
 *
 * @param {Object} scheduler The scheduler to wait for.
 * @param {!Function} fn The function to call.
 */
function callWhenIdle(scheduler, fn) {
  if (!scheduler.once || !scheduler.isIdle || scheduler.isIdle()) {
    fn();
  } else {
    scheduler.once(idleEventName, function() { fn(); });
  }
}


/**
 * Wraps a function so it runs inside a scheduler's `execute()` block.
 *
 * In the most common case, this means wrapping in a `webdriver.promise.ControlFlow` instance
 * to wait for the control flow to complete one task before starting the next.  See scheduler.md
 * for details.
 *
 * @param {!Object} scheduler See scheduler.md for details.
 * @param {!Function} newPromise Makes a new promise using whatever implementation the scheduler
 *   prefers.
 * @param {!Function} globalFn The function to wrap.
 * @param {!string} fnName The name of the function being wrapped (e.g. `'it'`).
 * @return {!Function} The new function.
 */
function wrapInScheduler(scheduler, newPromise, globalFn, fnName) {
  return function() {
    var driverError = new Error();
    driverError.stack = driverError.stack.replace(/ +at.+jasminewd.+\n/, '');

    function asyncTestFn(fn, description) {
      description = description ? ('("' + description + '")') : '';
      return function(done) {
        var async = fn.length > 0;
        var testFn = fn.bind(this);

        scheduler.execute(function schedulerExecute() {
          return newPromise(function(fulfill, reject) {
            function wrappedReject(err) {
              if(err instanceof Error)
                reject(err);
              else 
                reject(new Error(err));
            }
            if (async) {
              // If testFn is async (it expects a done callback), resolve the promise of this
              // test whenever that callback says to.  Any promises returned from testFn are
              // ignored.
              var proxyDone = fulfill;
              proxyDone.fail = wrappedReject;
              testFn(proxyDone);
            } else {
              // Without a callback, testFn can return a promise, or it will
              // be assumed to have completed synchronously.
              var ret = testFn();
              if (maybePromise.isPromise(ret)) {
                ret.then(fulfill, wrappedReject);
              } else {
                fulfill(ret);
              }
{'fraction_non_alphanumeric': 0.10844680081937583, 'fraction_numerical': 0.0004819857814194481, 'mean_word_length': 2.8730751283247784, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '28286', 'n_tokens_mistral': 2298, 'n_tokens_neox': 2295, 'n_words': 774}