Skip to content

Commit

Permalink
test: Complete test coverage for Reference<T> class (#1277)
Browse files Browse the repository at this point in the history
* test: Add test coverage for reference<T>

* test: Remove comments

* Add suppress desturct
  • Loading branch information
JckXia authored Mar 23, 2023
1 parent a8ad7e7 commit d4942cc
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
56 changes: 55 additions & 1 deletion test/reference.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,60 @@
#include "assert.h"
#include "napi.h"

#include "test_helper.h"
using namespace Napi;

static Reference<Buffer<uint8_t>> weak;

static void RefMoveAssignTests(const Napi::CallbackInfo& info) {
Napi::Object obj = Napi::Object::New(info.Env());
obj.Set("tPro", "tTEST");
Napi::Reference<Napi::Object> ref = Napi::Reference<Napi::Object>::New(obj);
ref.SuppressDestruct();

napi_ref obj_ref = static_cast<napi_ref>(ref);
Napi::Reference<Napi::Object> existingRef =
Napi::Reference<Napi::Object>(info.Env(), obj_ref);
assert(ref == existingRef);
assert(!(ref != existingRef));

std::string val =
MaybeUnwrap(existingRef.Value().Get("tPro")).As<Napi::String>();
assert(val == "tTEST");
// ------------------------------------------------------------ //
Napi::Reference<Napi::Object> copyMoveRef = std::move(existingRef);
assert(copyMoveRef == ref);

Napi::Reference<Napi::Object> copyAssignRef;
copyAssignRef = std::move(copyMoveRef);
assert(copyAssignRef == ref);
}

static void ReferenceRefTests(const Napi::CallbackInfo& info) {
Napi::Object obj = Napi::Object::New(info.Env());
Napi::Reference<Napi::Object> ref = Napi::Reference<Napi::Object>::New(obj);

assert(ref.Ref() == 1);
assert(ref.Unref() == 0);
}

static void ReferenceResetTests(const Napi::CallbackInfo& info) {
Napi::Object obj = Napi::Object::New(info.Env());
Napi::Reference<Napi::Object> ref = Napi::Reference<Napi::Object>::New(obj);
assert(!ref.IsEmpty());

ref.Reset();
assert(ref.IsEmpty());

Napi::Object newObject = Napi::Object::New(info.Env());
newObject.Set("n-api", "node");

ref.Reset(newObject, 1);
assert(!ref.IsEmpty());

std::string val = MaybeUnwrap(ref.Value().Get("n-api")).As<Napi::String>();
assert(val == "node");
}

void CreateWeakArray(const CallbackInfo& info) {
weak = Weak(Buffer<uint8_t>::New(info.Env(), 1));
weak.SuppressDestruct();
Expand All @@ -20,5 +71,8 @@ Object InitReference(Env env) {
exports["createWeakArray"] = Function::New(env, CreateWeakArray);
exports["accessWeakArrayEmpty"] = Function::New(env, AccessWeakArrayEmpty);

exports["refMoveAssignTest"] = Function::New(env, RefMoveAssignTests);
exports["referenceRefTest"] = Function::New(env, ReferenceRefTests);
exports["refResetTest"] = Function::New(env, ReferenceResetTests);
return exports;
}
8 changes: 7 additions & 1 deletion test/reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ function test (binding) {
return testUtil.runGCTests([
'test reference',
() => binding.reference.createWeakArray(),
() => assert.strictEqual(true, binding.reference.accessWeakArrayEmpty())
() => assert.strictEqual(true, binding.reference.accessWeakArrayEmpty()),
'test reference move op',
() => binding.reference.refMoveAssignTest(),
'test reference ref',
() => binding.reference.referenceRefTest(),
'test reference reset',
() => binding.reference.refResetTest()
]);
}

0 comments on commit d4942cc

Please sign in to comment.