mirror of
https://github.com/siemens-mobile-hacks/node-serialport-bindings-cpp.git
synced 2026-11-25 18:37:19 +00:00
refactor some test logic
This commit is contained in:
parent
f97ea3aec3
commit
b822721855
15 changed files with 39 additions and 38 deletions
|
|
@ -14,6 +14,10 @@
|
|||
"parserOptions": {
|
||||
"ecmaVersion": 2017
|
||||
},
|
||||
"globals": {
|
||||
"assert": false,
|
||||
"makeTestFeature": false
|
||||
},
|
||||
"rules": {
|
||||
"no-unused-vars": [2, { "vars": "all", "args": "after-used" }],
|
||||
"no-var": 2,
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
"all": {},
|
||||
"win32": {
|
||||
"open.unlock": false,
|
||||
"baudrate.25000": false
|
||||
"baudrate.25000": false,
|
||||
"port.update-baudrate": false
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
// `npm run stress` to run these tests
|
||||
|
||||
const Buffer = require('safe-buffer').Buffer;
|
||||
const assert = require('chai').assert;
|
||||
const util = require('util');
|
||||
const SerialPort = require('../../');
|
||||
require('colors'); // this modifies String.prototype
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
'use strict';
|
||||
|
||||
const assert = require('chai').assert;
|
||||
const listLinux = require('./mocks/linux-list');
|
||||
|
||||
const ports = {
|
||||
|
|
|
|||
|
|
@ -1,9 +1,6 @@
|
|||
'use strict';
|
||||
/* eslint-disable no-new */
|
||||
|
||||
const assert = require('chai').assert;
|
||||
const Buffer = require('safe-buffer').Buffer;
|
||||
const testConfig = require('../test-config.json');
|
||||
|
||||
let platform;
|
||||
switch (process.platform) {
|
||||
|
|
@ -56,7 +53,6 @@ const readyData = Buffer.from('READY');
|
|||
bindingsToTest.forEach((bindingName) => {
|
||||
const binding = require(`../lib/bindings/${bindingName}`);
|
||||
let testPort = process.env.TEST_PORT;
|
||||
const localTestConfig = testConfig[bindingName] || {};
|
||||
|
||||
if (bindingName === 'mock') {
|
||||
testPort = '/dev/exists';
|
||||
|
|
@ -64,17 +60,11 @@ bindingsToTest.forEach((bindingName) => {
|
|||
}
|
||||
|
||||
// eslint-disable-next-line no-use-before-define
|
||||
testBinding(bindingName, binding, testPort, localTestConfig);
|
||||
testBinding(bindingName, binding, testPort);
|
||||
});
|
||||
|
||||
function testBinding(bindingName, Binding, testPort, localTestConfig) {
|
||||
function testFeature(feature, description, callback) {
|
||||
if (localTestConfig[feature] === false) {
|
||||
return it(`Feature "${feature}" is disabled. "${description}"`);
|
||||
}
|
||||
it(description, callback);
|
||||
}
|
||||
|
||||
function testBinding(bindingName, Binding, testPort) {
|
||||
const testFeature = makeTestFeature(bindingName);
|
||||
describe(`bindings/${bindingName}`, () => {
|
||||
describe('static method', () => {
|
||||
describe('.list', () => {
|
||||
|
|
|
|||
1
test/initializers/assert.js
Normal file
1
test/initializers/assert.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
global.assert = require('chai').assert;
|
||||
4
test/initializers/index.js
Normal file
4
test/initializers/index.js
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
'use strict';
|
||||
const fs = require('fs');
|
||||
const files = fs.readdirSync(__dirname).filter(file => file !== 'index.js' && file.match(/\.js$/));
|
||||
files.forEach(file => require(`./${file}`));
|
||||
11
test/initializers/test-config.js
Normal file
11
test/initializers/test-config.js
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
const testConfig = require('../../test-config.json');
|
||||
|
||||
global.makeTestFeature = function makeTestFeature(envName) {
|
||||
const config = Object.assign({}, testConfig.all, testConfig[envName]);
|
||||
return function testFeature(feature, description, callback) {
|
||||
if (config[feature] === false) {
|
||||
return it(`Feature "${feature}" is disabled in "${envName}. "${description}"`);
|
||||
}
|
||||
it(description, callback);
|
||||
};
|
||||
};
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
'use strict';
|
||||
const Buffer = require('safe-buffer').Buffer;
|
||||
const crypto = require('crypto');
|
||||
const assert = require('chai').assert;
|
||||
const SerialPort = require('../');
|
||||
|
||||
let platform;
|
||||
|
|
@ -33,6 +32,8 @@ integrationTest(platform, process.env.TEST_PORT, defaultBinding);
|
|||
// Be careful to close the ports when you're done with them
|
||||
// Ports are by default exclusively locked so a failure fails all tests
|
||||
function integrationTest(platform, testPort, binding) {
|
||||
const testFeature = makeTestFeature(platform);
|
||||
|
||||
describe(`${platform} SerialPort Integration Tests`, () => {
|
||||
if (!testPort) {
|
||||
it(`${platform} tests requires an Arduino loaded with the arduinoEcho program on a serialport set to the TEST_PORT env var`);
|
||||
|
|
@ -161,11 +162,7 @@ function integrationTest(platform, testPort, binding) {
|
|||
});
|
||||
|
||||
describe('#update', () => {
|
||||
if (platform === 'win32') {
|
||||
return it("Isn't supported on windows yet");
|
||||
}
|
||||
|
||||
it('allows changing the baud rate of an open port', (done) => {
|
||||
testFeature('port.update-baudrate', 'allows changing the baud rate of an open port', (done) => {
|
||||
const port = new SerialPort(testPort, () => {
|
||||
port.update({ baudRate: 57600 }, (err) => {
|
||||
assert.isNull(err);
|
||||
|
|
@ -179,22 +176,21 @@ function integrationTest(platform, testPort, binding) {
|
|||
it('2k test', function(done) {
|
||||
this.timeout(20000);
|
||||
// 2k of random data
|
||||
const output = crypto.randomBytes(1024 * 2);
|
||||
const expectedInput = Buffer.concat([readyData, output]);
|
||||
const input = crypto.randomBytes(1024 * 2);
|
||||
const port = new SerialPort(testPort);
|
||||
|
||||
const ready = port.pipe(new SerialPort.parsers.Ready({ delimiter: readyData }));
|
||||
// this will trigger from the "READY" the arduino sends when it's... ready
|
||||
port.once('data', () => {
|
||||
port.write(output);
|
||||
ready.on('ready', () => {
|
||||
port.write(input);
|
||||
});
|
||||
|
||||
let input = Buffer.alloc(0);
|
||||
port.on('data', (data) => {
|
||||
input = Buffer.concat([input, data]);
|
||||
if (input.length >= expectedInput.length) {
|
||||
let output = Buffer.alloc(0);
|
||||
ready.on('data', (data) => {
|
||||
output = Buffer.concat([output, data]);
|
||||
if (output.length >= input.length) {
|
||||
try {
|
||||
assert.equal(input.length, expectedInput.length, 'write length matches');
|
||||
assert.deepEqual(input, expectedInput, 'read data matches expected input');
|
||||
assert.equal(output.length, input.length, 'write length matches');
|
||||
assert.deepEqual(output, input, 'read data matches expected output');
|
||||
port.close(done);
|
||||
} catch (e) {
|
||||
done(e);
|
||||
|
|
|
|||
|
|
@ -1 +1,2 @@
|
|||
--bail
|
||||
--require ./test/initializers
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
'use strict';
|
||||
/* eslint-disable no-new */
|
||||
|
||||
const assert = require('chai').assert;
|
||||
const Buffer = require('safe-buffer').Buffer;
|
||||
const sinon = require('sinon');
|
||||
const ByteLengthParser = require('../lib/parsers/byte-length');
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
/* eslint-disable no-new */
|
||||
|
||||
const Buffer = require('safe-buffer').Buffer;
|
||||
const assert = require('chai').assert;
|
||||
const sinon = require('sinon');
|
||||
|
||||
const DelimiterParser = require('../lib/parsers/delimiter');
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
/* eslint-disable no-new */
|
||||
|
||||
const Buffer = require('safe-buffer').Buffer;
|
||||
const assert = require('chai').assert;
|
||||
const sinon = require('sinon');
|
||||
|
||||
const ReadlineParser = require('../lib/parsers/readline');
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
/* eslint-disable no-new */
|
||||
|
||||
const Buffer = require('safe-buffer').Buffer;
|
||||
const assert = require('chai').assert;
|
||||
const sinon = require('sinon');
|
||||
|
||||
const ReadyParser = require('../lib/parsers/ready');
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
'use strict';
|
||||
/* eslint-disable no-new */
|
||||
|
||||
const assert = require('chai').assert;
|
||||
const Buffer = require('safe-buffer').Buffer;
|
||||
const sinon = require('sinon');
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue