2014-02-28 09:43:34 +08:00
|
|
|
"use strict";
|
|
|
|
|
2015-06-09 22:21:03 +08:00
|
|
|
var Basestar = lib("basestar"),
|
|
|
|
Utils = lib("utils");
|
2013-11-23 07:12:30 +08:00
|
|
|
|
2014-12-15 07:21:28 +08:00
|
|
|
var EventEmitter = require("events").EventEmitter;
|
2013-11-23 07:12:30 +08:00
|
|
|
|
2014-12-15 07:21:28 +08:00
|
|
|
describe("Basestar", function() {
|
|
|
|
describe("#proxyMethods", function() {
|
|
|
|
var methods = ["asString", "toString", "returnString"];
|
2014-02-28 09:39:02 +08:00
|
|
|
|
2014-12-16 01:37:52 +08:00
|
|
|
var ProxyClass = function ProxyClass() {};
|
2013-11-23 07:12:30 +08:00
|
|
|
|
2014-04-25 06:31:47 +08:00
|
|
|
ProxyClass.prototype.asString = function() {
|
|
|
|
return "[object ProxyClass]";
|
|
|
|
};
|
2013-11-23 07:12:30 +08:00
|
|
|
|
2014-04-25 06:31:47 +08:00
|
|
|
ProxyClass.prototype.toString = function() {
|
|
|
|
return "[object ProxyClass]";
|
|
|
|
};
|
2013-11-23 07:12:30 +08:00
|
|
|
|
2014-04-25 06:31:47 +08:00
|
|
|
ProxyClass.prototype.returnString = function(string) {
|
|
|
|
return string;
|
|
|
|
};
|
2013-11-23 07:12:30 +08:00
|
|
|
|
2014-04-25 06:31:47 +08:00
|
|
|
var TestClass = function TestClass() {
|
2014-12-16 01:37:52 +08:00
|
|
|
this.testInstance = new ProxyClass();
|
2014-04-25 06:31:47 +08:00
|
|
|
this.proxyMethods(methods, this.testInstance, this, true);
|
2014-12-16 01:37:52 +08:00
|
|
|
};
|
2013-11-23 07:12:30 +08:00
|
|
|
|
2014-06-06 03:11:37 +08:00
|
|
|
Utils.subclass(TestClass, Basestar);
|
2014-02-28 04:10:00 +08:00
|
|
|
|
2014-12-15 07:21:28 +08:00
|
|
|
it("can alias methods", function() {
|
2014-12-16 01:37:52 +08:00
|
|
|
var testclass = new TestClass();
|
|
|
|
expect(testclass.asString).to.be.a("function");
|
2014-02-28 09:43:34 +08:00
|
|
|
expect(testclass.asString()).to.be.equal("[object ProxyClass]");
|
2013-11-23 07:12:30 +08:00
|
|
|
});
|
|
|
|
|
2014-12-15 07:21:28 +08:00
|
|
|
it("can alias existing methods if forced to", function() {
|
2014-12-16 01:37:52 +08:00
|
|
|
var testclass = new TestClass();
|
|
|
|
expect(testclass.toString).to.be.a("function");
|
2014-02-28 09:43:34 +08:00
|
|
|
expect(testclass.toString()).to.be.equal("[object ProxyClass]");
|
2014-02-28 04:10:00 +08:00
|
|
|
});
|
|
|
|
|
2014-12-15 07:21:28 +08:00
|
|
|
it("can alias methods with arguments", function() {
|
2014-12-16 01:37:52 +08:00
|
|
|
var testclass = new TestClass();
|
|
|
|
expect(testclass.returnString).to.be.a("function");
|
2014-02-28 09:43:34 +08:00
|
|
|
expect(testclass.returnString("testString")).to.be.equal("testString");
|
2014-02-28 04:10:00 +08:00
|
|
|
});
|
|
|
|
});
|
2014-02-28 10:13:21 +08:00
|
|
|
|
2015-03-20 01:42:51 +08:00
|
|
|
describe("#respond", function() {
|
|
|
|
var listener, callback, child;
|
|
|
|
var Child = function Child() {};
|
|
|
|
|
|
|
|
Utils.subclass(Child, Basestar);
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
child = new Child();
|
|
|
|
|
|
|
|
listener = spy();
|
|
|
|
callback = spy();
|
|
|
|
|
|
|
|
child.on("event", listener);
|
|
|
|
|
2015-03-28 05:18:45 +08:00
|
|
|
child.respond("event", callback, null, "arg1", 2, { three: true });
|
2015-03-20 01:42:51 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("triggers the callback with all additional arguments", function() {
|
2015-03-28 05:18:45 +08:00
|
|
|
expect(callback).to.be.calledWith(null, "arg1", 2, { three: true });
|
2015-03-20 01:42:51 +08:00
|
|
|
});
|
2014-04-25 06:31:47 +08:00
|
|
|
|
2015-03-20 01:42:51 +08:00
|
|
|
it("emits an event with all additional arguments", function() {
|
|
|
|
expect(listener).to.be.calledWith("arg1", 2, { three: true });
|
|
|
|
});
|
2015-03-28 05:18:45 +08:00
|
|
|
|
|
|
|
context("when err is not null", function() {
|
2015-03-28 05:36:24 +08:00
|
|
|
var errListener;
|
2015-03-28 05:18:45 +08:00
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
errListener = spy();
|
|
|
|
child = new Child();
|
|
|
|
child.on("error", errListener);
|
2015-04-15 12:49:12 +08:00
|
|
|
child.respond(
|
|
|
|
"event",
|
|
|
|
callback,
|
|
|
|
"Error on event!",
|
|
|
|
"arg1",
|
|
|
|
2,
|
|
|
|
{ three: true });
|
2015-03-28 05:18:45 +08:00
|
|
|
});
|
|
|
|
|
2015-04-15 12:49:12 +08:00
|
|
|
it("emits an error event", function() {
|
2015-03-28 05:18:45 +08:00
|
|
|
expect(errListener).to.be.calledWith("Error on event!");
|
|
|
|
});
|
|
|
|
});
|
2015-03-20 01:42:51 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("#defineEvent", function() {
|
2014-04-25 06:31:47 +08:00
|
|
|
var ProxyClass = function ProxyClass() {};
|
|
|
|
|
|
|
|
var EmitterClass = function EmitterClass(update) {
|
|
|
|
update || (update = false);
|
|
|
|
this.proxy = new ProxyClass();
|
|
|
|
this.defineEvent({
|
|
|
|
eventName: "testevent",
|
|
|
|
source: this,
|
|
|
|
target: this.proxy,
|
|
|
|
sendUpdate: update
|
|
|
|
});
|
2014-12-16 01:37:52 +08:00
|
|
|
};
|
2014-04-25 06:31:47 +08:00
|
|
|
|
2014-06-06 03:11:37 +08:00
|
|
|
Utils.subclass(ProxyClass, Basestar);
|
|
|
|
Utils.subclass(EmitterClass, Basestar);
|
2014-02-28 10:13:21 +08:00
|
|
|
|
|
|
|
it("proxies events from one class to another", function() {
|
|
|
|
var eventSpy = spy(),
|
|
|
|
testclass = new EmitterClass(),
|
|
|
|
proxy = testclass.proxy;
|
|
|
|
|
2014-12-15 07:21:28 +08:00
|
|
|
proxy.on("testevent", eventSpy);
|
|
|
|
testclass.emit("testevent", "data");
|
2014-02-28 10:13:21 +08:00
|
|
|
|
2014-12-16 01:37:52 +08:00
|
|
|
expect(eventSpy).to.be.calledWith("data");
|
2014-02-28 10:13:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("emits an 'update' event if told to", function() {
|
|
|
|
var updateSpy = spy(),
|
|
|
|
testclass = new EmitterClass(true),
|
|
|
|
proxy = testclass.proxy;
|
|
|
|
|
2014-12-15 07:21:28 +08:00
|
|
|
proxy.on("update", updateSpy);
|
|
|
|
testclass.emit("testevent", "data");
|
2014-02-28 10:13:21 +08:00
|
|
|
|
2014-12-16 01:37:52 +08:00
|
|
|
expect(updateSpy).to.be.calledWith("testevent", "data");
|
2014-02-28 10:13:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("does not emit an 'update' event by default", function() {
|
|
|
|
var updateSpy = spy(),
|
|
|
|
testclass = new EmitterClass(),
|
|
|
|
proxy = testclass.proxy;
|
|
|
|
|
2014-12-15 07:21:28 +08:00
|
|
|
proxy.on("update", updateSpy);
|
|
|
|
testclass.emit("testevent", "data");
|
2014-02-28 10:13:21 +08:00
|
|
|
|
2014-12-16 01:37:52 +08:00
|
|
|
expect(updateSpy).to.not.be.calledWith("testevent", "data");
|
2014-02-28 10:13:21 +08:00
|
|
|
});
|
|
|
|
});
|
2014-02-28 13:17:44 +08:00
|
|
|
|
|
|
|
describe("#defineAdaptorEvent", function() {
|
|
|
|
var basestar;
|
|
|
|
|
2014-07-05 00:49:29 +08:00
|
|
|
beforeEach(function() {
|
2014-04-25 06:31:47 +08:00
|
|
|
basestar = new Basestar();
|
2014-02-28 13:17:44 +08:00
|
|
|
basestar.connector = new EventEmitter();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("proxies events between the connector and connection", function() {
|
|
|
|
var eventSpy = spy();
|
|
|
|
|
2014-12-15 07:21:28 +08:00
|
|
|
basestar.on("testevent", eventSpy);
|
2014-02-28 13:17:44 +08:00
|
|
|
basestar.defineAdaptorEvent({ eventName: "testevent" });
|
|
|
|
|
|
|
|
basestar.connector.emit("testevent", "data");
|
2014-12-16 01:37:52 +08:00
|
|
|
expect(eventSpy).to.be.calledWith("data");
|
2014-02-28 13:17:44 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
context("when given a string", function() {
|
|
|
|
it("uses it as the eventName", function() {
|
|
|
|
var eventSpy = spy();
|
|
|
|
|
2014-12-15 07:21:28 +08:00
|
|
|
basestar.on("testevent", eventSpy);
|
2014-02-28 13:17:44 +08:00
|
|
|
basestar.defineAdaptorEvent("testevent");
|
|
|
|
|
|
|
|
basestar.connector.emit("testevent", "data");
|
2014-12-16 01:37:52 +08:00
|
|
|
expect(eventSpy).to.be.calledWith("data");
|
2014-02-28 13:17:44 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#defineDriverEvent", function() {
|
|
|
|
var basestar;
|
|
|
|
|
2014-07-05 00:49:29 +08:00
|
|
|
beforeEach(function() {
|
2014-04-25 06:31:47 +08:00
|
|
|
basestar = new Basestar();
|
2014-11-15 02:56:45 +08:00
|
|
|
basestar.connection = new EventEmitter();
|
2014-02-28 13:17:44 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("proxies events between the connection and device", function() {
|
|
|
|
var eventSpy = spy();
|
|
|
|
|
2014-12-15 07:21:28 +08:00
|
|
|
basestar.on("testevent", eventSpy);
|
2014-02-28 13:17:44 +08:00
|
|
|
basestar.defineDriverEvent({ eventName: "testevent" });
|
|
|
|
|
2014-11-15 03:01:57 +08:00
|
|
|
basestar.connection.emit("testevent", "data");
|
2014-12-16 01:37:52 +08:00
|
|
|
expect(eventSpy).to.be.calledWith("data");
|
2014-02-28 13:17:44 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
context("when given a string", function() {
|
|
|
|
it("uses it as the eventName", function() {
|
|
|
|
var eventSpy = spy();
|
|
|
|
|
2014-12-15 07:21:28 +08:00
|
|
|
basestar.on("testevent", eventSpy);
|
2014-02-28 13:17:44 +08:00
|
|
|
basestar.defineDriverEvent("testevent");
|
|
|
|
|
2014-11-15 03:01:57 +08:00
|
|
|
basestar.connection.emit("testevent", "data");
|
2014-12-16 01:37:52 +08:00
|
|
|
expect(eventSpy).to.be.calledWith("data");
|
2014-02-28 13:17:44 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-02-28 04:10:00 +08:00
|
|
|
});
|