Comparing version 0.9.2 to 0.10.0
@@ -5,3 +5,3 @@ var path = require('path'); | ||
this.expectations = []; | ||
this.hijacked = {}; | ||
this.hijacked = {}; | ||
@@ -12,3 +12,3 @@ var self = this; | ||
}); | ||
}; | ||
} | ||
module.exports = Gently; | ||
@@ -19,9 +19,9 @@ | ||
return Stub['new'].apply(this, arguments); | ||
}; | ||
} | ||
Stub['new'] = function () {}; | ||
Stub['new'] = function() {}; | ||
var stubName = 'require('+JSON.stringify(location)+')'; | ||
var stubName = 'require(' + JSON.stringify(location) + ')'; | ||
if (exportsName) { | ||
stubName += '.'+exportsName; | ||
stubName += '.' + exportsName; | ||
} | ||
@@ -47,5 +47,7 @@ | ||
return function(location) { | ||
return self.hijacked[location] = (self.hijacked[location]) | ||
? self.hijacked[location] | ||
: realRequire(location); | ||
if (!self.hijacked[location]) { | ||
self.hijacked[location] = realRequire(location); | ||
} | ||
return self.hijacked[location]; | ||
}; | ||
@@ -55,18 +57,24 @@ }; | ||
Gently.prototype.expect = function(obj, method, count, stubFn) { | ||
var stackError = new Error(); | ||
var caller = stackError.stack.split('\n')[2].trim(); | ||
if (typeof obj != 'function' && typeof obj != 'object' && typeof obj != 'number') { | ||
throw new Error | ||
( 'Bad 1st argument for gently.expect(), ' | ||
+ 'object, function, or number expected, got: '+(typeof obj) | ||
); | ||
var eMsg = ''; | ||
eMsg += 'Bad 1st argument for gently.expect(), '; | ||
eMsg += 'object, function, or number expected, got: '; | ||
eMsg += (typeof obj); | ||
throw new Error(eMsg); | ||
} else if (typeof obj == 'function' && (typeof method != 'string')) { | ||
// expect(stubFn) interface | ||
stubFn = obj; | ||
obj = null; | ||
obj = null; | ||
method = null; | ||
count = 1; | ||
count = 1; | ||
} else if (typeof method == 'function') { | ||
// expect(count, stubFn) interface | ||
count = obj; | ||
count = obj; | ||
stubFn = method; | ||
obj = null; | ||
obj = null; | ||
method = null; | ||
@@ -76,3 +84,3 @@ } else if (typeof count == 'function') { | ||
stubFn = count; | ||
count = 1; | ||
count = 1; | ||
} else if (count === undefined) { | ||
@@ -84,5 +92,13 @@ // expect(obj, method) interface | ||
var name = this._name(obj, method, stubFn); | ||
this.expectations.push({obj: obj, method: method, stubFn: stubFn, name: name, count: count}); | ||
this.expectations.push({ | ||
obj : obj, | ||
method : method, | ||
stubFn : stubFn, | ||
name : name, | ||
count : count, | ||
caller : caller | ||
}); | ||
var self = this; | ||
function delegate() { | ||
@@ -96,8 +112,6 @@ return self._stubFn(this, obj, method, name, Array.prototype.slice.call(arguments)); | ||
var original = (obj[method]) | ||
? obj[method]._original || obj[method] | ||
: undefined; | ||
var original = (obj[method]) ? obj[method]._original || obj[method] : undefined; | ||
obj[method] = delegate; | ||
return obj[method]._original = original; | ||
return (obj[method]._original = original); | ||
}; | ||
@@ -107,3 +121,3 @@ | ||
if (!obj[method] || !obj[method]._original) { | ||
throw new Error(this._name(obj, method)+' is not gently stubbed'); | ||
throw new Error(this._name(obj, method) + ' is not gently stubbed'); | ||
} | ||
@@ -133,22 +147,24 @@ obj[method] = obj[method]._original; | ||
var expectation = validExpectations[0]; | ||
var firstExpectation = validExpectations[0]; | ||
throw new Error | ||
( 'Expected call to '+expectation.name+' did not happen' | ||
+ ( (msg) | ||
? ' ('+msg+')' | ||
: '' | ||
) | ||
); | ||
var eMsg = ''; | ||
eMsg += 'Expected call to ' + firstExpectation.name + ' did not happen'; | ||
if (firstExpectation.caller) { | ||
eMsg += ' ' + firstExpectation.caller; | ||
} | ||
if (msg) { | ||
eMsg += ' (' + msg + ')'; | ||
} | ||
throw new Error(eMsg); | ||
}; | ||
Gently.prototype._stubFn = function(self, obj, method, name, args) { | ||
var expectation = this.expectations[0], obj, method; | ||
var expectation = this.expectations[0]; | ||
if (!expectation) { | ||
throw new Error('Unexpected call to '+name+', no call was expected'); | ||
throw new Error('Unexpected call to ' + name + ', no call was expected'); | ||
} | ||
if (expectation.obj !== obj || expectation.method !== method) { | ||
throw new Error('Unexpected call to '+name+', expected call to '+ expectation.name); | ||
throw new Error('Unexpected call to ' + name + ', expected call to ' + expectation.name); | ||
} | ||
@@ -162,3 +178,3 @@ | ||
// and no more expectations on that object | ||
var has_more_expectations = this.expectations.reduce(function (memo, expectation) { | ||
var has_more_expectations = this.expectations.reduce(function(memo, expectation) { | ||
return memo || (expectation.obj === obj && expectation.method === method); | ||
@@ -185,12 +201,12 @@ }, false); | ||
if (objectName == '[object Object]' && obj.constructor.name) { | ||
objectName = '['+obj.constructor.name+']'; | ||
objectName = '[' + obj.constructor.name + ']'; | ||
} | ||
return (objectName)+'.'+method+'()'; | ||
return (objectName) + '.' + method + '()'; | ||
} | ||
if (stubFn.name) { | ||
return stubFn.name+'()'; | ||
return stubFn.name + '()'; | ||
} | ||
return '>> '+stubFn.toString()+' <<'; | ||
return '>> ' + stubFn.toString() + ' <<'; | ||
}; |
{ | ||
"name": "gently", | ||
"version": "0.9.2", | ||
"version": "0.10.0", | ||
"directories": { | ||
@@ -8,2 +8,6 @@ "lib": "./lib/gently" | ||
"main": "./lib/gently/index", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/felixge/node-gently.git" | ||
}, | ||
"dependencies": {}, | ||
@@ -10,0 +14,0 @@ "devDependencies": {}, |
var path = require('path') | ||
, sys = require('sys'); | ||
, util = require('util'); | ||
require.paths.unshift(path.dirname(__dirname)+'/lib'); | ||
global.puts = sys.puts; | ||
global.p = function() {sys.error(sys.inspect.apply(null, arguments))};; | ||
global.assert = require('assert'); | ||
global.puts = console.log; | ||
global.p = function() { | ||
console.error(util.inspect.apply(null, arguments)); | ||
}; | ||
global.assert = require('assert'); |
require('../common'); | ||
var Gently = require('gently') | ||
var Gently = require('../..') | ||
, gently; | ||
@@ -283,3 +283,3 @@ | ||
} catch (e) { | ||
assert.equal(e.message, 'Expected call to [OBJ].foo() did not happen'); | ||
assert.ok(e.message.match(/Expected call to \[OBJ\].foo\(\) did not happen at/), 'Expected call to [OBJ].foo() did not happen at <location>'); | ||
} | ||
@@ -289,5 +289,5 @@ | ||
gently.verify('foo'); | ||
assert.ok(false, 'throw needs to happen'); | ||
assert.ok(false, 'throw does not need to happen, as verify() cleared out the expectations'); | ||
} catch (e) { | ||
assert.equal(e.message, 'Expected call to [OBJ].foo() did not happen (foo)'); | ||
assert.equal(e.message, 'throw does not need to happen, as verify() cleared out the expectations'); | ||
} | ||
@@ -350,2 +350,2 @@ }); | ||
} | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
21396
11
483
175
1