mirror of
https://github.com/nodejs/node.git
synced 2024-11-30 23:43:09 +01:00
80441c8086
Previously only simple escape sequences were handled (i.e. \n, \t, r etc.). This commit adds escaping of other control symbols in the range of 0x00 to 0x20. Also, this replaces multiple find+replace calls with a single pass replacer. PR-URL: https://github.com/nodejs/node/pull/25626 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Richard Lau <riclau@uk.ibm.com>
27 lines
1015 B
C++
27 lines
1015 B
C++
#include "node_report.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
TEST(ReportUtilTest, EscapeJsonChars) {
|
|
using report::EscapeJsonChars;
|
|
EXPECT_EQ("abc", EscapeJsonChars("abc"));
|
|
EXPECT_EQ("abc\\n", EscapeJsonChars("abc\n"));
|
|
EXPECT_EQ("abc\\nabc", EscapeJsonChars("abc\nabc"));
|
|
EXPECT_EQ("abc\\\\", EscapeJsonChars("abc\\"));
|
|
EXPECT_EQ("abc\\\"", EscapeJsonChars("abc\""));
|
|
|
|
const std::string expected[0x20] = {
|
|
"\\u0000", "\\u0001", "\\u0002", "\\u0003", "\\u0004", "\\u0005",
|
|
"\\u0006", "\\u0007", "\\b", "\\t", "\\n", "\\v", "\\f", "\\r",
|
|
"\\u000e", "\\u000f", "\\u0010", "\\u0011", "\\u0012", "\\u0013",
|
|
"\\u0014", "\\u0015", "\\u0016", "\\u0017", "\\u0018", "\\u0019",
|
|
"\\u001a", "\\u001b", "\\u001c", "\\u001d", "\\u001e", "\\u001f"
|
|
};
|
|
for (int i = 0; i < 0x20; ++i) {
|
|
char symbols[1] = { static_cast<char>(i) };
|
|
std::string input(symbols, 1);
|
|
EXPECT_EQ(expected[i], EscapeJsonChars(input));
|
|
EXPECT_EQ("a" + expected[i], EscapeJsonChars("a" + input));
|
|
}
|
|
}
|