xref: /llvm-project/clang/unittests/Format/FormatTokenSourceTest.cpp (revision 04ed86ff1b7272faf8e62fa32da4acaa2d3c6add)
1 //===- unittest/Format/FormatTokenSourceTest.cpp --------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "../../lib/Format/FormatTokenSource.h"
10 #include "TestLexer.h"
11 #include "clang/Basic/TokenKinds.h"
12 #include "gtest/gtest.h"
13 
14 namespace clang {
15 namespace format {
16 namespace {
17 
18 class IndexedTokenSourceTest : public ::testing::Test {
19 protected:
20   TokenList lex(llvm::StringRef Code,
21                 const FormatStyle &Style = getLLVMStyle()) {
22     return TestLexer(Allocator, Buffers, Style).lex(Code);
23   }
24   llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
25   std::vector<std::unique_ptr<llvm::MemoryBuffer>> Buffers;
26 };
27 
28 #define EXPECT_TOKEN_KIND(FormatTok, Kind)                                     \
29   do {                                                                         \
30     FormatToken *Tok = FormatTok;                                              \
31     EXPECT_EQ((Tok)->Tok.getKind(), Kind) << *(Tok);                           \
32   } while (false);
33 
34 TEST_F(IndexedTokenSourceTest, EmptyInput) {
35   IndexedTokenSource Source(lex(""));
36   EXPECT_FALSE(Source.isEOF());
37   EXPECT_TOKEN_KIND(Source.getNextToken(), tok::eof);
38   EXPECT_TRUE(Source.isEOF());
39   EXPECT_TOKEN_KIND(Source.getNextToken(), tok::eof);
40   EXPECT_TRUE(Source.isEOF());
41   EXPECT_TOKEN_KIND(Source.peekNextToken(/*SkipComment=*/false), tok::eof);
42   EXPECT_TOKEN_KIND(Source.peekNextToken(/*SkipComment=*/true), tok::eof);
43   EXPECT_EQ(Source.getPreviousToken(), nullptr);
44   EXPECT_TRUE(Source.isEOF());
45 }
46 
47 TEST_F(IndexedTokenSourceTest, NavigateTokenStream) {
48   IndexedTokenSource Source(lex("int a;"));
49   EXPECT_TOKEN_KIND(Source.peekNextToken(), tok::kw_int);
50   EXPECT_TOKEN_KIND(Source.getNextToken(), tok::kw_int);
51   EXPECT_EQ(Source.getPreviousToken(), nullptr);
52   EXPECT_TOKEN_KIND(Source.peekNextToken(), tok::identifier);
53   EXPECT_TOKEN_KIND(Source.getNextToken(), tok::identifier);
54   EXPECT_TOKEN_KIND(Source.getPreviousToken(), tok::kw_int);
55   EXPECT_TOKEN_KIND(Source.peekNextToken(), tok::semi);
56   EXPECT_TOKEN_KIND(Source.getNextToken(), tok::semi);
57   EXPECT_TOKEN_KIND(Source.getPreviousToken(), tok::identifier);
58   EXPECT_TOKEN_KIND(Source.peekNextToken(), tok::eof);
59   EXPECT_TOKEN_KIND(Source.getNextToken(), tok::eof);
60   EXPECT_TOKEN_KIND(Source.getPreviousToken(), tok::semi);
61 }
62 
63 TEST_F(IndexedTokenSourceTest, ResetPosition) {
64   IndexedTokenSource Source(lex("int a;"));
65   Source.getNextToken();
66   unsigned Position = Source.getPosition();
67   Source.getNextToken();
68   Source.getNextToken();
69   EXPECT_TOKEN_KIND(Source.getNextToken(), tok::eof);
70   EXPECT_TOKEN_KIND(Source.setPosition(Position), tok::kw_int);
71 }
72 
73 } // namespace
74 } // namespace format
75 } // namespace clang