Skip to content

Commit a0f9789

Browse files
committed
Added assertions to buffer tests
1 parent 9f078c8 commit a0f9789

File tree

2 files changed

+16
-115
lines changed

2 files changed

+16
-115
lines changed

packages/node-addon-examples/tests/buffers/addon.c

Lines changed: 1 addition & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -2,113 +2,7 @@
22
#include <stdio.h>
33
#include <stdlib.h>
44
#include <string.h>
5-
6-
#define NODE_API_RETVAL_NOTHING // Intentionally blank #define
7-
8-
#define GET_AND_THROW_LAST_ERROR(env) \
9-
do { \
10-
const napi_extended_error_info* error_info; \
11-
napi_get_last_error_info((env), &error_info); \
12-
bool is_pending; \
13-
const char* err_message = error_info->error_message; \
14-
napi_is_exception_pending((env), &is_pending); \
15-
/* If an exception is already pending, don't rethrow it */ \
16-
if (!is_pending) { \
17-
const char* error_message = \
18-
err_message != NULL ? err_message : "empty error message"; \
19-
napi_throw_error((env), NULL, error_message); \
20-
} \
21-
} while (0)
22-
23-
// The basic version of GET_AND_THROW_LAST_ERROR. We cannot access any
24-
// exceptions and we cannot fail by way of JS exception, so we abort.
25-
#define FATALLY_FAIL_WITH_LAST_ERROR(env) \
26-
do { \
27-
const napi_extended_error_info* error_info; \
28-
napi_get_last_error_info((env), &error_info); \
29-
const char* err_message = error_info->error_message; \
30-
const char* error_message = \
31-
err_message != NULL ? err_message : "empty error message"; \
32-
fprintf(stderr, "%s\n", error_message); \
33-
abort(); \
34-
} while (0)
35-
36-
#define NODE_API_ASSERT_BASE(env, assertion, message, ret_val) \
37-
do { \
38-
if (!(assertion)) { \
39-
napi_throw_error( \
40-
(env), NULL, "assertion (" #assertion ") failed: " message); \
41-
return ret_val; \
42-
} \
43-
} while (0)
44-
45-
#define NODE_API_BASIC_ASSERT_BASE(assertion, message, ret_val) \
46-
do { \
47-
if (!(assertion)) { \
48-
fprintf(stderr, "assertion (" #assertion ") failed: " message); \
49-
abort(); \
50-
return ret_val; \
51-
} \
52-
} while (0)
53-
54-
// Returns NULL on failed assertion.
55-
// This is meant to be used inside napi_callback methods.
56-
#define NODE_API_ASSERT(env, assertion, message) \
57-
NODE_API_ASSERT_BASE(env, assertion, message, NULL)
58-
59-
// Returns empty on failed assertion.
60-
// This is meant to be used inside functions with void return type.
61-
#define NODE_API_ASSERT_RETURN_VOID(env, assertion, message) \
62-
NODE_API_ASSERT_BASE(env, assertion, message, NODE_API_RETVAL_NOTHING)
63-
64-
#define NODE_API_BASIC_ASSERT_RETURN_VOID(assertion, message) \
65-
NODE_API_BASIC_ASSERT_BASE(assertion, message, NODE_API_RETVAL_NOTHING)
66-
67-
#define NODE_API_CALL_BASE(env, the_call, ret_val) \
68-
do { \
69-
if ((the_call) != napi_ok) { \
70-
GET_AND_THROW_LAST_ERROR((env)); \
71-
return ret_val; \
72-
} \
73-
} while (0)
74-
75-
#define NODE_API_BASIC_CALL_BASE(env, the_call, ret_val) \
76-
do { \
77-
if ((the_call) != napi_ok) { \
78-
FATALLY_FAIL_WITH_LAST_ERROR((env)); \
79-
return ret_val; \
80-
} \
81-
} while (0)
82-
83-
// Returns NULL if the_call doesn't return napi_ok.
84-
#define NODE_API_CALL(env, the_call) NODE_API_CALL_BASE(env, the_call, NULL)
85-
86-
// Returns empty if the_call doesn't return napi_ok.
87-
#define NODE_API_CALL_RETURN_VOID(env, the_call) \
88-
NODE_API_CALL_BASE(env, the_call, NODE_API_RETVAL_NOTHING)
89-
90-
#define NODE_API_BASIC_CALL_RETURN_VOID(env, the_call) \
91-
NODE_API_BASIC_CALL_BASE(env, the_call, NODE_API_RETVAL_NOTHING)
92-
93-
#define NODE_API_CHECK_STATUS(the_call) \
94-
do { \
95-
napi_status status = (the_call); \
96-
if (status != napi_ok) { \
97-
return status; \
98-
} \
99-
} while (0)
100-
101-
#define NODE_API_ASSERT_STATUS(env, assertion, message) \
102-
NODE_API_ASSERT_BASE(env, assertion, message, napi_generic_failure)
103-
104-
#define DECLARE_NODE_API_PROPERTY(name, func) \
105-
{(name), NULL, (func), NULL, NULL, NULL, napi_default, NULL}
106-
107-
#define DECLARE_NODE_API_GETTER(name, func) \
108-
{(name), NULL, NULL, (func), NULL, NULL, napi_default, NULL}
109-
110-
#define DECLARE_NODE_API_PROPERTY_VALUE(name, value) \
111-
{(name), NULL, NULL, NULL, NULL, (value), napi_default, NULL}
5+
#include "../RuntimeNodeApiTestsCommon.h"
1126

1137
static inline void add_returned_status(napi_env env,
1148
const char* key,
Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
/* eslint-disable @typescript-eslint/no-require-imports */
22
/* eslint-disable no-undef */
33
const addon = require("bindings")("addon.node");
4+
const assert = require("assert");
45

56
const toLocaleString = (text) => {
67
return text
7-
.toLocaleString()
8+
.toString()
89
.split(",")
910
.map((code) => String.fromCharCode(parseInt(code, 10)))
1011
.join("");
1112
};
1213

13-
console.log(addon.newBuffer().toLocaleString(), addon.theText);
14-
console.log(toLocaleString(addon.newExternalBuffer()), addon.theText);
15-
console.log(addon.copyBuffer(), addon.theText);
16-
let buffer = addon.staticBuffer();
17-
console.log(addon.bufferHasInstance(buffer), true);
18-
console.log(addon.bufferInfo(buffer), true);
19-
addon.invalidObjectAsBuffer({});
14+
module.exports = async () => {
15+
assert.strictEqual(toLocaleString(addon.newBuffer()), addon.theText);
16+
assert.strictEqual(toLocaleString(addon.newExternalBuffer()), addon.theText);
17+
assert.strictEqual(toLocaleString(addon.copyBuffer()), addon.theText);
18+
let buffer = addon.staticBuffer();
19+
assert.strictEqual(addon.bufferHasInstance(buffer), true);
20+
assert.strictEqual(addon.bufferInfo(buffer), true);
21+
addon.invalidObjectAsBuffer({});
22+
23+
// TODO: Add gc tests
24+
// @see
25+
// https://github.com/callstackincubator/react-native-node-api/issues/182
26+
};

0 commit comments

Comments
 (0)