From 16cee5086cbc0a1b657e60bec3af0d9a968ade3c Mon Sep 17 00:00:00 2001 From: JckXia Date: Thu, 9 Mar 2023 14:24:03 -0500 Subject: [PATCH 1/3] test: Add tests for copy/move semantics --- test/error.cc | 27 +++++++++++++++++++++++++++ test/error.js | 2 ++ 2 files changed, 29 insertions(+) diff --git a/test/error.cc b/test/error.cc index f6a43984a..627484f61 100644 --- a/test/error.cc +++ b/test/error.cc @@ -1,4 +1,5 @@ #include +#include "assert.h" #include "napi.h" using namespace Napi; @@ -69,6 +70,28 @@ void LastExceptionErrorCode(const CallbackInfo& info) { NAPI_THROW_VOID(Error::New(env)); } +void TestErrorCopySemantics(const Napi::CallbackInfo& info) { + Napi::Error newError = Napi::Error::New(info.Env(), "errorCopyCtor"); + Napi::Error existingErr; + + Napi::Error errCopyCtor = newError; + assert(errCopyCtor.Message() == "errorCopyCtor"); + + existingErr = newError; + assert(existingErr.Message() == "errorCopyCtor"); +} + +void TestErrorMoveSemantics(const Napi::CallbackInfo& info) { + Napi::Error newError = Napi::Error::New(info.Env(), "errorMoveCtor"); + Napi::Error errFromMove = std::move(newError); + assert(errFromMove.Message() == "errorMoveCtor"); + + newError = Napi::Error::New(info.Env(), "errorMoveAssign"); + Napi::Error existingErr = std::move(newError); + + assert(existingErr.Message() == "errorMoveAssign"); +} + #ifdef NAPI_CPP_EXCEPTIONS void ThrowJSError(const CallbackInfo& info) { @@ -266,6 +289,10 @@ void ThrowDefaultError(const CallbackInfo& info) { Object InitError(Env env) { Object exports = Object::New(env); exports["throwApiError"] = Function::New(env, ThrowApiError); + exports["testErrorCopySemantics"] = + Function::New(env, TestErrorCopySemantics); + exports["testErrorMoveSemantics"] = + Function::New(env, TestErrorMoveSemantics); exports["lastExceptionErrorCode"] = Function::New(env, LastExceptionErrorCode); exports["throwJSError"] = Function::New(env, ThrowJSError); diff --git a/test/error.js b/test/error.js index d1519ec8e..d9acd9c02 100644 --- a/test/error.js +++ b/test/error.js @@ -11,6 +11,8 @@ module.exports = require('./common').runTestWithBindingPath(test); function test (bindingPath) { const binding = require(bindingPath); + binding.error.testErrorCopySemantics(); + binding.error.testErrorMoveSemantics(); assert.throws(() => binding.error.throwApiError('test'), function (err) { return err instanceof Error && err.message.includes('Invalid'); From 0d7e7eb24138a1e880b0995f6f68825e929d9f49 Mon Sep 17 00:00:00 2001 From: JckXia Date: Thu, 9 Mar 2023 14:34:29 -0500 Subject: [PATCH 2/3] Use c string overload --- test/error.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/error.cc b/test/error.cc index 627484f61..293832cc9 100644 --- a/test/error.cc +++ b/test/error.cc @@ -82,7 +82,8 @@ void TestErrorCopySemantics(const Napi::CallbackInfo& info) { } void TestErrorMoveSemantics(const Napi::CallbackInfo& info) { - Napi::Error newError = Napi::Error::New(info.Env(), "errorMoveCtor"); + std::string errorMsg = "errorMoveCtor"; + Napi::Error newError = Napi::Error::New(info.Env(), errorMsg.c_str()); Napi::Error errFromMove = std::move(newError); assert(errFromMove.Message() == "errorMoveCtor"); From baef52eb01ece0945aafe0b30bc107e6623d7d48 Mon Sep 17 00:00:00 2001 From: JckXia Date: Thu, 9 Mar 2023 14:50:01 -0500 Subject: [PATCH 3/3] test: Add a small test for what() --- test/error.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/error.cc b/test/error.cc index 293832cc9..4a942333f 100644 --- a/test/error.cc +++ b/test/error.cc @@ -1,3 +1,4 @@ +#include #include #include "assert.h" #include "napi.h" @@ -74,6 +75,11 @@ void TestErrorCopySemantics(const Napi::CallbackInfo& info) { Napi::Error newError = Napi::Error::New(info.Env(), "errorCopyCtor"); Napi::Error existingErr; +#ifdef NAPI_CPP_EXCEPTIONS + std::string msg = "errorCopyCtor"; + assert(strcmp(newError.what(), msg.c_str()) == 0); +#endif + Napi::Error errCopyCtor = newError; assert(errCopyCtor.Message() == "errorCopyCtor");