Socket
Socket
Sign inDemoInstall

sinon

Package Overview
Dependencies
Maintainers
2
Versions
208
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sinon - npm Package Compare versions

Comparing version 1.3.1 to 1.3.2

pkg/sinon-1.3.2.js

14

Changelog.txt

@@ -0,1 +1,15 @@

== 1.3.2 / 2012-03-11
* Stronger Node inference in sandbox
* Fixed issue with sinon.useFakeTimers() and Rhino.js 1.7R3
* Formatting brush-up
* FIX Internet Explorer misreporting the type of Function objects
originating in an external window as "object" instead of "function".
* New methods stub.callsArgOn, stub.callsArgOnWith,
stub.yieldsOn, stub.yieldsToOn
* Implemented
* Fixing `clearTimeout` to not throw when called for nonexistant IDs.
* Spys that are created using 'withArgs' now get initialized with previous
calls to the original spy.
* Minor bug fixes and docs cleanup.
== 1.3.1 / 2012-01-04

@@ -2,0 +16,0 @@ * Fix bug in core sinon: isNode was used for both a variable and a function,

11

lib/sinon.js

@@ -40,2 +40,6 @@ /*jslint eqeqeq: false, onevar: false, forin: true, nomen: false, regexp: false, plusplus: false*/

function isFunction(obj) {
return !!(obj && obj.constructor && obj.call && obj.apply);
}
function mirrorProperties(target, source) {

@@ -60,6 +64,5 @@ for (var prop in source) {

var wrappedMethod = object[property];
var type = typeof wrappedMethod;
if (type != "function") {
throw new TypeError("Attempted to wrap " + type + " property " +
if (!isFunction(wrappedMethod)) {
throw new TypeError("Attempted to wrap " + (typeof wrappedMethod) + " property " +
property + " as function");

@@ -307,3 +310,3 @@ }

/* Node, but no util module - would be very old, but better safe than
sorry */
sorry */
}

@@ -310,0 +313,0 @@ }

@@ -122,5 +122,5 @@ /**

if (typeof module != "undefined") {
if (typeof module == "object" && typeof require == "function") {
module.exports = sinon.sandbox;
}
}());

@@ -86,2 +86,17 @@ /* @depend ../sinon.js */

function incrementCallCount() {
this.called = true;
this.callCount += 1;
this.calledOnce = this.callCount == 1;
this.calledTwice = this.callCount == 2;
this.calledThrice = this.callCount == 3;
}
function createCallProperties() {
this.firstCall = this.getCall(0);
this.secondCall = this.getCall(1);
this.thirdCall = this.getCall(2);
this.lastCall = this.getCall(this.callCount - 1);
}
var uuid = 0;

@@ -138,7 +153,4 @@

var exception, returnValue;
this.called = true;
this.callCount += 1;
this.calledOnce = this.callCount == 1;
this.calledTwice = this.callCount == 2;
this.calledThrice = this.callCount == 3;
incrementCallCount.call(this);
push.call(this.thisValues, thisValue);

@@ -164,6 +176,3 @@ push.call(this.args, args);

this.firstCall = this.getCall(0);
this.secondCall = this.getCall(1);
this.thirdCall = this.getCall(2);
this.lastCall = this.getCall(this.callCount - 1);
createCallProperties.call(this);

@@ -225,2 +234,14 @@ return returnValue;

for (var i = 0; i < this.args.length; i++) {
if (fake.matches(this.args[i])) {
incrementCallCount.call(fake);
push.call(fake.thisValues, this.thisValues[i]);
push.call(fake.args, this.args[i]);
push.call(fake.returnValues, this.returnValues[i]);
push.call(fake.exceptions, this.exceptions[i]);
push.call(fake.callIds, this.callIds[i]);
}
}
createCallProperties.call(fake);
return fake;

@@ -227,0 +248,0 @@ },

@@ -110,3 +110,3 @@ /**

func.apply(null, stub.callbackArguments);
func.apply(stub.callbackContext, stub.callbackArguments);
}

@@ -190,2 +190,17 @@ }

callsArgOn: function callsArgOn(pos, context) {
if (typeof pos != "number") {
throw new TypeError("argument index is not number");
}
if (typeof context != "object") {
throw new TypeError("argument context is not an object");
}
this.callArgAt = pos;
this.callbackArguments = [];
this.callbackContext = context;
return this;
},
callsArgWith: function callsArgWith(pos) {

@@ -202,2 +217,17 @@ if (typeof pos != "number") {

callsArgOnWith: function callsArgWith(pos, context) {
if (typeof pos != "number") {
throw new TypeError("argument index is not number");
}
if (typeof context != "object") {
throw new TypeError("argument context is not an object");
}
this.callArgAt = pos;
this.callbackArguments = slice.call(arguments, 2);
this.callbackContext = context;
return this;
},
yields: function () {

@@ -210,2 +240,14 @@ this.callArgAt = -1;

yieldsOn: function (context) {
if (typeof context != "object") {
throw new TypeError("argument context is not an object");
}
this.callArgAt = -1;
this.callbackArguments = slice.call(arguments, 1);
this.callbackContext = context;
return this;
},
yieldsTo: function (prop) {

@@ -217,2 +259,15 @@ this.callArgAt = -1;

return this;
},
yieldsToOn: function (prop, context) {
if (typeof context != "object") {
throw new TypeError("argument context is not an object");
}
this.callArgAt = -1;
this.callArgProp = prop;
this.callbackArguments = slice.call(arguments, 2);
this.callbackContext = context;
return this;
}

@@ -219,0 +274,0 @@ };

@@ -121,3 +121,5 @@ /*jslint eqeqeq: false, plusplus: false, evil: true, onevar: false, browser: true, forin: false*/

delete this.timeouts[timerId];
if (timerId in this.timeouts) {
delete this.timeouts[timerId];
}
},

@@ -326,3 +328,3 @@

};
}(typeof global != "undefined" ? global : this));
}(typeof global != "undefined" && typeof global !== "function" ? global : this));

@@ -329,0 +331,0 @@ sinon.timers = {

@@ -195,6 +195,7 @@ /**

});
if (defake) { sinon.FakeXMLHttpRequest.defake(this,arguments); }
} else {
this.readyStateChange(FakeXMLHttpRequest.OPENED);
if (defake) {
return sinon.FakeXMLHttpRequest.defake(this,arguments);
}
}
this.readyStateChange(FakeXMLHttpRequest.OPENED);
},

@@ -201,0 +202,0 @@

{
"name": "sinon",
"description": "JavaScript test spies, stubs and mocks.",
"version": "1.3.1",
"version": "1.3.2",
"homepage": "http://cjohansen.no/sinon/",

@@ -6,0 +6,0 @@ "author": "Christian Johansen",

/**
* Sinon.JS 1.3.1, 2012/01/04
* Sinon.JS 1.3.2, 2012/03/11
*

@@ -4,0 +4,0 @@ * @author Christian Johansen (christian@cjohansen.no)

@@ -9,3 +9,3 @@ # Sinon.JS

$ sudo npm install sinon
$ npm install sinon

@@ -15,5 +15,8 @@ or install via git by cloning the repository and including sinon.js

Don't forget to include the parts of Sinon.JS that you want to use as well
(i.e. spy.js).
## Usage
See the [sinon project homepage](http://cjohansen.no/sinon/)
See the [sinon project homepage](http://sinonjs.org/)

@@ -20,0 +23,0 @@ ## Goals

@@ -13,2 +13,3 @@ /*jslint onevar: false*/

assertEquals
assertNoException
assertException*/

@@ -278,2 +279,146 @@ /**

testCase("StubCallsArgOnTest", {
setUp: function () {
this.stub = sinon.stub.create();
this.fakeContext = {
foo: 'bar'
};
},
"should call argument at specified index": function () {
this.stub.callsArgOn(2, this.fakeContext);
var callback = sinon.stub.create();
this.stub(1, 2, callback);
assert(callback.called);
assert(callback.calledOn(this.fakeContext));
},
"should return stub": function () {
var stub = this.stub.callsArgOn(2, this.fakeContext);
assertFunction(stub);
},
"should throw if argument at specified index is not callable": function () {
this.stub.callsArgOn(0, this.fakeContext);
assertException(function () {
this.stub(1);
}, "TypeError");
},
"should throw if no index is specified": function () {
var stub = this.stub;
assertException(function () {
stub.callsArgOn();
}, "TypeError");
},
"should throw if no context is specified": function () {
var stub = this.stub;
assertException(function () {
stub.callsArgOn(3);
}, "TypeError");
},
"should throw if index is not number": function () {
var stub = this.stub;
assertException(function () {
stub.callsArgOn(this.fakeContext, 2);
}, "TypeError");
},
"should throw if context is not an object": function () {
var stub = this.stub;
assertException(function () {
stub.callsArgOn(2, 2);
}, "TypeError");
}
});
testCase("StubCallsArgOnWithTest", {
setUp: function () {
this.stub = sinon.stub.create();
this.fakeContext = {
foo: 'bar'
};
},
"should call argument at specified index with provided args": function () {
var object = {};
this.stub.callsArgOnWith(1, this.fakeContext, object);
var callback = sinon.stub.create();
this.stub(1, callback);
assert(callback.calledWith(object));
assert(callback.calledOn(this.fakeContext));
},
"should return function": function () {
var stub = this.stub.callsArgOnWith(2, this.fakeContext, 3);
assertFunction(stub);
},
"should call callback without args": function () {
this.stub.callsArgOnWith(1, this.fakeContext);
var callback = sinon.stub.create();
this.stub(1, callback);
assert(callback.calledWith());
assert(callback.calledOn(this.fakeContext));
},
"should call callback wit multiple args": function () {
var object = {};
var array = [];
this.stub.callsArgOnWith(1, this.fakeContext, object, array);
var callback = sinon.stub.create();
this.stub(1, callback);
assert(callback.calledWith(object, array));
assert(callback.calledOn(this.fakeContext));
},
"should throw if no index is specified": function () {
var stub = this.stub;
assertException(function () {
stub.callsArgOnWith();
}, "TypeError");
},
"should throw if no context is specified": function () {
var stub = this.stub;
assertException(function () {
stub.callsArgOnWith(3);
}, "TypeError");
},
"should throw if index is not number": function () {
var stub = this.stub;
assertException(function () {
stub.callsArgOnWith({});
}, "TypeError");
},
"should throw if context is not an object": function () {
var stub = this.stub;
assertException(function () {
stub.callsArgOnWith(2, 2);
}, "TypeError");
}
});
testCase("StubObjectMethodTest", {

@@ -429,3 +574,3 @@ setUp: function () {

"should stub prototype methods": function () {
function Obj() {};
function Obj() {}
Obj.prototype.func1 = function() {};

@@ -498,3 +643,2 @@ var obj = new Obj();

var stub = sinon.stub().yields();
var spy = sinon.spy();

@@ -513,3 +657,2 @@ try {

var stub = sinon.stub(myObj, "somethingAwesome").yields();
var spy = sinon.spy();

@@ -559,3 +702,3 @@ try {

assertException(function () {
stub(spy);
stub(callback);
});

@@ -565,2 +708,98 @@ }

testCase("StubYieldsOnTest", {
setUp: function () {
this.stub = sinon.stub.create();
this.fakeContext = {
foo: 'bar'
};
},
"should invoke only argument as callback": function () {
var spy = sinon.spy();
this.stub.yieldsOn(this.fakeContext);
this.stub(spy);
assert(spy.calledOnce);
assert(spy.calledOn(this.fakeContext));
assertEquals(0, spy.args[0].length);
},
"should throw if no context is specified": function () {
assertException(function () {
this.stub.yieldsOn();
}, "TypeError");
},
"should throw understandable error if no callback is passed": function () {
this.stub.yieldsOn(this.fakeContext);
try {
this.stub();
throw new Error();
} catch (e) {
assertEquals("stub expected to yield, but no callback was passed.",
e.message);
}
},
"should include stub name and actual arguments in error": function () {
var myObj = { somethingAwesome: function () {} };
var stub = sinon.stub(myObj, "somethingAwesome").yieldsOn(this.fakeContext);
try {
stub(23, 42);
throw new Error();
} catch (e) {
assertEquals("somethingAwesome expected to yield, but no callback " +
"was passed. Received [23, 42]", e.message);
}
},
"should invoke last argument as callback": function () {
var spy = sinon.spy();
this.stub.yieldsOn(this.fakeContext);
this.stub(24, {}, spy);
assert(spy.calledOnce);
assert(spy.calledOn(this.fakeContext));
assertEquals(0, spy.args[0].length);
},
"should invoke first of two callbacks": function () {
var spy = sinon.spy();
var spy2 = sinon.spy();
this.stub.yieldsOn(this.fakeContext);
this.stub(24, {}, spy, spy2);
assert(spy.calledOnce);
assert(spy.calledOn(this.fakeContext));
assert(!spy2.called);
},
"should invoke callback with arguments": function () {
var obj = { id: 42 };
var spy = sinon.spy();
this.stub.yieldsOn(this.fakeContext, obj, "Crazy");
this.stub(spy);
assert(spy.calledWith(obj, "Crazy"));
assert(spy.calledOn(this.fakeContext));
},
"should throw if callback throws": function () {
var obj = { id: 42 };
var callback = sinon.stub().throws();
this.stub.yieldsOn(this.fakeContext, obj, "Crazy");
assertException(function () {
this.stub(callback);
});
}
});
// yieldsTo burde kunne kalles flere ganger?

@@ -644,2 +883,100 @@ testCase("StubYieldsToTest", {

testCase("StubYieldsToOnTest", {
setUp: function () {
this.stub = sinon.stub.create();
this.fakeContext = {
foo: 'bar'
};
},
"should yield to property of object argument": function () {
this.stub.yieldsToOn("success", this.fakeContext);
var callback = sinon.spy();
this.stub({ success: callback });
assert(callback.calledOnce);
assert(callback.calledOn(this.fakeContext));
assertEquals(0, callback.args[0].length);
},
"should throw if no context is specified": function () {
assertException(function () {
this.stub.yieldsToOn("success");
}, "TypeError");
},
"should throw understandable error if no object with callback is passed": function () {
this.stub.yieldsToOn("success", this.fakeContext);
try {
this.stub();
throw new Error();
} catch (e) {
assertEquals("stub expected to yield to 'success', but no object "+
"with such a property was passed.",
e.message);
}
},
"should include stub name and actual arguments in error": function () {
var myObj = { somethingAwesome: function () {} };
var stub = sinon.stub(myObj, "somethingAwesome").yieldsToOn("success", this.fakeContext);
try {
stub(23, 42);
throw new Error();
} catch (e) {
assertEquals("somethingAwesome expected to yield to 'success', but " +
"no object with such a property was passed. " +
"Received [23, 42]", e.message);
}
},
"should invoke property on last argument as callback": function () {
var callback = sinon.spy();
this.stub.yieldsToOn("success", this.fakeContext);
this.stub(24, {}, { success: callback });
assert(callback.calledOnce);
assert(callback.calledOn(this.fakeContext));
assertEquals(0, callback.args[0].length);
},
"should invoke first of two possible callbacks": function () {
var callback = sinon.spy();
var callback2 = sinon.spy();
this.stub.yieldsToOn("error", this.fakeContext);
this.stub(24, {}, { error: callback }, { error: callback2 });
assert(callback.calledOnce);
assert(callback.calledOn(this.fakeContext));
assert(!callback2.called);
},
"should invoke callback with arguments": function () {
var obj = { id: 42 };
var callback = sinon.spy();
this.stub.yieldsToOn("success", this.fakeContext, obj, "Crazy");
this.stub({ success: callback });
assert(callback.calledOn(this.fakeContext));
assert(callback.calledWith(obj, "Crazy"));
},
"should throw if callback throws": function () {
var obj = { id: 42 };
var callback = sinon.stub().throws();
this.stub.yieldsToOn("error", this.fakeContext, obj, "Crazy");
assertException(function () {
this.stub({ error: callback });
});
}
});
testCase("StubWithArgsTest", {

@@ -646,0 +983,0 @@ "should define withArgs method": function () {

@@ -1046,2 +1046,13 @@ /*jslint onevar: false, eqeqeq: false, browser: true*/

});
},
"should perform initial readystatechange on opening when filters are being used, but don't match": function() {
try {
sinon.FakeXMLHttpRequest.useFilters = true;
var spy = sinon.spy();
fakeXhr.addEventListener("readystatechange",spy);
fakeXhr.open("GET","http://example.com",true);
sinon.assert.calledOnce(spy);
} finally {
sinon.FakeXMLHttpRequest.useFilters = false;
}
}

@@ -1059,2 +1070,3 @@ });

sinon.FakeXMLHttpRequest.useFilters = false;
sinon.FakeXMLHttpRequest.filters = [];
sinon.FakeXMLHttpRequest.restore();

@@ -1061,0 +1073,0 @@ },

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc