<commit_before>'use strict';

var Assertion = require('./model/assertion');
var UnaryOperator = require('./model/unary-operator');

var nodsl = require('./nodsl');

module.exports = MessageBuilder;

// code that builds error message is invoked only when assertion fails
// performace is not a concern here
function MessageBuilder(context) {
  var that = Object.create(MessageBuilder.prototype);
  that.context = context;
  that.assertions = [];
  return that;
}

MessageBuilder.prototype = {
  addAssertions: function addAssertions(assertions) {
    this.assertions = this.assertions.concat(assertions);
    return this;
  },

  build: function build() {
    nodsl.check(this.assertions.length !== 0, 'no assertions added');

    var groupByName = groupByVariableName.bind(null, this.context);
    var toString = groupToString.bind(null, this.context);

    var grouped = this.assertions
      .reduce(replaceEmptyWithChildren, [])
      .reduce(mergeWithOperators(), [])
//      .map(tee.bind(null, console.log))
      .reduce(removeDuplicates, [])
      .reduce(groupByName, []);

    function buildMessage(builder, group) {
      return builder + toString(group);
    }

    grouped[0].operators.binary = '';
    var message = grouped.reduce(buildMessage, '');
    return message;
  },
};

function removeDuplicates(retVal, assertion) {
  var previous = retVal[retVal.length - 1];
  if (retVal.length === 0 ||
      previous.message !== assertion.message ||
      !arrayEqual(previous.args, assertion.args) ||
      previous.operators.unary !== assertion.operators.unary) {<commit_msg>~ better error message when building message without failed assertions<commit_after>'use strict';

var Assertion = require('./model/assertion');
var UnaryOperator = require('./model/unary-operator');

var nodsl = require('./nodsl');

module.exports = MessageBuilder;

// code that builds error message is invoked only when assertion fails
// performace is not a concern here
function MessageBuilder(context) {
  var that = Object.create(MessageBuilder.prototype);
  that.context = context;
  that.assertions = [];
  return that;
}

MessageBuilder.prototype = {
  addAssertions: function addAssertions(assertions) {
    this.assertions = this.assertions.concat(assertions);
    return this;
  },

  build: function build() {
    nodsl.check(this.assertions.length !== 0, 'trying to build a message without failed assertions');

    var groupByName = groupByVariableName.bind(null, this.context);
    var toString = groupToString.bind(null, this.context);

    var grouped = this.assertions
      .reduce(replaceEmptyWithChildren, [])
      .reduce(mergeWithOperators(), [])
//      .map(tee.bind(null, console.log))
      .reduce(removeDuplicates, [])
      .reduce(groupByName, []);

    function buildMessage(builder, group) {
      return builder + toString(group);
    }

    grouped[0].operators.binary = '';
    var message = grouped.reduce(buildMessage, '');
    return message;
  },
};

function removeDuplicates(retVal, assertion) {
  var previous = retVal[retVal.length - 1];
  if (retVal.length === 0 ||
      previous.message !== assertion.message ||
      !arrayEqual(previous.args, assertion.args) ||
      previous.operators.unary !== assertion.operators.unary) {
{'fraction_non_alphanumeric': 0.1326186830015314, 'fraction_numerical': 0.002450229709035222, 'mean_word_length': 3.881913303437967, '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': '6236888', 'n_tokens_mistral': 957, 'n_tokens_neox': 931, 'n_words': 216}