tokenized_buffer_test_helpers.h 5.4 KB

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