2019-11-02 00:06:25 +01:00
|
|
|
const _ = require('underscore/underscore.js');
|
2017-03-11 19:45:10 +01:00
|
|
|
|
|
|
|
// Stubs don't do any magical modifications to your namespace. They
|
|
|
|
// just provide you a function that records what arguments get passed
|
|
|
|
// to it. To use stubs as something more like "spies," use something
|
|
|
|
// like set_global() to override your namespace.
|
|
|
|
|
|
|
|
exports.make_stub = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const self = {};
|
2017-03-11 19:45:10 +01:00
|
|
|
self.num_calls = 0;
|
|
|
|
|
2020-02-06 03:49:52 +01:00
|
|
|
self.f = function (...args) {
|
|
|
|
self.last_call_args = args;
|
2017-03-11 19:45:10 +01:00
|
|
|
self.num_calls += 1;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2020-02-06 03:49:52 +01:00
|
|
|
self.get_args = function (...param_names) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const result = {};
|
2017-03-11 19:45:10 +01:00
|
|
|
|
|
|
|
_.each(param_names, function (name, i) {
|
|
|
|
result[name] = self.last_call_args[i];
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
return self;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.with_stub = function (f) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stub = exports.make_stub();
|
2017-03-11 19:45:10 +01:00
|
|
|
f(stub);
|
|
|
|
assert.equal(stub.num_calls, 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
(function test_ourselves() {
|
|
|
|
exports.with_stub(function (stub) {
|
|
|
|
stub.f('blue', 42);
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = stub.get_args('color', 'n');
|
2017-03-11 19:45:10 +01:00
|
|
|
assert.equal(args.color, 'blue');
|
|
|
|
assert.equal(args.n, 42);
|
|
|
|
});
|
|
|
|
}());
|