tokenized_buffer_test_helpers.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 TOOLCHAIN_LEXER_TOKENIZED_BUFFER_TEST_HELPERS_H_
  5. #define 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.Name().str() << "'";
  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. llvm::Optional<llvm::StringRef> string_contents = llvm::None;
  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 "
  74. << actual_kind.Name().str() << ", expected a "
  75. << expected.kind.Name().str() << ".";
  76. matches = false;
  77. }
  78. int actual_line = buffer.GetLineNumber(token);
  79. if (expected.line != -1 && actual_line != expected.line) {
  80. *result_listener << "\nToken " << index << " is at line " << actual_line
  81. << ", expected " << expected.line << ".";
  82. matches = false;
  83. }
  84. int actual_column = buffer.GetColumnNumber(token);
  85. if (expected.column != -1 && actual_column != expected.column) {
  86. *result_listener << "\nToken " << index << " is at column "
  87. << actual_column << ", expected " << expected.column
  88. << ".";
  89. matches = false;
  90. }
  91. int actual_indent_column =
  92. buffer.GetIndentColumnNumber(buffer.GetLine(token));
  93. if (expected.indent_column != -1 &&
  94. actual_indent_column != expected.indent_column) {
  95. *result_listener << "\nToken " << index << " has column indent "
  96. << actual_indent_column << ", expected "
  97. << expected.indent_column << ".";
  98. matches = false;
  99. }
  100. int actual_recovery = buffer.IsRecoveryToken(token);
  101. if (expected.recovery != actual_recovery) {
  102. *result_listener << "\nToken " << index << " is "
  103. << (actual_recovery ? "recovery" : "non-recovery")
  104. << ", expected "
  105. << (expected.recovery ? "recovery" : "non-recovery")
  106. << ".";
  107. matches = false;
  108. }
  109. llvm::StringRef actual_text = buffer.GetTokenText(token);
  110. if (!expected.text.empty() && actual_text != expected.text) {
  111. *result_listener << "\nToken " << index << " has spelling `"
  112. << actual_text.str() << "`, expected `"
  113. << expected.text.str() << "`.";
  114. matches = false;
  115. }
  116. CARBON_CHECK(!expected.string_contents ||
  117. expected.kind == TokenKind::StringLiteral());
  118. if (expected.string_contents && actual_kind == TokenKind::StringLiteral()) {
  119. llvm::StringRef actual_contents = buffer.GetStringLiteral(token);
  120. if (actual_contents != *expected.string_contents) {
  121. *result_listener << "\nToken " << index << " has contents `"
  122. << actual_contents.str() << "`, expected `"
  123. << expected.string_contents->str() << "`.";
  124. matches = false;
  125. }
  126. }
  127. }
  128. int actual_size = buffer.tokens().end() - buffer.tokens().begin();
  129. if (static_cast<int>(all_expected.size()) != actual_size) {
  130. *result_listener << "\nExpected " << all_expected.size()
  131. << " tokens but found " << actual_size << ".";
  132. matches = false;
  133. }
  134. return matches;
  135. }
  136. } // namespace Testing
  137. } // namespace Carbon
  138. #endif // TOOLCHAIN_LEXER_TOKENIZED_BUFFER_TEST_HELPERS_H_