js: Log warning if calling convention is not explicitly set

This commit is contained in:
Stein Magnus Jodal 2014-06-20 11:16:46 +02:00
parent c5028a8576
commit d15c7d0797
4 changed files with 87 additions and 16 deletions

View File

@ -97,7 +97,9 @@ Feature release.
- Add support for method calls with by-name arguments. The old calling - Add support for method calls with by-name arguments. The old calling
convention, ``by-position-only``, is still the default, but this will convention, ``by-position-only``, is still the default, but this will
probably change in the future. See the :ref:`mopidy-js` docs for details. change in the future. A warning is logged to the console if you don't
explicitly select a calling convention. See the :ref:`mopidy-js` docs for
details.
**MPD frontend** **MPD frontend**

View File

@ -84,7 +84,8 @@ Changelog
- Add support for method calls with by-name arguments. The old calling - Add support for method calls with by-name arguments. The old calling
convention, "by-position-only", is still the default, but this will change in convention, "by-position-only", is still the default, but this will change in
the future. See the docs for details. the future. A warning is printed to the console if you don't explicitly
select a calling convention. See the docs for details.
### 0.3.0 (2014-06-16) ### 0.3.0 (2014-06-16)

View File

@ -67,6 +67,12 @@ Mopidy.prototype._configure = function (settings) {
settings.backoffDelayMin = settings.backoffDelayMin || 1000; settings.backoffDelayMin = settings.backoffDelayMin || 1000;
settings.backoffDelayMax = settings.backoffDelayMax || 64000; settings.backoffDelayMax = settings.backoffDelayMax || 64000;
if (typeof settings.callingConvention === "undefined") {
this._console.warn(
"Mopidy.js is using the default calling convention. The " +
"default will change in the future. You should explicitly " +
"specify which calling convention you use.");
}
settings.callingConvention = ( settings.callingConvention = (
settings.callingConvention || "by-position-only"); settings.callingConvention || "by-position-only");

View File

@ -33,7 +33,10 @@ buster.testCase("Mopidy", {
close: this.stub(), close: this.stub(),
send: this.stub() send: this.stub()
}; };
this.mopidy = new Mopidy({webSocket: this.webSocket}); this.mopidy = new Mopidy({
callingConvention: "by-position-or-by-name",
webSocket: this.webSocket
});
}, },
tearDown: function () { tearDown: function () {
@ -42,7 +45,10 @@ buster.testCase("Mopidy", {
"constructor": { "constructor": {
"connects when autoConnect is true": function () { "connects when autoConnect is true": function () {
new Mopidy({autoConnect: true}); new Mopidy({
autoConnect: true,
callingConvention: "by-position-or-by-name"
});
var currentHost = typeof document !== "undefined" && var currentHost = typeof document !== "undefined" &&
document.location.host || "localhost"; document.location.host || "localhost";
@ -52,21 +58,73 @@ buster.testCase("Mopidy", {
}, },
"does not connect when autoConnect is false": function () { "does not connect when autoConnect is false": function () {
new Mopidy({autoConnect: false}); new Mopidy({
autoConnect: false,
callingConvention: "by-position-or-by-name"
});
refute.called(this.webSocketConstructorStub); refute.called(this.webSocketConstructorStub);
}, },
"does not connect when passed a WebSocket": function () { "does not connect when passed a WebSocket": function () {
new Mopidy({webSocket: {}}); new Mopidy({
callingConvention: "by-position-or-by-name",
webSocket: {}
});
refute.called(this.webSocketConstructorStub); refute.called(this.webSocketConstructorStub);
}, },
"defaults to by-position-only calling convention": function () {
var console = {
warn: function () {}
};
var mopidy = new Mopidy({
console: console,
webSocket: this.webSocket,
});
assert.equals(
mopidy._settings.callingConvention,
"by-position-only");
},
"warns if no calling convention explicitly selected": function () {
var console = {
warn: function () {}
};
var stub = this.stub(console, "warn");
new Mopidy({console: console});
assert.calledOnceWith(
stub,
"Mopidy.js is using the default calling convention. The " +
"default will change in the future. You should explicitly " +
"specify which calling convention you use.");
},
"does not warn if calling convention chosen explicitly": function () {
var console = {
warn: function () {}
};
var stub = this.stub(console, "warn");
new Mopidy({
callingConvention: "by-position-or-by-name",
console: console
});
refute.called(stub);
},
"works without 'new' keyword": function () { "works without 'new' keyword": function () {
var mopidyConstructor = Mopidy; // To trick jshint into submission var mopidyConstructor = Mopidy; // To trick jshint into submission
var mopidy = mopidyConstructor({webSocket: {}}); var mopidy = mopidyConstructor({
callingConvention: "by-position-or-by-name",
webSocket: {}
});
assert.isObject(mopidy); assert.isObject(mopidy);
assert(mopidy instanceof Mopidy); assert(mopidy instanceof Mopidy);
@ -75,7 +133,10 @@ buster.testCase("Mopidy", {
".connect": { ".connect": {
"connects when autoConnect is false": function () { "connects when autoConnect is false": function () {
var mopidy = new Mopidy({autoConnect: false}); var mopidy = new Mopidy({
autoConnect: false,
callingConvention: "by-position-or-by-name"
});
refute.called(this.webSocketConstructorStub); refute.called(this.webSocketConstructorStub);
mopidy.connect(); mopidy.connect();
@ -89,7 +150,10 @@ buster.testCase("Mopidy", {
"does nothing when the WebSocket is open": function () { "does nothing when the WebSocket is open": function () {
this.webSocket.readyState = Mopidy.WebSocket.OPEN; this.webSocket.readyState = Mopidy.WebSocket.OPEN;
var mopidy = new Mopidy({webSocket: this.webSocket}); var mopidy = new Mopidy({
callingConvention: "by-position-or-by-name",
webSocket: this.webSocket
});
mopidy.connect(); mopidy.connect();
@ -766,8 +830,12 @@ buster.testCase("Mopidy", {
assert.calledOnceWith(spy); assert.calledOnceWith(spy);
}, },
"by-position calling convention": { "by-position-only calling convention": {
setUp: function () { setUp: function () {
this.mopidy = new Mopidy({
webSocket: this.webSocket,
callingConvention: "by-position-only"
});
this.mopidy._createApi({ this.mopidy._createApi({
foo: { foo: {
params: ["bar", "baz"] params: ["bar", "baz"]
@ -777,12 +845,6 @@ buster.testCase("Mopidy", {
}, },
"is the default": function () {
assert.equals(
this.mopidy._settings.callingConvention,
"by-position-only");
},
"sends no params if no arguments passed to function": function () { "sends no params if no arguments passed to function": function () {
this.mopidy.foo(); this.mopidy.foo();