Skip to content

Commit

Permalink
test: add missing value tests
Browse files Browse the repository at this point in the history
PR-URL: #1170
Reviewed-By: Michael Dawson <midawson@redhat.com
  • Loading branch information
JckXia authored and mhdawson committed May 5, 2022
1 parent e469ee6 commit d53843b
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 21 deletions.
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

0 comments on commit d53843b

Please sign in to comment.