From 82ef3d31a281985219919b20cfb4fccb84da98e0 Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Thu, 19 Mar 2015 17:33:31 -0700 Subject: [PATCH] Add exploding interface functions for Driver/Adaptor Explicit is better than implicit, and this causes errors to be more specific --- lib/adaptor.js | 28 ++++++++++++++++++++++++++++ lib/driver.js | 28 ++++++++++++++++++++++++++++ spec/lib/adaptor.spec.js | 31 ++++++++++++++++++++++++++++++- spec/lib/driver.spec.js | 31 ++++++++++++++++++++++++++++++- 4 files changed, 116 insertions(+), 2 deletions(-) diff --git a/lib/adaptor.js b/lib/adaptor.js index bb393f4..2e2d03e 100644 --- a/lib/adaptor.js +++ b/lib/adaptor.js @@ -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 diff --git a/lib/driver.js b/lib/driver.js index a574bfd..7b34109 100644 --- a/lib/driver.js +++ b/lib/driver.js @@ -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; diff --git a/spec/lib/adaptor.spec.js b/spec/lib/adaptor.spec.js index 3498f7c..28fa930 100644 --- a/spec/lib/adaptor.spec.js +++ b/spec/lib/adaptor.spec.js @@ -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(); + }); + }); + }); }); diff --git a/spec/lib/driver.spec.js b/spec/lib/driver.spec.js index 207b5a4..391b069 100644 --- a/spec/lib/driver.spec.js +++ b/spec/lib/driver.spec.js @@ -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;