<commit_before>var assert = require('assert');

var tapsert = module.exports = tapifyAssert(assert);

for (var a in assert) {
  tapsert[a] = tapifyAssert(assert[a]);
}

var assertions = 0;
var failures = 0;

function tapifyAssert(assert) {
  return tapifiedAssert;

  function tapifiedAssert() {
    var desc = (arguments.length === assert.length
                ? arguments[arguments.length-1]
                : '');
    maybeHeader();
    var n = assertions += 1;
    try {
      assert.apply(null, arguments);
      console.log('ok %d - %s', n, desc);
    } catch (e) {
      failures += 1;
      console.log('not ok %d - %s', n, desc)
    }
  }
}

function maybeHeader() {
  if (assertions === 0)
    console.log('TAP version 13');
}

process.on('exit', function(code) {
  maybeHeader();
  console.log('1..%d', assertions);
  process.exit(failures);
});<commit_msg>Improve formatting of assertion failures<commit_after>var assert = require('assert');
var fmt = require('util').format;

var tapsert = module.exports = tapifyAssert(assert);

for (var a in assert) {
  tapsert[a] = tapifyAssert(assert[a]);
}

var assertions = 0;
var failures = 0;

function tapifyAssert(assert) {
  return tapifiedAssert;

  function tapifiedAssert() {
    var desc = (arguments.length === assert.length
                ? arguments[arguments.length-1]
                : '');
    maybeHeader();
    var n = assertions += 1;
    try {
      assert.apply(null, arguments);
      console.log('ok %d - %s', n, desc);
    } catch (e) {
      failures += 1;
      console.log('not ok %d - %s', n, desc);
      console.log(formatAssertionError(e));
    }
  }
}

function formatAssertionError(e) {
  error = [
    fmt('actual: %j', e.actual),
    fmt('expected: %j', e.expected),
    fmt('operator: %j', e.operator),
    fmt('message: %s', e.message),
    e.stack,
  ].join('\n');
  return prefixLines('# ', error);
}

function prefixLines(prefix, text) {
  return text.split('\n')
             .map(prepend)
             .join('\n');

  function prepend(line) {
    return prefix + line;
  }
}

function maybeHeader() {
  if (assertions === 0)
    console.log('TAP version 13');
}

process.on('exit', function(code) {
  maybeHeader();
  console.log('1..%d', assertions);
  process.exit(failures);
});
{'fraction_non_alphanumeric': 0.1686429512516469, 'fraction_numerical': 0.007905138339920948, 'mean_word_length': 2.934369602763385, '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': '2901467', 'n_tokens_mistral': 771, 'n_tokens_neox': 747, 'n_words': 176}