Skip to content

Commit

Permalink
unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Deepak Rajamohan committed Sep 11, 2021
1 parent 577543b commit 528dcde
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 93 deletions.
2 changes: 0 additions & 2 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ exports.runTest = async function(test, buildType, buildPathRoot = process.env.RE
path.join(buildPathRoot, `../build/${buildType}/binding_noexcept.node`),
].map(it => require.resolve(it));

console.log('BINDINGS ... ', bindings)

for (const item of bindings) {
await Promise.resolve(test(require(item)))
.finally(exports.mustCall());
Expand Down
2 changes: 0 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ function loadTestModules(currentDirectory = getCurrentDir(), pre = '') {

loadTestModules();

testModules.push('objectwrap')

process.config.target_defaults.default_configuration =
fs
.readdirSync(path.join(__dirname, 'build'))
Expand Down
40 changes: 40 additions & 0 deletions unit-test/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const testFunctionsMapV5 = {
"addon": { "objectName": "Addon" },
"addon_data": { "objectName": "AddonData" }
};

const testFunctionsMapV3 = {
"asyncprogressqueueworker": { "objectName": "AsyncProgressQueueWorker" },
"asyncprogressworker": { "objectName": "AsyncProgressWorker" },
};

const testFunctionsMap = {
"arraybuffer": { "objectName": "ArrayBuffer" },
"asynccontext": { "objectName": "AsyncContext" },
"asyncworker-persistent": { "objectName": "PersistentAsyncWorker" },
"asyncworker": { "objectName": "AsyncWorker" },
"bigint": { "objectName": "BigInt" },
"buffer": { "objectName": "Buffer" },
"callbackscope": { "objectName": "CallbackScope" },
"date": { "objectName": "Date" },
"error": { "objectName": "Error" },
"external": { "objectName": "External" },
"function": { "objectName": "Function" },
"functionreference": { "objectName": "FunctionReference" },
"handlescope": { "objectName": "HandleScope" },
"memory_management": { "objectName": "MemoryManagement" },
"movable_callbacks": { "objectName": "MovableCallbacks" },
"name": { "objectName": "Name" },
"objectreference": { "objectName": "ObjectReference" },
"objectwrap-removewrap": { "objectName": "ObjectWrapRemoveWrap" },
"objectwrap": { "objectName": "ObjectWrap" },
"objectwrap_constructor_exception": { "objectName": "ObjectWrapConstructorException" },
"objectwrap_multiple_inheritance": { "objectName": "ObjectWrapMultipleInheritance" },
"promise": { "objectName": "Promise" },
"reference": { "objectName": "Reference" },
"run_script": { "objectName": "RunScript" },
"symbol": { "objectName": "Symbol" },
"thunking_manual": { "objectName": "ThunkingManual" },
"typedarray": { "objectName": "TypedArray" },
"version_management": { "objectName": "VersionManagement" }
};
78 changes: 33 additions & 45 deletions unit-test/generate-binding-cc.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,41 @@
const fs = require('fs');
const path = require('path');
const filesToRun = process.argv.slice(2);

const testFunctionsMapV5 = {
"addon": { "objectName": "Addon" },
"addon_data": { "objectName": "AddonData" }
};

const testFunctionsMapV3 = {
"asyncprogressqueueworker": { "objectName": "AsyncProgressQueueWorker" },
"asyncprogressworker": { "objectName": "AsyncProgressWorker" },
};
console.log('files to compile: ', process.argv, filesToRun);
const buildFiles = {};
const buidDirs = {};

const testFunctionsMap = {
"arraybuffer": { "objectName": "ArrayBuffer" },
"asynccontext": { "objectName": "AsyncContext" },
"asyncworker-persistent": { "objectName": "PersistentAsyncWorker" },
"asyncworker": { "objectName": "AsyncWorker" },
"bigint": { "objectName": "BigInt" },
"buffer": { "objectName": "Buffer" },
"callbackscope": { "objectName": "CallbackScope" },
"date": { "objectName": "Date" },
"error": { "objectName": "Error" },
"external": { "objectName": "External" },
"function": { "objectName": "Function" },
"functionreference": { "objectName": "FunctionReference" },
"handlescope": { "objectName": "HandleScope" },
"memory_management": { "objectName": "MemoryManagement" },
"movable_callbacks": { "objectName": "MovableCallbacks" },
"name": { "objectName": "Name" },
"objectreference": { "objectName": "ObjectReference" },
"objectwrap-removewrap": { "objectName": "ObjectWrapRemoveWrap" },
"objectwrap": { "objectName": "ObjectWrap" },
"objectwrap_constructor_exception": { "objectName": "ObjectWrapConstructorException" },
"objectwrap_multiple_inheritance": { "objectName": "ObjectWrapMultipleInheritance" },
"promise": { "objectName": "Promise" },
"reference": { "objectName": "Reference" },
"run_script": { "objectName": "RunScript" },
"symbol": { "objectName": "Symbol" },
"thunking_manual": { "objectName": "ThunkingManual" },
"typedarray": { "objectName": "TypedArray" },
"version_management": { "objectName": "VersionManagement" }
};
function getExportName(fileName) {
const str = fileName.replace(/(\_\w)/g, (k) => k[1].toUpperCase());
return str.charAt(0).toUpperCase() + str.substring(1);
}

console.log('generate start', new Date())
function listOfTestModules(currentDirectory = __dirname + '/../test', pre = '') {
fs.readdirSync(currentDirectory).forEach((file) => {
if (file === 'binding.cc' ||
file === 'binding.gyp' ||
file === 'build' ||
file === 'common' ||
file === 'thunking_manual.cc' ||
file === 'addon_build' ||
file[0] === '.') {
return;
}
const absoluteFilepath = path.join(currentDirectory, file);
const fileName = file.toLowerCase().replace('.cc', '');
if (fs.statSync(absoluteFilepath).isDirectory()) {
buidDirs[fileName] = {}
listOfTestModules(absoluteFilepath, pre + file + '/');
} else {
if (!file.toLowerCase().endsWith('.cc')) return;
buildFiles[fileName] = { objectName: getExportName(fileName) };
}
});
}
listOfTestModules();

const filesToRun = process.argv.slice(2);
console.log('files to compile: ', process.argv, filesToRun);
console.log('testBinaries', buildFiles)

p = new Promise((resolve, reject) => {
/*setTimeout(() => {
Expand All @@ -57,7 +47,7 @@ p = new Promise((resolve, reject) => {

filesToRun.forEach((file) => {
const configName = file.split('.cc')[0];
const config = testFunctionsMap[configName] || testFunctionsMapV5[configName] || testFunctionsMapV3[configName];
const config = buidDirs[configName];
if (!config) {
console.log('not found', file, configName);
return
Expand All @@ -68,7 +58,6 @@ p = new Promise((resolve, reject) => {
});

content.push("#include \"napi.h\"");
content.push("#include \"hello.h\"");
content.push("using namespace Napi;");

//content.push("Object InitName(Env env);");
Expand All @@ -79,7 +68,6 @@ p = new Promise((resolve, reject) => {
//content.push("exports.Set(\"name\", InitName(env));");
exports.forEach(exp => content.push(exp));

content.push("exports.Set(Napi::String::New(env, \"HelloWorld\"), Napi::Function::New(env, HelloWorld));")
content.push("return exports;");
content.push("}");
content.push("NODE_API_MODULE(addon, Init);")
Expand Down
4 changes: 0 additions & 4 deletions unit-test/generated/binding.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#include "napi.h"
#include "hello.h"
using namespace Napi;
Object InitFunction(Env env);
Object Init(Env env, Object exports) {
exports.Set("function", InitFunction(env));
exports.Set(Napi::String::New(env, "HelloWorld"), Napi::Function::New(env, HelloWorld));
return exports;
}
NODE_API_MODULE(addon, Init);
4 changes: 2 additions & 2 deletions unit-test/injectTestParams.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

module.exports.filesToCompile = function () {
const filterCondition = process.env.filter;
let files_to_compile = './generated/binding.cc ../test/hello.cc';
let files_to_compile = './generated/binding.cc';
let files = filterCondition.split(' ').length ? filterCondition.split(' ') : [filterCondition];
let addedFiles = '';
files.forEach((file) => addedFiles = `${addedFiles} ../test/${file}.cc`);
Expand All @@ -10,7 +10,7 @@ module.exports.filesToCompile = function () {

module.exports.filesForBinding = function () {
const filterCondition = process.env.filter;
let files_to_compile = 'hello.cc';
let files_to_compile = '';
let files = filterCondition.split(' ').length ? filterCondition.split(' ') : [filterCondition];
let addedFiles = '';
files.forEach((file) => addedFiles = `${addedFiles} ${file}.cc`);
Expand Down
58 changes: 20 additions & 38 deletions unit-test/test.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,29 @@
'use strict';
const path = require('path');
const addon = require('./build/Release/binding.node');
const assert = require("assert");
const { spawn } = require("child_process");

process.argv.forEach(function (val, index, array) {
console.log(index + ': ' + val);
});
const executeTests = async function () {
const childProcess = spawn("node", ["test"], {cwd: path.join(__dirname, '../'), env: { ...process.env, REL_BUILD_PATH: path.join(__dirname, './build') }});

const ls = spawn("node", ["test"], {cwd: path.join(__dirname, '../'), env: { ...process.env, REL_BUILD_PATH: path.join(__dirname, './build') }});
childProcess.stdout.on("data", data => {
console.log(`${data}`);
});

ls.stdout.on("data", data => {
console.log(`stdout: ${data}`);
});
childProcess.stderr.on("data", data => {
console.log(`error: ${data}`);
});

ls.stderr.on("data", data => {
console.log(`stderr: ${data}`);
});
return new Promise((resolve, reject) => {
childProcess.on('error', (error) => {
console.log(`error: ${error.message}`);
reject(error);
});

ls.on('error', (error) => {
console.log(`error: ${error.message}`);
});
childProcess.on("close", code => {
console.log(`child process exited with code ${code}`);
resolve();
});
});
}

ls.on("close", code => {
console.log(`child process exited with code ${code}`);
});

/**
*
* Hello world test
*
*/
const HelloWorld = addon.HelloWorld;

assert(HelloWorld, "The expected function is undefined");

function testBasic()
{
const result = HelloWorld("hello");
assert.strictEqual(result, "world", "Unexpected value returned");
}

assert.doesNotThrow(testBasic, undefined, "testBasic threw an expection");

console.log("Tests passed- everything looks OK!");

executeTests();

0 comments on commit 528dcde

Please sign in to comment.