Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: null string #54

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ add_test("async" "./async/binding.c" OFF ON "")
add_test("tsfn2" "./tsfn2/binding.c" OFF ON "")

if((NOT IS_WASM) OR IS_EMSCRIPTEN OR IS_WASI_THREADS)
add_test("string_mt" "./string/binding.c" ON ON "")
add_test("string_mt" "./string/binding.c;./string/test_null.c" ON ON "")
if(IS_EMSCRIPTEN)
add_test("pool" "./pool/binding.c" OFF ON "--pre-js=../pool/pre.js")
else()
Expand All @@ -271,7 +271,7 @@ add_test("objfac" "./objfac/binding.c" ON OFF "")
add_test("fnfac" "./fnfac/binding.c" ON OFF "")
add_test("general" "./general/binding.c" ON OFF "")
add_test("filename" "./filename/binding.c" ON OFF "")
add_test("string" "./string/binding.c" ON OFF "")
add_test("string" "./string/binding.c;./string/test_null.c" ON OFF "")
add_test("property" "./property/binding.c" ON OFF "")
add_test("promise" "./promise/binding.c" ON OFF "")
add_test("object" "./object/test_null.c;./object/test_object.c" ON OFF "")
Expand Down
3 changes: 3 additions & 0 deletions packages/test/string/binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// #include <stdlib.h>
#include <js_native_api.h>
#include "../common.h"
#include "test_null.h"

void* memset(void* dst, int c, size_t n);
void* malloc(size_t size);
Expand Down Expand Up @@ -304,6 +305,8 @@ napi_value Init(napi_env env, napi_value exports) {
DECLARE_NAPI_PROPERTY("TestUtf8Large", TestUtf8Large),
};

init_test_null(env, exports);

NAPI_CALL(env, napi_define_properties(
env, exports, sizeof(properties) / sizeof(*properties), properties));

Expand Down
16 changes: 16 additions & 0 deletions packages/test/string/string-null.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'
const { load } = require('../util')
const assert = require('assert')

module.exports = load('string').then(({ testNull }) => {
const expectedResult = {
envIsNull: 'Invalid argument',
stringIsNullNonZeroLength: 'Invalid argument',
stringIsNullZeroLength: 'napi_ok',
resultIsNull: 'Invalid argument'
}

assert.deepStrictEqual(expectedResult, testNull.test_create_latin1())
assert.deepStrictEqual(expectedResult, testNull.test_create_utf8())
assert.deepStrictEqual(expectedResult, testNull.test_create_utf16())
})
71 changes: 71 additions & 0 deletions packages/test/string/test_null.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <js_native_api.h>

#include "../common.h"
#include "test_null.h"

#define DECLARE_TEST(charset, str_arg) \
static napi_value \
test_create_##charset(napi_env env, napi_callback_info info) { \
napi_value return_value, result; \
NAPI_CALL(env, napi_create_object(env, &return_value)); \
\
add_returned_status(env, \
"envIsNull", \
return_value, \
"Invalid argument", \
napi_invalid_arg, \
napi_create_string_##charset(NULL, \
(str_arg), \
NAPI_AUTO_LENGTH, \
&result)); \
\
napi_create_string_##charset(env, NULL, NAPI_AUTO_LENGTH, &result); \
add_last_status(env, "stringIsNullNonZeroLength", return_value); \
\
napi_create_string_##charset(env, NULL, 0, &result); \
add_last_status(env, "stringIsNullZeroLength", return_value); \
\
napi_create_string_##charset(env, (str_arg), NAPI_AUTO_LENGTH, NULL); \
add_last_status(env, "resultIsNull", return_value); \
\
return return_value; \
}

static const char16_t something[] = {
(char16_t)'s',
(char16_t)'o',
(char16_t)'m',
(char16_t)'e',
(char16_t)'t',
(char16_t)'h',
(char16_t)'i',
(char16_t)'n',
(char16_t)'g',
(char16_t)'\0'
};

DECLARE_TEST(utf8, "something")
DECLARE_TEST(latin1, "something")
DECLARE_TEST(utf16, something)

void init_test_null(napi_env env, napi_value exports) {
napi_value test_null;

const napi_property_descriptor test_null_props[] = {
DECLARE_NAPI_PROPERTY("test_create_utf8", test_create_utf8),
DECLARE_NAPI_PROPERTY("test_create_latin1", test_create_latin1),
DECLARE_NAPI_PROPERTY("test_create_utf16", test_create_utf16),
};

NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &test_null));
NAPI_CALL_RETURN_VOID(env, napi_define_properties(
env, test_null, sizeof(test_null_props) / sizeof(*test_null_props),
test_null_props));

const napi_property_descriptor test_null_set = {
"testNull", NULL, NULL, NULL, NULL, test_null, napi_enumerable, NULL
};

NAPI_CALL_RETURN_VOID(env,
napi_define_properties(env, exports, 1, &test_null_set));
}
8 changes: 8 additions & 0 deletions packages/test/string/test_null.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef TEST_JS_NATIVE_API_TEST_STRING_TEST_NULL_H_
#define TEST_JS_NATIVE_API_TEST_STRING_TEST_NULL_H_

#include <js_native_api.h>

void init_test_null(napi_env env, napi_value exports);

#endif // TEST_JS_NATIVE_API_TEST_STRING_TEST_NULL_H_