diff --git a/dist/robot.js b/dist/robot.js index 292bf00..a173355 100644 --- a/dist/robot.js +++ b/dist/robot.js @@ -62,6 +62,7 @@ this.drivers = {}; this.commands = []; this.registerAdaptor("./test/loopback", "loopback"); + this.registerAdaptor("./test/test-adaptor", "test"); this.registerDriver("./test/ping", "ping"); this.testing = process.env['CYLON_TEST']; this.initConnections(opts.connection || opts.connections); @@ -194,20 +195,29 @@ }; Robot.prototype.initAdaptor = function(adaptorName, connection, opts) { + var realAdaptor, testAdaptor; if (opts == null) { opts = {}; } - return this.robot.requireAdaptor(adaptorName).adaptor({ + realAdaptor = this.robot.requireAdaptor(adaptorName).adaptor({ name: adaptorName, connection: connection, extraParams: opts }); + if (this.robot.testing != null) { + testAdaptor = this.robot.requireAdaptor('test').adaptor({ + name: adaptorName, + connection: connection, + extraParams: opts + }); + testAdaptor.proxyTestCommands(realAdaptor); + return testAdaptor; + } else { + return realAdaptor; + } }; Robot.prototype.requireAdaptor = function(adaptorName) { - if (this.robot.testing != null) { - return this.robot.adaptors['loopback']; - } if (this.robot.adaptors[adaptorName] == null) { this.robot.registerAdaptor("cylon-" + adaptorName, adaptorName); this.robot.adaptors[adaptorName].register(this); @@ -216,9 +226,11 @@ }; Robot.prototype.registerAdaptor = function(moduleName, adaptorName) { + console.log('here ' + moduleName); if (this.adaptors[adaptorName] == null) { - return this.adaptors[adaptorName] = require(moduleName); + this.adaptors[adaptorName] = require(moduleName); } + return console.log('there'); }; Robot.prototype.initDriver = function(driverName, device, opts) { diff --git a/dist/test/test-adaptor.js b/dist/test/test-adaptor.js new file mode 100644 index 0000000..fe948d0 --- /dev/null +++ b/dist/test/test-adaptor.js @@ -0,0 +1,74 @@ +/* + * Test adaptor + * cylonjs.com + * + * Copyright (c) 2013 The Hybrid Group + * Licensed under the Apache 2.0 license. +*/ + + +(function() { + 'use strict'; + var namespace, + __slice = [].slice, + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; + + namespace = require('node-namespace'); + + module.exports = { + adaptor: function() { + var args; + args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; + return (function(func, args, ctor) { + ctor.prototype = func.prototype; + var child = new ctor, result = func.apply(child, args); + return Object(result) === result ? result : child; + })(Cylon.Adaptors.TestAdaptor, args, function(){}); + } + }; + + namespace('Cylon.Adaptors', function() { + return this.TestAdaptor = (function(_super) { + var method, _fn, _i, _len, _ref; + + __extends(TestAdaptor, _super); + + function TestAdaptor(opts) { + if (opts == null) { + opts = {}; + } + TestAdaptor.__super__.constructor.apply(this, arguments); + this.commandList = []; + } + + TestAdaptor.prototype.commands = function() { + return this.commandList; + }; + + TestAdaptor.prototype.proxyTestCommands = function(realAdaptor) {}; + + _ref = realAdaptor.commands(); + _fn = function(method) { + this.self[method] = function() { + var args; + args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; + return this.self.success(); + }; + return this.commandList.push(method); + }; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + method = _ref[_i]; + _fn(method); + } + + TestAdaptor.prototype.success = function() { + return true; + }; + + return TestAdaptor; + + })(Cylon.Adaptor); + }); + +}).call(this); diff --git a/src/robot.coffee b/src/robot.coffee index 57cb3ba..28c0550 100644 --- a/src/robot.coffee +++ b/src/robot.coffee @@ -60,6 +60,7 @@ namespace 'Cylon', -> @commands = [] @registerAdaptor "./test/loopback", "loopback" + @registerAdaptor "./test/test-adaptor", "test" @registerDriver "./test/ping", "ping" @testing = process.env['CYLON_TEST'] @@ -175,18 +176,29 @@ namespace 'Cylon', -> # # Returns the adaptor initAdaptor: (adaptorName, connection, opts = {}) -> - @robot.requireAdaptor(adaptorName).adaptor + realAdaptor = @robot.requireAdaptor(adaptorName).adaptor name: adaptorName, connection: connection, extraParams: opts + if @robot.testing? + testAdaptor = @robot.requireAdaptor('test').adaptor + name: adaptorName, + connection: connection, + extraParams: opts + + testAdaptor.proxyTestCommands(realAdaptor) + testAdaptor + else + realAdaptor + # Public: Requires a hardware adaptor and adds it to @robot.adaptors # # adaptorName - module name of adaptor to require # # Returns the module for the adaptor requireAdaptor: (adaptorName) => - return @robot.adaptors['loopback'] if @robot.testing? + #return @robot.adaptors['test'] if @robot.testing? unless @robot.adaptors[adaptorName]? @robot.registerAdaptor "cylon-#{adaptorName}", adaptorName @@ -201,7 +213,9 @@ namespace 'Cylon', -> # # Returns the registered module name registerAdaptor: (moduleName, adaptorName) => + console.log 'here ' + moduleName @adaptors[adaptorName] = require(moduleName) unless @adaptors[adaptorName]? + console.log 'there' # Public: Init a hardware driver # diff --git a/src/test/test-adaptor.coffee b/src/test/test-adaptor.coffee new file mode 100644 index 0000000..46c324c --- /dev/null +++ b/src/test/test-adaptor.coffee @@ -0,0 +1,33 @@ +### + * Test adaptor + * cylonjs.com + * + * Copyright (c) 2013 The Hybrid Group + * Licensed under the Apache 2.0 license. +### + +'use strict' + +namespace = require 'node-namespace' + +module.exports = + adaptor: (args...) -> + new Cylon.Adaptors.TestAdaptor(args...) + +namespace 'Cylon.Adaptors', -> + class @TestAdaptor extends Cylon.Adaptor + constructor: (opts={}) -> + super + @commandList = [] + + commands: -> + @commandList + + proxyTestCommands: (realAdaptor) -> + for method in realAdaptor.commands() + do (method) -> + @self[method] = (args...) -> @self.success() + @commandList.push method + + success: -> + true