Skip to content

Commit

Permalink
Fix unicode character usage in windows tests
Browse files Browse the repository at this point in the history
Summary:
X-link: facebook/react-native#48840

MSVC unicode handling is causing our windows test to fail. To fix,
simply replace the special characters with the their explicit UTF-16
encoding.

Changelog: [Internal]

Reviewed By: neildhar

Differential Revision: D68466808

fbshipit-source-id: 1bfc689972e1e5862fe6525bc48d5ebc508a95da
  • Loading branch information
tsaichien authored and facebook-github-bot committed Jan 22, 2025
1 parent 778ac28 commit e7b1b32
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 28 deletions.
20 changes: 13 additions & 7 deletions API/jsi/jsi/test/testlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1601,14 +1601,19 @@ TEST_P(JSITest, UTF16Test) {
rd.utf8Str = "foobar";
EXPECT_EQ(str.utf16(rd), u"foobar");

rd.utf8Str = "你好";
EXPECT_EQ(str.utf16(rd), u"你好");
// 你 in UTF-8 encoding is 0xe4 0xbd 0xa0 and 好 is 0xe5 0xa5 0xbd
// 你 in UTF-16 encoding is 0x4f60 and 好 is 0x597d
rd.utf8Str = "\xe4\xbd\xa0\xe5\xa5\xbd";
EXPECT_EQ(str.utf16(rd), u"\x4f60\x597d");

rd.utf8Str = "👍";
EXPECT_EQ(str.utf16(rd), u"👍");
// 👍 in UTF-8 encoding is 0xf0 0x9f 0x91 0x8d
// 👍 in UTF-16 encoding is 0xd83d 0xdc4d
rd.utf8Str = "\xf0\x9f\x91\x8d";
EXPECT_EQ(str.utf16(rd), u"\xd83d\xdc4d");

rd.utf8Str = "foobar👍你好";
EXPECT_EQ(str.utf16(rd), u"foobar👍你好");
// String is foobar👍你好
rd.utf8Str = "foobar\xf0\x9f\x91\x8d\xe4\xbd\xa0\xe5\xa5\xbd";
EXPECT_EQ(str.utf16(rd), u"foobar\xd83d\xdc4d\x4f60\x597d");

// String ended before second byte of the encoding
rd.utf8Str = "\xcf";
Expand Down Expand Up @@ -1652,7 +1657,8 @@ TEST_P(JSITest, GetStringDataTest) {
};

RD rd = RD(rt);
String str = String::createFromUtf8(rd, "hello👋");
// 👋 in UTF8 encoding is 0xf0 0x9f 0x91 0x8b
String str = String::createFromUtf8(rd, "hello\xf0\x9f\x91\x8b");

std::u16string buf;
auto cb = [&buf](bool ascii, const void* data, size_t num) {
Expand Down
30 changes: 20 additions & 10 deletions unittests/API/APITest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1174,14 +1174,20 @@ TEST_P(HermesRuntimeTest, UTF16Test) {
String foobar = String::createFromUtf8(*rt, "foobar");
EXPECT_EQ(foobar.utf16(*rt), u"foobar");

String chineseHello = String::createFromUtf8(*rt, "你好");
EXPECT_EQ(chineseHello.utf16(*rt), u"你好");
// 你 in UTF-8 encoding is 0xe4 0xbd 0xa0 and 好 is 0xe5 0xa5 0xbd
// 你 in UTF-16 encoding is 0x4f60 and 好 is 0x597d
String chineseHello = String::createFromUtf8(*rt, "\xe4\xbd\xa0\xe5\xa5\xbd");
EXPECT_EQ(chineseHello.utf16(*rt), u"\x4f60\x597d");

String thumbsUpEmoji = String::createFromUtf8(*rt, "👍");
EXPECT_EQ(thumbsUpEmoji.utf16(*rt), u"👍");
// 👍 in UTF-8 encoding is 0xf0 0x9f 0x91 0x8d
// 👍 in UTF-16 encoding is 0xd83d 0xdc4d
String thumbsUpEmoji = String::createFromUtf8(*rt, "\xf0\x9f\x91\x8d");
EXPECT_EQ(thumbsUpEmoji.utf16(*rt), u"\xd83d\xdc4d");

String combined = String::createFromUtf8(*rt, "foobar👍你好");
EXPECT_EQ(combined.utf16(*rt), u"foobar👍你好");
// String is foobar👍你好
String combined = String::createFromUtf8(
*rt, "foobar\xf0\x9f\x91\x8d\xe4\xbd\xa0\xe5\xa5\xbd");
EXPECT_EQ(combined.utf16(*rt), u"foobar\xd83d\xdc4d\x4f60\x597d");

// We've only added specific implementations for HermesRuntime. The ABI
// runtime will convert to UTF8 first, causing the lone surrogates to be
Expand Down Expand Up @@ -1230,9 +1236,11 @@ TEST_P(HermesRuntimeTest, GetStringDataTest) {
EXPECT_EQ(buf, u"fbar");
buf.clear();

String utf16Str = String::createFromUtf8(*rt, "👍foobar你好");
// String is foobar👍你好
String utf16Str = String::createFromUtf8(
*rt, "foobar\xf0\x9f\x91\x8d\xe4\xbd\xa0\xe5\xa5\xbd");
utf16Str.getStringData(*rt, cb);
EXPECT_EQ(buf, u"👍fbar你好");
EXPECT_EQ(buf, u"fbar\xd83d\xdc4d\x4f60\x597d");
buf.clear();
}

Expand Down Expand Up @@ -1268,9 +1276,11 @@ TEST_P(HermesRuntimeTest, GetPropNameIdDataTest) {
EXPECT_EQ(buf, u"fbar");
buf.clear();

PropNameID utf16 = PropNameID::forUtf8(*rt, "👍foobar你好");
// String is foobar👍你好
PropNameID utf16 = PropNameID::forUtf8(
*rt, "foobar\xf0\x9f\x91\x8d\xe4\xbd\xa0\xe5\xa5\xbd");
utf16.getPropNameIdData(*rt, cb);
EXPECT_EQ(buf, u"👍fbar你好");
EXPECT_EQ(buf, u"fbar\xd83d\xdc4d\x4f60\x597d");
buf.clear();
}

Expand Down
4 changes: 2 additions & 2 deletions unittests/API/SynthTraceParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ TEST_F(SynthTraceParserTest, ParseUtf16Record) {

auto record2 =
dynamic_cast<const SynthTrace::Utf16Record &>(*trace.records().at(2));
ASSERT_EQ(record2.retVal_, u"nice👍");
ASSERT_EQ(record2.retVal_, u"nice\xd83d\xdc4d");
}

TEST_F(SynthTraceParserTest, ParseGetStringDataRecord) {
Expand Down Expand Up @@ -357,7 +357,7 @@ TEST_F(SynthTraceParserTest, ParseGetStringDataRecord) {

auto record0 = dynamic_cast<const SynthTrace::GetStringDataRecord &>(
*trace.records().at(0));
ASSERT_EQ(record0.strData_, u"\nhello👋\\");
ASSERT_EQ(record0.strData_, u"\nhello\xd83d\xdc4b\\");
ASSERT_EQ(record0.objID_, SynthTrace::encodeString(1110));

auto record1 = dynamic_cast<const SynthTrace::GetStringDataRecord &>(
Expand Down
4 changes: 2 additions & 2 deletions unittests/API/SynthTraceSerializationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ TEST_F(SynthTraceSerializationTest, Utf16Record) {
EXPECT_EQ(
serialized,
to_string(SynthTrace::Utf16Record(
dummyTime, SynthTrace::encodeString(123), u"hi👋")));
dummyTime, SynthTrace::encodeString(123), u"hi\xd83d\xdc4b")));
serialized =
R"({"type":"Utf16Record","time":0,"objID":"string:111","retval":"\ud83d"})";
EXPECT_EQ(
Expand All @@ -416,7 +416,7 @@ TEST_F(SynthTraceSerializationTest, GetStringDataRecord) {
EXPECT_EQ(
serialized,
to_string(SynthTrace::GetStringDataRecord(
dummyTime, SynthTrace::encodeString(123), u"\nhello👋\\")));
dummyTime, SynthTrace::encodeString(123), u"\nhello\xd83d\xdc4b\\")));
serialized =
R"({"type":"GetStringDataRecord","time":0,"objID":"propNameID:111","strData":"\ud83d"})";
EXPECT_EQ(
Expand Down
21 changes: 15 additions & 6 deletions unittests/API/SynthTraceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ TEST_F(SynthTraceTest, PropNameIDUtf8) {
}

TEST_F(SynthTraceTest, PropNameIDUtf16) {
const std::string utf8 = "hello👍\n";
// 👍 in UTF8 encoding is 0xf0 0x9f 0x91 0x8d
const std::string utf8 = "hello\xf0\x9f\x91\x8d\n";
const jsi::PropNameID name = jsi::PropNameID::forUtf8(*rt, utf8);
name.utf16(*rt);

Expand All @@ -121,7 +122,9 @@ TEST_F(SynthTraceTest, PropNameIDUtf16) {
*records[0]);
EXPECT_EQ_RECORD(
SynthTrace::Utf16Record(
records[1]->time_, SynthTrace::encodePropNameID(objId), u"hello👍\n"),
records[1]->time_,
SynthTrace::encodePropNameID(objId),
u"hello\xd83d\xdc4d\n"),
*records[1]);
}

Expand All @@ -145,7 +148,8 @@ TEST_F(SynthTraceTest, StringUtf8) {
}

TEST_F(SynthTraceTest, StringUtf16) {
const std::string utf8 = "hello👍\n";
// 👍 in UTF8 encoding is 0xf0 0x9f 0x91 0x8d
const std::string utf8 = "hello\xf0\x9f\x91\x8d\n";

const jsi::String str = jsi::String::createFromUtf8(*rt, utf8);
str.utf16(*rt);
Expand All @@ -159,13 +163,16 @@ TEST_F(SynthTraceTest, StringUtf16) {
*records[0]);
EXPECT_EQ_RECORD(
SynthTrace::Utf16Record(
records[1]->time_, SynthTrace::encodeString(objId), u"hello👍\n"),
records[1]->time_,
SynthTrace::encodeString(objId),
u"hello\xd83d\xdc4d\n"),
*records[1]);
}

TEST_F(SynthTraceTest, GetStringData) {
const std::string ascii = "foo";
const std::string emoji = "hello👋";
// 👋 in UTF8 encoding is 0xf0 0x9f 0x91 0x8b
const std::string emoji = "hello\xf0\x9f\x91\x8b";

auto cb = [](bool ascii, const void *data, size_t num) {};

Expand Down Expand Up @@ -197,7 +204,9 @@ TEST_F(SynthTraceTest, GetStringData) {
*records[2]);
EXPECT_EQ_RECORD(
SynthTrace::GetStringDataRecord(
records[3]->time_, SynthTrace::encodeString(emojiId), u"hello👋"),
records[3]->time_,
SynthTrace::encodeString(emojiId),
u"hello\xd83d\xdc4b"),
*records[3]);
}

Expand Down
2 changes: 1 addition & 1 deletion unittests/Support/JSONEmitterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ TEST(JSONEmitterTest, EmitUTF16) {
llvh::raw_string_ostream OS(storage);
JSONEmitter json(OS);

std::u16string str = u"hi👋";
std::u16string str = u"hi\xd83d\xdc4b";
json.openDict();
json.emitKeyValue("str", llvh::ArrayRef(str.data(), str.size()));
json.closeDict();
Expand Down

0 comments on commit e7b1b32

Please sign in to comment.