Skip to content

Commit

Permalink
Merge pull request #2186 from sinonjs/move-printf-to-proxy
Browse files Browse the repository at this point in the history
Move printf to proxy
  • Loading branch information
fatso83 authored Jan 6, 2020
2 parents 5dbf8e2 + dd506cb commit eea3551
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 216 deletions.
20 changes: 20 additions & 0 deletions lib/sinon/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var functionToString = require("./util/core/function-to-string");
var proxyCall = require("./proxy-call");
var proxyCallUtil = require("./proxy-call-util");
var proxyInvoke = require("./proxy-invoke");
var sinonFormat = require("./util/core/format");

var push = arrayProto.push;
var forEach = arrayProto.forEach;
Expand Down Expand Up @@ -102,6 +103,25 @@ var proxyApi = {
return this.callIds[this.callCount - 1] === proxy.callIds[proxy.callCount - 1] + 1;
},

formatters: require("./spy-formatters"),
printf: function(format) {
var spyInstance = this;
var args = slice(arguments, 1);
var formatter;

return (format || "").replace(/%(.)/g, function(match, specifyer) {
formatter = proxyApi.formatters[specifyer];

if (typeof formatter === "function") {
return String(formatter(spyInstance, args));
} else if (!isNaN(parseInt(specifyer, 10))) {
return sinonFormat(args[specifyer - 1]);
}

return "%" + specifyer;
});
},

resetHistory: function() {
if (this.invoking) {
var err = new Error(
Expand Down
21 changes: 0 additions & 21 deletions lib/sinon/spy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ var isEsModule = require("./util/core/is-es-module");
var proxyCallUtil = require("./proxy-call-util");
var walkObject = require("./util/core/walk-object");
var wrapMethod = require("./util/core/wrap-method");
var sinonFormat = require("./util/core/format");
var valueToString = require("@sinonjs/commons").valueToString;

/* cache references to library methods so that they also can be stubbed without problems */
Expand All @@ -32,8 +31,6 @@ function matches(fake, args, strict) {

// Public API
var spyApi = {
formatters: require("./spy-formatters"),

withArgs: function() {
var args = slice(arguments);
var matching = pop(this.matchingFakes(args, true));
Expand Down Expand Up @@ -74,24 +71,6 @@ var spyApi = {
return filter.call(this.fakes, function(fake) {
return matches(fake, args, strict);
});
},

printf: function(format) {
var spyInstance = this;
var args = slice(arguments, 1);
var formatter;

return (format || "").replace(/%(.)/g, function(match, specifyer) {
formatter = spyApi.formatters[specifyer];

if (typeof formatter === "function") {
return String(formatter(spyInstance, args));
} else if (!isNaN(parseInt(specifyer, 10))) {
return sinonFormat(args[specifyer - 1]);
}

return "%" + specifyer;
});
}
};

Expand Down
10 changes: 10 additions & 0 deletions test/fake-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

var sinon = require("../lib/sinon.js");
var createProxy = require("../lib/sinon/proxy");
var fake = sinon.fake;
var referee = require("@sinonjs/referee");
var assert = referee.assert;
Expand Down Expand Up @@ -435,4 +436,13 @@ describe("fake", function() {
assert.isFalse(fakeA.calledImmediatelyAfter(fakeB));
});
});

describe(".printf", function() {
it("is delegated to proxy", function() {
var myFake = fake();
var proxy = createProxy(noop, noop);

assert.same(myFake.printf, proxy.printf);
});
});
});
195 changes: 0 additions & 195 deletions test/proxy-call-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";

var color = require("../lib/sinon/color");
var referee = require("@sinonjs/referee");
var proxyCall = require("../lib/sinon/proxy-call");
var sinonSpy = require("../lib/sinon/spy");
Expand Down Expand Up @@ -1541,200 +1540,6 @@ describe("sinonSpy.call", function() {
});
});

describe(".printf", function() {
describe("name", function() {
it("named", function() {
var named = sinonSpy(function cool() {
return;
});
assert.equals(named.printf("%n"), "cool");
});
it("anon", function() {
var anon = sinonSpy(function() {
return;
});
assert.equals(anon.printf("%n"), "spy");

var noFn = sinonSpy();
assert.equals(noFn.printf("%n"), "spy");
});
});

it("count", function() {
// Throwing just to make sure it has no effect.
var spy = sinonSpy(sinonStub().throws());
function call() {
assert.exception(function() {
spy();
});
}

call();
assert.equals(spy.printf("%c"), "once");
call();
assert.equals(spy.printf("%c"), "twice");
call();
assert.equals(spy.printf("%c"), "thrice");
call();
assert.equals(spy.printf("%c"), "4 times");
});

describe("calls", function() {
it("oneLine", function() {
function verify(arg, expected) {
var spy = sinonSpy();
spy(arg);
assert.equals(spy.printf("%C").replace(/ at.*/g, ""), "\n " + expected);
}

verify(true, "spy(true)");
verify(false, "spy(false)");
verify(undefined, "spy(undefined)");
verify(1, "spy(1)");
verify(0, "spy(0)");
verify(-1, "spy(-1)");
verify(-1.1, "spy(-1.1)");
verify(Infinity, "spy(Infinity)");
verify(["a"], 'spy(["a"])');
verify({ a: "a" }, 'spy({ a: "a" })');
});

it("multiline", function() {
var str = "spy\ntest";
var spy = sinonSpy();

spy(str);
spy(str);
spy(str);

assert.equals(
spy.printf("%C").replace(/ at.*/g, ""),
"\n spy(" + str + ")\n\n spy(" + str + ")\n\n spy(" + str + ")"
);

spy.resetHistory();

spy("test");
spy("spy\ntest");
spy("spy\ntest");

assert.equals(
spy.printf("%C").replace(/ at.*/g, ""),
"\n spy(test)\n spy(" + str + ")\n\n spy(" + str + ")"
);
});
});

it("thisValues", function() {
var spy = sinonSpy();
spy();
assert.equals(spy.printf("%t"), "undefined");

spy.resetHistory();
spy.call(true);
assert.equals(spy.printf("%t"), "true");
});

it("unmatched", function() {
var spy = sinonSpy();

assert.equals(spy.printf("%λ"), "%λ");
});

it("*", function() {
var spy = sinonSpy();

assert.equals(
spy.printf("%*", 1.4567, "a", true, {}, [], undefined, null),
"1.4567, a, true, { }, [], undefined, null"
);
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() {
var spy = sinonSpy();
spy();
Expand Down
Loading

0 comments on commit eea3551

Please sign in to comment.