<commit_before>"use strict"
var process = require('child_process');
var assert = require('./lib/assertion');

module.exports = function() {
  return new constructor;
}

var constructor = function() {
  this.options = {};
  this.assertions = [];
};

var proto = constructor.prototype;

proto.cmd = buildSetter("cmd");
proto.cwd = buildSetter("cwd");
proto.env = buildSetter("env");
proto.timeout = buildSetter("timeout");
proto.uid = buildSetter("uid");
proto.gid = buildSetter("gid");

// Expect takes user input and pushes
// assertion objects onto the test
proto.expect = function(arg1, arg2) {
  if (arguments.length > 1) {
    this.assertions.push(new assert.value(arg1, arg2));
  } else {
    this.assertions.push(new assert.exitCode(arg1));
  }

  return this;
};

proto.end = function(cb) {
  var assertions = this.assertions;
  if (this.cmd === null) { throw new Error(".end called before command set") }
  process.exec(this.cmd, this.options, function(err, stdout, stderr){
    try {
      var code = err === null ? 0 : err.code;
      runAssertions(assertions, code, stdout, stderr);
    } catch (e) {
      if (cb) {
        cb(e, stdout, stderr);
        return
      } else {
        throw e
      }
    }
    if (cb) {
      cb(err ? err : null, stdout, stderr);
    }
  });
  return this;
};

// buildSetter returns a function that
// takes a value and assigns it to the
// key on the object.
function buildSetter(key) {
  // If the key is cmd, set the cmd var
  // not the cmd key on the options obj
  if (key === "cmd") {
    return function(val) {
      this.cmd = val;
      return this;
    }
  } else {
    return function(val) {
      this.options[key] = val;
      return this;
    }
  }
}

function runAssertions(assertions, code, stdout, stderr) {
  assertions.forEach(function(assertion) {
    var value;
    if (assertion.attribute === 'stdout') { value = stdout; }
    if (assertion.attribute === 'stderr') { value = stderr; }

    assertion.assert(value, code);
  });
}<commit_msg>Adding comment around runAssertions function<commit_after>"use strict"
var process = require('child_process');
var assert = require('./lib/assertion');

module.exports = function() {
  return new constructor;
}

var constructor = function() {
  this.options = {};
  this.assertions = [];
};

var proto = constructor.prototype;

proto.cmd = buildSetter("cmd");
proto.cwd = buildSetter("cwd");
proto.env = buildSetter("env");
proto.timeout = buildSetter("timeout");
proto.uid = buildSetter("uid");
proto.gid = buildSetter("gid");

// Expect takes user input and pushes
// assertion objects onto the test
proto.expect = function(arg1, arg2) {
  if (arguments.length > 1) {
    this.assertions.push(new assert.value(arg1, arg2));
  } else {
    this.assertions.push(new assert.exitCode(arg1));
  }

  return this;
};

proto.end = function(cb) {
  var assertions = this.assertions;
  if (this.cmd === null) { throw new Error(".end called before command set") }
  process.exec(this.cmd, this.options, function(err, stdout, stderr){
    try {
      var code = err === null ? 0 : err.code;
      runAssertions(assertions, code, stdout, stderr);
    } catch (e) {
      if (cb) {
        cb(e, stdout, stderr);
        return
      } else {
        throw e
      }
    }
    if (cb) {
      cb(err ? err : null, stdout, stderr);
    }
  });
  return this;
};

// buildSetter returns a function that
// takes a value and assigns it to the
// key on the object.
function buildSetter(key) {
  // If the key is cmd, set the cmd var
  // not the cmd key on the options obj
  if (key === "cmd") {
    return function(val) {
      this.cmd = val;
      return this;
    }
  } else {
    return function(val) {
      this.options[key] = val;
      return this;
    }
  }
}

// RunAssertions takes an array of assertion objects,
// a test execution's code, stdout and stderr, and
// invokes each assertion with the appropriate value.
function runAssertions(assertions, code, stdout, stderr) {
  assertions.forEach(function(assertion) {
    var value;
    if (assertion.attribute === 'stdout') { value = stdout; }
    if (assertion.attribute === 'stderr') { value = stderr; }

    assertion.assert(value, code);
  });
}
{'fraction_non_alphanumeric': 0.14323040380047505, 'fraction_numerical': 0.00332541567695962, 'mean_word_length': 3.0219675262655206, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1245583', 'n_tokens_mistral': 1373, 'n_tokens_neox': 1331, 'n_words': 388}