From a8ad7e7a7b5d4279347521a6cb192e15822d51a6 Mon Sep 17 00:00:00 2001 From: JckXia Date: Thu, 9 Mar 2023 14:24:03 -0500 Subject: [PATCH] test: Add tests for copy/move semantics PR-URL: https://github.com/nodejs/node-addon-api/pull/1295 Reviewed-By: Michael Dawson #include +#include "assert.h" #include "napi.h" using namespace Napi; @@ -69,6 +71,34 @@ 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; + +#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"); + + existingErr = newError; + assert(existingErr.Message() == "errorCopyCtor"); +} + +void TestErrorMoveSemantics(const Napi::CallbackInfo& info) { + 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"); + + 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) { @@ -328,6 +358,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 4d4a72b56..ae3496af5 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');