1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #include "googletest.h"
27 
28 #include "clangcompareoperators.h"
29 #include "unittest-utility-functions.h"
30 
31 #include <clangdocument.h>
32 #include <clangdocuments.h>
33 #include <clangstring.h>
34 #include <cursor.h>
35 #include <sourcelocation.h>
36 #include <sourcerange.h>
37 #include <clangtranslationunit.h>
38 #include <unsavedfiles.h>
39 
40 using ClangBackEnd::Cursor;
41 using ClangBackEnd::Document;
42 using ClangBackEnd::TranslationUnit;
43 using ClangBackEnd::UnsavedFiles;
44 using ClangBackEnd::Documents;
45 using ClangBackEnd::ClangString;
46 using ClangBackEnd::SourceRange;
47 
48 using testing::IsNull;
49 using testing::NotNull;
50 using testing::Gt;
51 using testing::Contains;
52 using testing::EndsWith;
53 using testing::AllOf;
54 using testing::Not;
55 using testing::IsEmpty;
56 using testing::StrEq;
57 using testing::Eq;
58 
59 namespace {
60 
61 struct Data {
62     ClangBackEnd::UnsavedFiles unsavedFiles;
63     ClangBackEnd::Documents documents{unsavedFiles};
64     Utf8String filePath{Utf8StringLiteral(TESTDATA_DIR"/cursor.cpp")};
65     Document document{filePath,
66                       UnitTest::addPlatformArguments({Utf8StringLiteral("-std=c++11")}),
67                       {},
68                       documents};
69     TranslationUnit translationUnit{filePath,
70                                     filePath,
71                                     document.translationUnit().cxIndex(),
72                                     document.translationUnit().cxTranslationUnit()};
73 };
74 
75 class Cursor : public ::testing::Test
76 {
77 public:
78     static void SetUpTestCase();
79     static void TearDownTestCase();
80 
81 protected:
82     static Data *d;
83     const Document &document = d->document;
84     const TranslationUnit &translationUnit = d->translationUnit;
85 };
86 
87 
88 
TEST_F(Cursor,CreateNullCursor)89 TEST_F(Cursor, CreateNullCursor)
90 {
91     ::Cursor cursor;
92 
93     ASSERT_TRUE(cursor.isNull());
94 }
95 
TEST_F(Cursor,CompareNullCursors)96 TEST_F(Cursor, CompareNullCursors)
97 {
98     ::Cursor cursor;
99     ::Cursor cursor2;
100 
101     ASSERT_THAT(cursor, cursor2);
102 }
103 
TEST_F(Cursor,IsNotValid)104 TEST_F(Cursor, IsNotValid)
105 {
106     ::Cursor cursor;
107 
108     ASSERT_FALSE(cursor.isValid());
109 }
110 
TEST_F(Cursor,IsValid)111 TEST_F(Cursor, IsValid)
112 {
113     auto cursor = translationUnit.cursor();
114 
115     ASSERT_TRUE(cursor.isValid());
116 }
117 
TEST_F(Cursor,IsTranslationUnit)118 TEST_F(Cursor, IsTranslationUnit)
119 {
120     auto cursor = translationUnit.cursor();
121 
122     ASSERT_TRUE(cursor.isTranslationUnit());
123 }
124 
TEST_F(Cursor,NullCursorIsNotTranslationUnit)125 TEST_F(Cursor, NullCursorIsNotTranslationUnit)
126 {
127     ::Cursor cursor;
128 
129     ASSERT_FALSE(cursor.isTranslationUnit());
130 }
131 
TEST_F(Cursor,UnifiedSymbolResolution)132 TEST_F(Cursor, UnifiedSymbolResolution)
133 {
134      ::Cursor cursor;
135 
136      ASSERT_FALSE(cursor.unifiedSymbolResolution().hasContent());
137 }
138 
TEST_F(Cursor,GetCursorAtLocation)139 TEST_F(Cursor, GetCursorAtLocation)
140 {
141     auto cursor = translationUnit.cursorAt(3, 6);
142 
143     ASSERT_THAT(cursor.unifiedSymbolResolution(), Eq("c:@F@function#I#"));
144 }
145 
TEST_F(Cursor,GetCursoSourceLocation)146 TEST_F(Cursor, GetCursoSourceLocation)
147 {
148     auto cursor = translationUnit.cursorAt(3, 6);
149 
150     ASSERT_THAT(cursor.sourceLocation(), translationUnit.sourceLocationAt(3, 6));
151 }
152 
TEST_F(Cursor,GetCursoSourceRange)153 TEST_F(Cursor, GetCursoSourceRange)
154 {
155     auto cursor = translationUnit.cursorAt(3, 6);
156 
157     ASSERT_THAT(cursor.sourceRange(), SourceRange(translationUnit.sourceLocationAt(3, 1),
158                                                   translationUnit.sourceLocationAt(6, 2)));
159 }
160 
TEST_F(Cursor,Mangling)161 TEST_F(Cursor, Mangling)
162 {
163     auto cursor = translationUnit.cursorAt(3, 6);
164 
165     ASSERT_TRUE(cursor.mangling().hasContent());
166 }
167 
TEST_F(Cursor,Spelling)168 TEST_F(Cursor, Spelling)
169 {
170     auto cursor = translationUnit.cursorAt(3, 6);
171 
172     ASSERT_THAT(cursor.spelling().cString(), StrEq("function"));
173 }
174 
TEST_F(Cursor,DisplayName)175 TEST_F(Cursor, DisplayName)
176 {
177     auto cursor = translationUnit.cursorAt(3, 6);
178 
179     ASSERT_THAT(cursor.displayName(), Eq("function(int)"));
180 }
181 
TEST_F(Cursor,BriefComment)182 TEST_F(Cursor, BriefComment)
183 {
184     auto cursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 10, 7);
185 
186     ASSERT_THAT(cursor.briefComment(), Eq("A brief comment"));
187 }
188 
TEST_F(Cursor,RawComment)189 TEST_F(Cursor, RawComment)
190 {
191     auto cursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 10, 7);
192 
193     ASSERT_THAT(cursor.rawComment(), Eq("/**\n * A brief comment\n */"));
194 }
195 
TEST_F(Cursor,CommentRange)196 TEST_F(Cursor, CommentRange)
197 {
198     auto cursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 10, 7);
199 
200 
201     ASSERT_THAT(cursor.commentRange(),
202                 SourceRange(translationUnit.sourceLocationAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 7, 1),
203                             translationUnit.sourceLocationAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 9, 4)));
204 }
205 
TEST_F(Cursor,IsDefinition)206 TEST_F(Cursor, IsDefinition)
207 {
208     auto cursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 10, 7);
209 
210     ASSERT_TRUE(cursor.isDefinition());
211 }
212 
TEST_F(Cursor,ForwardDeclarationIsNotDefinition)213 TEST_F(Cursor, ForwardDeclarationIsNotDefinition)
214 {
215     auto cursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 6, 7);
216 
217     ASSERT_FALSE(cursor.isDefinition());
218 }
219 
TEST_F(Cursor,GetDefinitionOfFowardDeclaration)220 TEST_F(Cursor, GetDefinitionOfFowardDeclaration)
221 {
222     auto forwardDeclarationcursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 6, 7);
223     auto definitionCursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 10, 7);
224 
225     ASSERT_THAT(forwardDeclarationcursor.definition(), definitionCursor);
226 }
227 
TEST_F(Cursor,CallToMethodeIsNotDynamic)228 TEST_F(Cursor, CallToMethodeIsNotDynamic)
229 {
230     auto cursor = translationUnit.cursorAt(18, 5);
231 
232     ASSERT_FALSE(cursor.isDynamicCall());
233 }
234 
TEST_F(Cursor,CallToAbstractVirtualMethodeIsDynamic)235 TEST_F(Cursor, CallToAbstractVirtualMethodeIsDynamic)
236 {
237     auto cursor = translationUnit.cursorAt(19, 5);
238 
239     ASSERT_TRUE(cursor.isDynamicCall());
240 }
241 
TEST_F(Cursor,CanonicalCursor)242 TEST_F(Cursor, CanonicalCursor)
243 {
244     auto forwardDeclarationcursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 6, 7);
245     auto definitionCursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 10, 7);
246 
247     ASSERT_THAT(definitionCursor.canonical(), forwardDeclarationcursor);
248 }
249 
TEST_F(Cursor,ReferencedCursor)250 TEST_F(Cursor, ReferencedCursor)
251 {
252     auto functionCallCursor = translationUnit.cursorAt(18, 5);
253     auto functionCursor = translationUnit.cursorAt(16, 17);
254 
255     ASSERT_THAT(functionCallCursor.referenced(), functionCursor);
256 }
257 
TEST_F(Cursor,IsVirtual)258 TEST_F(Cursor, IsVirtual)
259 {
260     auto cursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 15, 17);
261 
262     ASSERT_TRUE(cursor.isVirtualMethod());
263 }
264 
TEST_F(Cursor,IsNotPureVirtualOnlyVirtual)265 TEST_F(Cursor, IsNotPureVirtualOnlyVirtual)
266 {
267     auto cursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 15, 17);
268 
269     ASSERT_FALSE(cursor.isPureVirtualMethod());
270 }
271 
TEST_F(Cursor,IsPureVirtual)272 TEST_F(Cursor, IsPureVirtual)
273 {
274     auto cursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 16, 17);
275 
276     ASSERT_TRUE(cursor.isPureVirtualMethod());
277 }
278 
TEST_F(Cursor,ConstantMethod)279 TEST_F(Cursor, ConstantMethod)
280 {
281     auto cursor = translationUnit.cursorAt(31, 18);
282 
283     ASSERT_TRUE(cursor.isConstantMethod());
284 }
285 
TEST_F(Cursor,IsStaticMethod)286 TEST_F(Cursor, IsStaticMethod)
287 {
288     auto cursor = translationUnit.cursorAt(36, 18);
289 
290     ASSERT_TRUE(cursor.isStaticMethod());
291 }
292 
TEST_F(Cursor,TypeSpelling)293 TEST_F(Cursor, TypeSpelling)
294 {
295     auto cursor = translationUnit.cursorAt(43, 5);
296 
297     ASSERT_THAT(cursor.type().utf8Spelling(), Utf8StringLiteral("lint"));
298 }
299 
TEST_F(Cursor,CanonicalTypeSpelling)300 TEST_F(Cursor, CanonicalTypeSpelling)
301 {
302     auto cursor = translationUnit.cursorAt(43, 5);
303 
304     ASSERT_THAT(cursor.type().canonical().utf8Spelling(), Utf8StringLiteral("long long"));
305 }
306 
TEST_F(Cursor,CanonicalTypeCStringSpelling)307 TEST_F(Cursor, CanonicalTypeCStringSpelling)
308 {
309     auto cursor = translationUnit.cursorAt(43, 5);
310 
311     auto spelling = cursor.type().canonical().spelling();
312 
313     ASSERT_THAT(spelling.cString(), StrEq("long long"));
314 }
315 
TEST_F(Cursor,CanonicalTypeIsNotType)316 TEST_F(Cursor, CanonicalTypeIsNotType)
317 {
318     auto cursor = translationUnit.cursorAt(43, 5);
319 
320     ASSERT_THAT(cursor.type().canonical(), Not(cursor.type()));
321 }
322 
TEST_F(Cursor,TypeDeclartionIsAlias)323 TEST_F(Cursor, TypeDeclartionIsAlias)
324 {
325     auto declarationCursor = translationUnit.cursorAt(41, 5);
326     auto lintCursor = translationUnit.cursorAt(39, 11);
327 
328     ASSERT_THAT(declarationCursor.type().declaration().type(), lintCursor.type());
329 }
330 
TEST_F(Cursor,TypeIsConstantWithoutAliasLookup)331 TEST_F(Cursor, TypeIsConstantWithoutAliasLookup)
332 {
333     auto cursor = translationUnit.cursorAt(45, 16);
334 
335     ASSERT_TRUE(cursor.type().isConstant());
336 }
337 
TEST_F(Cursor,ClassIsCompoundType)338 TEST_F(Cursor, ClassIsCompoundType)
339 {
340     auto cursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 10, 7);
341 
342     ASSERT_TRUE(cursor.isCompoundType());
343 }
344 
TEST_F(Cursor,StructIsCompoundType)345 TEST_F(Cursor, StructIsCompoundType)
346 {
347     auto cursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 28, 8);
348 
349     ASSERT_TRUE(cursor.isCompoundType());
350 }
351 
TEST_F(Cursor,UnionIsCompoundType)352 TEST_F(Cursor, UnionIsCompoundType)
353 {
354     auto cursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 33, 7);
355 
356     ASSERT_TRUE(cursor.isCompoundType());
357 }
358 
TEST_F(Cursor,IsDeclaration)359 TEST_F(Cursor, IsDeclaration)
360 {
361     auto cursor = translationUnit.cursorAt(41, 10);
362 
363     ASSERT_TRUE(cursor.isDeclaration());
364 }
365 
TEST_F(Cursor,SemanticParent)366 TEST_F(Cursor, SemanticParent)
367 {
368     auto cursor = translationUnit.cursorAt(43, 6);
369     auto expectedSemanticParent = translationUnit.cursorAt(36, 18);
370 
371     auto semanticParent = cursor.semanticParent();
372 
373     ASSERT_THAT(semanticParent, expectedSemanticParent);
374 }
375 
TEST_F(Cursor,IsLocalVariableInMethod)376 TEST_F(Cursor, IsLocalVariableInMethod)
377 {
378     auto cursor = translationUnit.cursorAt(20, 9);
379 
380     ASSERT_TRUE(cursor.isLocalVariable());
381 }
382 
TEST_F(Cursor,IsLocalVariableInStaticFunction)383 TEST_F(Cursor, IsLocalVariableInStaticFunction)
384 {
385     auto cursor = translationUnit.cursorAt(43, 5);
386 
387     ASSERT_TRUE(cursor.isLocalVariable());
388 }
389 
TEST_F(Cursor,IsLocalVariableInTemplateFunction)390 TEST_F(Cursor, IsLocalVariableInTemplateFunction)
391 {
392     auto cursor = translationUnit.cursorAt(52, 7);
393 
394     ASSERT_TRUE(cursor.isLocalVariable());
395 }
396 
TEST_F(Cursor,IsLocalVariableInConversionOperator)397 TEST_F(Cursor, IsLocalVariableInConversionOperator)
398 {
399     auto cursor = translationUnit.cursorAt(57, 9);
400 
401     ASSERT_TRUE(cursor.isLocalVariable());
402 }
403 
TEST_F(Cursor,IsLocalVariableInOperator)404 TEST_F(Cursor, IsLocalVariableInOperator)
405 {
406     auto cursor = translationUnit.cursorAt(62, 9);
407 
408     ASSERT_TRUE(cursor.isLocalVariable());
409 }
410 
TEST_F(Cursor,IsLocalVariableInConstructor)411 TEST_F(Cursor, IsLocalVariableInConstructor)
412 {
413     auto cursor = translationUnit.cursorAt(13, 9);
414 
415     ASSERT_TRUE(cursor.isLocalVariable());
416 }
417 
TEST_F(Cursor,IsLocalVariableInDestructor)418 TEST_F(Cursor, IsLocalVariableInDestructor)
419 {
420     auto cursor = translationUnit.cursorAt(69, 9);
421 
422     ASSERT_TRUE(cursor.isLocalVariable());
423 }
424 
TEST_F(Cursor,FindFunctionCaller)425 TEST_F(Cursor, FindFunctionCaller)
426 {
427     auto functionCursor = translationUnit.cursorAt(92, 24);
428     auto structCursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 28, 8);
429 
430     ASSERT_THAT(functionCursor.functionBaseDeclaration(), structCursor);
431 }
432 
TEST_F(Cursor,FindFunctionCallerPointer)433 TEST_F(Cursor, FindFunctionCallerPointer)
434 {
435     auto functionCursor = translationUnit.cursorAt(79, 25);
436     auto structCursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 28, 8);
437 
438     ASSERT_THAT(functionCursor.functionBaseDeclaration(), structCursor);
439 }
440 
TEST_F(Cursor,FindFunctionCallerThis)441 TEST_F(Cursor, FindFunctionCallerThis)
442 {
443     auto functionCursor = translationUnit.cursorAt(106, 5);
444     auto structCursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 38, 8);
445 
446     ASSERT_THAT(functionCursor.functionBaseDeclaration(), structCursor);
447 }
448 
TEST_F(Cursor,NonPointerTypeForValue)449 TEST_F(Cursor, NonPointerTypeForValue)
450 {
451     auto variableCursor = translationUnit.cursorAt(101, 10);
452     auto variablePointerCursor = translationUnit.cursorAt(100, 11);
453 
454     ASSERT_THAT(variableCursor.nonPointerTupe(), variablePointerCursor.nonPointerTupe());
455 }
456 
TEST_F(Cursor,HasFinalAttributeInFunction)457 TEST_F(Cursor, HasFinalAttributeInFunction)
458 {
459     auto cursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 30, 18);
460 
461     ASSERT_TRUE(cursor.hasFinalFunctionAttribute());
462 }
463 
TEST_F(Cursor,HasNotFinalAttributeInFunction)464 TEST_F(Cursor, HasNotFinalAttributeInFunction)
465 {
466     auto cursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 15, 17);
467 
468     ASSERT_FALSE(cursor.hasFinalFunctionAttribute());
469 }
470 
TEST_F(Cursor,HasFinalAttributeInClass)471 TEST_F(Cursor, HasFinalAttributeInClass)
472 {
473     auto cursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 28, 8);
474 
475     ASSERT_TRUE(cursor.hasFinalClassAttribute());
476 }
477 
TEST_F(Cursor,HasNotFinaAttributeInClass)478 TEST_F(Cursor, HasNotFinaAttributeInClass)
479 {
480     auto cursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 38, 8);
481 
482     ASSERT_FALSE(cursor.hasFinalClassAttribute());
483 }
484 
TEST_F(Cursor,HasOutputValues)485 TEST_F(Cursor, HasOutputValues)
486 {
487     auto callExpressionCursor = translationUnit.cursorAt(117, 19);
488     auto outputArgumentExpectedSourceLocation = translationUnit.cursorAt(117, 20).cxSourceRange();
489 
490     auto outputArgumentLocations = callExpressionCursor.outputArgumentRanges();
491 
492     ASSERT_THAT(outputArgumentLocations.size(), 2);
493     ASSERT_THAT(outputArgumentLocations[0], outputArgumentExpectedSourceLocation);
494 }
495 
TEST_F(Cursor,HasOnlyInputValues)496 TEST_F(Cursor, HasOnlyInputValues)
497 {
498     auto callExpressionCursor = translationUnit.cursorAt(118, 18);
499 
500     auto outputArgumentLocations = callExpressionCursor.outputArgumentRanges();
501 
502     ASSERT_THAT(outputArgumentLocations, IsEmpty());
503 }
504 
TEST_F(Cursor,ArgumentCountIsZero)505 TEST_F(Cursor, ArgumentCountIsZero)
506 {
507     auto cursor = translationUnit.cursorAt(121, 23);
508 
509     auto count = cursor.type().argumentCount();
510 
511     ASSERT_THAT(count, 0);
512 }
513 
TEST_F(Cursor,ArgumentCountIsTwo)514 TEST_F(Cursor, ArgumentCountIsTwo)
515 {
516     auto cursor = translationUnit.cursorAt(122, 22);
517 
518     auto count = cursor.type().argumentCount();
519 
520     ASSERT_THAT(count, 2);
521 }
522 
TEST_F(Cursor,ArgumentOneIsValue)523 TEST_F(Cursor, ArgumentOneIsValue)
524 {
525     auto callExpressionCursor = translationUnit.cursorAt(122, 22);
526 
527     auto argument = callExpressionCursor.type().argument(0);
528 
529     ASSERT_FALSE(argument.isConstant());
530     ASSERT_THAT(argument.kind(), CXType_Int);
531 }
532 
TEST_F(Cursor,ArgumentTwoIsLValueReference)533 TEST_F(Cursor, ArgumentTwoIsLValueReference)
534 {
535     auto callExpressionCursor = translationUnit.cursorAt(122, 22);
536 
537     auto argument = callExpressionCursor.type().argument(1);
538 
539     ASSERT_THAT(argument.kind(), CXType_LValueReference);
540 }
541 
TEST_F(Cursor,ArgumentTwoIsConstantReference)542 TEST_F(Cursor, ArgumentTwoIsConstantReference)
543 {
544     auto callExpressionCursor = translationUnit.cursorAt(122, 22);
545 
546     auto argumentPointee = callExpressionCursor.type().argument(1);
547 
548     ASSERT_TRUE(argumentPointee.isConstantReference());
549 }
550 
TEST_F(Cursor,CursorArgumentCount)551 TEST_F(Cursor, CursorArgumentCount)
552 {
553     auto cursor = translationUnit.cursorAt(117, 19);
554 
555     ASSERT_THAT(cursor.kind(), CXCursor_CallExpr);
556     ASSERT_THAT(cursor.argumentCount(), 4);
557 }
558 
TEST_F(Cursor,CursorArgumentInputValue)559 TEST_F(Cursor, CursorArgumentInputValue)
560 {
561     auto callExpressionCursor = translationUnit.cursorAt(117, 19);
562     auto declarationReferenceExpressionCursor = translationUnit.cursorAt(117, 20);
563 
564     ASSERT_THAT(callExpressionCursor.argument(0), declarationReferenceExpressionCursor);
565 }
566 
TEST_F(Cursor,IsConstantLValueReference)567 TEST_F(Cursor, IsConstantLValueReference)
568 {
569     auto callExpressionCursor = translationUnit.cursorAt(125, 26);
570 
571     auto argument = callExpressionCursor.type().argument(0);
572 
573     ASSERT_TRUE(argument.isConstantReference());
574 }
575 
TEST_F(Cursor,LValueReferenceIsNotConstantLValueReference)576 TEST_F(Cursor, LValueReferenceIsNotConstantLValueReference)
577 {
578     auto callExpressionCursor = translationUnit.cursorAt(124, 21);
579 
580     auto argument = callExpressionCursor.type().argument(0);
581 
582     ASSERT_FALSE(argument.isConstantReference());
583 }
584 
TEST_F(Cursor,ValueIsNotConstantLValueReference)585 TEST_F(Cursor, ValueIsNotConstantLValueReference)
586 {
587     auto callExpressionCursor = translationUnit.cursorAt(123, 18);
588 
589     auto argument = callExpressionCursor.type().argument(0);
590 
591     ASSERT_FALSE(argument.isConstantReference());
592 }
593 
TEST_F(Cursor,PointerToConstantNotConstantLValueReference)594 TEST_F(Cursor, PointerToConstantNotConstantLValueReference)
595 {
596     auto callExpressionCursor = translationUnit.cursorAt(126, 20);
597 
598     auto argument = callExpressionCursor.type().argument(0);
599 
600     ASSERT_FALSE(argument.isConstantReference());
601 }
602 
TEST_F(Cursor,IsLValueReference)603 TEST_F(Cursor, IsLValueReference)
604 {
605     auto callExpressionCursor = translationUnit.cursorAt(124, 21);
606 
607     auto argument = callExpressionCursor.type().argument(0);
608 
609     ASSERT_TRUE(argument.isLValueReference());
610 }
611 
TEST_F(Cursor,ConstantLValueReferenceIsLValueReference)612 TEST_F(Cursor, ConstantLValueReferenceIsLValueReference)
613 {
614     auto callExpressionCursor = translationUnit.cursorAt(125, 26);
615 
616     auto argument = callExpressionCursor.type().argument(0);
617 
618     ASSERT_TRUE(argument.isLValueReference());
619 }
620 
TEST_F(Cursor,ValueIsNotLValueReference)621 TEST_F(Cursor, ValueIsNotLValueReference)
622 {
623     auto callExpressionCursor = translationUnit.cursorAt(123, 18);
624 
625     auto argument = callExpressionCursor.type().argument(0);
626 
627     ASSERT_FALSE(argument.isLValueReference());
628 }
629 
TEST_F(Cursor,PointerIsNotLValueReference)630 TEST_F(Cursor, PointerIsNotLValueReference)
631 {
632     auto callExpressionCursor = translationUnit.cursorAt(126, 20);
633 
634     auto argument = callExpressionCursor.type().argument(0);
635 
636     ASSERT_FALSE(argument.isLValueReference());
637 }
638 
TEST_F(Cursor,PointerToConstant)639 TEST_F(Cursor, PointerToConstant)
640 {
641     auto callExpressionCursor = translationUnit.cursorAt(126, 20);
642 
643     auto argument = callExpressionCursor.type().argument(0);
644 
645     ASSERT_TRUE(argument.isPointerToConstant());
646 }
647 
TEST_F(Cursor,ValueIsNotPointerToConstant)648 TEST_F(Cursor, ValueIsNotPointerToConstant)
649 {
650     auto callExpressionCursor = translationUnit.cursorAt(123, 18);
651 
652     auto argument = callExpressionCursor.type().argument(0);
653 
654     ASSERT_FALSE(argument.isPointerToConstant());
655 }
656 
TEST_F(Cursor,PointerNotPointerToConstant)657 TEST_F(Cursor, PointerNotPointerToConstant)
658 {
659     auto callExpressionCursor = translationUnit.cursorAt(127, 13);
660 
661     auto argument = callExpressionCursor.type().argument(0);
662 
663     ASSERT_FALSE(argument.isPointerToConstant());
664 }
665 
TEST_F(Cursor,ConstantLValueReferenceIsNotPointerToConstant)666 TEST_F(Cursor, ConstantLValueReferenceIsNotPointerToConstant)
667 {
668     auto callExpressionCursor = translationUnit.cursorAt(125, 26);
669 
670     auto argument = callExpressionCursor.type().argument(0);
671 
672     ASSERT_FALSE(argument.isPointerToConstant());
673 }
674 
TEST_F(Cursor,IsConstantPointer)675 TEST_F(Cursor, IsConstantPointer)
676 {
677     auto callExpressionCursor = translationUnit.cursorAt(128, 21);
678 
679     auto argument = callExpressionCursor.type().argument(0);
680 
681     ASSERT_TRUE(argument.isConstantPointer());
682 }
683 
TEST_F(Cursor,PointerToConstantIsNotConstantPointer)684 TEST_F(Cursor, PointerToConstantIsNotConstantPointer)
685 {
686     auto callExpressionCursor = translationUnit.cursorAt(126, 20);
687 
688     auto argument = callExpressionCursor.type().argument(0);
689 
690     ASSERT_FALSE(argument.isConstantPointer());
691 }
692 
TEST_F(Cursor,ConstValueIsNotConstantPointer)693 TEST_F(Cursor, ConstValueIsNotConstantPointer)
694 {
695     auto callExpressionCursor = translationUnit.cursorAt(129, 23);
696 
697     auto argument = callExpressionCursor.type().argument(0);
698 
699     ASSERT_FALSE(argument.isConstantPointer());
700 }
701 
TEST_F(Cursor,PointerToConstantIsReferencingConstant)702 TEST_F(Cursor, PointerToConstantIsReferencingConstant)
703 {
704     auto callExpressionCursor = translationUnit.cursorAt(126, 20);
705 
706     auto argument = callExpressionCursor.type().argument(0);
707 
708     ASSERT_TRUE(argument.isReferencingConstant());
709 }
710 
TEST_F(Cursor,ConstantReferenceIsReferencingConstant)711 TEST_F(Cursor, ConstantReferenceIsReferencingConstant)
712 {
713     auto callExpressionCursor = translationUnit.cursorAt(125, 26);
714 
715     auto argument = callExpressionCursor.type().argument(0);
716 
717     ASSERT_TRUE(argument.isReferencingConstant());
718 }
719 
TEST_F(Cursor,LValueReferenceIsNotReferencingConstant)720 TEST_F(Cursor, LValueReferenceIsNotReferencingConstant)
721 {
722     auto callExpressionCursor = translationUnit.cursorAt(124, 21);
723 
724     auto argument = callExpressionCursor.type().argument(0);
725 
726     ASSERT_FALSE(argument.isReferencingConstant());
727 }
728 
TEST_F(Cursor,ValueIsNotReferencingConstant)729 TEST_F(Cursor, ValueIsNotReferencingConstant)
730 {
731     auto callExpressionCursor = translationUnit.cursorAt(123, 18);
732 
733     auto argument = callExpressionCursor.type().argument(0);
734 
735     ASSERT_FALSE(argument.isReferencingConstant());
736 }
737 
TEST_F(Cursor,PointerIsNotRefencingConstant)738 TEST_F(Cursor, PointerIsNotRefencingConstant)
739 {
740     auto callExpressionCursor = translationUnit.cursorAt(127, 13);
741 
742     auto argument = callExpressionCursor.type().argument(0);
743 
744     ASSERT_FALSE(argument.isReferencingConstant());
745 }
746 
TEST_F(Cursor,PointerIsOutputArgument)747 TEST_F(Cursor, PointerIsOutputArgument)
748 {
749     auto callExpressionCursor = translationUnit.cursorAt(127, 13);
750 
751     auto argument = callExpressionCursor.type().argument(0);
752 
753     ASSERT_TRUE(argument.isOutputArgument());
754 }
755 
TEST_F(Cursor,ConstantReferenceIsNotOutputArgument)756 TEST_F(Cursor, ConstantReferenceIsNotOutputArgument)
757 {
758     auto callExpressionCursor = translationUnit.cursorAt(125, 26);
759 
760     auto argument = callExpressionCursor.type().argument(0);
761 
762     ASSERT_FALSE(argument.isOutputArgument());
763 }
764 
TEST_F(Cursor,PointerToConstantIsNotOutputArgument)765 TEST_F(Cursor, PointerToConstantIsNotOutputArgument)
766 {
767     auto callExpressionCursor = translationUnit.cursorAt(126, 20);
768 
769     auto argument = callExpressionCursor.type().argument(0);
770 
771     ASSERT_FALSE(argument.isOutputArgument()) << argument.isConstant() << argument.pointeeType().isConstant();
772 }
773 
TEST_F(Cursor,ConstantPointerIsOutputArgument)774 TEST_F(Cursor, ConstantPointerIsOutputArgument)
775 {
776     auto callExpressionCursor = translationUnit.cursorAt(128, 21);
777 
778     auto argument = callExpressionCursor.type().argument(0);
779 
780     ASSERT_TRUE(argument.isOutputArgument());
781 }
782 
TEST_F(Cursor,ReferenceIsOutputArgument)783 TEST_F(Cursor, ReferenceIsOutputArgument)
784 {
785     auto callExpressionCursor = translationUnit.cursorAt(124, 21);
786 
787     auto argument = callExpressionCursor.type().argument(0);
788 
789     ASSERT_TRUE(argument.isOutputArgument());
790 }
791 
TEST_F(Cursor,ConstReferenceIsNotOutputArgument)792 TEST_F(Cursor, ConstReferenceIsNotOutputArgument)
793 {
794     auto callExpressionCursor = translationUnit.cursorAt(125, 26);
795 
796     auto argument = callExpressionCursor.type().argument(0);
797 
798     ASSERT_FALSE(argument.isOutputArgument());
799 }
800 
TEST_F(Cursor,ResultType)801 TEST_F(Cursor, ResultType)
802 {
803     auto methodCursor = translationUnit.cursorAt(31, 18);
804 
805     Utf8String resultType = methodCursor.type().resultType().spelling();
806 
807     ASSERT_THAT(resultType, Utf8String("bool", 4));
808 }
809 
TEST_F(Cursor,PrivateMethodAccessSpecifier)810 TEST_F(Cursor, PrivateMethodAccessSpecifier)
811 {
812     auto methodCursor = translationUnit.cursorAt(16, 17);
813 
814     auto accessSpecifier = methodCursor.accessSpecifier();
815 
816     ASSERT_THAT(accessSpecifier, ClangBackEnd::AccessSpecifier::Private);
817 }
818 
TEST_F(Cursor,PublicMethodAccessSpecifier)819 TEST_F(Cursor, PublicMethodAccessSpecifier)
820 {
821     auto methodCursor = translationUnit.cursorAt(79, 25);
822 
823     auto accessSpecifier = methodCursor.accessSpecifier();
824 
825     ASSERT_THAT(accessSpecifier, ClangBackEnd::AccessSpecifier::Public);
826 }
827 
TEST_F(Cursor,ProtectedMethodAccessSpecifier)828 TEST_F(Cursor, ProtectedMethodAccessSpecifier)
829 {
830     auto methodCursor = translationUnit.cursorAt(131, 22);
831 
832     auto accessSpecifier = methodCursor.accessSpecifier();
833 
834     ASSERT_THAT(accessSpecifier, ClangBackEnd::AccessSpecifier::Protected);
835 }
836 
TEST_F(Cursor,PrivateFieldAccessSpecifier)837 TEST_F(Cursor, PrivateFieldAccessSpecifier)
838 {
839     auto fieldCursor = translationUnit.cursorAt(21, 12);
840 
841     auto accessSpecifier = fieldCursor.accessSpecifier();
842 
843     ASSERT_THAT(accessSpecifier, ClangBackEnd::AccessSpecifier::Private);
844 }
845 
TEST_F(Cursor,InvalidAccessSpecifier)846 TEST_F(Cursor, InvalidAccessSpecifier)
847 {
848     auto localVarCursor = translationUnit.cursorAt(62, 9);
849 
850     auto accessSpecifier = localVarCursor.accessSpecifier();
851 
852     ASSERT_THAT(accessSpecifier, ClangBackEnd::AccessSpecifier::Invalid);
853 }
854 
TEST_F(Cursor,NoStorageClass)855 TEST_F(Cursor, NoStorageClass)
856 {
857     auto localVarCursor = translationUnit.cursorAt(62, 9);
858 
859     auto storageClass = localVarCursor.storageClass();
860 
861     ASSERT_THAT(storageClass, ClangBackEnd::StorageClass::None);
862 }
863 
TEST_F(Cursor,ExternVarStorageClass)864 TEST_F(Cursor, ExternVarStorageClass)
865 {
866     auto externalVarCursor = translationUnit.cursorAt(133, 12);
867 
868     auto storageClass = externalVarCursor.storageClass();
869 
870     ASSERT_THAT(storageClass, ClangBackEnd::StorageClass::Extern);
871 }
872 
TEST_F(Cursor,StaticMethodStorageClass)873 TEST_F(Cursor, StaticMethodStorageClass)
874 {
875     auto methodCursor = translationUnit.cursorAt(135, 13);
876 
877     auto storageClass = methodCursor.storageClass();
878 
879     ASSERT_THAT(storageClass, ClangBackEnd::StorageClass::Static);
880 }
881 
TEST_F(Cursor,InvalidStorageClass)882 TEST_F(Cursor, InvalidStorageClass)
883 {
884     auto functionTemplateCursor = translationUnit.cursorAt(137, 28);
885 
886     auto storageClass = functionTemplateCursor.storageClass();
887 
888     ASSERT_THAT(storageClass, ClangBackEnd::StorageClass::Invalid);
889 }
890 
TEST_F(Cursor,IsAnonymousNamespace)891 TEST_F(Cursor, IsAnonymousNamespace)
892 {
893     auto anonymousCursor = translationUnit.cursorAt(140, 1);
894 
895     bool anonymous = anonymousCursor.isAnonymous();
896 
897     ASSERT_THAT(anonymous, true);
898 }
899 
TEST_F(Cursor,IsNotAnonymousNamespace)900 TEST_F(Cursor, IsNotAnonymousNamespace)
901 {
902     auto anonymousCursor = translationUnit.cursorAt(139, 1);
903 
904     bool anonymous = anonymousCursor.isAnonymous();
905 
906     ASSERT_THAT(anonymous, false);
907 }
908 
TEST_F(Cursor,AnonymousNamespaceDisplayName)909 TEST_F(Cursor, AnonymousNamespaceDisplayName)
910 {
911     auto anonymousCursor = translationUnit.cursorAt(140, 1);
912 
913     auto name = anonymousCursor.displayName();
914 
915     ASSERT_THAT(name, Utf8String("(anonymous)"));
916 }
917 
TEST_F(Cursor,AnonymousEnumDisplayName)918 TEST_F(Cursor, AnonymousEnumDisplayName)
919 {
920     auto anonymousCursor = translationUnit.cursorAt(144, 1);
921 
922     auto name = anonymousCursor.displayName();
923 
924     ASSERT_THAT(name, Utf8String("(anonymous)"));
925 }
926 
927 Data *Cursor::d;
928 
SetUpTestCase()929 void Cursor::SetUpTestCase()
930 {
931     d = new Data;
932     d->document.parse();
933 }
934 
TearDownTestCase()935 void Cursor::TearDownTestCase()
936 {
937     delete d;
938     d = nullptr;
939 }
940 
941 }
942