string_helpers_test.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "common/string_helpers.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include <string>
  8. using ::testing::Eq;
  9. using ::testing::Optional;
  10. namespace Carbon {
  11. namespace {
  12. TEST(UnescapeStringLiteral, Valid) {
  13. EXPECT_THAT(UnescapeStringLiteral("test"), Optional(Eq("test")));
  14. EXPECT_THAT(UnescapeStringLiteral("okay whitespace"),
  15. Optional(Eq("okay whitespace")));
  16. EXPECT_THAT(UnescapeStringLiteral("test\n"), Optional(Eq("test\n")));
  17. EXPECT_THAT(UnescapeStringLiteral("test\\n"), Optional(Eq("test\n")));
  18. EXPECT_THAT(UnescapeStringLiteral("abc\\ndef"), Optional(Eq("abc\ndef")));
  19. EXPECT_THAT(UnescapeStringLiteral("test\\\\n"), Optional(Eq("test\\n")));
  20. EXPECT_THAT(UnescapeStringLiteral("\\xAA"), Optional(Eq("\xAA")));
  21. EXPECT_THAT(UnescapeStringLiteral("\\x12"), Optional(Eq("\x12")));
  22. EXPECT_THAT(UnescapeStringLiteral("test", 1), Optional(Eq("test")));
  23. EXPECT_THAT(UnescapeStringLiteral("test\\#n", 1), Optional(Eq("test\n")));
  24. EXPECT_THAT(UnescapeStringLiteral(
  25. "r\\u{000000E9}al \\u{2764}\\u{FE0F}\\u{1F50A}!\\u{10FFFF}"),
  26. Optional(Eq("réal ❤️🔊!􏿿")));
  27. }
  28. TEST(UnescapeStringLiteral, Invalid) {
  29. // Missing char after `\`.
  30. EXPECT_THAT(UnescapeStringLiteral("a\\"), Eq(std::nullopt));
  31. // Not a supported escape.
  32. EXPECT_THAT(UnescapeStringLiteral("\\e"), Eq(std::nullopt));
  33. // Needs 2 hex chars.
  34. EXPECT_THAT(UnescapeStringLiteral("\\x"), Eq(std::nullopt));
  35. // Needs 2 hex chars.
  36. EXPECT_THAT(UnescapeStringLiteral("\\xA"), Eq(std::nullopt));
  37. // Needs uppercase hex.
  38. EXPECT_THAT(UnescapeStringLiteral("\\xaa"), Eq(std::nullopt));
  39. // Reserved.
  40. EXPECT_THAT(UnescapeStringLiteral("\\00"), Eq(std::nullopt));
  41. EXPECT_THAT(UnescapeStringLiteral("\\#00", 1), Eq(std::nullopt));
  42. }
  43. TEST(UnescapeStringLiteral, InvalidUnicodes) {
  44. // Various incomplete Unicode specifiers
  45. EXPECT_THAT(UnescapeStringLiteral("\\u"), Eq(std::nullopt));
  46. EXPECT_THAT(UnescapeStringLiteral("\\u1"), Eq(std::nullopt));
  47. EXPECT_THAT(UnescapeStringLiteral("\\uz"), Eq(std::nullopt));
  48. EXPECT_THAT(UnescapeStringLiteral("\\u{"), Eq(std::nullopt));
  49. EXPECT_THAT(UnescapeStringLiteral("\\u{z"), Eq(std::nullopt));
  50. EXPECT_THAT(UnescapeStringLiteral("\\u{E9"), Eq(std::nullopt));
  51. EXPECT_THAT(UnescapeStringLiteral("\\u{E9z"), Eq(std::nullopt));
  52. EXPECT_THAT(UnescapeStringLiteral("\\u{}"), Eq(std::nullopt));
  53. // invalid characters in unicode
  54. EXPECT_THAT(UnescapeStringLiteral("\\u{z}"), Eq(std::nullopt));
  55. // lowercase hexadecimal
  56. EXPECT_THAT(UnescapeStringLiteral("\\u{e9}"), Eq(std::nullopt));
  57. // Codepoint number too high
  58. EXPECT_THAT(UnescapeStringLiteral("\\u{110000}"), Eq(std::nullopt));
  59. // codepoint more than 8 hex digits
  60. EXPECT_THAT(UnescapeStringLiteral("\\u{FF000000E9}"), Eq(std::nullopt));
  61. }
  62. TEST(UnescapeStringLiteral, Nul) {
  63. std::optional<std::string> str = UnescapeStringLiteral("a\\0b");
  64. ASSERT_NE(str, std::nullopt);
  65. EXPECT_THAT(str->size(), Eq(3));
  66. EXPECT_THAT(strlen(str->c_str()), Eq(1));
  67. EXPECT_THAT((*str)[0], Eq('a'));
  68. EXPECT_THAT((*str)[1], Eq('\0'));
  69. EXPECT_THAT((*str)[2], Eq('b'));
  70. }
  71. TEST(ParseBlockStringLiteral, FailTooFewLines) {
  72. EXPECT_THAT(ParseBlockStringLiteral("").error().message(),
  73. Eq("Too few lines"));
  74. }
  75. TEST(ParseBlockStringLiteral, FailNoLeadingTripleQuotes) {
  76. EXPECT_THAT(ParseBlockStringLiteral("'a'\n").error().message(),
  77. Eq("Should start with triple quotes: 'a'"));
  78. }
  79. TEST(ParseBlockStringLiteral, FailInvalideFiletypeIndicator) {
  80. EXPECT_THAT(ParseBlockStringLiteral("'''carbon file\n").error().message(),
  81. Eq("Invalid characters in file type indicator: carbon file"));
  82. }
  83. TEST(ParseBlockStringLiteral, FailEndingTripleQuotes) {
  84. EXPECT_THAT(ParseBlockStringLiteral("'''\n").error().message(),
  85. Eq("Should end with triple quotes: "));
  86. }
  87. TEST(ParseBlockStringLiteral, FailWrongIndent) {
  88. constexpr char Input[] = R"('''
  89. A block string literal
  90. with wrong indent
  91. ''')";
  92. EXPECT_THAT(ParseBlockStringLiteral(Input).error().message(),
  93. Eq("Wrong indent for line: with wrong indent, expected 5"));
  94. }
  95. TEST(ParseBlockStringLiteral, FailInvalidEscaping) {
  96. constexpr char Input[] = R"('''
  97. \q
  98. ''')";
  99. EXPECT_THAT(ParseBlockStringLiteral(Input).error().message(),
  100. Eq("Invalid escaping in \\q"));
  101. constexpr char InputRaw[] = R"('''
  102. \#q
  103. ''')";
  104. EXPECT_THAT(ParseBlockStringLiteral(InputRaw, 1).error().message(),
  105. Eq("Invalid escaping in \\#q"));
  106. }
  107. TEST(ParseBlockStringLiteral, OkEmptyString) {
  108. constexpr char Input[] = R"('''
  109. ''')";
  110. EXPECT_THAT(*ParseBlockStringLiteral(Input), Eq(""));
  111. }
  112. TEST(ParseBlockStringLiteral, OkOneLineString) {
  113. constexpr char Input[] = R"('''
  114. A block string literal
  115. ''')";
  116. constexpr char Expected[] = R"(A block string literal
  117. )";
  118. EXPECT_THAT(*ParseBlockStringLiteral(Input), Eq(Expected));
  119. }
  120. TEST(ParseBlockStringLiteral, OkTwoLineString) {
  121. constexpr char Input[] = R"('''
  122. A block string literal
  123. with indent.
  124. ''')";
  125. constexpr char Expected[] = R"(A block string literal
  126. with indent.
  127. )";
  128. EXPECT_THAT(*ParseBlockStringLiteral(Input), Eq(Expected));
  129. }
  130. TEST(ParseBlockStringLiteral, OkWithFileTypeIndicator) {
  131. constexpr char Input[] = R"('''carbon
  132. A block string literal
  133. with file type indicator.
  134. ''')";
  135. constexpr char Expected[] = R"(A block string literal
  136. with file type indicator.
  137. )";
  138. EXPECT_THAT(*ParseBlockStringLiteral(Input), Eq(Expected));
  139. }
  140. TEST(ParseBlockStringLiteral, OkWhitespaceAfterOpeningQuotes) {
  141. constexpr char Input[] = R"('''
  142. A block string literal
  143. ''')";
  144. constexpr char Expected[] = R"(A block string literal
  145. )";
  146. EXPECT_THAT(*ParseBlockStringLiteral(Input), Eq(Expected));
  147. }
  148. TEST(ParseBlockStringLiteral, OkWithEmptyLines) {
  149. constexpr char Input[] = R"('''
  150. A block string literal
  151. with
  152. empty
  153. lines.
  154. ''')";
  155. constexpr char Expected[] = R"(A block string literal
  156. with
  157. empty
  158. lines.
  159. )";
  160. EXPECT_THAT(*ParseBlockStringLiteral(Input), Eq(Expected));
  161. }
  162. TEST(ParseBlockStringLiteral, OkWithSlashNewlineEscape) {
  163. constexpr char Input[] = R"('''
  164. A block string literal\
  165. ''')";
  166. constexpr char Expected[] = "A block string literal";
  167. EXPECT_THAT(*ParseBlockStringLiteral(Input), Eq(Expected));
  168. }
  169. TEST(ParseBlockStringLiteral, OkWithDoubleSlashNewline) {
  170. constexpr char Input[] = R"('''
  171. A block string literal\\
  172. ''')";
  173. constexpr char Expected[] = R"(A block string literal\
  174. )";
  175. EXPECT_THAT(*ParseBlockStringLiteral(Input), Eq(Expected));
  176. }
  177. TEST(ParseBlockStringLiteral, OkWithTripleSlashNewline) {
  178. constexpr char Input[] = R"('''
  179. A block string literal\\\
  180. ''')";
  181. constexpr char Expected[] = R"(A block string literal\)";
  182. EXPECT_THAT(*ParseBlockStringLiteral(Input), Eq(Expected));
  183. }
  184. TEST(ParseBlockStringLiteral, OkMultipleSlashes) {
  185. constexpr char Input[] = R"('''
  186. A block string literal\
  187. \
  188. \
  189. \
  190. ''')";
  191. constexpr char Expected[] = "A block string literal";
  192. EXPECT_THAT(*ParseBlockStringLiteral(Input), Eq(Expected));
  193. }
  194. } // namespace
  195. } // namespace Carbon