Skip to content

Commit 6d518e4

Browse files
romandevmhdawson
authored andcommitted
test: Write tests for Value class
This change includes the following new tests: - TypeChecker(e.g. IsUndefined, IsObject) tests - TypeConverter from Native to JS(e.g. ToString) tests PR-URL: #197 Reviewed-By: Michael Dawson <[email protected]>
1 parent 8e31e24 commit 6d518e4

File tree

5 files changed

+216
-0
lines changed

5 files changed

+216
-0
lines changed

test/basic_types/value.cc

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include "napi.h"
2+
3+
using namespace Napi;
4+
5+
static Value IsEmpty(const CallbackInfo& info) {
6+
Value value;
7+
return Boolean::New(info.Env(), value.IsEmpty());
8+
}
9+
10+
static Value IsUndefined(const CallbackInfo& info) {
11+
return Boolean::New(info.Env(), info[0].IsUndefined());
12+
}
13+
14+
static Value IsNull(const CallbackInfo& info) {
15+
return Boolean::New(info.Env(), info[0].IsNull());
16+
}
17+
18+
static Value IsBoolean(const CallbackInfo& info) {
19+
return Boolean::New(info.Env(), info[0].IsBoolean());
20+
}
21+
22+
static Value IsNumber(const CallbackInfo& info) {
23+
return Boolean::New(info.Env(), info[0].IsNumber());
24+
}
25+
26+
static Value IsString(const CallbackInfo& info) {
27+
return Boolean::New(info.Env(), info[0].IsString());
28+
}
29+
30+
static Value IsSymbol(const CallbackInfo& info) {
31+
return Boolean::New(info.Env(), info[0].IsSymbol());
32+
}
33+
34+
static Value IsArray(const CallbackInfo& info) {
35+
return Boolean::New(info.Env(), info[0].IsArray());
36+
}
37+
38+
static Value IsArrayBuffer(const CallbackInfo& info) {
39+
return Boolean::New(info.Env(), info[0].IsArrayBuffer());
40+
}
41+
42+
static Value IsTypedArray(const CallbackInfo& info) {
43+
return Boolean::New(info.Env(), info[0].IsTypedArray());
44+
}
45+
46+
static Value IsObject(const CallbackInfo& info) {
47+
return Boolean::New(info.Env(), info[0].IsObject());
48+
}
49+
50+
static Value IsFunction(const CallbackInfo& info) {
51+
return Boolean::New(info.Env(), info[0].IsFunction());
52+
}
53+
54+
static Value IsPromise(const CallbackInfo& info) {
55+
return Boolean::New(info.Env(), info[0].IsPromise());
56+
}
57+
58+
static Value ToBoolean(const CallbackInfo& info) {
59+
return info[0].ToBoolean();
60+
}
61+
62+
static Value ToNumber(const CallbackInfo& info) {
63+
return info[0].ToNumber();
64+
}
65+
66+
static Value ToString(const CallbackInfo& info) {
67+
return info[0].ToString();
68+
}
69+
70+
static Value ToObject(const CallbackInfo& info) {
71+
return info[0].ToObject();
72+
}
73+
74+
Object InitBasicTypesValue(Env env) {
75+
Object exports = Object::New(env);
76+
77+
exports["isEmpty"] = Function::New(env, IsEmpty);
78+
exports["isUndefined"] = Function::New(env, IsUndefined);
79+
exports["isNull"] = Function::New(env, IsNull);
80+
exports["isBoolean"] = Function::New(env, IsBoolean);
81+
exports["isNumber"] = Function::New(env, IsNumber);
82+
exports["isString"] = Function::New(env, IsString);
83+
exports["isSymbol"] = Function::New(env, IsSymbol);
84+
exports["isArray"] = Function::New(env, IsArray);
85+
exports["isArrayBuffer"] = Function::New(env, IsArrayBuffer);
86+
exports["isTypedArray"] = Function::New(env, IsTypedArray);
87+
exports["isObject"] = Function::New(env, IsObject);
88+
exports["isFunction"] = Function::New(env, IsFunction);
89+
exports["isPromise"] = Function::New(env, IsPromise);
90+
exports["toBoolean"] = Function::New(env, ToBoolean);
91+
exports["toNumber"] = Function::New(env, ToNumber);
92+
exports["toString"] = Function::New(env, ToString);
93+
exports["toObject"] = Function::New(env, ToObject);
94+
95+
return exports;
96+
}

test/basic_types/value.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
'use strict';
2+
3+
const buildType = process.config.target_defaults.default_configuration;
4+
const assert = require('assert');
5+
6+
test(require(`../build/${buildType}/binding.node`));
7+
test(require(`../build/${buildType}/binding_noexcept.node`));
8+
9+
function test(binding) {
10+
function detailedTypeOf(value) {
11+
const type = typeof value;
12+
if (type !== 'object')
13+
return type;
14+
15+
if (value === null)
16+
return 'null';
17+
18+
if (Array.isArray(value))
19+
return 'array';
20+
21+
if (!value.constructor)
22+
return type;
23+
24+
if (value instanceof ArrayBuffer)
25+
return 'arraybuffer';
26+
27+
if (ArrayBuffer.isView(value))
28+
return 'typedarray';
29+
30+
if (value instanceof Promise)
31+
return 'promise';
32+
33+
return 'object';
34+
}
35+
36+
function typeCheckerTest(typeChecker, expectedType) {
37+
const testValueList = [
38+
undefined,
39+
null,
40+
true,
41+
10,
42+
'string',
43+
Symbol('symbol'),
44+
[],
45+
new ArrayBuffer(10),
46+
new Int32Array(new ArrayBuffer(12)),
47+
{},
48+
function() {},
49+
new Promise((resolve, reject) => {})
50+
];
51+
52+
testValueList.forEach((testValue) => {
53+
if (testValue !== null && expectedType === 'object') {
54+
assert.strictEqual(typeChecker(testValue), typeof testValue === expectedType);
55+
} else {
56+
assert.strictEqual(typeChecker(testValue), detailedTypeOf(testValue) === expectedType);
57+
}
58+
});
59+
}
60+
61+
function typeConverterTest(typeConverter, expectedType) {
62+
const testValueList = [
63+
true,
64+
false,
65+
0,
66+
10,
67+
'string',
68+
[],
69+
new ArrayBuffer(10),
70+
new Int32Array(new ArrayBuffer(12)),
71+
{},
72+
function() {},
73+
new Promise((resolve, reject) => {})
74+
];
75+
76+
testValueList.forEach((testValue) => {
77+
const expected = expectedType(testValue);
78+
const actual = typeConverter(testValue);
79+
80+
if (isNaN(expected)) {
81+
assert(isNaN(actual));
82+
} else {
83+
assert.deepStrictEqual(typeConverter(testValue), expectedType(testValue));
84+
}
85+
});
86+
}
87+
88+
const value = binding.basic_types_value;
89+
90+
typeCheckerTest(value.isUndefined, 'undefined');
91+
typeCheckerTest(value.isNull, 'null');
92+
typeCheckerTest(value.isBoolean, 'boolean');
93+
typeCheckerTest(value.isNumber, 'number');
94+
typeCheckerTest(value.isString, 'string');
95+
typeCheckerTest(value.isSymbol, 'symbol');
96+
typeCheckerTest(value.isArray, 'array');
97+
typeCheckerTest(value.isArrayBuffer, 'arraybuffer');
98+
typeCheckerTest(value.isTypedArray, 'typedarray');
99+
typeCheckerTest(value.isObject, 'object');
100+
typeCheckerTest(value.isFunction, 'function');
101+
typeCheckerTest(value.isPromise, 'promise');
102+
103+
typeConverterTest(value.toBoolean, Boolean);
104+
assert.strictEqual(value.toBoolean(undefined), false);
105+
assert.strictEqual(value.toBoolean(null), false);
106+
107+
typeConverterTest(value.toNumber, Number);
108+
assert(isNaN(value.toNumber(undefined)));
109+
assert.strictEqual(value.toNumber(null), 0);
110+
111+
typeConverterTest(value.toString, String);
112+
assert.strictEqual(value.toString(undefined), 'undefined');
113+
assert.strictEqual(value.toString(null), 'null');
114+
115+
typeConverterTest(value.toObject, Object);
116+
}

test/binding.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ using namespace Napi;
55
Object InitArrayBuffer(Env env);
66
Object InitAsyncWorker(Env env);
77
Object InitBasicTypesNumber(Env env);
8+
Object InitBasicTypesValue(Env env);
89
Object InitBuffer(Env env);
910
Object InitError(Env env);
1011
Object InitExternal(Env env);
@@ -19,6 +20,7 @@ Object Init(Env env, Object exports) {
1920
exports.Set("arraybuffer", InitArrayBuffer(env));
2021
exports.Set("asyncworker", InitAsyncWorker(env));
2122
exports.Set("basic_types_number", InitBasicTypesNumber(env));
23+
exports.Set("basic_types_value", InitBasicTypesValue(env));
2224
exports.Set("buffer", InitBuffer(env));
2325
exports.Set("error", InitError(env));
2426
exports.Set("external", InitExternal(env));

test/binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
'arraybuffer.cc',
55
'asyncworker.cc',
66
'basic_types/number.cc',
7+
'basic_types/value.cc',
78
'binding.cc',
89
'buffer.cc',
910
'error.cc',

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ let testModules = [
1111
'arraybuffer',
1212
'asyncworker',
1313
'basic_types/number',
14+
'basic_types/value',
1415
'buffer',
1516
'error',
1617
'external',

0 commit comments

Comments
 (0)