tokenized_buffer_test_helpers.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 "llvm/ADT/SmallString.h"
  8. #include "llvm/Support/Casting.h"
  9. #include "llvm/Support/YAMLParser.h"
  10. #include "toolchain/lexer/tokenized_buffer.h"
  11. namespace Carbon {
  12. inline void PrintTo(const TokenizedBuffer& buffer, std::ostream* output) {
  13. std::string message;
  14. llvm::raw_string_ostream message_stream(message);
  15. message_stream << "\n";
  16. buffer.Print(message_stream);
  17. *output << message_stream.str();
  18. }
  19. namespace Testing {
  20. struct ExpectedToken {
  21. friend auto operator<<(std::ostream& output, const ExpectedToken& expected)
  22. -> std::ostream& {
  23. output << "\ntoken: { kind: '" << expected.kind.Name().str() << "'";
  24. if (expected.line != -1) {
  25. output << ", line: " << expected.line;
  26. }
  27. if (expected.column != -1) {
  28. output << ", column " << expected.column;
  29. }
  30. if (expected.indent_column != -1) {
  31. output << ", indent: " << expected.indent_column;
  32. }
  33. if (!expected.text.empty()) {
  34. output << ", spelling: '" << expected.text.str() << "'";
  35. }
  36. if (expected.string_contents) {
  37. output << ", string contents: '" << expected.string_contents->str()
  38. << "'";
  39. }
  40. if (expected.recovery) {
  41. output << ", recovery: true";
  42. }
  43. output << " }";
  44. return output;
  45. }
  46. TokenKind kind;
  47. int line = -1;
  48. int column = -1;
  49. int indent_column = -1;
  50. bool recovery = false;
  51. llvm::StringRef text = "";
  52. llvm::Optional<llvm::StringRef> string_contents = llvm::None;
  53. };
  54. // TODO: Consider rewriting this into a `TokenEq` matcher which is used inside
  55. // `ElementsAre`. If that isn't easily done, potentially worth checking for size
  56. // mismatches first.
  57. // NOLINTNEXTLINE: Expands from GoogleTest.
  58. MATCHER_P(HasTokens, raw_all_expected, "") {
  59. const TokenizedBuffer& buffer = arg;
  60. llvm::ArrayRef<ExpectedToken> all_expected = raw_all_expected;
  61. bool matches = true;
  62. auto buffer_it = buffer.Tokens().begin();
  63. for (const ExpectedToken& expected : all_expected) {
  64. if (buffer_it == buffer.Tokens().end()) {
  65. // The size check outside the loop will fail and print useful info.
  66. break;
  67. }
  68. int index = buffer_it - buffer.Tokens().begin();
  69. auto token = *buffer_it++;
  70. TokenKind actual_kind = buffer.GetKind(token);
  71. if (actual_kind != expected.kind) {
  72. *result_listener << "\nToken " << index << " is a "
  73. << actual_kind.Name().str() << ", expected a "
  74. << expected.kind.Name().str() << ".";
  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. assert(!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 // TOOLCHAIN_LEXER_TOKENIZED_BUFFER_TEST_HELPERS_H_