UNPKG

noflo

Version:

Flow-Based Programming environment for JavaScript

202 lines (184 loc) 5.86 kB
var chai, noflo, extend = 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; }, hasProp = {}.hasOwnProperty; if (typeof process !== 'undefined' && process.execPath && process.execPath.match(/node|iojs/)) { if (!chai) { chai = require('chai'); } noflo = require('../src/lib/NoFlo.coffee'); } else { noflo = require('noflo'); } describe('AsyncComponent with missing ports', function() { var C1, C2; C1 = (function(superClass) { extend(C1, superClass); function C1() { return C1.__super__.constructor.apply(this, arguments); } return C1; })(noflo.AsyncComponent); C2 = (function(superClass) { extend(C2, superClass); function C2() { this.inPorts = { "in": new noflo.Port }; C2.__super__.constructor.call(this); } return C2; })(noflo.AsyncComponent); it('should throw an error on instantiation when no IN defined', function() { return chai.expect(function() { return new C1; }).to["throw"](Error); }); return it('should throw an error on instantion when no OUT defined', function() { return chai.expect(function() { return new C2; }).to["throw"](Error); }); }); describe('AsyncComponent without a doAsync method', function() { var Unimplemented; Unimplemented = (function(superClass) { extend(Unimplemented, superClass); function Unimplemented() { this.inPorts = { "in": new noflo.Port }; this.outPorts = { out: new noflo.Port, error: new noflo.Port }; Unimplemented.__super__.constructor.call(this); } return Unimplemented; })(noflo.AsyncComponent); it('should throw an error if there is no connection to the ERROR port', function() { var ins, u; u = new Unimplemented; ins = noflo.internalSocket.createSocket(); u.inPorts["in"].attach(ins); return chai.expect(function() { return ins.send('Foo'); }).to["throw"](Error); }); it('should send an error to the ERROR port if connected', function(done) { var err, ins, u; u = new Unimplemented; ins = noflo.internalSocket.createSocket(); u.inPorts["in"].attach(ins); err = noflo.internalSocket.createSocket(); u.outPorts.error.attach(err); err.on('data', function(data) { chai.expect(data).to.be.an["instanceof"](Error); return done(); }); return ins.send('Bar'); }); return it('should send groups and error to the ERROR port if connected', function(done) { var err, group, groups, i, ins, j, len, received, ref, results, u; u = new Unimplemented; ins = noflo.internalSocket.createSocket(); u.inPorts["in"].attach(ins); err = noflo.internalSocket.createSocket(); groups = ['one', 'two', 'three', 'one', 'two', 'four']; u.outPorts.out.attach(noflo.internalSocket.createSocket()); u.outPorts.error.attach(err); err.on('begingroup', function(group) { return chai.expect(group).to.equal(groups.shift()); }); received = 0; err.on('data', function(data) { received++; chai.expect(data).to.be.an["instanceof"](Error); if (received === 2) { chai.expect(groups.length).to.equal(0); return done(); } }); ref = ['one', 'two', 'three']; for (i = 0, len = ref.length; i < len; i++) { group = ref[i]; ins.beginGroup(group); } ins.send('Bar'); ins.endGroup(); ins.beginGroup('four'); ins.send('Baz'); results = []; for (group = j = 0; j <= 2; group = ++j) { results.push(ins.endGroup()); } return results; }); }); describe('Implemented AsyncComponent', function() { var Timer, err, ins, lod, out, t; Timer = (function(superClass) { extend(Timer, superClass); function Timer() { this.inPorts = { "in": new noflo.Port }; this.outPorts = { out: new noflo.Port, error: new noflo.Port }; Timer.__super__.constructor.call(this); } Timer.prototype.doAsync = function(data, callback) { return setTimeout(((function(_this) { return function() { _this.outPorts.out.send("waited " + data); return callback(); }; })(this)), data); }; return Timer; })(noflo.AsyncComponent); t = null; ins = null; out = null; lod = null; err = null; beforeEach(function() { t = new Timer; ins = noflo.internalSocket.createSocket(); out = noflo.internalSocket.createSocket(); lod = noflo.internalSocket.createSocket(); err = noflo.internalSocket.createSocket(); t.inPorts["in"].attach(ins); t.outPorts.out.attach(out); t.outPorts.load.attach(lod); return t.outPorts.error.attach(err); }); return it('should send load information and packets in correct order', function(done) { var expected, inspect, received; received = []; expected = ['load 1', 'load 2', 'load 3', 'out waited 100', 'load 2', 'out waited 200', 'load 1', 'out waited 300', 'load 0']; inspect = function() { var i, key, len, value; chai.expect(received.length).to.equal(expected.length); for (key = i = 0, len = expected.length; i < len; key = ++i) { value = expected[key]; chai.expect(received[key]).to.equal(value); } return done(); }; out.on('data', function(data) { return received.push("out " + data); }); lod.on('data', function(data) { received.push("load " + data); if (data === 0) { return inspect(); } }); ins.send(300); ins.send(200); ins.send(100); return ins.disconnect(); }); });