From dd506cbe399a757014a5e0617b1ee193423937d3 Mon Sep 17 00:00:00 2001 From: Morgan Roderick Date: Tue, 31 Dec 2019 12:35:20 +0000 Subject: [PATCH] Use test-only proxy instance Instead of using spy, use a test only proxy instance for tests, showing that proxy can be used outside of spies. --- test/proxy-test.js | 151 ++++++++++++++++++++++++++------------------- 1 file changed, 89 insertions(+), 62 deletions(-) diff --git a/test/proxy-test.js b/test/proxy-test.js index 8cb7f6a3f..ee18417e0 100644 --- a/test/proxy-test.js +++ b/test/proxy-test.js @@ -1,16 +1,43 @@ "use strict"; var assert = require("@sinonjs/referee").assert; +var extend = require("../lib/sinon/util/core/extend"); +var createProxy = require("../lib/sinon/proxy"); var color = require("../lib/sinon/color"); var sinonSpy = require("../lib/sinon/spy"); var sinonStub = require("../lib/sinon/stub"); +var functionName = require("@sinonjs/commons").functionName; + +var uuid = 0; +function createFaux(func) { + var name; + var funk = func; + + if (typeof funk !== "function") { + funk = function() { + return; + }; + } else { + name = functionName(funk); + } + + var proxy = createProxy(funk, funk); + + extend.nonEnum(proxy, { + displayName: name || "faux", + fakes: [], + instantiateFake: createFaux, + id: "faux#" + uuid++ + }); + return proxy; +} describe("proxy", function() { describe(".printf", function() { describe("name", function() { it("named", function() { - var named = sinonSpy(function cool() { + var named = createFaux(function cool() { return; }); assert.equals(named.printf("%n"), "cool"); @@ -28,109 +55,109 @@ describe("proxy", function() { it("count", function() { // Throwing just to make sure it has no effect. - var spy = sinonSpy(sinonStub().throws()); + var faux = createFaux(sinonStub().throws()); function call() { assert.exception(function() { - spy(); + faux(); }); } call(); - assert.equals(spy.printf("%c"), "once"); + assert.equals(faux.printf("%c"), "once"); call(); - assert.equals(spy.printf("%c"), "twice"); + assert.equals(faux.printf("%c"), "twice"); call(); - assert.equals(spy.printf("%c"), "thrice"); + assert.equals(faux.printf("%c"), "thrice"); call(); - assert.equals(spy.printf("%c"), "4 times"); + assert.equals(faux.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); + var faux = createFaux(); + faux(arg); + assert.equals(faux.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" })'); + verify(true, "faux(true)"); + verify(false, "faux(false)"); + verify(undefined, "faux(undefined)"); + verify(1, "faux(1)"); + verify(0, "faux(0)"); + verify(-1, "faux(-1)"); + verify(-1.1, "faux(-1.1)"); + verify(Infinity, "faux(Infinity)"); + verify(["a"], 'faux(["a"])'); + verify({ a: "a" }, 'faux({ a: "a" })'); }); it("multiline", function() { - var str = "spy\ntest"; - var spy = sinonSpy(); + var str = "faux\ntest"; + var faux = createFaux(); - spy(str); - spy(str); - spy(str); + faux(str); + faux(str); + faux(str); assert.equals( - spy.printf("%C").replace(/ at.*/g, ""), - "\n spy(" + str + ")\n\n spy(" + str + ")\n\n spy(" + str + ")" + faux.printf("%C").replace(/ at.*/g, ""), + "\n faux(" + str + ")\n\n faux(" + str + ")\n\n faux(" + str + ")" ); - spy.resetHistory(); + faux.resetHistory(); - spy("test"); - spy("spy\ntest"); - spy("spy\ntest"); + faux("test"); + faux("faux\ntest"); + faux("faux\ntest"); assert.equals( - spy.printf("%C").replace(/ at.*/g, ""), - "\n spy(test)\n spy(" + str + ")\n\n spy(" + str + ")" + faux.printf("%C").replace(/ at.*/g, ""), + "\n faux(test)\n faux(" + str + ")\n\n faux(" + str + ")" ); }); }); it("thisValues", function() { - var spy = sinonSpy(); - spy(); - assert.equals(spy.printf("%t"), "undefined"); + var faux = createFaux(); + faux(); + assert.equals(faux.printf("%t"), "undefined"); - spy.resetHistory(); - spy.call(true); - assert.equals(spy.printf("%t"), "true"); + faux.resetHistory(); + faux.call(true); + assert.equals(faux.printf("%t"), "true"); }); it("unmatched", function() { - var spy = sinonSpy(); + var faux = createFaux(); - assert.equals(spy.printf("%λ"), "%λ"); + assert.equals(faux.printf("%λ"), "%λ"); }); it("*", function() { - var spy = sinonSpy(); + var faux = createFaux(); assert.equals( - spy.printf("%*", 1.4567, "a", true, {}, [], undefined, null), + faux.printf("%*", 1.4567, "a", true, {}, [], undefined, null), "1.4567, a, true, { }, [], undefined, null" ); - assert.equals(spy.printf("%*", "a", "b", "c"), "a, b, c"); + assert.equals(faux.printf("%*", "a", "b", "c"), "a, b, c"); }); describe("arguments", function() { it("no calls", function() { - var spy = sinonSpy(); + var faux = createFaux(); - assert.equals(spy.printf("%D"), ""); + assert.equals(faux.printf("%D"), ""); }); it("single call with arguments", function() { - var spy = sinonSpy(); + var faux = createFaux(); - spy(1, "a", true, false, [], {}, null, undefined); + faux(1, "a", true, false, [], {}, null, undefined); assert.equals( - spy.printf("%D"), + faux.printf("%D"), "\n" + color.red("1") + "\n" + @@ -151,22 +178,22 @@ describe("proxy", function() { }); it("single call without arguments", function() { - var spy = sinonSpy(); + var faux = createFaux(); - spy(); + faux(); - assert.equals(spy.printf("%D"), ""); + assert.equals(faux.printf("%D"), ""); }); it("multiple calls with arguments", function() { - var spy = sinonSpy(); + var faux = createFaux(); - spy(1, "a", true); - spy(false, [], {}); - spy(null, undefined); + faux(1, "a", true); + faux(false, [], {}); + faux(null, undefined); assert.equals( - spy.printf("%D"), + faux.printf("%D"), "\nCall 1:" + "\n" + color.red("1") + @@ -190,13 +217,13 @@ describe("proxy", function() { }); it("multiple calls without arguments", function() { - var spy = sinonSpy(); + var faux = createFaux(); - spy(); - spy(); - spy(); + faux(); + faux(); + faux(); - assert.equals(spy.printf("%D"), "\nCall 1:\nCall 2:\nCall 3:"); + assert.equals(faux.printf("%D"), "\nCall 1:\nCall 2:\nCall 3:"); }); }); });