xref: /llvm-project/clang/unittests/Tooling/Syntax/SynthesisTest.cpp (revision 5d152127d48fbcf47a8d059aa68a84c365ae3cb9)
1 //===- SynthesisTest.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 // This file tests synthesis API for syntax trees.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "TreeTestBase.h"
14 #include "clang/Tooling/Syntax/BuildTree.h"
15 #include "gtest/gtest.h"
16 
17 using namespace clang;
18 using namespace clang::syntax;
19 
20 namespace {
21 
22 class SynthesisTest : public SyntaxTreeTest {
23 protected:
24   ::testing::AssertionResult treeDumpEqual(syntax::Node *Root, StringRef Dump) {
25     if (!Root)
26       return ::testing::AssertionFailure()
27              << "Root was not built successfully.";
28 
29     auto Actual = StringRef(Root->dump(Arena->getSourceManager())).trim().str();
30     auto Expected = Dump.trim().str();
31     // EXPECT_EQ shows the diff between the two strings if they are different.
32     EXPECT_EQ(Expected, Actual);
33     if (Actual != Expected) {
34       return ::testing::AssertionFailure();
35     }
36     return ::testing::AssertionSuccess();
37   }
38 };
39 
40 INSTANTIATE_TEST_CASE_P(SynthesisTests, SynthesisTest,
41                         ::testing::ValuesIn(allTestClangConfigs()), );
42 
43 TEST_P(SynthesisTest, Leaf_Punctuation) {
44   buildTree("", GetParam());
45 
46   auto *Leaf = createLeaf(*Arena, tok::comma);
47 
48   EXPECT_TRUE(treeDumpEqual(Leaf, R"txt(
49 ',' Detached synthesized
50   )txt"));
51 }
52 
53 TEST_P(SynthesisTest, Leaf_Keyword) {
54   buildTree("", GetParam());
55 
56   auto *Leaf = createLeaf(*Arena, tok::kw_if);
57 
58   EXPECT_TRUE(treeDumpEqual(Leaf, R"txt(
59 'if' Detached synthesized
60   )txt"));
61 }
62 
63 TEST_P(SynthesisTest, Leaf_Identifier) {
64   buildTree("", GetParam());
65 
66   auto *Leaf = createLeaf(*Arena, tok::identifier, "a");
67 
68   EXPECT_TRUE(treeDumpEqual(Leaf, R"txt(
69 'a' Detached synthesized
70   )txt"));
71 }
72 
73 TEST_P(SynthesisTest, Leaf_Number) {
74   buildTree("", GetParam());
75 
76   auto *Leaf = createLeaf(*Arena, tok::numeric_constant, "1");
77 
78   EXPECT_TRUE(treeDumpEqual(Leaf, R"txt(
79 '1' Detached synthesized
80   )txt"));
81 }
82 
83 TEST_P(SynthesisTest, Statement_EmptyStatement) {
84   buildTree("", GetParam());
85 
86   auto *S = createEmptyStatement(*Arena);
87   EXPECT_TRUE(treeDumpEqual(S, R"txt(
88 EmptyStatement Detached synthesized
89 `-';' synthesized
90   )txt"));
91 }
92 } // namespace
93