Add exploding interface functions for Driver/Adaptor

Explicit is better than implicit, and this causes errors to be more
specific
This commit is contained in:
Andrew Stewart 2015-03-19 17:33:31 -07:00
parent af5300f6cd
commit 82ef3d31a2
4 changed files with 116 additions and 2 deletions

View File

@ -12,6 +12,10 @@ var Basestar = require("./basestar"),
Utils = require("./utils"),
_ = require("./utils/helpers");
function formatErrorMessage(name, message) {
return ["Error in connection", "'" + name + "'", "- " + message].join(" ");
}
// Public: Creates a new Adaptor
//
// opts - hash of acceptable params
@ -43,6 +47,30 @@ var Adaptor = module.exports = function Adaptor(opts) {
Utils.subclass(Adaptor, Basestar);
// Public: Basic #connect function. Must be overwritten by a descendent class
//
// Returns nothing, throws an error
Adaptor.prototype.connect = function() {
var message = formatErrorMessage(
this.name,
"Adaptor#connect method must be overwritten by descendant classes."
);
throw new Error(message);
};
// Public: Basic #disconnect function. Must be overwritten by a descendent class
//
// Returns nothing, throws an error
Adaptor.prototype.disconnect = function() {
var message = formatErrorMessage(
this.name,
"Adaptor#disconnect method must be overwritten by descendant classes."
);
throw new Error(message);
};
// Public: Expresses the Connection in JSON format
//
// Returns an Object containing Connection data

View File

@ -12,6 +12,10 @@ var Basestar = require("./basestar"),
Utils = require("./utils"),
_ = require("./utils/helpers");
function formatErrorMessage(name, message) {
return ["Error in driver", "'" + name + "'", "- " + message].join(" ");
}
// Public: Creates a new Driver
//
// opts - hash of acceptable params
@ -47,6 +51,30 @@ var Driver = module.exports = function Driver(opts) {
Utils.subclass(Driver, Basestar);
// Public: Basic #start function. Must be overwritten by a descendent class
//
// Returns nothing, throws an error
Driver.prototype.start = function() {
var message = formatErrorMessage(
this.name,
"Driver#start method must be overwritten by descendant classes."
);
throw new Error(message);
};
// Public: Basic #halt function. Must be overwritten by a descendent class
//
// Returns nothing, throws an error
Driver.prototype.halt = function() {
var message = formatErrorMessage(
this.name,
"Driver#halt method must be overwritten by descendant classes."
);
throw new Error(message);
};
Driver.prototype.setupCommands = function(commands, proxy) {
if (proxy == null) {
proxy = this.connection;

View File

@ -1,7 +1,8 @@
/* jshint expr:true */
"use strict";
var Adaptor = source("adaptor");
var Adaptor = source("adaptor"),
Utils = source("utils");
describe("Adaptor", function() {
var adaptor;
@ -15,4 +16,32 @@ describe("Adaptor", function() {
expect(adaptor.name).to.be.eql("adaptor");
});
});
describe("#interface methods", function() {
var child;
var Child = function Child() {};
Utils.subclass(Child, Adaptor);
beforeEach(function() {
child = new Child();
});
describe("#connect", function() {
it("throws an error unless overwritten", function() {
expect(child.connect).to.throw();
child.connect = function() {};
expect(child.connect).to.not.throw();
});
});
describe("#disconnect", function() {
it("throws an error unless overwritten", function() {
expect(child.disconnect).to.throw();
child.disconnect = function() {};
expect(child.disconnect).to.not.throw();
});
});
});
});

View File

@ -1,7 +1,8 @@
/* jshint expr:true */
"use strict";
var Driver = source("driver");
var Driver = source("driver"),
Utils = source("utils");
describe("Driver", function() {
var connection, driver;
@ -47,6 +48,34 @@ describe("Driver", function() {
});
});
describe("#interface methods", function() {
var child;
var Child = function Child() {};
Utils.subclass(Child, Driver);
beforeEach(function() {
child = new Child();
});
describe("#start", function() {
it("throws an error unless overwritten", function() {
expect(child.start).to.throw();
child.start = function() {};
expect(child.start).to.not.throw();
});
});
describe("#halt", function() {
it("throws an error unless overwritten", function() {
expect(child.halt).to.throw();
child.halt = function() {};
expect(child.halt).to.not.throw();
});
});
});
describe("#toJSON", function() {
var driver, json;