string_helpers_test.cpp 5.8 KB

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