Restructure lex keyword tests to reduce duplication. (#3941)

Rather than having a file listing all keywords, do it programmatically.
Conversely, move the limited keyword tests out to a file test.
This commit is contained in:
Jon Ross-Perkins
2024-05-08 16:45:57 +00:00
committed by GitHub
parent 20c0322bbe
commit ec300ef3ad
3 changed files with 51 additions and 151 deletions
+14 -26
View File
@@ -586,32 +586,20 @@ TEST_F(LexerTest, Whitespace) {
}
TEST_F(LexerTest, Keywords) {
auto buffer = Lex(" fn");
EXPECT_FALSE(buffer.has_errors());
EXPECT_THAT(buffer,
HasTokens(llvm::ArrayRef<ExpectedToken>{
{TokenKind::FileStart},
{.kind = TokenKind::Fn, .column = 4, .indent_column = 4},
{TokenKind::FileEnd},
}));
buffer = Lex("and or not if else for return var break continue _");
EXPECT_FALSE(buffer.has_errors());
EXPECT_THAT(buffer, HasTokens(llvm::ArrayRef<ExpectedToken>{
{TokenKind::FileStart},
{TokenKind::And},
{TokenKind::Or},
{TokenKind::Not},
{TokenKind::If},
{TokenKind::Else},
{TokenKind::For},
{TokenKind::Return},
{TokenKind::Var},
{TokenKind::Break},
{TokenKind::Continue},
{TokenKind::Underscore},
{TokenKind::FileEnd},
}));
TokenKind keywords[] = {
#define CARBON_TOKEN(TokenName)
#define CARBON_KEYWORD_TOKEN(TokenName, ...) TokenKind::TokenName,
#include "toolchain/lex/token_kind.def"
};
for (const auto& keyword : keywords) {
auto buffer = Lex(keyword.fixed_spelling());
EXPECT_FALSE(buffer.has_errors());
EXPECT_THAT(buffer, HasTokens(llvm::ArrayRef<ExpectedToken>{
{TokenKind::FileStart},
{.kind = keyword, .column = 1, .indent_column = 1},
{TokenKind::FileEnd},
}));
}
}
TEST_F(LexerTest, Comments) {