Skip to content

Commit

Permalink
Merge pull request #1503 from cypress-io/set-formatter
Browse files Browse the repository at this point in the history
Add method to set custom formatter
  • Loading branch information
fatso83 authored Aug 1, 2017
2 parents fcd2af6 + fb1a01f commit 3938c12
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/sinon.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,6 @@ var behavior = require("./sinon/behavior");
exports.addBehavior = function (name, fn) {
behavior.addBehavior(exports.stub, name, fn);
};

var format = require("./sinon/util/core/format");
exports.setFormatter = format.setFormatter;
18 changes: 17 additions & 1 deletion lib/sinon/util/core/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ var formatter = formatio.configure({
limitChildrenCount: 250
});

module.exports = function format() {
var customFormatter;

function format() {
if (customFormatter) {
return customFormatter.apply(null, arguments);
}

return formatter.ascii.apply(formatter, arguments);
}

format.setFormatter = function (aCustomFormatter) {
if (typeof aCustomFormatter !== "function") {
throw new Error("format.setFormatter must be called with a function");
}

customFormatter = aCustomFormatter;
};

module.exports = format;
20 changes: 20 additions & 0 deletions test/util/core/format-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

var referee = require("referee");
var sinon = require("../../../lib/sinon");
var format = require("../../../lib/sinon/util/core/format");
var assert = referee.assert;

Expand All @@ -20,4 +21,23 @@ describe("util/core/format", function () {
it("formats strings without quotes", function () {
assert.equals(format("Hey"), "Hey");
});

describe("format.setFormatter", function () {
it("sets custom formatter", function () {
format.setFormatter(function () { return "formatted"; });
assert.equals(format("Hey"), "formatted");
});

it("throws if custom formatter is not a function", function () {
assert.exception(function () {
format.setFormatter("foo");
}, {
message: "format.setFormatter must be called with a function"
});
});

it("exposes method on sinon", function () {
assert.equals(sinon.setFormatter, format.setFormatter);
});
});
});

0 comments on commit 3938c12

Please sign in to comment.