blob: 7258a7ef576c6777e19d695eb7f5438b017ece27 [file] [log] [blame]
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001//===- unittests/AST/CommentParser.cpp ------ Comment parser tests --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/Basic/SourceManager.h"
11#include "clang/Basic/FileManager.h"
12#include "clang/Basic/Diagnostic.h"
13#include "clang/AST/Comment.h"
14#include "clang/AST/CommentLexer.h"
15#include "clang/AST/CommentParser.h"
16#include "clang/AST/CommentSema.h"
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000017#include "clang/AST/CommentCommandTraits.h"
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000018#include "llvm/ADT/STLExtras.h"
19#include "llvm/Support/Allocator.h"
20#include <vector>
21
22#include "gtest/gtest.h"
23
24using namespace llvm;
25using namespace clang;
26
27namespace clang {
28namespace comments {
29
30namespace {
31
32const bool DEBUG = true;
33
34class CommentParserTest : public ::testing::Test {
35protected:
36 CommentParserTest()
37 : FileMgr(FileMgrOpts),
38 DiagID(new DiagnosticIDs()),
39 Diags(DiagID, new IgnoringDiagConsumer()),
40 SourceMgr(Diags, FileMgr) {
41 }
42
43 FileSystemOptions FileMgrOpts;
44 FileManager FileMgr;
45 IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
46 DiagnosticsEngine Diags;
47 SourceManager SourceMgr;
48 llvm::BumpPtrAllocator Allocator;
49
50 FullComment *parseString(const char *Source);
51};
52
53FullComment *CommentParserTest::parseString(const char *Source) {
54 MemoryBuffer *Buf = MemoryBuffer::getMemBuffer(Source);
55 FileID File = SourceMgr.createFileIDForMemBuffer(Buf);
56 SourceLocation Begin = SourceMgr.getLocForStartOfFile(File);
57
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000058 comments::CommandTraits Traits;
59 comments::Lexer L(Allocator, Traits, Begin, CommentOptions(),
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000060 Source, Source + strlen(Source));
61
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000062 comments::Sema S(Allocator, SourceMgr, Diags, Traits);
63 comments::Parser P(L, S, Allocator, SourceMgr, Diags, Traits);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +000064 comments::FullComment *FC = P.parseFullComment();
65
66 if (DEBUG) {
67 llvm::errs() << "=== Source:\n" << Source << "\n=== AST:\n";
68 FC->dump(SourceMgr);
69 }
70
71 Token Tok;
72 L.lex(Tok);
73 if (Tok.is(tok::eof))
74 return FC;
75 else
76 return NULL;
77}
78
79::testing::AssertionResult HasChildCount(const Comment *C, size_t Count) {
80 if (!C)
81 return ::testing::AssertionFailure() << "Comment is NULL";
82
83 if (Count != C->child_count())
84 return ::testing::AssertionFailure()
85 << "Count = " << Count
86 << ", child_count = " << C->child_count();
87
88 return ::testing::AssertionSuccess();
89}
90
91template <typename T>
92::testing::AssertionResult GetChildAt(const Comment *C,
93 size_t Idx,
94 T *&Child) {
95 if (!C)
96 return ::testing::AssertionFailure() << "Comment is NULL";
97
98 if (Idx >= C->child_count())
99 return ::testing::AssertionFailure()
100 << "Idx out of range. Idx = " << Idx
101 << ", child_count = " << C->child_count();
102
103 Comment::child_iterator I = C->child_begin() + Idx;
104 Comment *CommentChild = *I;
105 if (!CommentChild)
106 return ::testing::AssertionFailure() << "Child is NULL";
107
108 Child = dyn_cast<T>(CommentChild);
109 if (!Child)
110 return ::testing::AssertionFailure()
111 << "Child is not of requested type, but a "
112 << CommentChild->getCommentKindName();
113
114 return ::testing::AssertionSuccess();
115}
116
117::testing::AssertionResult HasTextAt(const Comment *C,
118 size_t Idx,
119 StringRef Text) {
120 TextComment *TC;
121 ::testing::AssertionResult AR = GetChildAt(C, Idx, TC);
122 if (!AR)
123 return AR;
124
125 StringRef ActualText = TC->getText();
126 if (ActualText != Text)
127 return ::testing::AssertionFailure()
128 << "TextComment has text \"" << ActualText.str() << "\", "
129 "expected \"" << Text.str() << "\"";
130
131 if (TC->hasTrailingNewline())
132 return ::testing::AssertionFailure()
133 << "TextComment has a trailing newline";
134
135 return ::testing::AssertionSuccess();
136}
137
138::testing::AssertionResult HasTextWithNewlineAt(const Comment *C,
139 size_t Idx,
140 StringRef Text) {
141 TextComment *TC;
142 ::testing::AssertionResult AR = GetChildAt(C, Idx, TC);
143 if (!AR)
144 return AR;
145
146 StringRef ActualText = TC->getText();
147 if (ActualText != Text)
148 return ::testing::AssertionFailure()
149 << "TextComment has text \"" << ActualText.str() << "\", "
150 "expected \"" << Text.str() << "\"";
151
152 if (!TC->hasTrailingNewline())
153 return ::testing::AssertionFailure()
154 << "TextComment has no trailing newline";
155
156 return ::testing::AssertionSuccess();
157}
158
159::testing::AssertionResult HasBlockCommandAt(const Comment *C,
160 size_t Idx,
161 BlockCommandComment *&BCC,
162 StringRef Name,
163 ParagraphComment *&Paragraph) {
164 ::testing::AssertionResult AR = GetChildAt(C, Idx, BCC);
165 if (!AR)
166 return AR;
167
168 StringRef ActualName = BCC->getCommandName();
169 if (ActualName != Name)
170 return ::testing::AssertionFailure()
171 << "BlockCommandComment has name \"" << ActualName.str() << "\", "
172 "expected \"" << Name.str() << "\"";
173
174 Paragraph = BCC->getParagraph();
175
176 return ::testing::AssertionSuccess();
177}
178
179::testing::AssertionResult HasParamCommandAt(
180 const Comment *C,
181 size_t Idx,
182 ParamCommandComment *&PCC,
183 StringRef CommandName,
184 ParamCommandComment::PassDirection Direction,
185 bool IsDirectionExplicit,
186 StringRef ParamName,
187 ParagraphComment *&Paragraph) {
188 ::testing::AssertionResult AR = GetChildAt(C, Idx, PCC);
189 if (!AR)
190 return AR;
191
192 StringRef ActualCommandName = PCC->getCommandName();
193 if (ActualCommandName != CommandName)
194 return ::testing::AssertionFailure()
195 << "ParamCommandComment has name \"" << ActualCommandName.str() << "\", "
196 "expected \"" << CommandName.str() << "\"";
197
198 if (PCC->getDirection() != Direction)
199 return ::testing::AssertionFailure()
200 << "ParamCommandComment has direction " << PCC->getDirection() << ", "
201 "expected " << Direction;
202
203 if (PCC->isDirectionExplicit() != IsDirectionExplicit)
204 return ::testing::AssertionFailure()
205 << "ParamCommandComment has "
206 << (PCC->isDirectionExplicit() ? "explicit" : "implicit")
207 << " direction, "
208 "expected " << (IsDirectionExplicit ? "explicit" : "implicit");
209
Dmitri Gribenko8a903932012-08-06 23:48:44 +0000210 if (!ParamName.empty() && !PCC->hasParamName())
Dmitri Gribenko0c43a922012-07-24 18:23:31 +0000211 return ::testing::AssertionFailure()
212 << "ParamCommandComment has no parameter name";
213
Dmitri Gribenko8a903932012-08-06 23:48:44 +0000214 StringRef ActualParamName = PCC->hasParamName() ? PCC->getParamName() : "";
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000215 if (ActualParamName != ParamName)
216 return ::testing::AssertionFailure()
Dmitri Gribenko0c43a922012-07-24 18:23:31 +0000217 << "ParamCommandComment has parameter name \"" << ActualParamName.str()
218 << "\", "
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000219 "expected \"" << ParamName.str() << "\"";
220
221 Paragraph = PCC->getParagraph();
222
223 return ::testing::AssertionSuccess();
224}
225
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000226::testing::AssertionResult HasTParamCommandAt(
227 const Comment *C,
228 size_t Idx,
229 TParamCommandComment *&TPCC,
230 StringRef CommandName,
231 StringRef ParamName,
232 ParagraphComment *&Paragraph) {
233 ::testing::AssertionResult AR = GetChildAt(C, Idx, TPCC);
234 if (!AR)
235 return AR;
236
237 StringRef ActualCommandName = TPCC->getCommandName();
238 if (ActualCommandName != CommandName)
239 return ::testing::AssertionFailure()
240 << "TParamCommandComment has name \"" << ActualCommandName.str() << "\", "
241 "expected \"" << CommandName.str() << "\"";
242
Dmitri Gribenko8a903932012-08-06 23:48:44 +0000243 if (!ParamName.empty() && !TPCC->hasParamName())
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000244 return ::testing::AssertionFailure()
245 << "TParamCommandComment has no parameter name";
246
Dmitri Gribenko8a903932012-08-06 23:48:44 +0000247 StringRef ActualParamName = TPCC->hasParamName() ? TPCC->getParamName() : "";
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000248 if (ActualParamName != ParamName)
249 return ::testing::AssertionFailure()
250 << "TParamCommandComment has parameter name \"" << ActualParamName.str()
251 << "\", "
252 "expected \"" << ParamName.str() << "\"";
253
254 Paragraph = TPCC->getParagraph();
255
256 return ::testing::AssertionSuccess();
257}
258
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000259::testing::AssertionResult HasInlineCommandAt(const Comment *C,
260 size_t Idx,
261 InlineCommandComment *&ICC,
262 StringRef Name) {
263 ::testing::AssertionResult AR = GetChildAt(C, Idx, ICC);
264 if (!AR)
265 return AR;
266
267 StringRef ActualName = ICC->getCommandName();
268 if (ActualName != Name)
269 return ::testing::AssertionFailure()
270 << "InlineCommandComment has name \"" << ActualName.str() << "\", "
271 "expected \"" << Name.str() << "\"";
272
273 return ::testing::AssertionSuccess();
274}
275
276struct NoArgs {};
277
278::testing::AssertionResult HasInlineCommandAt(const Comment *C,
279 size_t Idx,
280 InlineCommandComment *&ICC,
281 StringRef Name,
282 NoArgs) {
283 ::testing::AssertionResult AR = HasInlineCommandAt(C, Idx, ICC, Name);
284 if (!AR)
285 return AR;
286
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000287 if (ICC->getNumArgs() != 0)
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000288 return ::testing::AssertionFailure()
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000289 << "InlineCommandComment has " << ICC->getNumArgs() << " arg(s), "
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000290 "expected 0";
291
292 return ::testing::AssertionSuccess();
293}
294
295::testing::AssertionResult HasInlineCommandAt(const Comment *C,
296 size_t Idx,
297 InlineCommandComment *&ICC,
298 StringRef Name,
299 StringRef Arg) {
300 ::testing::AssertionResult AR = HasInlineCommandAt(C, Idx, ICC, Name);
301 if (!AR)
302 return AR;
303
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000304 if (ICC->getNumArgs() != 1)
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000305 return ::testing::AssertionFailure()
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000306 << "InlineCommandComment has " << ICC->getNumArgs() << " arg(s), "
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000307 "expected 1";
308
309 StringRef ActualArg = ICC->getArgText(0);
310 if (ActualArg != Arg)
311 return ::testing::AssertionFailure()
312 << "InlineCommandComment has argument \"" << ActualArg.str() << "\", "
313 "expected \"" << Arg.str() << "\"";
314
315 return ::testing::AssertionSuccess();
316}
317
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000318::testing::AssertionResult HasHTMLStartTagAt(const Comment *C,
319 size_t Idx,
320 HTMLStartTagComment *&HST,
321 StringRef TagName) {
322 ::testing::AssertionResult AR = GetChildAt(C, Idx, HST);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000323 if (!AR)
324 return AR;
325
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000326 StringRef ActualTagName = HST->getTagName();
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000327 if (ActualTagName != TagName)
328 return ::testing::AssertionFailure()
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000329 << "HTMLStartTagComment has name \"" << ActualTagName.str() << "\", "
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000330 "expected \"" << TagName.str() << "\"";
331
332 return ::testing::AssertionSuccess();
333}
334
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000335struct SelfClosing {};
336
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000337::testing::AssertionResult HasHTMLStartTagAt(const Comment *C,
338 size_t Idx,
339 HTMLStartTagComment *&HST,
340 StringRef TagName,
341 SelfClosing) {
342 ::testing::AssertionResult AR = HasHTMLStartTagAt(C, Idx, HST, TagName);
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000343 if (!AR)
344 return AR;
345
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000346 if (!HST->isSelfClosing())
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000347 return ::testing::AssertionFailure()
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000348 << "HTMLStartTagComment is not self-closing";
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000349
350 return ::testing::AssertionSuccess();
351}
352
353
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000354struct NoAttrs {};
355
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000356::testing::AssertionResult HasHTMLStartTagAt(const Comment *C,
357 size_t Idx,
358 HTMLStartTagComment *&HST,
359 StringRef TagName,
360 NoAttrs) {
361 ::testing::AssertionResult AR = HasHTMLStartTagAt(C, Idx, HST, TagName);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000362 if (!AR)
363 return AR;
364
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000365 if (HST->isSelfClosing())
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000366 return ::testing::AssertionFailure()
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000367 << "HTMLStartTagComment is self-closing";
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000368
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000369 if (HST->getNumAttrs() != 0)
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000370 return ::testing::AssertionFailure()
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000371 << "HTMLStartTagComment has " << HST->getNumAttrs() << " attr(s), "
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000372 "expected 0";
373
374 return ::testing::AssertionSuccess();
375}
376
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000377::testing::AssertionResult HasHTMLStartTagAt(const Comment *C,
378 size_t Idx,
379 HTMLStartTagComment *&HST,
380 StringRef TagName,
381 StringRef AttrName,
382 StringRef AttrValue) {
383 ::testing::AssertionResult AR = HasHTMLStartTagAt(C, Idx, HST, TagName);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000384 if (!AR)
385 return AR;
386
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000387 if (HST->isSelfClosing())
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000388 return ::testing::AssertionFailure()
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000389 << "HTMLStartTagComment is self-closing";
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000390
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000391 if (HST->getNumAttrs() != 1)
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000392 return ::testing::AssertionFailure()
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000393 << "HTMLStartTagComment has " << HST->getNumAttrs() << " attr(s), "
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000394 "expected 1";
395
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000396 StringRef ActualName = HST->getAttr(0).Name;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000397 if (ActualName != AttrName)
398 return ::testing::AssertionFailure()
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000399 << "HTMLStartTagComment has attr \"" << ActualName.str() << "\", "
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000400 "expected \"" << AttrName.str() << "\"";
401
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000402 StringRef ActualValue = HST->getAttr(0).Value;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000403 if (ActualValue != AttrValue)
404 return ::testing::AssertionFailure()
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000405 << "HTMLStartTagComment has attr value \"" << ActualValue.str() << "\", "
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000406 "expected \"" << AttrValue.str() << "\"";
407
408 return ::testing::AssertionSuccess();
409}
410
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000411::testing::AssertionResult HasHTMLEndTagAt(const Comment *C,
412 size_t Idx,
413 HTMLEndTagComment *&HET,
414 StringRef TagName) {
415 ::testing::AssertionResult AR = GetChildAt(C, Idx, HET);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000416 if (!AR)
417 return AR;
418
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000419 StringRef ActualTagName = HET->getTagName();
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000420 if (ActualTagName != TagName)
421 return ::testing::AssertionFailure()
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000422 << "HTMLEndTagComment has name \"" << ActualTagName.str() << "\", "
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000423 "expected \"" << TagName.str() << "\"";
424
425 return ::testing::AssertionSuccess();
426}
427
Dmitri Gribenkodebd16f2012-07-23 23:09:32 +0000428::testing::AssertionResult HasParagraphCommentAt(const Comment *C,
429 size_t Idx,
430 StringRef Text) {
431 ParagraphComment *PC;
432
433 {
434 ::testing::AssertionResult AR = GetChildAt(C, Idx, PC);
435 if (!AR)
436 return AR;
437 }
438
439 {
440 ::testing::AssertionResult AR = HasChildCount(PC, 1);
441 if (!AR)
442 return AR;
443 }
444
445 {
446 ::testing::AssertionResult AR = HasTextAt(PC, 0, Text);
447 if (!AR)
448 return AR;
449 }
450
451 return ::testing::AssertionSuccess();
452}
453
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000454::testing::AssertionResult HasVerbatimBlockAt(const Comment *C,
455 size_t Idx,
456 VerbatimBlockComment *&VBC,
Dmitri Gribenko9f08f492012-07-20 20:18:53 +0000457 StringRef Name,
458 StringRef CloseName) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000459 ::testing::AssertionResult AR = GetChildAt(C, Idx, VBC);
460 if (!AR)
461 return AR;
462
463 StringRef ActualName = VBC->getCommandName();
464 if (ActualName != Name)
465 return ::testing::AssertionFailure()
466 << "VerbatimBlockComment has name \"" << ActualName.str() << "\", "
467 "expected \"" << Name.str() << "\"";
468
Dmitri Gribenko9f08f492012-07-20 20:18:53 +0000469 StringRef ActualCloseName = VBC->getCloseName();
470 if (ActualCloseName != CloseName)
471 return ::testing::AssertionFailure()
472 << "VerbatimBlockComment has closing command name \""
473 << ActualCloseName.str() << "\", "
474 "expected \"" << CloseName.str() << "\"";
475
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000476 return ::testing::AssertionSuccess();
477}
478
479struct NoLines {};
Dmitri Gribenko9f08f492012-07-20 20:18:53 +0000480struct Lines {};
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000481
482::testing::AssertionResult HasVerbatimBlockAt(const Comment *C,
483 size_t Idx,
484 VerbatimBlockComment *&VBC,
485 StringRef Name,
Dmitri Gribenko9f08f492012-07-20 20:18:53 +0000486 StringRef CloseName,
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000487 NoLines) {
Dmitri Gribenko9f08f492012-07-20 20:18:53 +0000488 ::testing::AssertionResult AR = HasVerbatimBlockAt(C, Idx, VBC, Name,
489 CloseName);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000490 if (!AR)
491 return AR;
492
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000493 if (VBC->getNumLines() != 0)
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000494 return ::testing::AssertionFailure()
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000495 << "VerbatimBlockComment has " << VBC->getNumLines() << " lines(s), "
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000496 "expected 0";
497
498 return ::testing::AssertionSuccess();
499}
500
501::testing::AssertionResult HasVerbatimBlockAt(const Comment *C,
502 size_t Idx,
503 VerbatimBlockComment *&VBC,
504 StringRef Name,
Dmitri Gribenko9f08f492012-07-20 20:18:53 +0000505 StringRef CloseName,
506 Lines,
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000507 StringRef Line0) {
Dmitri Gribenko9f08f492012-07-20 20:18:53 +0000508 ::testing::AssertionResult AR = HasVerbatimBlockAt(C, Idx, VBC, Name,
509 CloseName);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000510 if (!AR)
511 return AR;
512
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000513 if (VBC->getNumLines() != 1)
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000514 return ::testing::AssertionFailure()
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000515 << "VerbatimBlockComment has " << VBC->getNumLines() << " lines(s), "
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000516 "expected 1";
517
518 StringRef ActualLine0 = VBC->getText(0);
519 if (ActualLine0 != Line0)
520 return ::testing::AssertionFailure()
521 << "VerbatimBlockComment has lines[0] \"" << ActualLine0.str() << "\", "
522 "expected \"" << Line0.str() << "\"";
523
524 return ::testing::AssertionSuccess();
525}
526
527::testing::AssertionResult HasVerbatimBlockAt(const Comment *C,
528 size_t Idx,
529 VerbatimBlockComment *&VBC,
530 StringRef Name,
Dmitri Gribenko9f08f492012-07-20 20:18:53 +0000531 StringRef CloseName,
532 Lines,
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000533 StringRef Line0,
534 StringRef Line1) {
Dmitri Gribenko9f08f492012-07-20 20:18:53 +0000535 ::testing::AssertionResult AR = HasVerbatimBlockAt(C, Idx, VBC, Name,
536 CloseName);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000537 if (!AR)
538 return AR;
539
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000540 if (VBC->getNumLines() != 2)
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000541 return ::testing::AssertionFailure()
Dmitri Gribenko0eaf69d2012-07-13 19:02:42 +0000542 << "VerbatimBlockComment has " << VBC->getNumLines() << " lines(s), "
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000543 "expected 2";
544
545 StringRef ActualLine0 = VBC->getText(0);
546 if (ActualLine0 != Line0)
547 return ::testing::AssertionFailure()
548 << "VerbatimBlockComment has lines[0] \"" << ActualLine0.str() << "\", "
549 "expected \"" << Line0.str() << "\"";
550
551 StringRef ActualLine1 = VBC->getText(1);
552 if (ActualLine1 != Line1)
553 return ::testing::AssertionFailure()
554 << "VerbatimBlockComment has lines[1] \"" << ActualLine1.str() << "\", "
555 "expected \"" << Line1.str() << "\"";
556
557 return ::testing::AssertionSuccess();
558}
559
560::testing::AssertionResult HasVerbatimLineAt(const Comment *C,
561 size_t Idx,
562 VerbatimLineComment *&VLC,
563 StringRef Name,
564 StringRef Text) {
565 ::testing::AssertionResult AR = GetChildAt(C, Idx, VLC);
566 if (!AR)
567 return AR;
568
569 StringRef ActualName = VLC->getCommandName();
570 if (ActualName != Name)
571 return ::testing::AssertionFailure()
572 << "VerbatimLineComment has name \"" << ActualName.str() << "\", "
573 "expected \"" << Name.str() << "\"";
574
575 StringRef ActualText = VLC->getText();
576 if (ActualText != Text)
577 return ::testing::AssertionFailure()
578 << "VerbatimLineComment has text \"" << ActualText.str() << "\", "
579 "expected \"" << Text.str() << "\"";
580
581 return ::testing::AssertionSuccess();
582}
583
584
585TEST_F(CommentParserTest, Basic1) {
586 const char *Source = "//";
587
588 FullComment *FC = parseString(Source);
589 ASSERT_TRUE(HasChildCount(FC, 0));
590}
591
592TEST_F(CommentParserTest, Basic2) {
593 const char *Source = "// Meow";
594
595 FullComment *FC = parseString(Source);
596 ASSERT_TRUE(HasChildCount(FC, 1));
597
Dmitri Gribenkodebd16f2012-07-23 23:09:32 +0000598 ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " Meow"));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000599}
600
601TEST_F(CommentParserTest, Basic3) {
602 const char *Source =
603 "// Aaa\n"
604 "// Bbb";
605
606 FullComment *FC = parseString(Source);
607 ASSERT_TRUE(HasChildCount(FC, 1));
608
609 {
610 ParagraphComment *PC;
611 ASSERT_TRUE(GetChildAt(FC, 0, PC));
612
613 ASSERT_TRUE(HasChildCount(PC, 2));
614 ASSERT_TRUE(HasTextWithNewlineAt(PC, 0, " Aaa"));
615 ASSERT_TRUE(HasTextAt(PC, 1, " Bbb"));
616 }
617}
618
619TEST_F(CommentParserTest, Paragraph1) {
620 const char *Sources[] = {
621 "// Aaa\n"
622 "//\n"
623 "// Bbb",
624
625 "// Aaa\n"
626 "//\n"
627 "//\n"
628 "// Bbb",
629 };
630
631
632 for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
633 FullComment *FC = parseString(Sources[i]);
634 ASSERT_TRUE(HasChildCount(FC, 2));
635
Dmitri Gribenkodebd16f2012-07-23 23:09:32 +0000636 ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " Aaa"));
637 ASSERT_TRUE(HasParagraphCommentAt(FC, 1, " Bbb"));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000638 }
639}
640
641TEST_F(CommentParserTest, Paragraph2) {
642 const char *Source =
643 "// \\brief Aaa\n"
644 "//\n"
645 "// Bbb";
646
647 FullComment *FC = parseString(Source);
648 ASSERT_TRUE(HasChildCount(FC, 3));
649
Dmitri Gribenkodebd16f2012-07-23 23:09:32 +0000650 ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000651 {
652 BlockCommandComment *BCC;
653 ParagraphComment *PC;
654 ASSERT_TRUE(HasBlockCommandAt(FC, 1, BCC, "brief", PC));
655
Dmitri Gribenkodebd16f2012-07-23 23:09:32 +0000656 ASSERT_TRUE(HasParagraphCommentAt(BCC, 0, " Aaa"));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000657 }
Dmitri Gribenkodebd16f2012-07-23 23:09:32 +0000658 ASSERT_TRUE(HasParagraphCommentAt(FC, 2, " Bbb"));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000659}
660
661TEST_F(CommentParserTest, Paragraph3) {
662 const char *Source = "// \\brief \\author";
663
664 FullComment *FC = parseString(Source);
665 ASSERT_TRUE(HasChildCount(FC, 3));
666
Dmitri Gribenkodebd16f2012-07-23 23:09:32 +0000667 ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000668 {
669 BlockCommandComment *BCC;
670 ParagraphComment *PC;
671 ASSERT_TRUE(HasBlockCommandAt(FC, 1, BCC, "brief", PC));
672
Dmitri Gribenkodebd16f2012-07-23 23:09:32 +0000673 ASSERT_TRUE(HasParagraphCommentAt(BCC, 0, " "));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000674 }
675 {
676 BlockCommandComment *BCC;
677 ParagraphComment *PC;
678 ASSERT_TRUE(HasBlockCommandAt(FC, 2, BCC, "author", PC));
679
680 ASSERT_TRUE(GetChildAt(BCC, 0, PC));
681 ASSERT_TRUE(HasChildCount(PC, 0));
682 }
683}
684
685TEST_F(CommentParserTest, Paragraph4) {
686 const char *Source =
687 "// \\brief Aaa\n"
688 "// Bbb \\author\n"
689 "// Ccc";
690
691 FullComment *FC = parseString(Source);
692 ASSERT_TRUE(HasChildCount(FC, 3));
693
Dmitri Gribenkodebd16f2012-07-23 23:09:32 +0000694 ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000695 {
696 BlockCommandComment *BCC;
697 ParagraphComment *PC;
698 ASSERT_TRUE(HasBlockCommandAt(FC, 1, BCC, "brief", PC));
699
700 ASSERT_TRUE(GetChildAt(BCC, 0, PC));
701 ASSERT_TRUE(HasChildCount(PC, 2));
702 ASSERT_TRUE(HasTextWithNewlineAt(PC, 0, " Aaa"));
703 ASSERT_TRUE(HasTextAt(PC, 1, " Bbb "));
704 }
705 {
706 BlockCommandComment *BCC;
707 ParagraphComment *PC;
708 ASSERT_TRUE(HasBlockCommandAt(FC, 2, BCC, "author", PC));
709
Dmitri Gribenkodebd16f2012-07-23 23:09:32 +0000710 ASSERT_TRUE(HasParagraphCommentAt(BCC, 0, " Ccc"));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000711 }
712}
713
714TEST_F(CommentParserTest, ParamCommand1) {
Dmitri Gribenko3ccc1732012-07-30 16:52:51 +0000715 const char *Source = "// \\param aaa";
716
717 FullComment *FC = parseString(Source);
718 ASSERT_TRUE(HasChildCount(FC, 2));
719
720 ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
721 {
722 ParamCommandComment *PCC;
723 ParagraphComment *PC;
724 ASSERT_TRUE(HasParamCommandAt(FC, 1, PCC, "param",
725 ParamCommandComment::In,
726 /* IsDirectionExplicit = */ false,
727 "aaa", PC));
728 ASSERT_TRUE(HasChildCount(PCC, 1));
729 ASSERT_TRUE(HasChildCount(PC, 0));
730 }
731}
732
733TEST_F(CommentParserTest, ParamCommand2) {
Dmitri Gribenko8a903932012-08-06 23:48:44 +0000734 const char *Source = "// \\param\\brief";
735
736 FullComment *FC = parseString(Source);
737 ASSERT_TRUE(HasChildCount(FC, 3));
738
739 ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
740 {
741 ParamCommandComment *PCC;
742 ParagraphComment *PC;
743 ASSERT_TRUE(HasParamCommandAt(FC, 1, PCC, "param",
744 ParamCommandComment::In,
745 /* IsDirectionExplicit = */ false,
746 "", PC));
747 ASSERT_TRUE(HasChildCount(PCC, 1));
748 ASSERT_TRUE(HasChildCount(PC, 0));
749 }
750 {
751 BlockCommandComment *BCC;
752 ParagraphComment *PC;
753 ASSERT_TRUE(HasBlockCommandAt(FC, 2, BCC, "brief", PC));
754 ASSERT_TRUE(HasChildCount(PC, 0));
755 }
756}
757
758TEST_F(CommentParserTest, ParamCommand3) {
Dmitri Gribenko0c43a922012-07-24 18:23:31 +0000759 const char *Sources[] = {
760 "// \\param aaa Bbb\n",
761 "// \\param\n"
762 "// aaa Bbb\n",
763 "// \\param \n"
764 "// aaa Bbb\n",
765 "// \\param aaa\n"
766 "// Bbb\n"
767 };
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000768
Dmitri Gribenko0c43a922012-07-24 18:23:31 +0000769 for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
770 FullComment *FC = parseString(Sources[i]);
771 ASSERT_TRUE(HasChildCount(FC, 2));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000772
Dmitri Gribenko0c43a922012-07-24 18:23:31 +0000773 ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
774 {
775 ParamCommandComment *PCC;
776 ParagraphComment *PC;
777 ASSERT_TRUE(HasParamCommandAt(FC, 1, PCC, "param",
778 ParamCommandComment::In,
779 /* IsDirectionExplicit = */ false,
780 "aaa", PC));
781 ASSERT_TRUE(HasChildCount(PCC, 1));
782 ASSERT_TRUE(HasParagraphCommentAt(PCC, 0, " Bbb"));
783 }
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000784 }
Dmitri Gribenkoe68c2292012-07-23 23:37:11 +0000785}
786
Dmitri Gribenko8a903932012-08-06 23:48:44 +0000787TEST_F(CommentParserTest, ParamCommand4) {
Dmitri Gribenko0c43a922012-07-24 18:23:31 +0000788 const char *Sources[] = {
789 "// \\param [in] aaa Bbb\n",
Dmitri Gribenkod076e012012-08-01 23:49:32 +0000790 "// \\param[in] aaa Bbb\n",
Dmitri Gribenko0c43a922012-07-24 18:23:31 +0000791 "// \\param\n"
792 "// [in] aaa Bbb\n",
793 "// \\param [in]\n"
794 "// aaa Bbb\n",
795 "// \\param [in] aaa\n"
796 "// Bbb\n",
797 };
Dmitri Gribenkoe68c2292012-07-23 23:37:11 +0000798
Dmitri Gribenko0c43a922012-07-24 18:23:31 +0000799 for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
800 FullComment *FC = parseString(Sources[i]);
801 ASSERT_TRUE(HasChildCount(FC, 2));
Dmitri Gribenkoe68c2292012-07-23 23:37:11 +0000802
Dmitri Gribenko0c43a922012-07-24 18:23:31 +0000803 ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
804 {
805 ParamCommandComment *PCC;
806 ParagraphComment *PC;
807 ASSERT_TRUE(HasParamCommandAt(FC, 1, PCC, "param",
808 ParamCommandComment::In,
809 /* IsDirectionExplicit = */ true,
810 "aaa", PC));
811 ASSERT_TRUE(HasChildCount(PCC, 1));
812 ASSERT_TRUE(HasParagraphCommentAt(PCC, 0, " Bbb"));
813 }
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000814 }
Dmitri Gribenkoe68c2292012-07-23 23:37:11 +0000815}
816
Dmitri Gribenko8a903932012-08-06 23:48:44 +0000817TEST_F(CommentParserTest, ParamCommand5) {
Dmitri Gribenko0c43a922012-07-24 18:23:31 +0000818 const char *Sources[] = {
819 "// \\param [out] aaa Bbb\n",
Dmitri Gribenkod076e012012-08-01 23:49:32 +0000820 "// \\param[out] aaa Bbb\n",
Dmitri Gribenko0c43a922012-07-24 18:23:31 +0000821 "// \\param\n"
822 "// [out] aaa Bbb\n",
823 "// \\param [out]\n"
824 "// aaa Bbb\n",
825 "// \\param [out] aaa\n"
826 "// Bbb\n",
827 };
Dmitri Gribenkoe68c2292012-07-23 23:37:11 +0000828
Dmitri Gribenko0c43a922012-07-24 18:23:31 +0000829 for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
830 FullComment *FC = parseString(Sources[i]);
831 ASSERT_TRUE(HasChildCount(FC, 2));
Dmitri Gribenkoe68c2292012-07-23 23:37:11 +0000832
Dmitri Gribenko0c43a922012-07-24 18:23:31 +0000833 ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
834 {
835 ParamCommandComment *PCC;
836 ParagraphComment *PC;
837 ASSERT_TRUE(HasParamCommandAt(FC, 1, PCC, "param",
838 ParamCommandComment::Out,
839 /* IsDirectionExplicit = */ true,
840 "aaa", PC));
841 ASSERT_TRUE(HasChildCount(PCC, 1));
842 ASSERT_TRUE(HasParagraphCommentAt(PCC, 0, " Bbb"));
843 }
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000844 }
845}
846
Dmitri Gribenko8a903932012-08-06 23:48:44 +0000847TEST_F(CommentParserTest, ParamCommand6) {
Dmitri Gribenkoe68c2292012-07-23 23:37:11 +0000848 const char *Sources[] = {
849 "// \\param [in,out] aaa Bbb\n",
Dmitri Gribenkod076e012012-08-01 23:49:32 +0000850 "// \\param[in,out] aaa Bbb\n",
Dmitri Gribenko0c43a922012-07-24 18:23:31 +0000851 "// \\param [in, out] aaa Bbb\n",
852 "// \\param [in,\n"
853 "// out] aaa Bbb\n",
854 "// \\param [in,out]\n"
855 "// aaa Bbb\n",
856 "// \\param [in,out] aaa\n"
857 "// Bbb\n"
Dmitri Gribenkoe68c2292012-07-23 23:37:11 +0000858 };
859
860 for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
861 FullComment *FC = parseString(Sources[i]);
862 ASSERT_TRUE(HasChildCount(FC, 2));
863
864 ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
865 {
866 ParamCommandComment *PCC;
867 ParagraphComment *PC;
868 ASSERT_TRUE(HasParamCommandAt(FC, 1, PCC, "param",
869 ParamCommandComment::InOut,
870 /* IsDirectionExplicit = */ true,
871 "aaa", PC));
872 ASSERT_TRUE(HasChildCount(PCC, 1));
873 ASSERT_TRUE(HasParagraphCommentAt(PCC, 0, " Bbb"));
874 }
875 }
876}
877
Dmitri Gribenko8a903932012-08-06 23:48:44 +0000878TEST_F(CommentParserTest, ParamCommand7) {
Dmitri Gribenkofd939162012-07-24 16:10:47 +0000879 const char *Source =
880 "// \\param aaa \\% Bbb \\$ ccc\n";
881
882 FullComment *FC = parseString(Source);
883 ASSERT_TRUE(HasChildCount(FC, 2));
884
885 ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
886 {
887 ParamCommandComment *PCC;
888 ParagraphComment *PC;
889 ASSERT_TRUE(HasParamCommandAt(FC, 1, PCC, "param",
890 ParamCommandComment::In,
891 /* IsDirectionExplicit = */ false,
892 "aaa", PC));
893 ASSERT_TRUE(HasChildCount(PCC, 1));
894
895 ASSERT_TRUE(HasChildCount(PC, 5));
896 ASSERT_TRUE(HasTextAt(PC, 0, " "));
897 ASSERT_TRUE(HasTextAt(PC, 1, "%"));
898 ASSERT_TRUE(HasTextAt(PC, 2, " Bbb "));
899 ASSERT_TRUE(HasTextAt(PC, 3, "$"));
900 ASSERT_TRUE(HasTextAt(PC, 4, " ccc"));
901 }
902}
Dmitri Gribenkoe68c2292012-07-23 23:37:11 +0000903
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000904TEST_F(CommentParserTest, TParamCommand1) {
905 const char *Sources[] = {
906 "// \\tparam aaa Bbb\n",
907 "// \\tparam\n"
908 "// aaa Bbb\n",
909 "// \\tparam \n"
910 "// aaa Bbb\n",
911 "// \\tparam aaa\n"
912 "// Bbb\n"
913 };
914
915 for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
916 FullComment *FC = parseString(Sources[i]);
917 ASSERT_TRUE(HasChildCount(FC, 2));
918
919 ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
920 {
921 TParamCommandComment *TPCC;
922 ParagraphComment *PC;
923 ASSERT_TRUE(HasTParamCommandAt(FC, 1, TPCC, "tparam",
924 "aaa", PC));
925 ASSERT_TRUE(HasChildCount(TPCC, 1));
926 ASSERT_TRUE(HasParagraphCommentAt(TPCC, 0, " Bbb"));
927 }
928 }
929}
930
Dmitri Gribenko8a903932012-08-06 23:48:44 +0000931TEST_F(CommentParserTest, TParamCommand2) {
932 const char *Source = "// \\tparam\\brief";
933
934 FullComment *FC = parseString(Source);
935 ASSERT_TRUE(HasChildCount(FC, 3));
936
937 ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
938 {
939 TParamCommandComment *TPCC;
940 ParagraphComment *PC;
941 ASSERT_TRUE(HasTParamCommandAt(FC, 1, TPCC, "tparam", "", PC));
942 ASSERT_TRUE(HasChildCount(TPCC, 1));
943 ASSERT_TRUE(HasChildCount(PC, 0));
944 }
945 {
946 BlockCommandComment *BCC;
947 ParagraphComment *PC;
948 ASSERT_TRUE(HasBlockCommandAt(FC, 2, BCC, "brief", PC));
949 ASSERT_TRUE(HasChildCount(PC, 0));
950 }
951}
952
953
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000954TEST_F(CommentParserTest, InlineCommand1) {
955 const char *Source = "// \\c";
956
957 FullComment *FC = parseString(Source);
958 ASSERT_TRUE(HasChildCount(FC, 1));
959
960 {
961 ParagraphComment *PC;
962 InlineCommandComment *ICC;
963 ASSERT_TRUE(GetChildAt(FC, 0, PC));
964
965 ASSERT_TRUE(HasChildCount(PC, 2));
966 ASSERT_TRUE(HasTextAt(PC, 0, " "));
967 ASSERT_TRUE(HasInlineCommandAt(PC, 1, ICC, "c", NoArgs()));
968 }
969}
970
971TEST_F(CommentParserTest, InlineCommand2) {
972 const char *Source = "// \\c ";
973
974 FullComment *FC = parseString(Source);
975 ASSERT_TRUE(HasChildCount(FC, 1));
976
977 {
978 ParagraphComment *PC;
979 InlineCommandComment *ICC;
980 ASSERT_TRUE(GetChildAt(FC, 0, PC));
981
982 ASSERT_TRUE(HasChildCount(PC, 3));
983 ASSERT_TRUE(HasTextAt(PC, 0, " "));
984 ASSERT_TRUE(HasInlineCommandAt(PC, 1, ICC, "c", NoArgs()));
985 ASSERT_TRUE(HasTextAt(PC, 2, " "));
986 }
987}
988
989TEST_F(CommentParserTest, InlineCommand3) {
990 const char *Source = "// \\c aaa\n";
991
992 FullComment *FC = parseString(Source);
993 ASSERT_TRUE(HasChildCount(FC, 1));
994
995 {
996 ParagraphComment *PC;
997 InlineCommandComment *ICC;
998 ASSERT_TRUE(GetChildAt(FC, 0, PC));
999
1000 ASSERT_TRUE(HasChildCount(PC, 2));
1001 ASSERT_TRUE(HasTextAt(PC, 0, " "));
1002 ASSERT_TRUE(HasInlineCommandAt(PC, 1, ICC, "c", "aaa"));
1003 }
1004}
1005
1006TEST_F(CommentParserTest, InlineCommand4) {
1007 const char *Source = "// \\c aaa bbb";
1008
1009 FullComment *FC = parseString(Source);
1010 ASSERT_TRUE(HasChildCount(FC, 1));
1011
1012 {
1013 ParagraphComment *PC;
1014 InlineCommandComment *ICC;
1015 ASSERT_TRUE(GetChildAt(FC, 0, PC));
1016
1017 ASSERT_TRUE(HasChildCount(PC, 3));
1018 ASSERT_TRUE(HasTextAt(PC, 0, " "));
1019 ASSERT_TRUE(HasInlineCommandAt(PC, 1, ICC, "c", "aaa"));
1020 ASSERT_TRUE(HasTextAt(PC, 2, " bbb"));
1021 }
1022}
1023
1024TEST_F(CommentParserTest, InlineCommand5) {
1025 const char *Source = "// \\unknown aaa\n";
1026
1027 FullComment *FC = parseString(Source);
1028 ASSERT_TRUE(HasChildCount(FC, 1));
1029
1030 {
1031 ParagraphComment *PC;
1032 InlineCommandComment *ICC;
1033 ASSERT_TRUE(GetChildAt(FC, 0, PC));
1034
1035 ASSERT_TRUE(HasChildCount(PC, 3));
1036 ASSERT_TRUE(HasTextAt(PC, 0, " "));
1037 ASSERT_TRUE(HasInlineCommandAt(PC, 1, ICC, "unknown", NoArgs()));
1038 ASSERT_TRUE(HasTextAt(PC, 2, " aaa"));
1039 }
1040}
1041
1042TEST_F(CommentParserTest, HTML1) {
1043 const char *Sources[] = {
1044 "// <a",
1045 "// <a>",
1046 "// <a >"
1047 };
1048
1049 for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
1050 FullComment *FC = parseString(Sources[i]);
1051 ASSERT_TRUE(HasChildCount(FC, 1));
1052
1053 {
1054 ParagraphComment *PC;
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +00001055 HTMLStartTagComment *HST;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001056 ASSERT_TRUE(GetChildAt(FC, 0, PC));
1057
1058 ASSERT_TRUE(HasChildCount(PC, 2));
1059 ASSERT_TRUE(HasTextAt(PC, 0, " "));
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +00001060 ASSERT_TRUE(HasHTMLStartTagAt(PC, 1, HST, "a", NoAttrs()));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001061 }
1062 }
1063}
1064
1065TEST_F(CommentParserTest, HTML2) {
1066 const char *Sources[] = {
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00001067 "// <br/>",
1068 "// <br />"
1069 };
1070
1071 for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
1072 FullComment *FC = parseString(Sources[i]);
1073 ASSERT_TRUE(HasChildCount(FC, 1));
1074
1075 {
1076 ParagraphComment *PC;
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +00001077 HTMLStartTagComment *HST;
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00001078 ASSERT_TRUE(GetChildAt(FC, 0, PC));
1079
1080 ASSERT_TRUE(HasChildCount(PC, 2));
1081 ASSERT_TRUE(HasTextAt(PC, 0, " "));
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +00001082 ASSERT_TRUE(HasHTMLStartTagAt(PC, 1, HST, "br", SelfClosing()));
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00001083 }
1084 }
1085}
1086
1087TEST_F(CommentParserTest, HTML3) {
1088 const char *Sources[] = {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001089 "// <a href",
1090 "// <a href ",
1091 "// <a href>",
1092 "// <a href >",
1093 };
1094
1095 for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
1096 FullComment *FC = parseString(Sources[i]);
1097 ASSERT_TRUE(HasChildCount(FC, 1));
1098
1099 {
1100 ParagraphComment *PC;
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +00001101 HTMLStartTagComment *HST;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001102 ASSERT_TRUE(GetChildAt(FC, 0, PC));
1103
1104 ASSERT_TRUE(HasChildCount(PC, 2));
1105 ASSERT_TRUE(HasTextAt(PC, 0, " "));
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +00001106 ASSERT_TRUE(HasHTMLStartTagAt(PC, 1, HST, "a", "href", ""));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001107 }
1108 }
1109}
1110
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00001111TEST_F(CommentParserTest, HTML4) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001112 const char *Sources[] = {
1113 "// <a href=\"bbb\"",
1114 "// <a href=\"bbb\">",
1115 };
1116
1117 for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
1118 FullComment *FC = parseString(Sources[i]);
1119 ASSERT_TRUE(HasChildCount(FC, 1));
1120
1121 {
1122 ParagraphComment *PC;
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +00001123 HTMLStartTagComment *HST;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001124 ASSERT_TRUE(GetChildAt(FC, 0, PC));
1125
1126 ASSERT_TRUE(HasChildCount(PC, 2));
1127 ASSERT_TRUE(HasTextAt(PC, 0, " "));
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +00001128 ASSERT_TRUE(HasHTMLStartTagAt(PC, 1, HST, "a", "href", "bbb"));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001129 }
1130 }
1131}
1132
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00001133TEST_F(CommentParserTest, HTML5) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001134 const char *Sources[] = {
1135 "// </a",
1136 "// </a>",
1137 "// </a >"
1138 };
1139
1140 for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
1141 FullComment *FC = parseString(Sources[i]);
1142 ASSERT_TRUE(HasChildCount(FC, 1));
1143
1144 {
1145 ParagraphComment *PC;
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +00001146 HTMLEndTagComment *HET;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001147 ASSERT_TRUE(GetChildAt(FC, 0, PC));
1148
1149 ASSERT_TRUE(HasChildCount(PC, 2));
1150 ASSERT_TRUE(HasTextAt(PC, 0, " "));
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +00001151 ASSERT_TRUE(HasHTMLEndTagAt(PC, 1, HET, "a"));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001152 }
1153 }
1154}
1155
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00001156TEST_F(CommentParserTest, HTML6) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001157 const char *Source =
1158 "// <pre>\n"
1159 "// Aaa\n"
1160 "// Bbb\n"
1161 "// </pre>\n";
1162
1163 FullComment *FC = parseString(Source);
1164 ASSERT_TRUE(HasChildCount(FC, 1));
1165
1166 {
1167 ParagraphComment *PC;
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +00001168 HTMLStartTagComment *HST;
1169 HTMLEndTagComment *HET;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001170 ASSERT_TRUE(GetChildAt(FC, 0, PC));
1171
1172 ASSERT_TRUE(HasChildCount(PC, 6));
1173 ASSERT_TRUE(HasTextAt(PC, 0, " "));
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +00001174 ASSERT_TRUE(HasHTMLStartTagAt(PC, 1, HST, "pre", NoAttrs()));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001175 ASSERT_TRUE(HasTextWithNewlineAt(PC, 2, " Aaa"));
1176 ASSERT_TRUE(HasTextWithNewlineAt(PC, 3, " Bbb"));
1177 ASSERT_TRUE(HasTextAt(PC, 4, " "));
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +00001178 ASSERT_TRUE(HasHTMLEndTagAt(PC, 5, HET, "pre"));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001179 }
1180}
1181
1182TEST_F(CommentParserTest, VerbatimBlock1) {
1183 const char *Source = "// \\verbatim\\endverbatim\n";
1184
1185 FullComment *FC = parseString(Source);
1186 ASSERT_TRUE(HasChildCount(FC, 2));
1187
Dmitri Gribenkodebd16f2012-07-23 23:09:32 +00001188 ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001189 {
1190 VerbatimBlockComment *VCC;
Dmitri Gribenko9f08f492012-07-20 20:18:53 +00001191 ASSERT_TRUE(HasVerbatimBlockAt(FC, 1, VCC, "verbatim", "endverbatim",
1192 NoLines()));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001193 }
1194}
1195
1196TEST_F(CommentParserTest, VerbatimBlock2) {
1197 const char *Source = "// \\verbatim Aaa \\endverbatim\n";
1198
1199 FullComment *FC = parseString(Source);
1200 ASSERT_TRUE(HasChildCount(FC, 2));
1201
Dmitri Gribenkodebd16f2012-07-23 23:09:32 +00001202 ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001203 {
1204 VerbatimBlockComment *VBC;
Dmitri Gribenko9f08f492012-07-20 20:18:53 +00001205 ASSERT_TRUE(HasVerbatimBlockAt(FC, 1, VBC, "verbatim", "endverbatim",
1206 Lines(), " Aaa "));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001207 }
1208}
1209
1210TEST_F(CommentParserTest, VerbatimBlock3) {
Dmitri Gribenko9f08f492012-07-20 20:18:53 +00001211 const char *Source = "// \\verbatim Aaa\n";
1212
1213 FullComment *FC = parseString(Source);
1214 ASSERT_TRUE(HasChildCount(FC, 2));
1215
Dmitri Gribenkodebd16f2012-07-23 23:09:32 +00001216 ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
Dmitri Gribenko9f08f492012-07-20 20:18:53 +00001217 {
1218 VerbatimBlockComment *VBC;
1219 ASSERT_TRUE(HasVerbatimBlockAt(FC, 1, VBC, "verbatim", "",
1220 Lines(), " Aaa"));
1221 }
1222}
1223
1224TEST_F(CommentParserTest, VerbatimBlock4) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001225 const char *Source =
1226 "//\\verbatim\n"
1227 "//\\endverbatim\n";
1228
1229 FullComment *FC = parseString(Source);
1230 ASSERT_TRUE(HasChildCount(FC, 1));
1231
1232 {
1233 VerbatimBlockComment *VBC;
Dmitri Gribenko9f08f492012-07-20 20:18:53 +00001234 ASSERT_TRUE(HasVerbatimBlockAt(FC, 0, VBC, "verbatim", "endverbatim",
1235 NoLines()));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001236 }
1237}
1238
Dmitri Gribenko9f08f492012-07-20 20:18:53 +00001239TEST_F(CommentParserTest, VerbatimBlock5) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001240 const char *Sources[] = {
1241 "//\\verbatim\n"
1242 "// Aaa\n"
1243 "//\\endverbatim\n",
1244
1245 "/*\\verbatim\n"
1246 " * Aaa\n"
1247 " *\\endverbatim*/"
1248 };
1249
1250 for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
1251 FullComment *FC = parseString(Sources[i]);
1252 ASSERT_TRUE(HasChildCount(FC, 1));
1253
1254 {
1255 VerbatimBlockComment *VBC;
Dmitri Gribenko9f08f492012-07-20 20:18:53 +00001256 ASSERT_TRUE(HasVerbatimBlockAt(FC, 0, VBC, "verbatim", "endverbatim",
1257 Lines(), " Aaa"));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001258 }
1259 }
1260}
1261
Dmitri Gribenko9f08f492012-07-20 20:18:53 +00001262TEST_F(CommentParserTest, VerbatimBlock6) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001263 const char *Sources[] = {
1264 "// \\verbatim\n"
1265 "// Aaa\n"
1266 "// \\endverbatim\n",
1267
1268 "/* \\verbatim\n"
1269 " * Aaa\n"
1270 " * \\endverbatim*/"
1271 };
1272
1273 for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
1274 FullComment *FC = parseString(Sources[i]);
1275 ASSERT_TRUE(HasChildCount(FC, 2));
1276
Dmitri Gribenkodebd16f2012-07-23 23:09:32 +00001277 ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001278 {
1279 VerbatimBlockComment *VBC;
Dmitri Gribenko9f08f492012-07-20 20:18:53 +00001280 ASSERT_TRUE(HasVerbatimBlockAt(FC, 1, VBC, "verbatim", "endverbatim",
1281 Lines(), " Aaa"));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001282 }
1283 }
1284}
1285
Dmitri Gribenko9f08f492012-07-20 20:18:53 +00001286TEST_F(CommentParserTest, VerbatimBlock7) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001287 const char *Sources[] = {
1288 "// \\verbatim\n"
1289 "// Aaa\n"
Dmitri Gribenko64da4e52012-07-18 23:01:58 +00001290 "// Bbb\n"
1291 "// \\endverbatim\n",
1292
1293 "/* \\verbatim\n"
1294 " * Aaa\n"
1295 " * Bbb\n"
1296 " * \\endverbatim*/"
1297 };
1298
1299 for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
1300 FullComment *FC = parseString(Sources[i]);
1301 ASSERT_TRUE(HasChildCount(FC, 2));
1302
Dmitri Gribenkodebd16f2012-07-23 23:09:32 +00001303 ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
Dmitri Gribenko64da4e52012-07-18 23:01:58 +00001304 {
1305 VerbatimBlockComment *VBC;
Dmitri Gribenko9f08f492012-07-20 20:18:53 +00001306 ASSERT_TRUE(HasVerbatimBlockAt(FC, 1, VBC, "verbatim", "endverbatim",
1307 Lines(), " Aaa", " Bbb"));
Dmitri Gribenko64da4e52012-07-18 23:01:58 +00001308 }
1309 }
1310}
1311
Dmitri Gribenko9f08f492012-07-20 20:18:53 +00001312TEST_F(CommentParserTest, VerbatimBlock8) {
Dmitri Gribenko64da4e52012-07-18 23:01:58 +00001313 const char *Sources[] = {
1314 "// \\verbatim\n"
1315 "// Aaa\n"
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001316 "//\n"
1317 "// Bbb\n"
1318 "// \\endverbatim\n",
1319
1320 "/* \\verbatim\n"
1321 " * Aaa\n"
1322 " *\n"
1323 " * Bbb\n"
1324 " * \\endverbatim*/"
1325 };
1326 for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
Dmitri Gribenko64da4e52012-07-18 23:01:58 +00001327 FullComment *FC = parseString(Sources[i]);
1328 ASSERT_TRUE(HasChildCount(FC, 2));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001329
Dmitri Gribenkodebd16f2012-07-23 23:09:32 +00001330 ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
Dmitri Gribenko64da4e52012-07-18 23:01:58 +00001331 {
1332 VerbatimBlockComment *VBC;
Dmitri Gribenko9f08f492012-07-20 20:18:53 +00001333 ASSERT_TRUE(HasVerbatimBlockAt(FC, 1, VBC, "verbatim", "endverbatim"));
Dmitri Gribenko64da4e52012-07-18 23:01:58 +00001334 ASSERT_EQ(3U, VBC->getNumLines());
1335 ASSERT_EQ(" Aaa", VBC->getText(0));
1336 ASSERT_EQ("", VBC->getText(1));
1337 ASSERT_EQ(" Bbb", VBC->getText(2));
1338 }
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001339 }
1340}
1341
1342TEST_F(CommentParserTest, VerbatimLine1) {
1343 const char *Sources[] = {
1344 "// \\fn",
1345 "// \\fn\n"
1346 };
1347
1348 for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
1349 FullComment *FC = parseString(Sources[i]);
1350 ASSERT_TRUE(HasChildCount(FC, 2));
1351
Dmitri Gribenkodebd16f2012-07-23 23:09:32 +00001352 ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001353 {
1354 VerbatimLineComment *VLC;
1355 ASSERT_TRUE(HasVerbatimLineAt(FC, 1, VLC, "fn", ""));
1356 }
1357 }
1358}
1359
1360TEST_F(CommentParserTest, VerbatimLine2) {
1361 const char *Sources[] = {
1362 "/// \\fn void *foo(const char *zzz = \"\\$\");\n//",
1363 "/** \\fn void *foo(const char *zzz = \"\\$\");*/"
1364 };
1365
1366 for (size_t i = 0, e = array_lengthof(Sources); i != e; i++) {
1367 FullComment *FC = parseString(Sources[i]);
1368 ASSERT_TRUE(HasChildCount(FC, 2));
1369
Dmitri Gribenkodebd16f2012-07-23 23:09:32 +00001370 ASSERT_TRUE(HasParagraphCommentAt(FC, 0, " "));
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +00001371 {
1372 VerbatimLineComment *VLC;
1373 ASSERT_TRUE(HasVerbatimLineAt(FC, 1, VLC, "fn",
1374 " void *foo(const char *zzz = \"\\$\");"));
1375 }
1376 }
1377}
1378
1379} // unnamed namespace
1380
1381} // end namespace comments
1382} // end namespace clang
1383