tokenized_buffer_test_helpers.h 5.1 KB

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