Skip to content

Commit a8ad7e7

Browse files
JckXiamhdawson
authored andcommitted
test: Add tests for copy/move semantics
PR-URL: #1295 Reviewed-By: Michael Dawson <[email protected]
1 parent e484327 commit a8ad7e7

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

test/error.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#include <string.h>
12
#include <future>
3+
#include "assert.h"
24
#include "napi.h"
35

46
using namespace Napi;
@@ -69,6 +71,34 @@ void LastExceptionErrorCode(const CallbackInfo& info) {
6971
NAPI_THROW_VOID(Error::New(env));
7072
}
7173

74+
void TestErrorCopySemantics(const Napi::CallbackInfo& info) {
75+
Napi::Error newError = Napi::Error::New(info.Env(), "errorCopyCtor");
76+
Napi::Error existingErr;
77+
78+
#ifdef NAPI_CPP_EXCEPTIONS
79+
std::string msg = "errorCopyCtor";
80+
assert(strcmp(newError.what(), msg.c_str()) == 0);
81+
#endif
82+
83+
Napi::Error errCopyCtor = newError;
84+
assert(errCopyCtor.Message() == "errorCopyCtor");
85+
86+
existingErr = newError;
87+
assert(existingErr.Message() == "errorCopyCtor");
88+
}
89+
90+
void TestErrorMoveSemantics(const Napi::CallbackInfo& info) {
91+
std::string errorMsg = "errorMoveCtor";
92+
Napi::Error newError = Napi::Error::New(info.Env(), errorMsg.c_str());
93+
Napi::Error errFromMove = std::move(newError);
94+
assert(errFromMove.Message() == "errorMoveCtor");
95+
96+
newError = Napi::Error::New(info.Env(), "errorMoveAssign");
97+
Napi::Error existingErr = std::move(newError);
98+
99+
assert(existingErr.Message() == "errorMoveAssign");
100+
}
101+
72102
#ifdef NAPI_CPP_EXCEPTIONS
73103

74104
void ThrowJSError(const CallbackInfo& info) {
@@ -328,6 +358,10 @@ void ThrowDefaultError(const CallbackInfo& info) {
328358
Object InitError(Env env) {
329359
Object exports = Object::New(env);
330360
exports["throwApiError"] = Function::New(env, ThrowApiError);
361+
exports["testErrorCopySemantics"] =
362+
Function::New(env, TestErrorCopySemantics);
363+
exports["testErrorMoveSemantics"] =
364+
Function::New(env, TestErrorMoveSemantics);
331365
exports["lastExceptionErrorCode"] =
332366
Function::New(env, LastExceptionErrorCode);
333367
exports["throwJSError"] = Function::New(env, ThrowJSError);

test/error.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module.exports = require('./common').runTestWithBindingPath(test);
1111

1212
function test (bindingPath) {
1313
const binding = require(bindingPath);
14+
binding.error.testErrorCopySemantics();
15+
binding.error.testErrorMoveSemantics();
1416

1517
assert.throws(() => binding.error.throwApiError('test'), function (err) {
1618
return err instanceof Error && err.message.includes('Invalid');

0 commit comments

Comments
 (0)