-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR-URL: nodejs/node-addon-api#137 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Jason Ginchereau <[email protected]>
- Loading branch information
1 parent
3462b4b
commit 938fde4
Showing
8 changed files
with
185 additions
and
0 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
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 |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
'function.cc', | ||
'name.cc', | ||
'object.cc', | ||
'promise.cc', | ||
'typedarray.cc', | ||
'objectwrap.cc', | ||
], | ||
|
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,70 @@ | ||
/* Test helpers ported from test/common/index.js in Node.js project. */ | ||
'use strict'; | ||
const assert = require('assert'); | ||
|
||
const noop = () => {}; | ||
|
||
const mustCallChecks = []; | ||
|
||
function runCallChecks(exitCode) { | ||
if (exitCode !== 0) return; | ||
|
||
const failed = mustCallChecks.filter(function(context) { | ||
if ('minimum' in context) { | ||
context.messageSegment = `at least ${context.minimum}`; | ||
return context.actual < context.minimum; | ||
} else { | ||
context.messageSegment = `exactly ${context.exact}`; | ||
return context.actual !== context.exact; | ||
} | ||
}); | ||
|
||
failed.forEach(function(context) { | ||
console.log('Mismatched %s function calls. Expected %s, actual %d.', | ||
context.name, | ||
context.messageSegment, | ||
context.actual); | ||
console.log(context.stack.split('\n').slice(2).join('\n')); | ||
}); | ||
|
||
if (failed.length) process.exit(1); | ||
} | ||
|
||
exports.mustCall = function(fn, exact) { | ||
return _mustCallInner(fn, exact, 'exact'); | ||
}; | ||
|
||
function _mustCallInner(fn, criteria = 1, field) { | ||
if (typeof fn === 'number') { | ||
criteria = fn; | ||
fn = noop; | ||
} else if (fn === undefined) { | ||
fn = noop; | ||
} | ||
|
||
if (typeof criteria !== 'number') | ||
throw new TypeError(`Invalid ${field} value: ${criteria}`); | ||
|
||
const context = { | ||
[field]: criteria, | ||
actual: 0, | ||
stack: (new Error()).stack, | ||
name: fn.name || '<anonymous>' | ||
}; | ||
|
||
// add the exit listener only once to avoid listener leak warnings | ||
if (mustCallChecks.length === 0) process.on('exit', runCallChecks); | ||
|
||
mustCallChecks.push(context); | ||
|
||
return function() { | ||
context.actual++; | ||
return fn.apply(this, arguments); | ||
}; | ||
} | ||
|
||
exports.mustNotCall = function(msg) { | ||
return function mustNotCall() { | ||
assert.fail(msg || 'function should not have been called'); | ||
}; | ||
}; |
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 |
---|---|---|
|
@@ -14,6 +14,7 @@ let testModules = [ | |
'function', | ||
'name', | ||
'object', | ||
'promise', | ||
'typedarray', | ||
'objectwrap' | ||
]; | ||
|
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,29 @@ | ||
#include "napi.h" | ||
|
||
using namespace Napi; | ||
|
||
Value IsPromise(const CallbackInfo& info) { | ||
return Boolean::New(info.Env(), info[0].IsPromise()); | ||
} | ||
|
||
Value ResolvePromise(const CallbackInfo& info) { | ||
auto deferred = Promise::Deferred::New(info.Env()); | ||
deferred.Resolve(info[0]); | ||
return deferred.Promise(); | ||
} | ||
|
||
Value RejectPromise(const CallbackInfo& info) { | ||
auto deferred = Promise::Deferred::New(info.Env()); | ||
deferred.Reject(info[0]); | ||
return deferred.Promise(); | ||
} | ||
|
||
Object InitPromise(Env env) { | ||
Object exports = Object::New(env); | ||
|
||
exports["isPromise"] = Function::New(env, IsPromise); | ||
exports["resolvePromise"] = Function::New(env, ResolvePromise); | ||
exports["rejectPromise"] = Function::New(env, RejectPromise); | ||
|
||
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,19 @@ | ||
'use strict'; | ||
const buildType = process.config.target_defaults.default_configuration; | ||
const assert = require('assert'); | ||
const common = require('./common'); | ||
|
||
test(require(`./build/${buildType}/binding.node`)); | ||
test(require(`./build/${buildType}/binding_noexcept.node`)); | ||
|
||
function test(binding) { | ||
assert.strictEqual(binding.promise.isPromise({}), false); | ||
|
||
const resolving = binding.promise.resolvePromise('resolved'); | ||
assert.strictEqual(binding.promise.isPromise(resolving), true); | ||
resolving.then(common.mustCall()).catch(common.mustNotCall()); | ||
|
||
const rejecting = binding.promise.rejectPromise('error'); | ||
assert.strictEqual(binding.promise.isPromise(rejecting), true); | ||
rejecting.then(common.mustNotCall()).catch(common.mustCall()); | ||
} |