<commit_before>import assert from 'power-assert';
import sinon from 'sinon';
import forEach from '../lib';

/** @test {forEach} */
describe('mocha-each', () => {
  const mochaIt = it;
  let _it = null;

  beforeEach(() => {
    _it = sinon.spy();
    global.it = mochaIt;
  });

  it('uses global `it` automatically', () => {
    global.it = _it;
    forEach([0]).it('', () => {});
    assert(_it.calledOnce);
  });

  it('accepts a second argument which specifies `it` explicitly', () => {
    forEach([0], _it).it('', () => {});
    assert(_it.calledOnce);
  });

  it('binds `this` to the context of `it` (Mocha instance)', () => {
    const test = sinon.spy();
    const _this = {};
    const _it = (name, b) => body = b;
    let body;
    forEach([0], _it).it('', test);
    body.call(_this);
    assert.equal(test.thisValues[0], _this);
  });

  context('without Mocha', () => {
    it('throws an error when called', () => {
      delete global.it;
      assert.throws(() => {
        forEach([0]).it('', () => {});
      }, /it is not a function/);
    });
  });

  context('with string title', () => {
    it('defines titles using the specified one and each index', () => {
      forEach([3, 2, 1, 0], _it).it('title', () => {});
      assert.deepEqual(
        _it.args.map(a => a[0]),
        [1, 2, 3, 4].map(i => `title (case ${i})`)
      );
    });
  });

  context('with function title', () => {
    it('calls the function and defines titles using its return value', () => {
      forEach([
        4, 8, 12, 16
      ], _it)
      .it(
        p => `generated ${p / 2}`,
        () => {}
      );
      assert.deepEqual(
        _it.args.map(a => a[0]),<commit_msg>:bug: Fix expected error message<commit_after>import assert from 'power-assert';
import sinon from 'sinon';
import forEach from '../lib';

/** @test {forEach} */
describe('mocha-each', () => {
  const mochaIt = it;
  let _it = null;

  beforeEach(() => {
    _it = sinon.spy();
    global.it = mochaIt;
  });

  it('uses global `it` automatically', () => {
    global.it = _it;
    forEach([0]).it('', () => {});
    assert(_it.calledOnce);
  });

  it('accepts a second argument which specifies `it` explicitly', () => {
    forEach([0], _it).it('', () => {});
    assert(_it.calledOnce);
  });

  it('binds `this` to the context of `it` (Mocha instance)', () => {
    const test = sinon.spy();
    const _this = {};
    const _it = (name, b) => body = b;
    let body;
    forEach([0], _it).it('', test);
    body.call(_this);
    assert.equal(test.thisValues[0], _this);
  });

  context('without Mocha', () => {
    it('throws an error when called', () => {
      delete global.it;
      assert.throws(() => {
        forEach([0]).it('', () => {});
      }, /is not a function/);
    });
  });

  context('with string title', () => {
    it('defines titles using the specified one and each index', () => {
      forEach([3, 2, 1, 0], _it).it('title', () => {});
      assert.deepEqual(
        _it.args.map(a => a[0]),
        [1, 2, 3, 4].map(i => `title (case ${i})`)
      );
    });
  });

  context('with function title', () => {
    it('calls the function and defines titles using its return value', () => {
      forEach([
        4, 8, 12, 16
      ], _it)
      .it(
        p => `generated ${p / 2}`,
        () => {}
      );
      assert.deepEqual(
        _it.args.map(a => a[0]),
{'fraction_non_alphanumeric': 0.22320899940793368, 'fraction_numerical': 0.013025458851391355, 'mean_word_length': 2.426977687626775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 47, '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': '5352028', 'n_tokens_mistral': 1205, 'n_tokens_neox': 1138, 'n_words': 288}