string_helpers_test.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. }
  27. TEST(UnescapeStringLiteral, Invalid) {
  28. // Missing char after `\`.
  29. EXPECT_THAT(UnescapeStringLiteral("a\\"), Eq(std::nullopt));
  30. // Not a supported escape.
  31. EXPECT_THAT(UnescapeStringLiteral("\\e"), Eq(std::nullopt));
  32. // Needs 2 hex chars.
  33. EXPECT_THAT(UnescapeStringLiteral("\\x"), Eq(std::nullopt));
  34. // Needs 2 hex chars.
  35. EXPECT_THAT(UnescapeStringLiteral("\\xA"), Eq(std::nullopt));
  36. // Needs uppercase hex.
  37. EXPECT_THAT(UnescapeStringLiteral("\\xaa"), Eq(std::nullopt));
  38. // Reserved.
  39. EXPECT_THAT(UnescapeStringLiteral("\\00"), Eq(std::nullopt));
  40. EXPECT_THAT(UnescapeStringLiteral("\\#00", 1), Eq(std::nullopt));
  41. }
  42. TEST(UnescapeStringLiteral, Nul) {
  43. std::optional<std::string> str = UnescapeStringLiteral("a\\0b");
  44. ASSERT_NE(str, std::nullopt);
  45. EXPECT_THAT(str->size(), Eq(3));
  46. EXPECT_THAT(strlen(str->c_str()), Eq(1));
  47. EXPECT_THAT((*str)[0], Eq('a'));
  48. EXPECT_THAT((*str)[1], Eq('\0'));
  49. EXPECT_THAT((*str)[2], Eq('b'));
  50. }
  51. TEST(ParseBlockStringLiteral, FailTooFewLines) {
  52. EXPECT_THAT(ParseBlockStringLiteral("").error().message(),
  53. Eq("Too few lines"));
  54. }
  55. TEST(ParseBlockStringLiteral, FailNoLeadingTripleQuotes) {
  56. EXPECT_THAT(ParseBlockStringLiteral("'a'\n").error().message(),
  57. Eq("Should start with triple quotes: 'a'"));
  58. }
  59. TEST(ParseBlockStringLiteral, FailInvalideFiletypeIndicator) {
  60. EXPECT_THAT(ParseBlockStringLiteral("\"\"\"carbon file\n").error().message(),
  61. Eq("Invalid characters in file type indicator: carbon file"));
  62. }
  63. TEST(ParseBlockStringLiteral, FailEndingTripleQuotes) {
  64. EXPECT_THAT(ParseBlockStringLiteral("\"\"\"\n").error().message(),
  65. Eq("Should end with triple quotes: "));
  66. }
  67. TEST(ParseBlockStringLiteral, FailWrongIndent) {
  68. constexpr char Input[] = R"("""
  69. A block string literal
  70. with wrong indent
  71. """)";
  72. EXPECT_THAT(ParseBlockStringLiteral(Input).error().message(),
  73. Eq("Wrong indent for line: with wrong indent, expected 5"));
  74. }
  75. TEST(ParseBlockStringLiteral, FailInvalidEscaping) {
  76. constexpr char Input[] = R"("""
  77. \q
  78. """)";
  79. EXPECT_THAT(ParseBlockStringLiteral(Input).error().message(),
  80. Eq("Invalid escaping in \\q"));
  81. constexpr char InputRaw[] = R"("""
  82. \#q
  83. """)";
  84. EXPECT_THAT(ParseBlockStringLiteral(InputRaw, 1).error().message(),
  85. Eq("Invalid escaping in \\#q"));
  86. }
  87. TEST(ParseBlockStringLiteral, OkEmptyString) {
  88. constexpr char Input[] = R"("""
  89. """)";
  90. EXPECT_THAT(*ParseBlockStringLiteral(Input), Eq(""));
  91. }
  92. TEST(ParseBlockStringLiteral, OkOneLineString) {
  93. constexpr char Input[] = R"("""
  94. A block string literal
  95. """)";
  96. constexpr char Expected[] = R"(A block string literal
  97. )";
  98. EXPECT_THAT(*ParseBlockStringLiteral(Input), Eq(Expected));
  99. }
  100. TEST(ParseBlockStringLiteral, OkTwoLineString) {
  101. constexpr char Input[] = R"("""
  102. A block string literal
  103. with indent.
  104. """)";
  105. constexpr char Expected[] = R"(A block string literal
  106. with indent.
  107. )";
  108. EXPECT_THAT(*ParseBlockStringLiteral(Input), Eq(Expected));
  109. }
  110. TEST(ParseBlockStringLiteral, OkWithFileTypeIndicator) {
  111. constexpr char Input[] = R"("""carbon
  112. A block string literal
  113. with file type indicator.
  114. """)";
  115. constexpr char Expected[] = R"(A block string literal
  116. with file type indicator.
  117. )";
  118. EXPECT_THAT(*ParseBlockStringLiteral(Input), Eq(Expected));
  119. }
  120. TEST(ParseBlockStringLiteral, OkWhitespaceAfterOpeningQuotes) {
  121. constexpr char Input[] = R"("""
  122. A block string literal
  123. """)";
  124. constexpr char Expected[] = R"(A block string literal
  125. )";
  126. EXPECT_THAT(*ParseBlockStringLiteral(Input), Eq(Expected));
  127. }
  128. TEST(ParseBlockStringLiteral, OkWithEmptyLines) {
  129. constexpr char Input[] = R"("""
  130. A block string literal
  131. with
  132. empty
  133. lines.
  134. """)";
  135. constexpr char Expected[] = R"(A block string literal
  136. with
  137. empty
  138. lines.
  139. )";
  140. EXPECT_THAT(*ParseBlockStringLiteral(Input), Eq(Expected));
  141. }
  142. TEST(ParseBlockStringLiteral, OkWithSlashNewlineEscape) {
  143. constexpr char Input[] = R"("""
  144. A block string literal\
  145. """)";
  146. constexpr char Expected[] = "A block string literal";
  147. EXPECT_THAT(*ParseBlockStringLiteral(Input), Eq(Expected));
  148. }
  149. TEST(ParseBlockStringLiteral, OkWithDoubleSlashNewline) {
  150. constexpr char Input[] = R"("""
  151. A block string literal\\
  152. """)";
  153. constexpr char Expected[] = R"(A block string literal\
  154. )";
  155. EXPECT_THAT(*ParseBlockStringLiteral(Input), Eq(Expected));
  156. }
  157. TEST(ParseBlockStringLiteral, OkWithTripleSlashNewline) {
  158. constexpr char Input[] = R"("""
  159. A block string literal\\\
  160. """)";
  161. constexpr char Expected[] = R"(A block string literal\)";
  162. EXPECT_THAT(*ParseBlockStringLiteral(Input), Eq(Expected));
  163. }
  164. TEST(ParseBlockStringLiteral, OkMultipleSlashes) {
  165. constexpr char Input[] = R"("""
  166. A block string literal\
  167. \
  168. \
  169. \
  170. """)";
  171. constexpr char Expected[] = "A block string literal";
  172. EXPECT_THAT(*ParseBlockStringLiteral(Input), Eq(Expected));
  173. }
  174. } // namespace
  175. } // namespace Carbon::Testing