tokenized_buffer_test_helpers.h 5.3 KB

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