Comparing version 0.10.0-beta5 to 0.10.0-beta6
@@ -30,10 +30,10 @@ "use strict"; | ||
// types go through to the C++ side it can lead to hard to diagnose exceptions. | ||
this._checkType('name', opt.name ? opt.name : null, 'string'); | ||
this._checkType('cols', opt.cols ? opt.cols : null, 'number'); | ||
this._checkType('rows', opt.rows ? opt.rows : null, 'number'); | ||
this._checkType('cwd', opt.cwd ? opt.cwd : null, 'string'); | ||
this._checkType('env', opt.env ? opt.env : null, 'object'); | ||
this._checkType('uid', opt.uid ? opt.uid : null, 'number'); | ||
this._checkType('gid', opt.gid ? opt.gid : null, 'number'); | ||
this._checkType('encoding', opt.encoding ? opt.encoding : null, 'string'); | ||
this._checkType('name', opt.name ? opt.name : undefined, 'string'); | ||
this._checkType('cols', opt.cols ? opt.cols : undefined, 'number'); | ||
this._checkType('rows', opt.rows ? opt.rows : undefined, 'number'); | ||
this._checkType('cwd', opt.cwd ? opt.cwd : undefined, 'string'); | ||
this._checkType('env', opt.env ? opt.env : undefined, 'object'); | ||
this._checkType('uid', opt.uid ? opt.uid : undefined, 'number'); | ||
this._checkType('gid', opt.gid ? opt.gid : undefined, 'number'); | ||
this._checkType('encoding', opt.encoding ? opt.encoding : undefined, 'string'); | ||
// setup flow control handling | ||
@@ -89,4 +89,18 @@ this.handleFlowControl = !!(opt.handleFlowControl); | ||
}; | ||
Terminal.prototype._checkType = function (name, value, type) { | ||
if (value && typeof value !== type) { | ||
Terminal.prototype._checkType = function (name, value, type, allowArray) { | ||
if (allowArray === void 0) { allowArray = false; } | ||
if (value === undefined) { | ||
return; | ||
} | ||
if (allowArray) { | ||
if (Array.isArray(value)) { | ||
value.forEach(function (v, i) { | ||
if (typeof v !== type) { | ||
throw new Error(name + "[" + i + "] must be a " + type + " (not a " + typeof v[i] + ")"); | ||
} | ||
}); | ||
return; | ||
} | ||
} | ||
if (typeof value !== type) { | ||
throw new Error(name + " must be a " + type + " (not a " + typeof value + ")"); | ||
@@ -93,0 +107,0 @@ } |
@@ -6,2 +6,15 @@ "use strict"; | ||
*/ | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -11,2 +24,3 @@ var assert = require("assert"); | ||
var unixTerminal_1 = require("./unixTerminal"); | ||
var terminal_1 = require("./terminal"); | ||
var terminalConstructor = (process.platform === 'win32') ? windowsTerminal_1.WindowsTerminal : unixTerminal_1.UnixTerminal; | ||
@@ -21,2 +35,46 @@ var SHELL = (process.platform === 'win32') ? 'cmd.exe' : '/bin/bash'; | ||
} | ||
var TestTerminal = /** @class */ (function (_super) { | ||
__extends(TestTerminal, _super); | ||
function TestTerminal() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
TestTerminal.prototype.checkType = function (name, value, type, allowArray) { | ||
if (allowArray === void 0) { allowArray = false; } | ||
this._checkType(name, value, type, allowArray); | ||
}; | ||
TestTerminal.prototype._write = function (data) { | ||
throw new Error('Method not implemented.'); | ||
}; | ||
TestTerminal.prototype.resize = function (cols, rows) { | ||
throw new Error('Method not implemented.'); | ||
}; | ||
TestTerminal.prototype.destroy = function () { | ||
throw new Error('Method not implemented.'); | ||
}; | ||
TestTerminal.prototype.kill = function (signal) { | ||
throw new Error('Method not implemented.'); | ||
}; | ||
Object.defineProperty(TestTerminal.prototype, "process", { | ||
get: function () { | ||
throw new Error('Method not implemented.'); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(TestTerminal.prototype, "master", { | ||
get: function () { | ||
throw new Error('Method not implemented.'); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(TestTerminal.prototype, "slave", { | ||
get: function () { | ||
throw new Error('Method not implemented.'); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
return TestTerminal; | ||
}(terminal_1.Terminal)); | ||
describe('Terminal', function () { | ||
@@ -28,2 +86,22 @@ describe('constructor', function () { | ||
}); | ||
describe('checkType', function () { | ||
it('should throw for the wrong type', function () { | ||
var t = new TestTerminal(); | ||
assert.doesNotThrow(function () { return t.checkType('foo', 'test', 'string'); }); | ||
assert.doesNotThrow(function () { return t.checkType('foo', 1, 'number'); }); | ||
assert.doesNotThrow(function () { return t.checkType('foo', {}, 'object'); }); | ||
assert.throws(function () { return t.checkType('foo', 'test', 'number'); }); | ||
assert.throws(function () { return t.checkType('foo', 1, 'object'); }); | ||
assert.throws(function () { return t.checkType('foo', {}, 'string'); }); | ||
}); | ||
it('should throw for wrong types within arrays', function () { | ||
var t = new TestTerminal(); | ||
assert.doesNotThrow(function () { return t.checkType('foo', ['test'], 'string', true); }); | ||
assert.doesNotThrow(function () { return t.checkType('foo', [1], 'number', true); }); | ||
assert.doesNotThrow(function () { return t.checkType('foo', [{}], 'object', true); }); | ||
assert.throws(function () { return t.checkType('foo', ['test'], 'number', true); }); | ||
assert.throws(function () { return t.checkType('foo', [1], 'object', true); }); | ||
assert.throws(function () { return t.checkType('foo', [{}], 'string', true); }); | ||
}); | ||
}); | ||
describe('automatic flow control', function () { | ||
@@ -30,0 +108,0 @@ it('should respect ctor flow control options', function () { |
@@ -30,2 +30,3 @@ "use strict"; | ||
var _this = _super.call(this, opt) || this; | ||
_this._checkType('args', args, 'string', true); | ||
// Initialize arguments | ||
@@ -32,0 +33,0 @@ args = args || []; |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "0.10.0-beta5", | ||
"version": "0.10.0-beta6", | ||
"license": "MIT", | ||
@@ -10,0 +10,0 @@ "main": "./lib/index.js", |
@@ -9,3 +9,4 @@ /** | ||
import { UnixTerminal } from './unixTerminal'; | ||
import { pollUntil } from './testUtils.test'; | ||
import { Terminal } from './terminal'; | ||
import { Socket } from 'net'; | ||
@@ -22,2 +23,28 @@ const terminalConstructor = (process.platform === 'win32') ? WindowsTerminal : UnixTerminal; | ||
class TestTerminal extends Terminal { | ||
public checkType<T>(name: string, value: T, type: string, allowArray: boolean = false): void { | ||
this._checkType(name, value, type, allowArray); | ||
} | ||
protected _write(data: string): void { | ||
throw new Error('Method not implemented.'); | ||
} | ||
public resize(cols: number, rows: number): void { | ||
throw new Error('Method not implemented.'); | ||
} | ||
public destroy(): void { | ||
throw new Error('Method not implemented.'); | ||
} | ||
public kill(signal?: string): void { | ||
throw new Error('Method not implemented.'); | ||
} | ||
public get process(): string { | ||
throw new Error('Method not implemented.'); | ||
} | ||
public get master(): Socket { | ||
throw new Error('Method not implemented.'); | ||
} | ||
public get slave(): Socket { | ||
throw new Error('Method not implemented.'); | ||
} | ||
} | ||
@@ -34,2 +61,25 @@ describe('Terminal', () => { | ||
describe('checkType', () => { | ||
it('should throw for the wrong type', () => { | ||
const t = new TestTerminal(); | ||
assert.doesNotThrow(() => t.checkType('foo', 'test', 'string')); | ||
assert.doesNotThrow(() => t.checkType('foo', 1, 'number')); | ||
assert.doesNotThrow(() => t.checkType('foo', {}, 'object')); | ||
assert.throws(() => t.checkType('foo', 'test', 'number')); | ||
assert.throws(() => t.checkType('foo', 1, 'object')); | ||
assert.throws(() => t.checkType('foo', {}, 'string')); | ||
}); | ||
it('should throw for wrong types within arrays', () => { | ||
const t = new TestTerminal(); | ||
assert.doesNotThrow(() => t.checkType('foo', ['test'], 'string', true)); | ||
assert.doesNotThrow(() => t.checkType('foo', [1], 'number', true)); | ||
assert.doesNotThrow(() => t.checkType('foo', [{}], 'object', true)); | ||
assert.throws(() => t.checkType('foo', ['test'], 'number', true)); | ||
assert.throws(() => t.checkType('foo', [1], 'object', true)); | ||
assert.throws(() => t.checkType('foo', [{}], 'string', true)); | ||
}); | ||
}); | ||
describe('automatic flow control', () => { | ||
@@ -36,0 +86,0 @@ it('should respect ctor flow control options', () => { |
@@ -62,10 +62,10 @@ /** | ||
// types go through to the C++ side it can lead to hard to diagnose exceptions. | ||
this._checkType('name', opt.name ? opt.name : null, 'string'); | ||
this._checkType('cols', opt.cols ? opt.cols : null, 'number'); | ||
this._checkType('rows', opt.rows ? opt.rows : null, 'number'); | ||
this._checkType('cwd', opt.cwd ? opt.cwd : null, 'string'); | ||
this._checkType('env', opt.env ? opt.env : null, 'object'); | ||
this._checkType('uid', opt.uid ? opt.uid : null, 'number'); | ||
this._checkType('gid', opt.gid ? opt.gid : null, 'number'); | ||
this._checkType('encoding', opt.encoding ? opt.encoding : null, 'string'); | ||
this._checkType('name', opt.name ? opt.name : undefined, 'string'); | ||
this._checkType('cols', opt.cols ? opt.cols : undefined, 'number'); | ||
this._checkType('rows', opt.rows ? opt.rows : undefined, 'number'); | ||
this._checkType('cwd', opt.cwd ? opt.cwd : undefined, 'string'); | ||
this._checkType('env', opt.env ? opt.env : undefined, 'object'); | ||
this._checkType('uid', opt.uid ? opt.uid : undefined, 'number'); | ||
this._checkType('gid', opt.gid ? opt.gid : undefined, 'number'); | ||
this._checkType('encoding', opt.encoding ? opt.encoding : undefined, 'string'); | ||
@@ -101,4 +101,17 @@ // setup flow control handling | ||
private _checkType(name: string, value: any, type: string): void { | ||
if (value && typeof value !== type) { | ||
protected _checkType<T>(name: string, value: T | undefined, type: string, allowArray: boolean = false): void { | ||
if (value === undefined) { | ||
return; | ||
} | ||
if (allowArray) { | ||
if (Array.isArray(value)) { | ||
value.forEach((v, i) => { | ||
if (typeof v !== type) { | ||
throw new Error(`${name}[${i}] must be a ${type} (not a ${typeof v[i]})`); | ||
} | ||
}); | ||
return; | ||
} | ||
} | ||
if (typeof value !== type) { | ||
throw new Error(`${name} must be a ${type} (not a ${typeof value})`); | ||
@@ -105,0 +118,0 @@ } |
@@ -25,2 +25,4 @@ /** | ||
this._checkType('args', args, 'string', true); | ||
// Initialize arguments | ||
@@ -27,0 +29,0 @@ args = args || []; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1089614
3610