-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Set up a pattern for organizing .cc and .js test files roughly corresponding to each class to be tested - Add unit tests for the Function and Error classes - Update test binding.gyp file to build on Windows (fix quotes) - Update test binding.gyp and README to enable exceptions with MSVC - Fix type of CallbackInfo::This
- Loading branch information
Showing
12 changed files
with
282 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/node_modules | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include "napi.h" | ||
|
||
using namespace Napi; | ||
|
||
void ThrowError(const CallbackInfo& info) { | ||
std::string message = info[0].As<String>().Utf8Value(); | ||
throw Error::New(info.Env(), message); | ||
} | ||
|
||
void ThrowTypeError(const CallbackInfo& info) { | ||
std::string message = info[0].As<String>().Utf8Value(); | ||
throw TypeError::New(info.Env(), message); | ||
} | ||
|
||
void ThrowRangeError(const CallbackInfo& info) { | ||
std::string message = info[0].As<String>().Utf8Value(); | ||
throw RangeError::New(info.Env(), message); | ||
} | ||
|
||
Value CatchError(const CallbackInfo& info) { | ||
Function thrower = info[0].As<Function>(); | ||
try { | ||
thrower({}); | ||
} catch (const Error& e) { | ||
return e; | ||
} | ||
return info.Env().Null(); | ||
} | ||
|
||
Value CatchErrorMessage(const CallbackInfo& info) { | ||
Function thrower = info[0].As<Function>(); | ||
try { | ||
thrower({}); | ||
} catch (const Error& e) { | ||
std::string message = e.Message(); | ||
return String::New(info.Env(), message); | ||
} | ||
return info.Env().Null(); | ||
} | ||
|
||
void DoNotCatch(const CallbackInfo& info) { | ||
Function thrower = info[0].As<Function>(); | ||
thrower({}); | ||
} | ||
|
||
void CatchAndRethrowError(const CallbackInfo& info) { | ||
Function thrower = info[0].As<Function>(); | ||
try { | ||
thrower({}); | ||
} catch (Error& e) { | ||
e["caught"] = Boolean::New(info.Env(), true); | ||
throw e; | ||
} | ||
} | ||
|
||
Object InitError(Env env) { | ||
Object exports = Object::New(env); | ||
exports["throwError"] = Function::New(env, ThrowError); | ||
exports["throwTypeError"] = Function::New(env, ThrowTypeError); | ||
exports["throwRangeError"] = Function::New(env, ThrowRangeError); | ||
exports["catchError"] = Function::New(env, CatchError); | ||
exports["catchErrorMessage"] = Function::New(env, CatchErrorMessage); | ||
exports["doNotCatch"] = Function::New(env, DoNotCatch); | ||
exports["catchAndRethrowError"] = Function::New(env, CatchAndRethrowError); | ||
return exports; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict'; | ||
const buildType = process.config.target_defaults.default_configuration; | ||
const binding = require(`./build/${buildType}/binding.node`); | ||
const assert = require('assert'); | ||
|
||
assert.throws(() => binding.error.throwError('test'), err => { | ||
return err instanceof Error && err.message === 'test'; | ||
}); | ||
|
||
assert.throws(() => binding.error.throwTypeError('test'), err => { | ||
return err instanceof TypeError && err.message === 'test'; | ||
}); | ||
|
||
assert.throws(() => binding.error.throwRangeError('test'), err => { | ||
return err instanceof RangeError && err.message === 'test'; | ||
}); | ||
|
||
assert.throws( | ||
() => binding.error.doNotCatch( | ||
() => { | ||
throw new TypeError('test'); | ||
}), | ||
err => { | ||
return err instanceof TypeError && err.message === 'test' && !err.caught; | ||
}); | ||
|
||
assert.throws( | ||
() => binding.error.catchAndRethrowError( | ||
() => { | ||
throw new TypeError('test'); | ||
}), | ||
err => { | ||
return err instanceof TypeError && err.message === 'test' && err.caught; | ||
}); | ||
|
||
const err = binding.error.catchError( | ||
() => { throw new TypeError('test'); }); | ||
assert(err instanceof TypeError); | ||
assert.strictEqual(err.message, 'test'); | ||
|
||
const msg = binding.error.catchErrorMessage( | ||
() => { throw new TypeError('test'); }); | ||
assert.strictEqual(msg, 'test'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include "napi.h" | ||
|
||
using namespace Napi; | ||
|
||
void VoidCallback(const CallbackInfo& info) { | ||
auto env = info.Env(); | ||
Object obj = info[0].As<Object>(); | ||
|
||
obj["foo"] = String::New(env, "bar"); | ||
} | ||
|
||
Value ValueCallback(const CallbackInfo& info) { | ||
auto env = info.Env(); | ||
Object obj = Object::New(env); | ||
|
||
obj["foo"] = String::New(env, "bar"); | ||
|
||
return obj; | ||
} | ||
|
||
Value CallWithArgs(const CallbackInfo& info) { | ||
Function func = info[0].As<Function>(); | ||
return func({ info[1], info[2], info[3] }); | ||
} | ||
|
||
Value CallWithVector(const CallbackInfo& info) { | ||
Function func = info[0].As<Function>(); | ||
std::vector<napi_value> args; | ||
args.reserve(3); | ||
args.push_back(info[1]); | ||
args.push_back(info[2]); | ||
args.push_back(info[3]); | ||
return func.Call(args); | ||
} | ||
|
||
Value CallWithReceiverAndArgs(const CallbackInfo& info) { | ||
Function func = info[0].As<Function>(); | ||
Value receiver = info[1]; | ||
return func.Call(receiver, { info[2], info[3], info[4] }); | ||
} | ||
|
||
Value CallWithReceiverAndVector(const CallbackInfo& info) { | ||
Function func = info[0].As<Function>(); | ||
Value receiver = info[1]; | ||
std::vector<napi_value> args; | ||
args.reserve(3); | ||
args.push_back(info[2]); | ||
args.push_back(info[3]); | ||
args.push_back(info[4]); | ||
return func.Call(receiver, args); | ||
} | ||
|
||
Value CallConstructorWithArgs(const CallbackInfo& info) { | ||
Function func = info[0].As<Function>(); | ||
return func.New({ info[1], info[2], info[3] }); | ||
} | ||
|
||
Value CallConstructorWithVector(const CallbackInfo& info) { | ||
Function func = info[0].As<Function>(); | ||
std::vector<napi_value> args; | ||
args.reserve(3); | ||
args.push_back(info[1]); | ||
args.push_back(info[2]); | ||
args.push_back(info[3]); | ||
return func.New(args); | ||
} | ||
|
||
Object InitFunction(Env env) { | ||
Object exports = Object::New(env); | ||
exports["voidCallback"] = Function::New(env, VoidCallback, "voidCallback"); | ||
exports["valueCallback"] = Function::New(env, ValueCallback, std::string("valueCallback")); | ||
exports["callWithArgs"] = Function::New(env, CallWithArgs); | ||
exports["callWithVector"] = Function::New(env, CallWithVector); | ||
exports["callWithReceiverAndArgs"] = Function::New(env, CallWithReceiverAndArgs); | ||
exports["callWithReceiverAndVector"] = Function::New(env, CallWithReceiverAndVector); | ||
exports["callConstructorWithArgs"] = Function::New(env, CallConstructorWithArgs); | ||
exports["callConstructorWithVector"] = Function::New(env, CallConstructorWithVector); | ||
return exports; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict'; | ||
const buildType = process.config.target_defaults.default_configuration; | ||
const binding = require(`./build/${buildType}/binding.node`); | ||
const assert = require('assert'); | ||
|
||
let obj = {}; | ||
assert.deepStrictEqual(binding.function.voidCallback(obj), undefined); | ||
assert.deepStrictEqual(obj, { "foo": "bar" }); | ||
|
||
assert.deepStrictEqual(binding.function.valueCallback(), { "foo": "bar" }); | ||
|
||
let args = null; | ||
let ret = null; | ||
let receiver = null; | ||
function testFunction() { | ||
receiver = this; | ||
args = [].slice.call(arguments); | ||
return ret; | ||
} | ||
function testConstructor() { | ||
args = [].slice.call(arguments); | ||
} | ||
|
||
ret = 4; | ||
assert.equal(binding.function.callWithArgs(testFunction, 1, 2, 3), 4); | ||
assert.strictEqual(receiver, undefined); | ||
assert.deepStrictEqual(args, [ 1, 2, 3 ]); | ||
|
||
ret = 5; | ||
assert.equal(binding.function.callWithVector(testFunction, 2, 3, 4), 5); | ||
assert.strictEqual(receiver, undefined); | ||
assert.deepStrictEqual(args, [ 2, 3, 4 ]); | ||
|
||
ret = 6; | ||
assert.equal(binding.function.callWithReceiverAndArgs(testFunction, obj, 3, 4, 5), 6); | ||
assert.deepStrictEqual(receiver, obj); | ||
assert.deepStrictEqual(args, [ 3, 4, 5 ]); | ||
|
||
ret = 7; | ||
assert.equal(binding.function.callWithReceiverAndVector(testFunction, obj, 4, 5, 6), 7); | ||
assert.deepStrictEqual(receiver, obj); | ||
assert.deepStrictEqual(args, [ 4, 5, 6 ]); | ||
|
||
obj = binding.function.callConstructorWithArgs(testConstructor, 5, 6, 7); | ||
assert(obj instanceof testConstructor); | ||
assert.deepStrictEqual(args, [ 5, 6, 7 ]); | ||
|
||
obj = binding.function.callConstructorWithVector(testConstructor, 6, 7, 8); | ||
assert(obj instanceof testConstructor); | ||
assert.deepStrictEqual(args, [ 6, 7, 8 ]); | ||
|
||
assert.equal(binding.function.voidCallback.name, 'voidCallback'); | ||
assert.equal(binding.function.valueCallback.name, 'valueCallback'); | ||
|
||
// TODO: Function::MakeCallback tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
'use strict'; | ||
const binding = require('./build/Release/binding.node'); | ||
const assert = require('assert'); | ||
|
||
assert.deepStrictEqual(binding.test1(), { "foo": "bar" }); | ||
let testModules = [ | ||
'error', | ||
'function', | ||
]; | ||
|
||
testModules.forEach(name => { | ||
try { | ||
require('./' + name); | ||
} | ||
catch (e) { | ||
console.error(e); | ||
} | ||
}); |