Skip to content

Commit

Permalink
test: Add tests for copy/move semantics
Browse files Browse the repository at this point in the history
PR-URL: #1295
Reviewed-By: Michael Dawson <midawson@redhat.com
  • Loading branch information
JckXia authored and mhdawson committed Mar 22, 2023
1 parent e484327 commit a8ad7e7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
34 changes: 34 additions & 0 deletions test/error.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <string.h>
#include <future>
#include "assert.h"
#include "napi.h"

using namespace Napi;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions test/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit a8ad7e7

Please sign in to comment.