Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing value tests #1170

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions test/basic_types/value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,43 @@ Value CreateExternal(const CallbackInfo& info) {

} // end anonymous namespace

static Value StrictlyEquals(const CallbackInfo& info) {
bool strictlyEquals = info[0].StrictEquals(info[1]);
return Boolean::New(info.Env(), strictlyEquals);
}

// tests the '==' overload
static Value StrictEqualsOverload(const CallbackInfo& info) {
bool strictlyEquals = info[0] == info[1];
return Boolean::New(info.Env(), strictlyEquals);
}

// tests the '!=' overload
static Value StrictlyNotEqualsOverload(const CallbackInfo& info) {
bool strictlyEquals = info[0] != info[1];
return Boolean::New(info.Env(), strictlyEquals);
}

static Value ValueReturnsCorrectEnv(const CallbackInfo& info) {
Value testValue = CreateExternal(info);
return Boolean::New(info.Env(), testValue.Env() == info.Env());
}

static Value EmptyValueReturnNullPtrOnCast(const CallbackInfo& info) {
Value emptyValue;
bool isNullPtr = static_cast<napi_value>(emptyValue) == nullptr;
return Boolean::New(info.Env(), isNullPtr);
}

static Value NonEmptyValueReturnValOnCast(const CallbackInfo& info) {
Value boolValue = Value::From(info.Env(), true);
return Boolean::New(info.Env(), static_cast<napi_value>(boolValue));
}

static Value CreateNonEmptyValue(const CallbackInfo& info) {
return Napi::Value(info.Env(), String::New(info.Env(), "non_empty_val"));
}

static Value IsEmpty(const CallbackInfo& info) {
Value value;
return Boolean::New(info.Env(), value.IsEmpty());
Expand Down Expand Up @@ -114,6 +151,20 @@ Object InitBasicTypesValue(Env env) {
exports["toString"] = Function::New(env, ToString);
exports["toObject"] = Function::New(env, ToObject);

exports["strictlyEquals"] = Function::New(env, StrictlyEquals);
exports["strictlyEqualsOverload"] = Function::New(env, StrictEqualsOverload);
exports["strictlyNotEqualsOverload"] =
Function::New(env, StrictlyNotEqualsOverload);

exports["assertValueReturnsCorrectEnv"] =
Function::New(env, ValueReturnsCorrectEnv);

exports["assertEmptyValReturnNullPtrOnCast"] =
Function::New(env, EmptyValueReturnNullPtrOnCast);
exports["assertNonEmptyReturnValOnCast"] =
Function::New(env, NonEmptyValueReturnValOnCast);

exports["createNonEmptyValue"] = Function::New(env, CreateNonEmptyValue);
exports["createExternal"] = Function::New(env, CreateExternal);

return exports;
Expand Down
65 changes: 44 additions & 21 deletions test/basic_types/value.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,27 @@ const assert = require('assert');

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

function test(binding) {
function test (binding) {
const externalValue = binding.basic_types_value.createExternal();

function isObject(value) {
function isObject (value) {
return (typeof value === 'object' && value !== externalValue) ||
(typeof value === 'function');
}

function detailedTypeOf(value) {
function detailedTypeOf (value) {
const type = typeof value;
if (type !== 'object')
return type;
if (type !== 'object') { return type; }

if (value === null)
return 'null';
if (value === null) { return 'null'; }

if (Array.isArray(value))
return 'array';
if (Array.isArray(value)) { return 'array'; }

if (value === externalValue)
return 'external';
if (value === externalValue) { return 'external'; }

if (!value.constructor)
return type;
if (!value.constructor) { return type; }

if (value instanceof ArrayBuffer)
return 'arraybuffer';
if (value instanceof ArrayBuffer) { return 'arraybuffer'; }

if (ArrayBuffer.isView(value)) {
if (value instanceof DataView) {
Expand All @@ -40,13 +34,12 @@ function test(binding) {
}
}

if (value instanceof Promise)
return 'promise';
if (value instanceof Promise) { return 'promise'; }

return 'object';
}

function typeCheckerTest(typeChecker, expectedType) {
function typeCheckerTest (typeChecker, expectedType) {
const testValueList = [
undefined,
null,
Expand All @@ -58,7 +51,7 @@ function test(binding) {
new ArrayBuffer(10),
new Int32Array(new ArrayBuffer(12)),
{},
function() {},
function () {},
new Promise((resolve, reject) => {}),
new DataView(new ArrayBuffer(12)),
externalValue
Expand All @@ -73,7 +66,7 @@ function test(binding) {
});
}

function typeConverterTest(typeConverter, expectedType) {
function typeConverterTest (typeConverter, expectedType) {
const testValueList = [
true,
false,
Expand All @@ -84,7 +77,7 @@ function test(binding) {
new ArrayBuffer(10),
new Int32Array(new ArrayBuffer(12)),
{},
function() {},
function () {},
new Promise((resolve, reject) => {})
];

Expand All @@ -100,8 +93,38 @@ function test(binding) {
});
}

function assertValueStrictlyEqual (value) {
const newValue = value.createNonEmptyValue();
assert(value.strictlyEquals(newValue, newValue));
assert(value.strictlyEqualsOverload(newValue, newValue));
}

function assertValueStrictlyNonEqual (value) {
const valueA = value.createNonEmptyValue();
const valueB = value.createExternal();
assert(value.strictlyNotEqualsOverload(valueA, valueB));
}

function assertValueReturnsCorrectEnv (value) {
assert(value.assertValueReturnsCorrectEnv());
}

function assertEmptyValueNullPtrOnCast (value) {
assert(value.assertEmptyValReturnNullPtrOnCast());
}

function assertNonEmptyReturnValOnCast (value) {
assert(value.assertNonEmptyReturnValOnCast());
}

const value = binding.basic_types_value;

assertValueStrictlyEqual(value);
assertValueStrictlyNonEqual(value);
assertValueReturnsCorrectEnv(value);
assertEmptyValueNullPtrOnCast(value);
assertNonEmptyReturnValOnCast(value);

typeCheckerTest(value.isUndefined, 'undefined');
typeCheckerTest(value.isNull, 'null');
typeCheckerTest(value.isBoolean, 'boolean');
Expand Down