llvm-project/clang/unittests/Format/FormatTokenSourceTest.cpp
Manuel Klimek 04ed86ff1b [clang-format][NFC] Bring FormatTokenSource under test.
Add tests for FormatTokenSource and harden it against edge cases.
2023-01-31 16:06:46 +00:00

75 lines
2.9 KiB
C++

//===- unittest/Format/FormatTokenSourceTest.cpp --------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "../../lib/Format/FormatTokenSource.h"
#include "TestLexer.h"
#include "clang/Basic/TokenKinds.h"
#include "gtest/gtest.h"
namespace clang {
namespace format {
namespace {
class IndexedTokenSourceTest : public ::testing::Test {
protected:
TokenList lex(llvm::StringRef Code,
const FormatStyle &Style = getLLVMStyle()) {
return TestLexer(Allocator, Buffers, Style).lex(Code);
}
llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
std::vector<std::unique_ptr<llvm::MemoryBuffer>> Buffers;
};
#define EXPECT_TOKEN_KIND(FormatTok, Kind) \
do { \
FormatToken *Tok = FormatTok; \
EXPECT_EQ((Tok)->Tok.getKind(), Kind) << *(Tok); \
} while (false);
TEST_F(IndexedTokenSourceTest, EmptyInput) {
IndexedTokenSource Source(lex(""));
EXPECT_FALSE(Source.isEOF());
EXPECT_TOKEN_KIND(Source.getNextToken(), tok::eof);
EXPECT_TRUE(Source.isEOF());
EXPECT_TOKEN_KIND(Source.getNextToken(), tok::eof);
EXPECT_TRUE(Source.isEOF());
EXPECT_TOKEN_KIND(Source.peekNextToken(/*SkipComment=*/false), tok::eof);
EXPECT_TOKEN_KIND(Source.peekNextToken(/*SkipComment=*/true), tok::eof);
EXPECT_EQ(Source.getPreviousToken(), nullptr);
EXPECT_TRUE(Source.isEOF());
}
TEST_F(IndexedTokenSourceTest, NavigateTokenStream) {
IndexedTokenSource Source(lex("int a;"));
EXPECT_TOKEN_KIND(Source.peekNextToken(), tok::kw_int);
EXPECT_TOKEN_KIND(Source.getNextToken(), tok::kw_int);
EXPECT_EQ(Source.getPreviousToken(), nullptr);
EXPECT_TOKEN_KIND(Source.peekNextToken(), tok::identifier);
EXPECT_TOKEN_KIND(Source.getNextToken(), tok::identifier);
EXPECT_TOKEN_KIND(Source.getPreviousToken(), tok::kw_int);
EXPECT_TOKEN_KIND(Source.peekNextToken(), tok::semi);
EXPECT_TOKEN_KIND(Source.getNextToken(), tok::semi);
EXPECT_TOKEN_KIND(Source.getPreviousToken(), tok::identifier);
EXPECT_TOKEN_KIND(Source.peekNextToken(), tok::eof);
EXPECT_TOKEN_KIND(Source.getNextToken(), tok::eof);
EXPECT_TOKEN_KIND(Source.getPreviousToken(), tok::semi);
}
TEST_F(IndexedTokenSourceTest, ResetPosition) {
IndexedTokenSource Source(lex("int a;"));
Source.getNextToken();
unsigned Position = Source.getPosition();
Source.getNextToken();
Source.getNextToken();
EXPECT_TOKEN_KIND(Source.getNextToken(), tok::eof);
EXPECT_TOKEN_KIND(Source.setPosition(Position), tok::kw_int);
}
} // namespace
} // namespace format
} // namespace clang