Skip to content

Commit 1e1d0e2

Browse files
committed
Add String/Symbol::New with string view unit tests
1 parent 5b7448e commit 1e1d0e2

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

test/name.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#include "napi.h"
22

3+
#if __cplusplus >= 201703L
4+
#include <string_view>
5+
#endif
6+
37
using namespace Napi;
48

59
const char* testValueUtf8 = "123456789";
@@ -43,6 +47,12 @@ Value CreateString(const CallbackInfo& info) {
4347
}
4448
}
4549

50+
#if __cplusplus >= 201703L
51+
Value CreateStringFromStringView(const CallbackInfo& info) {
52+
return String::New(info.Env(), std::string_view("hello1"));
53+
}
54+
#endif
55+
4656
Value CheckString(const CallbackInfo& info) {
4757
String value = info[0].As<String>();
4858
String encoding = info[1].As<String>();
@@ -80,6 +90,12 @@ Value CreateSymbol(const CallbackInfo& info) {
8090
}
8191
}
8292

93+
#if __cplusplus >= 201703L
94+
Value CreateSymbolFromStringView(const CallbackInfo& info) {
95+
return Symbol::New(info.Env(), std::string_view("hello2"));
96+
}
97+
#endif
98+
8399
Value CheckSymbol(const CallbackInfo& info) {
84100
return Boolean::New(info.Env(), info[0].Type() == napi_symbol);
85101
}
@@ -99,11 +115,13 @@ Object InitName(Env env) {
99115

100116
exports["echoString"] = Function::New(env, EchoString);
101117
exports["createString"] = Function::New(env, CreateString);
118+
exports["createStringFromStringView"] = Function::New(env, CreateStringFromStringView);
102119
exports["nullStringShouldThrow"] = Function::New(env, NullStringShouldThrow);
103120
exports["nullString16ShouldThrow"] =
104121
Function::New(env, NullString16ShouldThrow);
105122
exports["checkString"] = Function::New(env, CheckString);
106123
exports["createSymbol"] = Function::New(env, CreateSymbol);
124+
exports["CreateSymbolFromStringView"] = Function::New(env, CreateSymbolFromStringView);
107125
exports["checkSymbol"] = Function::New(env, CheckSymbol);
108126

109127
return exports;

test/name.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,7 @@ function test (binding) {
5656
assert.strictEqual(binding.name.echoString(str, 'utf8'), str);
5757
assert.strictEqual(binding.name.echoString(str, 'utf16'), str);
5858
}
59+
60+
assert.strictEqual(binding.name.createStringFromStringView(), "hello1")
61+
assert.strictEqual(binding.name.createSymbolFromStringView(), "hello2")
5962
}

0 commit comments

Comments
 (0)