◐ Shell
clean mode source ↗

src: migrate WriteOneByte to WriteOneByteV2 · nodejs/node@56ac9a2

1+

#include "gtest/gtest.h"

2+

#include "node.h"

3+

#include "node_test_fixture.h"

4+

#include "string_bytes.h"

5+

#include "util-inl.h"

6+7+

using node::MaybeStackBuffer;

8+

using node::StringBytes;

9+

using v8::HandleScope;

10+

using v8::Local;

11+

using v8::Maybe;

12+

using v8::String;

13+14+

class StringBytesTest : public EnvironmentTestFixture {};

15+16+

// Data "Hello, ÆÊÎÖÿ"

17+

static const char latin1_data[] = "Hello, \xC6\xCA\xCE\xD6\xFF";

18+

static const char utf8_data[] = "Hello, ÆÊÎÖÿ";

19+20+

TEST_F(StringBytesTest, WriteLatin1WithOneByteString) {

21+

const HandleScope handle_scope(isolate_);

22+

const Argv argv;

23+

Env env_{handle_scope, argv};

24+25+

Local<String> one_byte_str =

26+

String::NewFromOneByte(isolate_,

27+

reinterpret_cast<const uint8_t*>(latin1_data))

28+

.ToLocalChecked();

29+30+

Maybe<size_t> size_maybe =

31+

StringBytes::StorageSize(isolate_, one_byte_str, node::LATIN1);

32+33+

ASSERT_TRUE(size_maybe.IsJust());

34+

size_t size = size_maybe.FromJust();

35+

ASSERT_EQ(size, 12u);

36+37+

MaybeStackBuffer<char> buf;

38+

size_t written = StringBytes::Write(

39+

isolate_, buf.out(), buf.capacity(), one_byte_str, node::LATIN1);

40+

ASSERT_EQ(written, 12u);

41+42+

// Null-terminate the buffer and compare the contents.

43+

buf.SetLength(13);

44+

buf[12] = '\0';

45+

ASSERT_STREQ(latin1_data, buf.out());

46+

}

47+48+

TEST_F(StringBytesTest, WriteLatin1WithUtf8String) {

49+

const HandleScope handle_scope(isolate_);

50+

const Argv argv;

51+

Env env_{handle_scope, argv};

52+53+

Local<String> utf8_str =

54+

String::NewFromUtf8(isolate_, utf8_data).ToLocalChecked();

55+56+

Maybe<size_t> size_maybe =

57+

StringBytes::StorageSize(isolate_, utf8_str, node::LATIN1);

58+59+

ASSERT_TRUE(size_maybe.IsJust());

60+

size_t size = size_maybe.FromJust();

61+

ASSERT_EQ(size, 12u);

62+63+

MaybeStackBuffer<char> buf;

64+

size_t written = StringBytes::Write(

65+

isolate_, buf.out(), buf.capacity(), utf8_str, node::LATIN1);

66+

ASSERT_EQ(written, 12u);

67+68+

// Null-terminate the buffer and compare the contents.

69+

buf.SetLength(13);

70+

buf[12] = '\0';

71+

ASSERT_STREQ(latin1_data, buf.out());

72+

}

73+74+

// Verify that StringBytes::Write converts two-byte characters to one-byte

75+

// characters, even if there is no valid one-byte representation.

76+

TEST_F(StringBytesTest, WriteLatin1WithInvalidChar) {

77+

const HandleScope handle_scope(isolate_);

78+

const Argv argv;

79+

Env env_{handle_scope, argv};

80+81+

Local<String> utf8_str =

82+

String::NewFromUtf8(isolate_, "Hello, 世界").ToLocalChecked();

83+84+

Maybe<size_t> size_maybe =

85+

StringBytes::StorageSize(isolate_, utf8_str, node::LATIN1);

86+87+

ASSERT_TRUE(size_maybe.IsJust());

88+

size_t size = size_maybe.FromJust();

89+

ASSERT_EQ(size, 9u);

90+91+

MaybeStackBuffer<char> buf;

92+

size_t written = StringBytes::Write(

93+

isolate_, buf.out(), buf.capacity(), utf8_str, node::LATIN1);

94+

ASSERT_EQ(written, 9u);

95+96+

// Null-terminate the buffer and compare the contents.

97+

buf.SetLength(10);

98+

buf[9] = '\0';

99+

ASSERT_STREQ("Hello, \x16\x4C", buf.out());

100+

}