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 TokenList Tokens = lex(""); 36 IndexedTokenSource Source(Tokens); 37 EXPECT_FALSE(Source.isEOF()); 38 EXPECT_TOKEN_KIND(Source.getNextToken(), tok::eof); 39 EXPECT_TRUE(Source.isEOF()); 40 EXPECT_TOKEN_KIND(Source.getNextToken(), tok::eof); 41 EXPECT_TRUE(Source.isEOF()); 42 EXPECT_TOKEN_KIND(Source.peekNextToken(/*SkipComment=*/false), tok::eof); 43 EXPECT_TOKEN_KIND(Source.peekNextToken(/*SkipComment=*/true), tok::eof); 44 EXPECT_EQ(Source.getPreviousToken(), nullptr); 45 EXPECT_TRUE(Source.isEOF()); 46 } 47 48 TEST_F(IndexedTokenSourceTest, NavigateTokenStream) { 49 TokenList Tokens = lex("int a;"); 50 IndexedTokenSource Source(Tokens); 51 EXPECT_TOKEN_KIND(Source.peekNextToken(), tok::kw_int); 52 EXPECT_TOKEN_KIND(Source.getNextToken(), tok::kw_int); 53 EXPECT_EQ(Source.getPreviousToken(), nullptr); 54 EXPECT_TOKEN_KIND(Source.peekNextToken(), tok::identifier); 55 EXPECT_TOKEN_KIND(Source.getNextToken(), tok::identifier); 56 EXPECT_TOKEN_KIND(Source.getPreviousToken(), tok::kw_int); 57 EXPECT_TOKEN_KIND(Source.peekNextToken(), tok::semi); 58 EXPECT_TOKEN_KIND(Source.getNextToken(), tok::semi); 59 EXPECT_TOKEN_KIND(Source.getPreviousToken(), tok::identifier); 60 EXPECT_TOKEN_KIND(Source.peekNextToken(), tok::eof); 61 EXPECT_TOKEN_KIND(Source.getNextToken(), tok::eof); 62 EXPECT_TOKEN_KIND(Source.getPreviousToken(), tok::semi); 63 } 64 65 TEST_F(IndexedTokenSourceTest, ResetPosition) { 66 TokenList Tokens = lex("int a;"); 67 IndexedTokenSource Source(Tokens); 68 Source.getNextToken(); 69 unsigned Position = Source.getPosition(); 70 Source.getNextToken(); 71 Source.getNextToken(); 72 EXPECT_TOKEN_KIND(Source.getNextToken(), tok::eof); 73 EXPECT_TOKEN_KIND(Source.setPosition(Position), tok::kw_int); 74 } 75 76 } // namespace 77 } // namespace format 78 } // namespace clang 79