string_helpers_test.cpp 7.3 KB

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