tokenized_buffer_test_helpers.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #ifndef CARBON_TOOLCHAIN_LEXER_TOKENIZED_BUFFER_TEST_HELPERS_H_
  5. #define CARBON_TOOLCHAIN_LEXER_TOKENIZED_BUFFER_TEST_HELPERS_H_
  6. #include <gmock/gmock.h>
  7. #include "common/check.h"
  8. #include "llvm/ADT/SmallString.h"
  9. #include "llvm/Support/Casting.h"
  10. #include "llvm/Support/YAMLParser.h"
  11. #include "toolchain/lexer/tokenized_buffer.h"
  12. namespace Carbon {
  13. inline void PrintTo(const TokenizedBuffer& buffer, std::ostream* output) {
  14. std::string message;
  15. llvm::raw_string_ostream message_stream(message);
  16. message_stream << "\n";
  17. buffer.Print(message_stream);
  18. *output << message_stream.str();
  19. }
  20. namespace Testing {
  21. struct ExpectedToken {
  22. friend auto operator<<(std::ostream& output, const ExpectedToken& expected)
  23. -> std::ostream& {
  24. output << "\ntoken: { kind: '" << expected.kind << "'";
  25. if (expected.line != -1) {
  26. output << ", line: " << expected.line;
  27. }
  28. if (expected.column != -1) {
  29. output << ", column " << expected.column;
  30. }
  31. if (expected.indent_column != -1) {
  32. output << ", indent: " << expected.indent_column;
  33. }
  34. if (!expected.text.empty()) {
  35. output << ", spelling: '" << expected.text.str() << "'";
  36. }
  37. if (expected.string_contents) {
  38. output << ", string contents: '" << expected.string_contents->str()
  39. << "'";
  40. }
  41. if (expected.recovery) {
  42. output << ", recovery: true";
  43. }
  44. output << " }";
  45. return output;
  46. }
  47. TokenKind kind;
  48. int line = -1;
  49. int column = -1;
  50. int indent_column = -1;
  51. bool recovery = false;
  52. llvm::StringRef text = "";
  53. std::optional<llvm::StringRef> string_contents = std::nullopt;
  54. };
  55. // TODO: Consider rewriting this into a `TokenEq` matcher which is used inside
  56. // `ElementsAre`. If that isn't easily done, potentially worth checking for size
  57. // mismatches first.
  58. // NOLINTNEXTLINE: Expands from GoogleTest.
  59. MATCHER_P(HasTokens, raw_all_expected, "") {
  60. const TokenizedBuffer& buffer = arg;
  61. llvm::ArrayRef<ExpectedToken> all_expected = raw_all_expected;
  62. bool matches = true;
  63. auto buffer_it = buffer.tokens().begin();
  64. for (const ExpectedToken& expected : all_expected) {
  65. if (buffer_it == buffer.tokens().end()) {
  66. // The size check outside the loop will fail and print useful info.
  67. break;
  68. }
  69. int index = buffer_it - buffer.tokens().begin();
  70. auto token = *buffer_it++;
  71. TokenKind actual_kind = buffer.GetKind(token);
  72. if (actual_kind != expected.kind) {
  73. *result_listener << "\nToken " << index << " is a " << actual_kind
  74. << ", expected a " << expected.kind << ".";
  75. matches = false;
  76. }
  77. int actual_line = buffer.GetLineNumber(token);
  78. if (expected.line != -1 && actual_line != expected.line) {
  79. *result_listener << "\nToken " << index << " is at line " << actual_line
  80. << ", expected " << expected.line << ".";
  81. matches = false;
  82. }
  83. int actual_column = buffer.GetColumnNumber(token);
  84. if (expected.column != -1 && actual_column != expected.column) {
  85. *result_listener << "\nToken " << index << " is at column "
  86. << actual_column << ", expected " << expected.column
  87. << ".";
  88. matches = false;
  89. }
  90. int actual_indent_column =
  91. buffer.GetIndentColumnNumber(buffer.GetLine(token));
  92. if (expected.indent_column != -1 &&
  93. actual_indent_column != expected.indent_column) {
  94. *result_listener << "\nToken " << index << " has column indent "
  95. << actual_indent_column << ", expected "
  96. << expected.indent_column << ".";
  97. matches = false;
  98. }
  99. int actual_recovery = buffer.IsRecoveryToken(token);
  100. if (expected.recovery != actual_recovery) {
  101. *result_listener << "\nToken " << index << " is "
  102. << (actual_recovery ? "recovery" : "non-recovery")
  103. << ", expected "
  104. << (expected.recovery ? "recovery" : "non-recovery")
  105. << ".";
  106. matches = false;
  107. }
  108. llvm::StringRef actual_text = buffer.GetTokenText(token);
  109. if (!expected.text.empty() && actual_text != expected.text) {
  110. *result_listener << "\nToken " << index << " has spelling `"
  111. << actual_text.str() << "`, expected `"
  112. << expected.text.str() << "`.";
  113. matches = false;
  114. }
  115. CARBON_CHECK(!expected.string_contents ||
  116. expected.kind == TokenKind::StringLiteral);
  117. if (expected.string_contents && actual_kind == TokenKind::StringLiteral) {
  118. llvm::StringRef actual_contents = buffer.GetStringLiteral(token);
  119. if (actual_contents != *expected.string_contents) {
  120. *result_listener << "\nToken " << index << " has contents `"
  121. << actual_contents.str() << "`, expected `"
  122. << expected.string_contents->str() << "`.";
  123. matches = false;
  124. }
  125. }
  126. }
  127. int actual_size = buffer.tokens().end() - buffer.tokens().begin();
  128. if (static_cast<int>(all_expected.size()) != actual_size) {
  129. *result_listener << "\nExpected " << all_expected.size()
  130. << " tokens but found " << actual_size << ".";
  131. matches = false;
  132. }
  133. return matches;
  134. }
  135. } // namespace Testing
  136. } // namespace Carbon
  137. #endif // CARBON_TOOLCHAIN_LEXER_TOKENIZED_BUFFER_TEST_HELPERS_H_