From f01d8478e14beb97060d6962e4408efd4dc15d96 Mon Sep 17 00:00:00 2001 From: Jay Sherby Date: Sun, 4 Mar 2018 18:38:00 -0600 Subject: [PATCH] Fix inconsistent newline usage %D Update affected unit tests Add tests for spy.printf %D text replacement --- lib/sinon/spy-formatters.js | 5 +-- test/assert-test.js | 11 +++--- test/call-test.js | 68 +++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 8 deletions(-) diff --git a/lib/sinon/spy-formatters.js b/lib/sinon/spy-formatters.js index 3c9ba95aa..309971323 100644 --- a/lib/sinon/spy-formatters.js +++ b/lib/sinon/spy-formatters.js @@ -48,10 +48,7 @@ module.exports = { for (var i = 0, l = spyInstance.callCount; i < l; ++i) { // describe multiple calls if (l > 1) { - if (i > 0) { - message += "\n"; - } - message += "Call " + (i + 1) + ":"; + message += "\nCall " + (i + 1) + ":"; } var calledArgs = spyInstance.getCall(i).args; for (var j = 0; j < calledArgs.length || j < args.length; ++j) { diff --git a/test/assert-test.js b/test/assert-test.js index 960d8fd66..0f4f0d5eb 100644 --- a/test/assert-test.js +++ b/test/assert-test.js @@ -1463,7 +1463,7 @@ describe("assert", function () { this.obj.doSomething(1, 3, "not"); assert.equals(this.message("calledWith", this.obj.doSomething, 1, 3, "hey").replace(/ at.*/g, ""), - "expected doSomething to be called with arguments " + + "expected doSomething to be called with arguments \n" + "Call 1:\n" + color.red("4") + " " + color.green("1") + " \n" + "3\n" + @@ -1652,7 +1652,8 @@ describe("assert", function () { this.obj.doSomething(1, "hey"); assert.equals(this.message("alwaysCalledWith", this.obj.doSomething, 1, "hey").replace(/ at.*/g, ""), - "expected doSomething to always be called with arguments Call 1:\n" + + "expected doSomething to always be called with arguments \n" + + "Call 1:\n" + "1\n" + color.red("3") + " " + color.green("hey") + " \n" + color.red("hey") + "\n" + @@ -1667,7 +1668,8 @@ describe("assert", function () { assert.equals( this.message("alwaysCalledWithMatch", this.obj.doSomething, 1, "hey").replace(/ at.*/g, ""), - "expected doSomething to always be called with match Call 1:\n" + + "expected doSomething to always be called with match \n" + + "Call 1:\n" + "1\n" + color.red("3") + " " + color.green("hey") + " \n" + color.red("hey") + "\n" + @@ -1691,7 +1693,8 @@ describe("assert", function () { this.obj.doSomething(1, 3); assert.equals(this.message("alwaysCalledWithExactly", this.obj.doSomething, 1, 3).replace(/ at.*/g, ""), - "expected doSomething to always be called with exact arguments Call 1:\n" + + "expected doSomething to always be called with exact arguments \n" + + "Call 1:\n" + "1\n" + "3\n" + color.red("hey") + "\n" + diff --git a/test/call-test.js b/test/call-test.js index aa602c0d9..a743e40e2 100644 --- a/test/call-test.js +++ b/test/call-test.js @@ -1,5 +1,6 @@ "use strict"; +var color = require("../lib/sinon/color"); var referee = require("@sinonjs/referee"); var sinonSpyCall = require("../lib/sinon/call"); var sinonSpy = require("../lib/sinon/spy"); @@ -1430,6 +1431,73 @@ describe("sinonSpy.call", function () { ); assert.equals(spy.printf("%*", "a", "b", "c"), "a, b, c"); }); + + describe("arguments", function () { + it("no calls", function () { + var spy = sinonSpy(); + + assert.equals(spy.printf("%D"), ""); + }); + + it("single call with arguments", function () { + var spy = sinonSpy(); + + spy(1, "a", true, false, [], {}, null, undefined); + + assert.equals( + spy.printf("%D"), + "\n" + color.red("1") + + "\n" + color.red("a") + + "\n" + color.red("true") + + "\n" + color.red("false") + + "\n" + color.red("[]") + + "\n" + color.red("{ }") + + "\n" + color.red("null") + + "\n" + color.red("undefined") + ); + }); + + it("single call without arguments", function () { + var spy = sinonSpy(); + + spy(); + + assert.equals(spy.printf("%D"), ""); + }); + + it("multiple calls with arguments", function () { + var spy = sinonSpy(); + + spy(1, "a", true); + spy(false, [], {}); + spy(null, undefined); + + assert.equals( + spy.printf("%D"), + "\nCall 1:" + + "\n" + color.red("1") + + "\n" + color.red("a") + + "\n" + color.red("true") + + "\nCall 2:" + + "\n" + color.red("false") + + "\n" + color.red("[]") + + "\n" + color.red("{ }") + + "\nCall 3:" + + "\n" + color.red("null") + + "\n" + color.red("undefined") + ); + }); + + it("multiple calls without arguments", function () { + var spy = sinonSpy(); + + spy(); + spy(); + spy(); + + assert.equals(spy.printf("%D"), "\nCall 1:\nCall 2:\nCall 3:"); + }); + }); }); it("captures a stack trace", function () {