1 //===- unittest/Format/FormatTokenSourceTest.cpp --------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "../../lib/Format/FormatTokenSource.h"
10 #include "TestLexer.h"
11 #include "clang/Basic/TokenKinds.h"
12 #include "gtest/gtest.h"
18 class IndexedTokenSourceTest
: public testing::Test
{
20 TokenList
lex(StringRef Code
, const FormatStyle
&Style
= getLLVMStyle()) {
21 return TestLexer(Allocator
, Buffers
, Style
).lex(Code
);
23 llvm::SpecificBumpPtrAllocator
<FormatToken
> Allocator
;
24 std::vector
<std::unique_ptr
<llvm::MemoryBuffer
>> Buffers
;
27 #define EXPECT_TOKEN_KIND(FormatTok, Kind) \
29 FormatToken *Tok = FormatTok; \
30 EXPECT_EQ(Tok->Tok.getKind(), Kind) << *Tok; \
32 #define EXPECT_TOKEN_ID(FormatTok, Name) \
34 FormatToken *Tok = FormatTok; \
35 EXPECT_EQ(Tok->Tok.getKind(), tok::identifier) << *Tok; \
36 EXPECT_EQ(Tok->TokenText, Name) << *Tok; \
39 TEST_F(IndexedTokenSourceTest
, EmptyInput
) {
40 IndexedTokenSource
Source(lex(""));
41 EXPECT_FALSE(Source
.isEOF());
42 EXPECT_TOKEN_KIND(Source
.getNextToken(), tok::eof
);
43 EXPECT_TRUE(Source
.isEOF());
44 EXPECT_TOKEN_KIND(Source
.getNextToken(), tok::eof
);
45 EXPECT_TRUE(Source
.isEOF());
46 EXPECT_TOKEN_KIND(Source
.peekNextToken(/*SkipComment=*/false), tok::eof
);
47 EXPECT_TOKEN_KIND(Source
.peekNextToken(/*SkipComment=*/true), tok::eof
);
48 EXPECT_EQ(Source
.getPreviousToken(), nullptr);
49 EXPECT_TRUE(Source
.isEOF());
52 TEST_F(IndexedTokenSourceTest
, NavigateTokenStream
) {
53 IndexedTokenSource
Source(lex("int a;"));
54 EXPECT_TOKEN_KIND(Source
.peekNextToken(), tok::kw_int
);
55 EXPECT_TOKEN_KIND(Source
.getNextToken(), tok::kw_int
);
56 EXPECT_EQ(Source
.getPreviousToken(), nullptr);
57 EXPECT_TOKEN_KIND(Source
.peekNextToken(), tok::identifier
);
58 EXPECT_TOKEN_KIND(Source
.getNextToken(), tok::identifier
);
59 EXPECT_TOKEN_KIND(Source
.getPreviousToken(), tok::kw_int
);
60 EXPECT_TOKEN_KIND(Source
.peekNextToken(), tok::semi
);
61 EXPECT_TOKEN_KIND(Source
.getNextToken(), tok::semi
);
62 EXPECT_TOKEN_KIND(Source
.getPreviousToken(), tok::identifier
);
63 EXPECT_TOKEN_KIND(Source
.peekNextToken(), tok::eof
);
64 EXPECT_TOKEN_KIND(Source
.getNextToken(), tok::eof
);
65 EXPECT_TOKEN_KIND(Source
.getPreviousToken(), tok::semi
);
66 EXPECT_TOKEN_KIND(Source
.getNextToken(), tok::eof
);
67 EXPECT_TOKEN_KIND(Source
.getPreviousToken(), tok::semi
);
70 TEST_F(IndexedTokenSourceTest
, ResetPosition
) {
71 IndexedTokenSource
Source(lex("int a;"));
72 Source
.getNextToken();
73 unsigned Position
= Source
.getPosition();
74 Source
.getNextToken();
75 Source
.getNextToken();
76 EXPECT_TOKEN_KIND(Source
.getNextToken(), tok::eof
);
77 EXPECT_TOKEN_KIND(Source
.setPosition(Position
), tok::kw_int
);
80 TEST_F(IndexedTokenSourceTest
, InsertTokens
) {
81 IndexedTokenSource
Source(lex("A1 A2"));
82 EXPECT_TOKEN_ID(Source
.getNextToken(), "A1");
83 EXPECT_TOKEN_ID(Source
.insertTokens(lex("B1 B2")), "B1");
84 EXPECT_TOKEN_ID(Source
.getNextToken(), "B2");
85 EXPECT_TOKEN_ID(Source
.getNextToken(), "A1");
86 EXPECT_TOKEN_ID(Source
.getNextToken(), "A2");
89 TEST_F(IndexedTokenSourceTest
, InsertTokensAtEOF
) {
90 IndexedTokenSource
Source(lex("A1"));
91 EXPECT_TOKEN_ID(Source
.getNextToken(), "A1");
92 EXPECT_TOKEN_KIND(Source
.getNextToken(), tok::eof
);
93 EXPECT_TOKEN_ID(Source
.insertTokens(lex("B1 B2")), "B1");
94 EXPECT_TOKEN_ID(Source
.getNextToken(), "B2");
95 EXPECT_TOKEN_KIND(Source
.getNextToken(), tok::eof
);
98 TEST_F(IndexedTokenSourceTest
, InsertTokensRecursive
) {
99 IndexedTokenSource
Source(lex("A1"));
100 EXPECT_TOKEN_ID(Source
.getNextToken(), "A1");
102 EXPECT_TOKEN_ID(Source
.insertTokens(lex("B1")), "B1");
104 EXPECT_TOKEN_ID(Source
.insertTokens(lex("C1")), "C1");
106 EXPECT_TOKEN_ID(Source
.insertTokens(lex("D1")), "D1");
108 EXPECT_TOKEN_ID(Source
.getNextToken(), "C1");
109 EXPECT_TOKEN_ID(Source
.getNextToken(), "B1");
110 EXPECT_TOKEN_ID(Source
.getNextToken(), "A1");
113 TEST_F(IndexedTokenSourceTest
, InsertTokensRecursiveAtEndOfSequence
) {
114 IndexedTokenSource
Source(lex("A1"));
115 EXPECT_TOKEN_ID(Source
.getNextToken(), "A1");
116 EXPECT_TOKEN_ID(Source
.insertTokens(lex("B1")), "B1");
117 EXPECT_TOKEN_ID(Source
.getNextToken(), "A1");
118 EXPECT_TOKEN_ID(Source
.insertTokens(lex("C1")), "C1");
119 EXPECT_TOKEN_ID(Source
.getNextToken(), "A1");
120 EXPECT_TOKEN_ID(Source
.insertTokens(lex("D1")), "D1");
121 EXPECT_TOKEN_ID(Source
.getNextToken(), "A1");
125 } // namespace format