<commit_before>"use strict";

require("@babel/polyfill/noConflict");
const { ono, Ono } = require("../../");
const { expect } = require("chai");
const { compareJSON } = require("../utils");

// NOTE: The default formatter is already tested in ono.spec.js
describe("ono.formatter", function () {
  let originalFormatter;

  before(function () {
    originalFormatter = ono.formatter;

    // A simple formatter that replaces $0, $1, $2, etc. with the corresponding param
    ono.formatter = function (message) {
      let params = Array.prototype.slice.call(arguments, 1);
      return params.reduce(function (msg, param, index) {
        return msg.replace("$" + index, param);
      }, message);
    };
  });

  after(function () {
    ono.formatter = originalFormatter;
  });

  it("should use a custom formatter", () => {
    let err = ono("$0 must be greater than $1", 4, 10);

    expect(err).to.be.an.instanceOf(Error);
    expect(err.name).to.equal("Error");
    expect(err.message).to.equal("4 must be greater than 10");

    let json = JSON.parse(JSON.stringify(err));
    expect(json).to.satisfy(compareJSON({
      name: err.name,
      message: err.message,
      stack: err.stack
    }));
  });

  it("should use a custom formatter for type-specific methods", () => {
    let err = ono.type("$0 must be greater than $1", 4, 10);

    expect(err).to.be.an.instanceOf(TypeError);
    expect(err.name).to.equal("TypeError");
    expect(err.message).to.equal("4 must be greater than 10");

    let json = JSON.parse(JSON.stringify(err));
    expect(json).to.satisfy(compareJSON({
      name: err.name,
      message: err.message,
      stack: err.stack
    }));
  });
    }
  );

});
<commit_msg>Added a test for `ono.formatter` with a custom Ono method<commit_after>"use strict";

require("@babel/polyfill/noConflict");
const { ono, Ono } = require("../../");
const { expect } = require("chai");
const { compareJSON } = require("../utils");

// NOTE: The default formatter is already tested in ono.spec.js
describe("ono.formatter", function () {
  let originalFormatter;

  before(function () {
    originalFormatter = ono.formatter;

    // A simple formatter that replaces $0, $1, $2, etc. with the corresponding param
    ono.formatter = function (message) {
      let params = Array.prototype.slice.call(arguments, 1);
      return params.reduce(function (msg, param, index) {
        return msg.replace("$" + index, param);
      }, message);
    };
  });

  after(function () {
    ono.formatter = originalFormatter;
  });

  it("should use a custom formatter", () => {
    let err = ono("$0 must be greater than $1", 4, 10);

    expect(err).to.be.an.instanceOf(Error);
    expect(err.name).to.equal("Error");
    expect(err.message).to.equal("4 must be greater than 10");

    let json = JSON.parse(JSON.stringify(err));
    expect(json).to.satisfy(compareJSON({
      name: err.name,
      message: err.message,
      stack: err.stack
    }));
  });

  it("should use a custom formatter for type-specific methods", () => {
    let err = ono.type("$0 must be greater than $1", 4, 10);

    expect(err).to.be.an.instanceOf(TypeError);
    expect(err.name).to.equal("TypeError");
    expect(err.message).to.equal("4 must be greater than 10");

    let json = JSON.parse(JSON.stringify(err));
    expect(json).to.satisfy(compareJSON({
      name: err.name,
      message: err.message,
      stack: err.stack
    }));
  });

  it("should use a custom formatter for custom Ono methods", () => {
    class MyCustomErrorClass extends Error {
      constructor () {
        super("This is my custom error message");
        this.name = "MyCustomErrorClass";
        this.code = 12345;
      }
    }

    let myCustomOno = new Ono(MyCustomErrorClass);

    let err = myCustomOno("$0 must be greater than $1", 4, 10);

    expect(err).to.be.an.instanceOf(MyCustomErrorClass);
    expect(err.name).to.equal("MyCustomErrorClass");
    expect(err.message).to.equal("This is my custom error message");

    let json = JSON.parse(JSON.stringify(err));
    expect(json).to.satisfy(compareJSON({
      name: err.name,
      message: err.message,
      stack: err.stack,
      code: 12345,
    }));
  });

});

{'fraction_non_alphanumeric': 0.16247030878859858, 'fraction_numerical': 0.013064133016627079, 'mean_word_length': 3.088349514563107, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '4485275', 'n_tokens_mistral': 1385, 'n_tokens_neox': 1312, 'n_words': 329}