File: TranslationUnit_test.cpp

package info (click to toggle)
ycmd 0%2B20230103%2Bgitf53e7ac%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,904 kB
  • sloc: python: 41,740; cpp: 6,055; java: 295; sh: 271; cs: 194; javascript: 150; ansic: 82; makefile: 46; xml: 18; objc: 10
file content (236 lines) | stat: -rw-r--r-- 7,809 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright (C) 2011-2018 ycmd contributors
//
// This file is part of ycmd.
//
// ycmd is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ycmd is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ycmd.  If not, see <http://www.gnu.org/licenses/>.

#include "CompletionData.h"
#include "TranslationUnitStore.h"
#include "Utils.h"
#include "../TestUtils.h"

#include <gtest/gtest.h>
#include <gmock/gmock.h>

#include <clang-c/Index.h>

using ::testing::ElementsAre;
using ::testing::WhenSorted;

namespace YouCompleteMe {

class TranslationUnitTest : public ::testing::Test {
protected:
  virtual void SetUp() {
    clang_index_ = clang_createIndex( 0, 0 );
  }

  virtual void TearDown() {
    clang_disposeIndex( clang_index_ );
  }

  CXIndex clang_index_;
};


TEST_F( TranslationUnitTest, ExceptionThrownOnParseFailure ) {
  // Create a translation unit for a C++ file that is not saved on disk.
  std::string filename = PathToTestFile( "unsaved_file.cpp" ).string();
  UnsavedFile unsaved_file;
  unsaved_file.filename_ = filename;

  try {
    // libclang requires a valid index to parse a file.
    TranslationUnit( filename,
                     std::vector< UnsavedFile >{ unsaved_file },
                     std::vector< std::string >(),
                     nullptr );
    FAIL() << "Expected ClangParseError exception.";
  } catch ( const ClangParseError &error ) {
    EXPECT_STREQ( error.what(), "Invalid arguments supplied "
                                "when parsing the translation unit." );
  } catch ( ... ) {
    FAIL() << "Expected ClangParseError exception.";
  }
}

TEST_F( TranslationUnitTest, GoToDefinitionWorks ) {
  auto test_file = PathToTestFile( "goto.cpp" ).string();
  TranslationUnit unit( test_file,
                        std::vector< UnsavedFile >(),
                        std::vector< std::string >(),
                        clang_index_ );

  Location location = unit.GetDefinitionLocation(
                        test_file,
                        17,
                        3,
                        std::vector< UnsavedFile >() );

  EXPECT_EQ( 1, location.line_number_ );
  EXPECT_EQ( 8, location.column_number_ );
  EXPECT_TRUE( !location.filename_.empty() );
}

TEST_F( TranslationUnitTest, GoToDefinitionFails ) {
  auto test_file = PathToTestFile( "goto.cpp" ).string();
  TranslationUnit unit( test_file,
                        std::vector< UnsavedFile >(),
                        std::vector< std::string >(),
                        clang_index_ );

  Location location = unit.GetDefinitionLocation(
                        test_file,
                        19,
                        3,
                        std::vector< UnsavedFile >() );

  EXPECT_FALSE( location.IsValid() );
}

TEST_F( TranslationUnitTest, GoToDeclarationWorks ) {
  auto test_file = PathToTestFile( "goto.cpp" ).string();
  TranslationUnit unit( test_file,
                        std::vector< UnsavedFile >(),
                        std::vector< std::string >(),
                        clang_index_ );

  Location location = unit.GetDeclarationLocation(
                        test_file,
                        19,
                        3,
                        std::vector< UnsavedFile >() );

  EXPECT_EQ( 12, location.line_number_ );
  EXPECT_EQ( 8, location.column_number_ );
  EXPECT_TRUE( !location.filename_.empty() );
}

TEST_F( TranslationUnitTest, GoToDeclarationWorksOnDefinition ) {
  auto test_file = PathToTestFile( "goto.cpp" ).string();
  TranslationUnit unit( test_file,
                        std::vector< UnsavedFile >(),
                        std::vector< std::string >(),
                        clang_index_ );

  Location location = unit.GetDeclarationLocation(
                        test_file,
                        16,
                        6,
                        std::vector< UnsavedFile >() );

  EXPECT_EQ( 14, location.line_number_ );
  EXPECT_EQ( 6, location.column_number_ );
  EXPECT_TRUE( !location.filename_.empty() );
}


TEST_F( TranslationUnitTest, GoToWorks ) {
  auto test_file = PathToTestFile( "goto.cpp" ).string();
  TranslationUnit unit( test_file,
                        std::vector< UnsavedFile >(),
                        std::vector< std::string >(),
                        clang_index_ );

  Location location = unit.GetDefinitionOrDeclarationLocation(
                        test_file,
                        16,
                        8,
                        std::vector< UnsavedFile >() );

  EXPECT_EQ( 14, location.line_number_ );
  EXPECT_EQ( 6, location.column_number_ );
  EXPECT_TRUE( !location.filename_.empty() );

  location = unit.GetDefinitionOrDeclarationLocation(
               test_file,
               14,
               9,
               std::vector< UnsavedFile >() );

  EXPECT_EQ( 16, location.line_number_ );
  EXPECT_EQ( 6, location.column_number_ );
  EXPECT_TRUE( !location.filename_.empty() );
}


TEST_F( TranslationUnitTest, InvalidTranslationUnitStore ) {
  // libclang fails to parse a file with no extension and no language flag -x
  // given.
  TranslationUnitStore translation_unit_store{ clang_index_ };
  try {
    translation_unit_store.GetOrCreate(
      PathToTestFile( "file_without_extension" ).string(),
      std::vector< UnsavedFile >(),
      std::vector< std::string >() );
    FAIL() << "Expected ClangParseError exception.";
  } catch ( const ClangParseError &error ) {
    EXPECT_STREQ( error.what(),
                  "An AST deserialization error occurred while parsing "
                  "the translation unit." );
  } catch ( ... ) {
    FAIL() << "Expected ClangParseError exception.";
  }
}


TEST_F( TranslationUnitTest, InvalidTranslationUnit ) {

  TranslationUnit unit;

  EXPECT_TRUE( unit.IsCurrentlyUpdating() );

  std::vector< CompletionData > completion_data_vector =
      unit.CandidatesForLocation( "", 1, 1, std::vector< UnsavedFile >() );
  EXPECT_TRUE( completion_data_vector.empty() );

  EXPECT_EQ( Location(),
             unit.GetDeclarationLocation( "",
                                          1,
                                          1,
                                          std::vector< UnsavedFile >() ) );

  EXPECT_EQ( Location(),
             unit.GetDefinitionLocation( "",
                                         1,
                                         1,
                                         std::vector< UnsavedFile >() ) );

  EXPECT_EQ( Location(),
             unit.GetDefinitionOrDeclarationLocation(
               "",
               1,
               1,
               std::vector< UnsavedFile >() ) );

  EXPECT_EQ( std::string( "Internal error: no translation unit" ),
             unit.GetTypeAtLocation( "",
                                     1,
                                     1,
                                     std::vector< UnsavedFile >() ) );

  EXPECT_EQ( std::string( "Internal error: no translation unit" ),
             unit.GetEnclosingFunctionAtLocation(
               "",
               1,
               1,
               std::vector< UnsavedFile >() ) );

  EXPECT_EQ( DocumentationData(),
             unit.GetDocsForLocation( Location(),
                                      std::vector< UnsavedFile >(),
                                      false ) );
}

} // namespace YouCompleteMe