Skip to content

Commit d53843b

Browse files
JckXiamhdawson
authored andcommitted
test: add missing value tests
PR-URL: #1170 Reviewed-By: Michael Dawson <[email protected]
1 parent e469ee6 commit d53843b

File tree

2 files changed

+95
-21
lines changed

2 files changed

+95
-21
lines changed

test/basic_types/value.cc

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,43 @@ Value CreateExternal(const CallbackInfo& info) {
1414

1515
} // end anonymous namespace
1616

17+
static Value StrictlyEquals(const CallbackInfo& info) {
18+
bool strictlyEquals = info[0].StrictEquals(info[1]);
19+
return Boolean::New(info.Env(), strictlyEquals);
20+
}
21+
22+
// tests the '==' overload
23+
static Value StrictEqualsOverload(const CallbackInfo& info) {
24+
bool strictlyEquals = info[0] == info[1];
25+
return Boolean::New(info.Env(), strictlyEquals);
26+
}
27+
28+
// tests the '!=' overload
29+
static Value StrictlyNotEqualsOverload(const CallbackInfo& info) {
30+
bool strictlyEquals = info[0] != info[1];
31+
return Boolean::New(info.Env(), strictlyEquals);
32+
}
33+
34+
static Value ValueReturnsCorrectEnv(const CallbackInfo& info) {
35+
Value testValue = CreateExternal(info);
36+
return Boolean::New(info.Env(), testValue.Env() == info.Env());
37+
}
38+
39+
static Value EmptyValueReturnNullPtrOnCast(const CallbackInfo& info) {
40+
Value emptyValue;
41+
bool isNullPtr = static_cast<napi_value>(emptyValue) == nullptr;
42+
return Boolean::New(info.Env(), isNullPtr);
43+
}
44+
45+
static Value NonEmptyValueReturnValOnCast(const CallbackInfo& info) {
46+
Value boolValue = Value::From(info.Env(), true);
47+
return Boolean::New(info.Env(), static_cast<napi_value>(boolValue));
48+
}
49+
50+
static Value CreateNonEmptyValue(const CallbackInfo& info) {
51+
return Napi::Value(info.Env(), String::New(info.Env(), "non_empty_val"));
52+
}
53+
1754
static Value IsEmpty(const CallbackInfo& info) {
1855
Value value;
1956
return Boolean::New(info.Env(), value.IsEmpty());
@@ -114,6 +151,20 @@ Object InitBasicTypesValue(Env env) {
114151
exports["toString"] = Function::New(env, ToString);
115152
exports["toObject"] = Function::New(env, ToObject);
116153

154+
exports["strictlyEquals"] = Function::New(env, StrictlyEquals);
155+
exports["strictlyEqualsOverload"] = Function::New(env, StrictEqualsOverload);
156+
exports["strictlyNotEqualsOverload"] =
157+
Function::New(env, StrictlyNotEqualsOverload);
158+
159+
exports["assertValueReturnsCorrectEnv"] =
160+
Function::New(env, ValueReturnsCorrectEnv);
161+
162+
exports["assertEmptyValReturnNullPtrOnCast"] =
163+
Function::New(env, EmptyValueReturnNullPtrOnCast);
164+
exports["assertNonEmptyReturnValOnCast"] =
165+
Function::New(env, NonEmptyValueReturnValOnCast);
166+
167+
exports["createNonEmptyValue"] = Function::New(env, CreateNonEmptyValue);
117168
exports["createExternal"] = Function::New(env, CreateExternal);
118169

119170
return exports;

test/basic_types/value.js

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,27 @@ const assert = require('assert');
44

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

7-
function test(binding) {
7+
function test (binding) {
88
const externalValue = binding.basic_types_value.createExternal();
99

10-
function isObject(value) {
10+
function isObject (value) {
1111
return (typeof value === 'object' && value !== externalValue) ||
1212
(typeof value === 'function');
1313
}
1414

15-
function detailedTypeOf(value) {
15+
function detailedTypeOf (value) {
1616
const type = typeof value;
17-
if (type !== 'object')
18-
return type;
17+
if (type !== 'object') { return type; }
1918

20-
if (value === null)
21-
return 'null';
19+
if (value === null) { return 'null'; }
2220

23-
if (Array.isArray(value))
24-
return 'array';
21+
if (Array.isArray(value)) { return 'array'; }
2522

26-
if (value === externalValue)
27-
return 'external';
23+
if (value === externalValue) { return 'external'; }
2824

29-
if (!value.constructor)
30-
return type;
25+
if (!value.constructor) { return type; }
3126

32-
if (value instanceof ArrayBuffer)
33-
return 'arraybuffer';
27+
if (value instanceof ArrayBuffer) { return 'arraybuffer'; }
3428

3529
if (ArrayBuffer.isView(value)) {
3630
if (value instanceof DataView) {
@@ -40,13 +34,12 @@ function test(binding) {
4034
}
4135
}
4236

43-
if (value instanceof Promise)
44-
return 'promise';
37+
if (value instanceof Promise) { return 'promise'; }
4538

4639
return 'object';
4740
}
4841

49-
function typeCheckerTest(typeChecker, expectedType) {
42+
function typeCheckerTest (typeChecker, expectedType) {
5043
const testValueList = [
5144
undefined,
5245
null,
@@ -58,7 +51,7 @@ function test(binding) {
5851
new ArrayBuffer(10),
5952
new Int32Array(new ArrayBuffer(12)),
6053
{},
61-
function() {},
54+
function () {},
6255
new Promise((resolve, reject) => {}),
6356
new DataView(new ArrayBuffer(12)),
6457
externalValue
@@ -73,7 +66,7 @@ function test(binding) {
7366
});
7467
}
7568

76-
function typeConverterTest(typeConverter, expectedType) {
69+
function typeConverterTest (typeConverter, expectedType) {
7770
const testValueList = [
7871
true,
7972
false,
@@ -84,7 +77,7 @@ function test(binding) {
8477
new ArrayBuffer(10),
8578
new Int32Array(new ArrayBuffer(12)),
8679
{},
87-
function() {},
80+
function () {},
8881
new Promise((resolve, reject) => {})
8982
];
9083

@@ -100,8 +93,38 @@ function test(binding) {
10093
});
10194
}
10295

96+
function assertValueStrictlyEqual (value) {
97+
const newValue = value.createNonEmptyValue();
98+
assert(value.strictlyEquals(newValue, newValue));
99+
assert(value.strictlyEqualsOverload(newValue, newValue));
100+
}
101+
102+
function assertValueStrictlyNonEqual (value) {
103+
const valueA = value.createNonEmptyValue();
104+
const valueB = value.createExternal();
105+
assert(value.strictlyNotEqualsOverload(valueA, valueB));
106+
}
107+
108+
function assertValueReturnsCorrectEnv (value) {
109+
assert(value.assertValueReturnsCorrectEnv());
110+
}
111+
112+
function assertEmptyValueNullPtrOnCast (value) {
113+
assert(value.assertEmptyValReturnNullPtrOnCast());
114+
}
115+
116+
function assertNonEmptyReturnValOnCast (value) {
117+
assert(value.assertNonEmptyReturnValOnCast());
118+
}
119+
103120
const value = binding.basic_types_value;
104121

122+
assertValueStrictlyEqual(value);
123+
assertValueStrictlyNonEqual(value);
124+
assertValueReturnsCorrectEnv(value);
125+
assertEmptyValueNullPtrOnCast(value);
126+
assertNonEmptyReturnValOnCast(value);
127+
105128
typeCheckerTest(value.isUndefined, 'undefined');
106129
typeCheckerTest(value.isNull, 'null');
107130
typeCheckerTest(value.isBoolean, 'boolean');

0 commit comments

Comments
 (0)