string_helpers_test.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #include "llvm/Testing/Support/Error.h"
  10. using ::llvm::FailedWithMessage;
  11. using ::llvm::HasValue;
  12. using ::testing::Eq;
  13. using ::testing::Optional;
  14. namespace Carbon {
  15. namespace {
  16. TEST(UnescapeStringLiteral, Valid) {
  17. EXPECT_THAT(UnescapeStringLiteral("test"), Optional(Eq("test")));
  18. EXPECT_THAT(UnescapeStringLiteral("okay whitespace"),
  19. Optional(Eq("okay whitespace")));
  20. EXPECT_THAT(UnescapeStringLiteral("test\n"), Optional(Eq("test\n")));
  21. EXPECT_THAT(UnescapeStringLiteral("test\\n"), Optional(Eq("test\n")));
  22. EXPECT_THAT(UnescapeStringLiteral("abc\\ndef"), Optional(Eq("abc\ndef")));
  23. EXPECT_THAT(UnescapeStringLiteral("test\\\\n"), Optional(Eq("test\\n")));
  24. EXPECT_THAT(UnescapeStringLiteral("\\xAA"), Optional(Eq("\xAA")));
  25. EXPECT_THAT(UnescapeStringLiteral("\\x12"), Optional(Eq("\x12")));
  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. }
  41. TEST(UnescapeStringLiteral, Nul) {
  42. std::optional<std::string> str = UnescapeStringLiteral("a\\0b");
  43. ASSERT_NE(str, std::nullopt);
  44. EXPECT_THAT(str->size(), Eq(3));
  45. EXPECT_THAT(strlen(str->c_str()), Eq(1));
  46. EXPECT_THAT((*str)[0], Eq('a'));
  47. EXPECT_THAT((*str)[1], Eq('\0'));
  48. EXPECT_THAT((*str)[2], Eq('b'));
  49. }
  50. TEST(ParseBlockStringLiteral, FailTooFewLines) {
  51. EXPECT_THAT_EXPECTED(ParseBlockStringLiteral(""),
  52. FailedWithMessage("Too few lines"));
  53. }
  54. TEST(ParseBlockStringLiteral, FailNoLeadingTripleQuotes) {
  55. EXPECT_THAT_EXPECTED(
  56. ParseBlockStringLiteral("'a'\n"),
  57. FailedWithMessage("Should start with triple quotes: 'a'"));
  58. }
  59. TEST(ParseBlockStringLiteral, FailInvalideFiletypeIndicator) {
  60. EXPECT_THAT_EXPECTED(
  61. ParseBlockStringLiteral("\"\"\"carbon file\n"),
  62. FailedWithMessage(
  63. "Invalid characters in file type indicator: carbon file"));
  64. }
  65. TEST(ParseBlockStringLiteral, FailEndingTripleQuotes) {
  66. EXPECT_THAT_EXPECTED(ParseBlockStringLiteral("\"\"\"\n"),
  67. FailedWithMessage("Should end with triple quotes: "));
  68. }
  69. TEST(ParseBlockStringLiteral, FailWrongIndent) {
  70. EXPECT_THAT_EXPECTED(
  71. ParseBlockStringLiteral(R"("""
  72. A block string literal
  73. with wrong indent
  74. """)"),
  75. FailedWithMessage(
  76. "Wrong indent for line: with wrong indent, expected 5"));
  77. }
  78. TEST(ParseBlockStringLiteral, FailInvalidEscaping) {
  79. EXPECT_THAT_EXPECTED(ParseBlockStringLiteral(R"("""
  80. \q
  81. """)"),
  82. FailedWithMessage("Invalid escaping in \\q"));
  83. }
  84. TEST(ParseBlockStringLiteral, OkEmptyString) {
  85. EXPECT_THAT_EXPECTED(ParseBlockStringLiteral(R"("""
  86. """)"),
  87. HasValue(""));
  88. }
  89. TEST(ParseBlockStringLiteral, OkOneLineString) {
  90. EXPECT_THAT_EXPECTED(ParseBlockStringLiteral(R"("""
  91. A block string literal
  92. """)"),
  93. HasValue(R"(A block string literal
  94. )"));
  95. }
  96. TEST(ParseBlockStringLiteral, OkTwoLineString) {
  97. EXPECT_THAT_EXPECTED(ParseBlockStringLiteral(R"("""
  98. A block string literal
  99. with indent.
  100. """)"),
  101. HasValue(R"(A block string literal
  102. with indent.
  103. )"));
  104. }
  105. TEST(ParseBlockStringLiteral, OkWithFileTypeIndicator) {
  106. EXPECT_THAT_EXPECTED(ParseBlockStringLiteral(R"("""carbon
  107. A block string literal
  108. with file type indicator.
  109. """)"),
  110. HasValue(R"(A block string literal
  111. with file type indicator.
  112. )"));
  113. }
  114. TEST(ParseBlockStringLiteral, OkWhitespaceAfterOpeningQuotes) {
  115. EXPECT_THAT_EXPECTED(ParseBlockStringLiteral(R"("""
  116. A block string literal
  117. """)"),
  118. HasValue(R"(A block string literal
  119. )"));
  120. }
  121. TEST(ParseBlockStringLiteral, OkWithEmptyLines) {
  122. EXPECT_THAT_EXPECTED(ParseBlockStringLiteral(R"("""
  123. A block string literal
  124. with
  125. empty
  126. lines.
  127. """)"),
  128. HasValue(R"(A block string literal
  129. with
  130. empty
  131. lines.
  132. )"));
  133. }
  134. TEST(ParseBlockStringLiteral, OkWithSlashNewlineEscape) {
  135. EXPECT_THAT_EXPECTED(ParseBlockStringLiteral(R"("""
  136. A block string literal\
  137. """)"),
  138. HasValue("A block string literal"));
  139. }
  140. TEST(ParseBlockStringLiteral, OkWithDoubleSlashNewline) {
  141. EXPECT_THAT_EXPECTED(ParseBlockStringLiteral(R"("""
  142. A block string literal\\
  143. """)"),
  144. HasValue(R"(A block string literal\
  145. )"));
  146. }
  147. TEST(ParseBlockStringLiteral, OkWithTripleSlashNewline) {
  148. EXPECT_THAT_EXPECTED(ParseBlockStringLiteral(R"("""
  149. A block string literal\\\
  150. """)"),
  151. HasValue(R"(A block string literal\)"));
  152. }
  153. TEST(ParseBlockStringLiteral, OkMultipleSlashes) {
  154. EXPECT_THAT_EXPECTED(ParseBlockStringLiteral(R"("""
  155. A block string literal\
  156. \
  157. \
  158. \
  159. """)"),
  160. HasValue("A block string literal"));
  161. }
  162. } // namespace
  163. } // namespace Carbon