Skip to content

Commit

Permalink
enable unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Deepak Rajamohan committed Sep 24, 2021
1 parent 5a9f229 commit a43bd0c
Show file tree
Hide file tree
Showing 86 changed files with 1,176 additions and 1,145 deletions.
12 changes: 6 additions & 6 deletions benchmark/function_args.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const path = require('path');
const Benchmark = require('benchmark');
const addonName = path.basename(__filename, '.js');

[ addonName, addonName + '_noexcept' ]
[addonName, addonName + '_noexcept']
.forEach((addonName) => {
const rootAddon = require('bindings')({
bindings: addonName,
Expand All @@ -20,23 +20,23 @@ const addonName = path.basename(__filename, '.js');
implems.reduce((suite, implem) => {
const fn = rootAddon[implem].noArgFunction;
return suite.add(implem.padStart(maxNameLength, ' '), () => fn());
}, new Benchmark.Suite)
}, new Benchmark.Suite())
.on('cycle', (event) => console.log(String(event.target)))
.run();

console.log('one argument:');
implems.reduce((suite, implem) => {
const fn = rootAddon[implem].oneArgFunction;
return suite.add(implem.padStart(maxNameLength, ' '), () => fn('x'));
}, new Benchmark.Suite)
}, new Benchmark.Suite())
.on('cycle', (event) => console.log(String(event.target)))
.run();

console.log('two arguments:');
implems.reduce((suite, implem) => {
const fn = rootAddon[implem].twoArgFunction;
return suite.add(implem.padStart(maxNameLength, ' '), () => fn('x', 12));
}, new Benchmark.Suite)
}, new Benchmark.Suite())
.on('cycle', (event) => console.log(String(event.target)))
.run();

Expand All @@ -45,7 +45,7 @@ const addonName = path.basename(__filename, '.js');
const fn = rootAddon[implem].threeArgFunction;
return suite.add(implem.padStart(maxNameLength, ' '),
() => fn('x', 12, true));
}, new Benchmark.Suite)
}, new Benchmark.Suite())
.on('cycle', (event) => console.log(String(event.target)))
.run();

Expand All @@ -54,7 +54,7 @@ const addonName = path.basename(__filename, '.js');
const fn = rootAddon[implem].fourArgFunction;
return suite.add(implem.padStart(maxNameLength, ' '),
() => fn('x', 12, true, anObject));
}, new Benchmark.Suite)
}, new Benchmark.Suite())
.on('cycle', (event) => console.log(String(event.target)))
.run();
});
2 changes: 1 addition & 1 deletion benchmark/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const path = require('path');

let benchmarks = [];

if (!!process.env.npm_config_benchmarks) {
if (process.env.npm_config_benchmarks) {
benchmarks = process.env.npm_config_benchmarks
.split(';')
.map((item) => (item + '.js'));
Expand Down
8 changes: 4 additions & 4 deletions benchmark/property_descriptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ const path = require('path');
const Benchmark = require('benchmark');
const addonName = path.basename(__filename, '.js');

[ addonName, addonName + '_noexcept' ]
[addonName, addonName + '_noexcept']
.forEach((addonName) => {
const rootAddon = require('bindings')({
bindings: addonName,
module_root: __dirname
});
delete rootAddon.path;
const getters = new Benchmark.Suite;
const setters = new Benchmark.Suite;
const getters = new Benchmark.Suite();
const setters = new Benchmark.Suite();
const maxNameLength = Object.keys(rootAddon)
.reduce((soFar, value) => Math.max(soFar, value.length), 0);

Expand All @@ -22,7 +22,7 @@ const addonName = path.basename(__filename, '.js');
});
setters.add(`${key} setter`.padStart(maxNameLength + 7), () => {
rootAddon[key] = 5;
})
});
});

getters
Expand Down
2 changes: 1 addition & 1 deletion test/addon.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const assert = require('assert');

module.exports = require('./common').runTest(test);

function test(binding) {
function test (binding) {
assert.strictEqual(binding.addon.increment(), 43);
assert.strictEqual(binding.addon.increment(), 44);
assert.strictEqual(binding.addon.subObject.decrement(), 43);
Expand Down
16 changes: 8 additions & 8 deletions test/addon_build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ const { promisify } = require('util');
const exec = promisify(require('child_process').exec);
const { copy, remove } = require('fs-extra');
const path = require('path');
const assert = require('assert')
const assert = require('assert');

const ADDONS_FOLDER = path.join(__dirname, 'addons');

const addons = [
'echo addon',
'echo-addon'
]
];

async function beforeAll(addons) {
console.log(' >Preparing native addons to build')
async function beforeAll (addons) {
console.log(' >Preparing native addons to build');
for (const addon of addons) {
await remove(path.join(ADDONS_FOLDER, addon));
await copy(path.join(__dirname, 'tpl'), path.join(ADDONS_FOLDER, addon));
}
}

async function test(addon) {
async function test (addon) {
console.log(` >Building addon: '${addon}'`);
const { stderr, stdout } = await exec('npm install', {
cwd: path.join(ADDONS_FOLDER, addon)
})
});
console.log(` >Running test for: '${addon}'`);
// Disabled the checks on stderr and stdout because of this issue on npm:
// Stop using process.umask(): https://github.com/npm/cli/issues/1103
Expand All @@ -41,9 +41,9 @@ async function test(addon) {
assert.strictEqual(binding.noexcept.echo(103), 103);
}

module.exports = (async function() {
module.exports = (async function () {
await beforeAll(addons);
for (const addon of addons) {
await test(addon);
}
})()
})();
8 changes: 4 additions & 4 deletions test/addon_build/tpl/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'
'use strict';

const except = require('bindings')('addon')
const noexcept = require('bindings')('addon_noexcept')
const except = require('bindings')('addon');
const noexcept = require('bindings')('addon_noexcept');

module.exports = {
except,
noexcept
}
};
6 changes: 3 additions & 3 deletions test/addon_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = require('./common').runTestWithBindingPath(test);

// Make sure the instance data finalizer is called at process exit. If the hint
// is non-zero, it will be printed out by the child process.
function testFinalizer(bindingName, hint, expected) {
function testFinalizer (bindingName, hint, expected) {
return new Promise((resolve) => {
bindingName = bindingName.split('\\').join('\\\\');
const child = spawn(process.execPath, [
Expand All @@ -30,7 +30,7 @@ function testFinalizer(bindingName, hint, expected) {
});
}

async function test(bindingName) {
async function test (bindingName) {
const binding = require(bindingName).addon_data(0);

// Make sure it is possible to get/set instance data.
Expand All @@ -42,5 +42,5 @@ async function test(bindingName) {

await testFinalizer(bindingName, 0, ['addon_data: Addon::~Addon']);
await testFinalizer(bindingName, 42,
['addon_data: Addon::~Addon', 'hint: 42']);
['addon_data: Addon::~Addon', 'hint: 42']);
}
4 changes: 2 additions & 2 deletions test/array_buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const testUtil = require('./testUtil');

module.exports = require('./common').runTest(test);

function test(binding) {
function test (binding) {
return testUtil.runGCTests([
'Internal ArrayBuffer',
() => {
Expand Down Expand Up @@ -64,6 +64,6 @@ function test(binding) {
// Let C++ detach the ArrayBuffer.
const extBuffer = binding.arraybuffer.createExternalBuffer();
binding.arraybuffer.checkDetachUpdatesData(extBuffer);
},
}
]);
}
38 changes: 20 additions & 18 deletions test/async_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const common = require('./common');

// we only check async hooks on 8.x an higher were
// they are closer to working properly
const nodeVersion = process.versions.node.split('.')[0]
let async_hooks = undefined;
function checkAsyncHooks() {
const nodeVersion = process.versions.node.split('.')[0];
let async_hooks;
function checkAsyncHooks () {
if (nodeVersion >= 8) {
if (async_hooks == undefined) {
async_hooks = require('async_hooks');
Expand All @@ -19,7 +19,7 @@ function checkAsyncHooks() {

module.exports = common.runTest(test);

function installAsyncHooksForTest() {
function installAsyncHooksForTest () {
return new Promise((resolve, reject) => {
let id;
const events = [];
Expand All @@ -38,23 +38,23 @@ function installAsyncHooksForTest() {
}, 10);

hook = async_hooks.createHook({
init(asyncId, type, triggerAsyncId, resource) {
init (asyncId, type, triggerAsyncId, resource) {
if (id === undefined && type === 'async_context_test') {
id = asyncId;
events.push({ eventName: 'init', type, triggerAsyncId, resource });
}
},
before(asyncId) {
before (asyncId) {
if (asyncId === id) {
events.push({ eventName: 'before' });
}
},
after(asyncId) {
after (asyncId) {
if (asyncId === id) {
events.push({ eventName: 'after' });
}
},
destroy(asyncId) {
destroy (asyncId) {
if (asyncId === id) {
events.push({ eventName: 'destroy' });
destroyed = true;
Expand All @@ -64,7 +64,7 @@ function installAsyncHooksForTest() {
});
}

function test(binding) {
function test (binding) {
if (!checkAsyncHooks()) {
return;
}
Expand All @@ -73,14 +73,16 @@ function test(binding) {
const triggerAsyncId = async_hooks.executionAsyncId();
binding.asynccontext.makeCallback(common.mustCall(), { foo: 'foo' });
return hooks.then(actual => {
assert.deepStrictEqual(actual, [
{ eventName: 'init',
type: 'async_context_test',
triggerAsyncId: triggerAsyncId,
resource: { foo: 'foo' } },
{ eventName: 'before' },
{ eventName: 'after' },
{ eventName: 'destroy' }
]);
assert.deepStrictEqual(actual, [
{
eventName: 'init',
type: 'async_context_test',
triggerAsyncId: triggerAsyncId,
resource: { foo: 'foo' }
},
{ eventName: 'before' },
{ eventName: 'after' },
{ eventName: 'destroy' }
]);
}).catch(common.mustNotCall());
}
10 changes: 5 additions & 5 deletions test/async_progress_queue_worker.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
'use strict';

const common = require('./common')
const common = require('./common');
const assert = require('assert');

module.exports = common.runTest(test);

async function test({ asyncprogressqueueworker }) {
async function test ({ asyncprogressqueueworker }) {
await success(asyncprogressqueueworker);
await fail(asyncprogressqueueworker);
}

function success(binding) {
function success (binding) {
return new Promise((resolve, reject) => {
const expected = [0, 1, 2, 3];
const actual = [];
Expand All @@ -32,11 +32,11 @@ function success(binding) {
});
}

function fail(binding) {
function fail (binding) {
return new Promise((resolve, reject) => {
const worker = binding.createWork(-1,
common.mustCall((err) => {
assert.throws(() => { throw err }, /test error/);
assert.throws(() => { throw err; }, /test error/);
resolve();
}),
common.mustNotCall()
Expand Down
12 changes: 6 additions & 6 deletions test/async_progress_worker.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
'use strict';

const common = require('./common')
const common = require('./common');
const assert = require('assert');

module.exports = common.runTest(test);

async function test({ asyncprogressworker }) {
async function test ({ asyncprogressworker }) {
await success(asyncprogressworker);
await fail(asyncprogressworker);
await malignTest(asyncprogressworker);
}

function success(binding) {
function success (binding) {
return new Promise((resolve, reject) => {
const expected = [0, 1, 2, 3];
const actual = [];
Expand All @@ -32,19 +32,19 @@ function success(binding) {
});
}

function fail(binding) {
function fail (binding) {
return new Promise((resolve) => {
binding.doWork(-1,
common.mustCall((err) => {
assert.throws(() => { throw err }, /test error/)
assert.throws(() => { throw err; }, /test error/);
resolve();
}),
common.mustNotCall()
);
});
}

function malignTest(binding) {
function malignTest (binding) {
return new Promise((resolve, reject) => {
binding.doMalignTest(
common.mustCall((err) => {
Expand Down
Loading

0 comments on commit a43bd0c

Please sign in to comment.