<commit_before>var painless = require('../../assertion/painless')
var test = painless.createGroup('Test promise/promtie::reduce')
var t = painless.assert

var reduce = require('../../../src/promise/promtie/reduce')

test('reduce(fn, initialValue)', () => {
  const input = [1, 2, 3, 4];

  return Promise.resolve([Promise.resolve(1), 2, Promise.resolve(3), 4])
    .then(reduce((acc, n, i, length) => {
      t.is(n, input[i]);
      t.is(length, input.length);

      return Promise.resolve(acc + n);
    }, 0))
    .then(result => t.is(result, 10));
});

test('reduce(fn)', () => {
  const input = [1, 2, 3, 4];

  return Promise.resolve([Promise.resolve(1), 2, Promise.resolve(3), 4])
    .then(reduce((acc, n, i, length) => {
      t.is(n, input[i]);
      t.is(length, input.length);

      return Promise.resolve(acc + n);
    }))
    .then(result => t.is(result, 10));
});

test('reduce(array, fn, initialValue)', () => {
  const input = [1, 2, 3, 4];

  return reduce([Promise.resolve(1), 2, Promise.resolve(3), 4],
      (acc, n, i, length) => {
        t.is(n, input[i]);
        t.is(length, input.length);

        return acc + n;
      }, 0
    )
    .then(result => t.deepEqual(result, 10));
});

test('reduce(array, fn, initialValue): initialValue is a promise', () => {
  const input = [1, 2, 3, 4];

  return reduce([Promise.resolve(1), 2, Promise.resolve(3), 4],
      (acc, n, i, length) => {
        t.is(n, input[i]);
        t.is(length, input.length);

        return acc + n;
      }, Promise.resolve(0)
    )
    .then(result => t.deepEqual(result, 10));
});

test('reduce(array, fn): fn returns a value', () => {
  const input = [1, 2, 3, 4];

  return reduce([Promise.resolve(1), 2, Promise.resolve(3), 4],
      (acc, n, i, length) => {
        t.is(n, input[i]);
        t.is(length, input.length);

        return acc + n;
      }
    )
    .then(result => t.deepEqual(result, 10));
});

test('reduce(array, fn): deal with promise failure', () => {
  const input = [1, 2, 3, 4];

  return t.throws(reduce([Promise.resolve(1), 2, Promise.reject(new Error('Failed to fetch third value')), 4],
    (acc, n, i, length) => {
      t.is(n, input[i]);
      t.is(length, input.length);

      return acc + n;
    }
  ), 'Failed to fetch third value');
});<commit_msg>update test promise/reduce — use chai-promised<commit_after>var painless = require('../../assertion/painless')
var test = painless.createGroup('Test promise/promtie::reduce')
var t = painless.assert
var p = painless.assert.chai

var reduce = require('../../../src/promise/promtie/reduce')

test('reduce(fn, initialValue)', () => {
  const input = [1, 2, 3, 4];

  return Promise.resolve([Promise.resolve(1), 2, Promise.resolve(3), 4])
    .then(reduce((acc, n, i, length) => {
      t.is(n, input[i]);
      t.is(length, input.length);

      return Promise.resolve(acc + n);
    }, 0))
    .then(result => t.is(result, 10));
});

test('reduce(fn)', () => {
  const input = [1, 2, 3, 4];

  return Promise.resolve([Promise.resolve(1), 2, Promise.resolve(3), 4])
    .then(reduce((acc, n, i, length) => {
      t.is(n, input[i]);
      t.is(length, input.length);

      return Promise.resolve(acc + n);
    }))
    .then(result => t.is(result, 10));
});

test('reduce(array, fn, initialValue)', () => {
  const input = [1, 2, 3, 4];

  return reduce([Promise.resolve(1), 2, Promise.resolve(3), 4],
      (acc, n, i, length) => {
        t.is(n, input[i]);
        t.is(length, input.length);

        return acc + n;
      }, 0
    )
    .then(result => t.deepEqual(result, 10));
});

test('reduce(array, fn, initialValue): initialValue is a promise', () => {
  const input = [1, 2, 3, 4];

  return reduce([Promise.resolve(1), 2, Promise.resolve(3), 4],
      (acc, n, i, length) => {
        t.is(n, input[i]);
        t.is(length, input.length);

        return acc + n;
      }, Promise.resolve(0)
    )
    .then(result => t.deepEqual(result, 10));
});

test('reduce(array, fn): fn returns a value', () => {
  const input = [1, 2, 3, 4];

  return reduce([Promise.resolve(1), 2, Promise.resolve(3), 4],
      (acc, n, i, length) => {
        t.is(n, input[i]);
        t.is(length, input.length);

        return acc + n;
      }
    )
    .then(result => t.deepEqual(result, 10));
});

test('reduce(array, fn): deal with promise failure', () => {
  const input = [1, 2, 3, 4];

  return p.isRejected(reduce([Promise.resolve(1), 2, Promise.reject(new Error('Failed to fetch third value')), 4],
    (acc, n, i, length) => {
      t.is(n, input[i]);
      t.is(length, input.length);

      return acc + n;
    }
  ), 'Failed to fetch third value');
});
{'fraction_non_alphanumeric': 0.21222557038312526, 'fraction_numerical': 0.02582866982350409, 'mean_word_length': 2.8468543046357615, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, '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': '940860', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1638, 'n_words': 404}