<commit_before>var util = require('util');
var events = require('events');
var Logger = require('./../util/logger.js');

module.exports = new (function() {
  var doneSymbol = String.fromCharCode(10004);
  var failSymbol = String.fromCharCode(10006);
  var initialized = false;
  var client;

  /**
   * Abstract assertion class that will subclass all defined assertions
   *
   * All assertions must implement the following api:
   *
   * - @type {boolean|function}
   *   expected
   * - @type {string}
   *   message
   * - @type {function}
   *   pass
   * - @type {function}
   *   value
   * - @type {function}
   *   command
   * - @type {function} - Optional
   *   failure
   *
   * @param {boolean} abortOnFailure
   * @param {Nightwatch} client
   * @constructor
   */
  function BaseAssertion(abortOnFailure, client) {
    events.EventEmitter.call(this);
    this.abortOnFailure = abortOnFailure;
    this.client = client;
    this.api = client.api;
    this.startTime = new Date().getTime();
    this.globals = this.api.globals || {};
    this.timeout = this.globals.assertWaitForConditionTimeout || 0; //ms
    this.rescheduleInterval = this.globals.waitForConditionPollInterval || 500; //ms
    this.shouldWait = this.timeout > 0;
  }

  util.inherits(BaseAssertion, events.EventEmitter);

  BaseAssertion.prototype.complete = function() {
    var self = this, args = Array.prototype.slice.call(arguments, 0);
    args.unshift('complete');

    setTimeout(function() {
      self.emit.apply(self, args);
    }, 0);
  };

  /**
   * Performs the command method
   * @returns {*}
   * @private
   */
  BaseAssertion.prototype._executeCommand = function() {
    var self = this;
    var methods = [
      'expected',
      'message',
      ['pass'],
      ['value'],
      ['command']
    ];
    methods.forEach(function(method) {
      if (Array.isArray(method)) {
        var name = method[0];
        if (typeof self[name] !== 'function') {
          throw new Error('Assertion must implement method ' + name);
        }
      } else if (typeof self[method] == 'undefined') {
        throw new Error('Assertion must implement method/property ' + method);
      }
    });

    return this._scheduleAssertion();
  };

  BaseAssertion.prototype._scheduleAssertion = function() {
    var self = this;
    return this.command(function(result) {
      var passed, value;

      if (typeof self.failure == 'function' && self.failure(result)) {
        passed = false;
        value = null;
      } else {
        value = self.value(result);
        passed = self.pass(value);
      }

      var timeSpent = new Date().getTime() - self.startTime;
      if (!passed && timeSpent < self.timeout) {
        return self._reschedule();
      }

      var expected = typeof self.expected == 'function' ? self.expected() : self.expected;
      var message = self._getFullMessage(passed, timeSpent);

      self.client.assertion(passed, value, expected, message, self.abortOnFailure);
      self.emit('complete');
    });
  };

  BaseAssertion.prototype._getFullMessage = function(passed, timeSpent) {
    if ( !this.shouldWait) {
      return this.message;
    }
    var timeLogged = passed? timeSpent : this.timeout;
    return this.message + ' after ' + timeLogged + ' milliseconds.';
  };

  BaseAssertion.prototype._reschedule = function() {
    setTimeout(function(){}, this.rescheduleInterval);
    return this._scheduleAssertion();
  };

  /**<commit_msg>Change property name for retrying failed assertions<commit_after>var util = require('util');
var events = require('events');
var Logger = require('./../util/logger.js');

module.exports = new (function() {
  var doneSymbol = String.fromCharCode(10004);
  var failSymbol = String.fromCharCode(10006);
  var initialized = false;
  var client;

  /**
   * Abstract assertion class that will subclass all defined assertions
   *
   * All assertions must implement the following api:
   *
   * - @type {boolean|function}
   *   expected
   * - @type {string}
   *   message
   * - @type {function}
   *   pass
   * - @type {function}
   *   value
   * - @type {function}
   *   command
   * - @type {function} - Optional
   *   failure
   *
   * @param {boolean} abortOnFailure
   * @param {Nightwatch} client
   * @constructor
   */
  function BaseAssertion(abortOnFailure, client) {
    events.EventEmitter.call(this);
    this.abortOnFailure = abortOnFailure;
    this.client = client;
    this.api = client.api;
    this.startTime = new Date().getTime();
    this.globals = this.api.globals || {};
    this.timeout = this.globals.retryAssertionTimeout || 0; //ms
    this.rescheduleInterval = this.globals.waitForConditionPollInterval || 500; //ms
    this.shouldRetry = this.timeout > 0;
  }

  util.inherits(BaseAssertion, events.EventEmitter);

  BaseAssertion.prototype.complete = function() {
    var self = this, args = Array.prototype.slice.call(arguments, 0);
    args.unshift('complete');

    setTimeout(function() {
      self.emit.apply(self, args);
    }, 0);
  };

  /**
   * Performs the command method
   * @returns {*}
   * @private
   */
  BaseAssertion.prototype._executeCommand = function() {
    var self = this;
    var methods = [
      'expected',
      'message',
      ['pass'],
      ['value'],
      ['command']
    ];
    methods.forEach(function(method) {
      if (Array.isArray(method)) {
        var name = method[0];
        if (typeof self[name] !== 'function') {
          throw new Error('Assertion must implement method ' + name);
        }
      } else if (typeof self[method] == 'undefined') {
        throw new Error('Assertion must implement method/property ' + method);
      }
    });

    return this._scheduleAssertion();
  };

  BaseAssertion.prototype._scheduleAssertion = function() {
    var self = this;
    return this.command(function(result) {
      var passed, value;

      if (typeof self.failure == 'function' && self.failure(result)) {
        passed = false;
        value = null;
      } else {
        value = self.value(result);
        passed = self.pass(value);
      }

      var timeSpent = new Date().getTime() - self.startTime;
      if (!passed && timeSpent < self.timeout) {
        return self._reschedule();
      }

      var expected = typeof self.expected == 'function' ? self.expected() : self.expected;
      var message = self._getFullMessage(passed, timeSpent);

      self.client.assertion(passed, value, expected, message, self.abortOnFailure);
      self.emit('complete');
    });
  };

  BaseAssertion.prototype._getFullMessage = function(passed, timeSpent) {
    if ( !this.shouldRetry) {
      return this.message;
    }
    var timeLogged = passed? timeSpent : this.timeout;
    return this.message + ' after ' + timeLogged + ' milliseconds.';
  };

  BaseAssertion.prototype._reschedule = function() {
    setTimeout(function(){}, this.rescheduleInterval);
    return this._scheduleAssertion();
  };

  /**
{'fraction_non_alphanumeric': 0.1371872303710095, 'fraction_numerical': 0.005176876617773943, 'mean_word_length': 2.7860642351660316, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4972145', 'n_tokens_mistral': 2114, 'n_tokens_neox': 2057, 'n_words': 466}