Skip to content

Commit

Permalink
n-api: wrap test macros in do/while
Browse files Browse the repository at this point in the history
PR-URL: nodejs#14095
Reviewed-By: Jason Ginchereau <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Daniel Bevenius <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
kfarnung authored and Gabriel Schulhof committed Apr 10, 2018
1 parent 99fc87e commit 1536f37
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
26 changes: 15 additions & 11 deletions test/addons-napi/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
"empty error message"; \
napi_throw_error((env), error_message); \
} \
} while(0);
} while (0)

#define NAPI_ASSERT_BASE(env, assertion, message, ret_val) \
if (!(assertion)) { \
napi_throw_error( \
(env), \
"assertion (" #assertion ") failed: " message); \
return ret_val; \
}
do { \
if (!(assertion)) { \
napi_throw_error( \
(env), \
"assertion (" #assertion ") failed: " message); \
return ret_val; \
} \
} while (0)

// Returns NULL on failed assertion.
// This is meant to be used inside napi_callback methods.
Expand All @@ -35,10 +37,12 @@
NAPI_ASSERT_BASE(env, assertion, message, NAPI_RETVAL_NOTHING)

#define NAPI_CALL_BASE(env, the_call, ret_val) \
if ((the_call) != napi_ok) { \
GET_AND_THROW_LAST_ERROR((env)); \
return ret_val; \
}
do { \
if ((the_call) != napi_ok) { \
GET_AND_THROW_LAST_ERROR((env)); \
return ret_val; \
} \
} while (0)

// Returns NULL if the_call doesn't return napi_ok.
#define NAPI_CALL(env, the_call) \
Expand Down
2 changes: 1 addition & 1 deletion test/addons-napi/test_object/test_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ napi_value Set(napi_env env, napi_callback_info info) {
NAPI_CALL(env, napi_set_property(env, args[0], args[1], args[2]));

napi_value valuetrue;
NAPI_CALL(env, napi_get_boolean(env, true, &valuetrue))
NAPI_CALL(env, napi_get_boolean(env, true, &valuetrue));

return valuetrue;
}
Expand Down
4 changes: 2 additions & 2 deletions test/addons-napi/test_reference/test_reference.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ napi_value CheckExternal(napi_env env, napi_callback_info info) {
napi_valuetype argtype;
NAPI_CALL(env, napi_typeof(env, arg, &argtype));

NAPI_ASSERT(env, argtype == napi_external, "Expected an external value.")
NAPI_ASSERT(env, argtype == napi_external, "Expected an external value.");

void* data;
NAPI_CALL(env, napi_get_value_external(env, arg, &data));

NAPI_ASSERT(env, data != NULL && *(int*)data == test_value,
"An external data value of 1 was expected.")
"An external data value of 1 was expected.");

return NULL;
}
Expand Down

0 comments on commit 1536f37

Please sign in to comment.