diff --git a/test/reference.cc b/test/reference.cc index 9b8f81563..b83c434a4 100644 --- a/test/reference.cc +++ b/test/reference.cc @@ -1,9 +1,60 @@ +#include "assert.h" #include "napi.h" - +#include "test_helper.h" using namespace Napi; static Reference> weak; +static void RefMoveAssignTests(const Napi::CallbackInfo& info) { + Napi::Object obj = Napi::Object::New(info.Env()); + obj.Set("tPro", "tTEST"); + Napi::Reference ref = Napi::Reference::New(obj); + ref.SuppressDestruct(); + + napi_ref obj_ref = static_cast(ref); + Napi::Reference existingRef = + Napi::Reference(info.Env(), obj_ref); + assert(ref == existingRef); + assert(!(ref != existingRef)); + + std::string val = + MaybeUnwrap(existingRef.Value().Get("tPro")).As(); + assert(val == "tTEST"); + // ------------------------------------------------------------ // + Napi::Reference copyMoveRef = std::move(existingRef); + assert(copyMoveRef == ref); + + Napi::Reference 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 ref = Napi::Reference::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 ref = Napi::Reference::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(); + assert(val == "node"); +} + void CreateWeakArray(const CallbackInfo& info) { weak = Weak(Buffer::New(info.Env(), 1)); weak.SuppressDestruct(); @@ -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; } diff --git a/test/reference.js b/test/reference.js index 34fe85172..2b4c4037f 100644 --- a/test/reference.js +++ b/test/reference.js @@ -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() ]); }