From 4c6374ab84e21a8796037b255115f3d807ae35fa Mon Sep 17 00:00:00 2001 From: Jonny Reeves Date: Fri, 26 Apr 2013 22:01:40 +0100 Subject: [PATCH 1/3] #270 Added sinon.restore() --- lib/sinon.js | 17 +++++++++++++++++ test/sinon_test.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/lib/sinon.js b/lib/sinon.js index 4116929d7..f518beb23 100644 --- a/lib/sinon.js +++ b/lib/sinon.js @@ -49,6 +49,10 @@ var sinon = (function (buster) { } } + function isRestorable (obj) { + return typeof obj === "function" && typeof obj.restore === "function" && obj.restore.sinon; + } + var sinon = { wrapMethod: function wrapMethod(object, property, method) { if (!object) { @@ -299,6 +303,19 @@ var sinon = (function (buster) { throw new TypeError("The constructor should be a function."); } return sinon.stub(sinon.create(constructor.prototype)); + }, + + restore: function (object) { + if (object !== null && typeof object === "object") { + for (var prop in object) { + if (isRestorable(object[prop])) { + object[prop].restore(); + } + } + } + else if (isRestorable(object)) { + object.restore(); + } } }; diff --git a/test/sinon_test.js b/test/sinon_test.js index 2e98c759c..2b8418d63 100644 --- a/test/sinon_test.js +++ b/test/sinon_test.js @@ -500,5 +500,42 @@ buster.testCase("sinon", { }); } } + }, + + ".restore": { + "restores all methods of supplied object": function () { + var methodA = function () { }; + var methodB = function () { }; + var obj = { methodA: methodA, methodB: methodB }; + + sinon.stub(obj); + sinon.restore(obj); + + assert.same(obj.methodA, methodA); + assert.same(obj.methodB, methodB); + }, + + "only restores restorable methods": function () { + var stubbedMethod = function () { }; + var vanillaMethod = function () { }; + var obj = { stubbedMethod: stubbedMethod, vanillaMethod: vanillaMethod }; + + sinon.stub(obj, "stubbedMethod"); + sinon.restore(obj); + + assert.same(obj.stubbedMethod, stubbedMethod); + }, + + "restores a single stubbed method": function () + { + var method = function () { }; + var obj = { method: method }; + + sinon.stub(obj); + sinon.restore(obj.method); + + assert.same(obj.method, method); + } + } }); From d2a24f67c6065337837897ad28faf773bae4763f Mon Sep 17 00:00:00 2001 From: Jonny Reeves Date: Fri, 26 Apr 2013 22:02:04 +0100 Subject: [PATCH 2/3] Added Jonny Reeves to AUTHORS --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index cb4105d2d..41845806f 100644 --- a/AUTHORS +++ b/AUTHORS @@ -40,3 +40,4 @@ Tomás Corral Casas, amischol@gmail.com Tristan Koch, tristan.koch@1und1.de Will Butler, will@butlerhq.com Wesley Walser, waw325@gmail.com +Jonny Reeves, github@jonnyreeves.co.uk From 96935417cc9a67b9541c48f81b0aed83f072359d Mon Sep 17 00:00:00 2001 From: Jonny Reeves Date: Fri, 26 Apr 2013 22:23:57 +0100 Subject: [PATCH 3/3] Formatting --- test/sinon_test.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/test/sinon_test.js b/test/sinon_test.js index 2b8418d63..ace589ab8 100644 --- a/test/sinon_test.js +++ b/test/sinon_test.js @@ -504,8 +504,8 @@ buster.testCase("sinon", { ".restore": { "restores all methods of supplied object": function () { - var methodA = function () { }; - var methodB = function () { }; + var methodA = function () {}; + var methodB = function () {}; var obj = { methodA: methodA, methodB: methodB }; sinon.stub(obj); @@ -516,8 +516,8 @@ buster.testCase("sinon", { }, "only restores restorable methods": function () { - var stubbedMethod = function () { }; - var vanillaMethod = function () { }; + var stubbedMethod = function () {}; + var vanillaMethod = function () {}; var obj = { stubbedMethod: stubbedMethod, vanillaMethod: vanillaMethod }; sinon.stub(obj, "stubbedMethod"); @@ -526,9 +526,8 @@ buster.testCase("sinon", { assert.same(obj.stubbedMethod, stubbedMethod); }, - "restores a single stubbed method": function () - { - var method = function () { }; + "restores a single stubbed method": function () { + var method = function () {}; var obj = { method: method }; sinon.stub(obj); @@ -536,6 +535,5 @@ buster.testCase("sinon", { assert.same(obj.method, method); } - } });