forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDeclSyntaxTests.cpp
620 lines (539 loc) · 23.3 KB
/
DeclSyntaxTests.cpp
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
#include "swift/Syntax/SyntaxFactory.h"
#include "swift/Syntax/SyntaxNodes.h"
#include "swift/Syntax/SyntaxBuilders.h"
#include "llvm/ADT/SmallString.h"
#include "gtest/gtest.h"
using llvm::None;
using llvm::SmallString;
using namespace swift;
using namespace swift::syntax;
#pragma mark - declaration-modifier
DeclModifierSyntax getCannedDeclModifier() {
auto Private = SyntaxFactory::makeIdentifier("private", {}, {});
auto LParen = SyntaxFactory::makeLeftParenToken({}, {});
auto Set = SyntaxFactory::makeIdentifier("set", {}, {});
auto RParen = SyntaxFactory::makeRightParenToken({}, {});
return SyntaxFactory::makeDeclModifier(Private, LParen, Set, RParen);
}
TEST(DeclSyntaxTests, DeclModifierMakeAPIs) {
{
SmallString<1> Scratch;
llvm::raw_svector_ostream OS(Scratch);
SyntaxFactory::makeBlankDeclModifier().print(OS);
ASSERT_EQ(OS.str().str(), "");
}
{
SmallString<24> Scratch;
llvm::raw_svector_ostream OS(Scratch);
getCannedDeclModifier().print(OS);
ASSERT_EQ(OS.str().str(), "private(set)");
}
}
TEST(DeclSyntaxTests, DeclModifierGetAPIs) {
auto Private = SyntaxFactory::makeIdentifier("private", {}, {});
auto LParen = SyntaxFactory::makeLeftParenToken({}, {});
auto Set = SyntaxFactory::makeIdentifier("set", {}, {});
auto RParen = SyntaxFactory::makeRightParenToken({}, {});
auto Mod = SyntaxFactory::makeDeclModifier(Private, LParen, Set, RParen);
ASSERT_EQ(Private.getRaw(), Mod.getName().getRaw());
ASSERT_EQ(LParen.getRaw(), Mod.getDetailLeftParen()->getRaw());
ASSERT_EQ(Set.getRaw(), Mod.getDetail()->getRaw());
ASSERT_EQ(RParen.getRaw(), Mod.getDetailRightParen()->getRaw());
}
TEST(DeclSyntaxTests, DeclModifierWithAPIs) {
auto Private = SyntaxFactory::makeIdentifier("private", {}, {});
auto LParen = SyntaxFactory::makeLeftParenToken({}, {});
auto Set = SyntaxFactory::makeIdentifier("set", {}, {});
auto RParen = SyntaxFactory::makeRightParenToken({}, {});
SmallString<24> Scratch;
llvm::raw_svector_ostream OS(Scratch);
SyntaxFactory::makeBlankDeclModifier()
.withName(Private)
.withDetailLeftParen(LParen)
.withDetail(Set)
.withDetailRightParen(RParen)
.print(OS);
ASSERT_EQ(OS.str().str(), "private(set)");
}
#pragma mark - typealias-decl
TEST(DeclSyntaxTests, TypealiasMakeAPIs) {
{
SmallString<1> Scratch;
llvm::raw_svector_ostream OS(Scratch);
SyntaxFactory::makeBlankTypealiasDecl().print(OS);
ASSERT_EQ(OS.str().str(), "");
}
{
SmallString<64> Scratch;
llvm::raw_svector_ostream OS(Scratch);
auto Typealias =
SyntaxFactory::makeTypealiasKeyword({}, { Trivia::spaces(1) });
auto Subsequence = SyntaxFactory::makeIdentifier("MyCollection", {}, {});
auto ElementName = SyntaxFactory::makeIdentifier("Element", {}, {});
auto ElementParam =
SyntaxFactory::makeGenericParameter(None, ElementName, None, None, None);
auto LeftAngle = SyntaxFactory::makeLeftAngleToken({}, {});
auto RightAngle = SyntaxFactory::makeRightAngleToken({}, Trivia::spaces(1));
auto GenericParams = GenericParameterClauseSyntaxBuilder()
.useLeftAngleBracket(LeftAngle)
.useRightAngleBracket(RightAngle)
.addGenericParameter(ElementParam)
.build();
auto Assignment = SyntaxFactory::makeEqualToken({}, { Trivia::spaces(1) });
auto ElementType = SyntaxFactory::makeTypeIdentifier("Element", {}, {});
auto ElementArg = SyntaxFactory::makeGenericArgument(ElementType, None);
auto GenericArgs = GenericArgumentClauseSyntaxBuilder()
.useLeftAngleBracket(LeftAngle)
.useRightAngleBracket(SyntaxFactory::makeRightAngleToken({}, {}))
.addArgument(ElementArg)
.build();
auto Array = SyntaxFactory::makeIdentifier("Array", {}, {});
auto Array_Int =
SyntaxFactory::makeSimpleTypeIdentifier(Array, GenericArgs);
auto TypeInit = SyntaxFactory::makeTypeInitializerClause(Assignment,
Array_Int);
SyntaxFactory::makeTypealiasDecl(None, None, Typealias,
Subsequence, GenericParams, TypeInit, None)
.print(OS);
ASSERT_EQ(OS.str().str(),
"typealias MyCollection<Element> = Array<Element>");
}
}
TEST(DeclSyntaxTests, TypealiasWithAPIs) {
auto Typealias =
SyntaxFactory::makeTypealiasKeyword({}, { Trivia::spaces(1) });
auto MyCollection = SyntaxFactory::makeIdentifier("MyCollection", {}, {});
auto ElementName = SyntaxFactory::makeIdentifier("Element", {}, {});
auto ElementParam =
SyntaxFactory::makeGenericParameter(None, ElementName, None, None, None);
auto LeftAngle = SyntaxFactory::makeLeftAngleToken({}, {});
auto RightAngle =
SyntaxFactory::makeRightAngleToken({}, { Trivia::spaces(1) });
auto GenericParams = GenericParameterClauseSyntaxBuilder()
.useLeftAngleBracket(LeftAngle)
.useRightAngleBracket(RightAngle)
.addGenericParameter(ElementParam)
.build();
auto Equal = SyntaxFactory::makeEqualToken({}, { Trivia::spaces(1) });
auto ElementType = SyntaxFactory::makeTypeIdentifier("Element", {} , {});
auto ElementArg = SyntaxFactory::makeGenericArgument(ElementType, None);
auto GenericArgs = GenericArgumentClauseSyntaxBuilder()
.useLeftAngleBracket(LeftAngle)
.useRightAngleBracket(SyntaxFactory::makeRightAngleToken({}, {}))
.addArgument(ElementArg)
.build();
auto Array = SyntaxFactory::makeIdentifier("Array", {}, {});
auto Array_Int = SyntaxFactory::makeSimpleTypeIdentifier(Array, GenericArgs);
auto Type_Init = SyntaxFactory::makeTypeInitializerClause(Equal, Array_Int);
{
SmallString<1> Scratch;
llvm::raw_svector_ostream OS(Scratch);
SyntaxFactory::makeBlankTypealiasDecl()
.withTypealiasKeyword(Typealias)
.withIdentifier(MyCollection)
.withGenericParameterClause(GenericParams)
.withInitializer(Type_Init)
.print(OS);
ASSERT_EQ(OS.str().str(),
"typealias MyCollection<Element> = Array<Element>");
}
}
TEST(DeclSyntaxTests, TypealiasBuilderAPIs) {
SmallString<64> Scratch;
llvm::raw_svector_ostream OS(Scratch);
auto Typealias =
SyntaxFactory::makeTypealiasKeyword({}, { Trivia::spaces(1) });
auto MyCollection = SyntaxFactory::makeIdentifier("MyCollection", {}, {});
auto ElementName = SyntaxFactory::makeIdentifier("Element", {}, {});
auto ElementParam = SyntaxFactory::makeGenericParameter(ElementName, None);
auto LeftAngle = SyntaxFactory::makeLeftAngleToken({}, {});
auto RightAngle =
SyntaxFactory::makeRightAngleToken({}, { Trivia::spaces(1) });
auto GenericParams = GenericParameterClauseSyntaxBuilder()
.useLeftAngleBracket(LeftAngle)
.useRightAngleBracket(RightAngle)
.addGenericParameter(ElementParam)
.build();
auto Equal =
SyntaxFactory::makeEqualToken({}, { Trivia::spaces(1) });
auto ElementType = SyntaxFactory::makeTypeIdentifier("Element", {}, {});
auto ElementArg = SyntaxFactory::makeGenericArgument(ElementType, None);
auto GenericArgs = GenericArgumentClauseSyntaxBuilder()
.useLeftAngleBracket(LeftAngle)
.useRightAngleBracket(SyntaxFactory::makeRightAngleToken({}, {}))
.addArgument(ElementArg)
.build();
auto Array = SyntaxFactory::makeIdentifier("Array", {}, {});
auto Array_Int = SyntaxFactory::makeSimpleTypeIdentifier(Array, GenericArgs);
auto Type_Init = SyntaxFactory::makeTypeInitializerClause(Equal, Array_Int);
TypealiasDeclSyntaxBuilder()
.useTypealiasKeyword(Typealias)
.useIdentifier(MyCollection)
.useGenericParameterClause(GenericParams)
.useInitializer(Type_Init)
.build()
.print(OS);
ASSERT_EQ(OS.str().str(),
"typealias MyCollection<Element> = Array<Element>");
}
#pragma mark - parameter
FunctionParameterSyntax getCannedFunctionParameter() {
auto ExternalName = SyntaxFactory::makeIdentifier("with", {},
Trivia::spaces(1));
auto LocalName = SyntaxFactory::makeIdentifier("radius", {}, {});
auto Colon = SyntaxFactory::makeColonToken({}, Trivia::spaces(1));
auto Int = SyntaxFactory::makeTypeIdentifier("Int", {},
Trivia::spaces(1));
auto NoEllipsis = TokenSyntax::missingToken(tok::identifier, "...");
auto Equal = SyntaxFactory::makeEqualToken({}, Trivia::spaces(1));
auto Sign = SyntaxFactory::makePrefixOperator("-", {}, {});
auto OneDigits = SyntaxFactory::makeIntegerLiteral("1", {}, {});
auto One = SyntaxFactory::makePrefixOperatorExpr(Sign,
SyntaxFactory::makeIntegerLiteralExpr(OneDigits));
auto DefaultArg = SyntaxFactory::makeInitializerClause(Equal, One);
auto Comma = SyntaxFactory::makeCommaToken({}, Trivia::spaces(1));
return SyntaxFactory::makeFunctionParameter(None, ExternalName, LocalName,
Colon, Int, NoEllipsis,
DefaultArg, Comma);
}
TEST(DeclSyntaxTests, FunctionParameterMakeAPIs) {
{
SmallString<48> Scratch;
llvm::raw_svector_ostream OS(Scratch);
getCannedFunctionParameter().print(OS);
ASSERT_EQ(OS.str().str(), "with radius: Int = -1, ");
}
{
SmallString<48> Scratch;
llvm::raw_svector_ostream OS(Scratch);
SyntaxFactory::makeBlankFunctionParameter().print(OS);
ASSERT_EQ(OS.str().str(), "");
}
}
TEST(DeclSyntaxTests, FunctionParameterGetAPIs) {
auto ExternalName = SyntaxFactory::makeIdentifier("with", {},
Trivia::spaces(1));
auto LocalName = SyntaxFactory::makeIdentifier("radius", {}, {});
auto Colon = SyntaxFactory::makeColonToken({}, Trivia::spaces(1));
auto Int = SyntaxFactory::makeTypeIdentifier("Int", {},
Trivia::spaces(1));
auto NoEllipsis = TokenSyntax::missingToken(tok::identifier, "...");
auto Equal = SyntaxFactory::makeEqualToken({}, Trivia::spaces(1));
auto Sign = SyntaxFactory::makePrefixOperator("-", {}, {});
auto OneDigits = SyntaxFactory::makeIntegerLiteral("1", {}, {});
auto One = SyntaxFactory::makePrefixOperatorExpr(Sign,
SyntaxFactory::makeIntegerLiteralExpr(OneDigits));
auto DefaultArg = SyntaxFactory::makeInitializerClause(Equal, One);
auto Comma = SyntaxFactory::makeCommaToken({}, {});
auto Param = SyntaxFactory::makeFunctionParameter(None, ExternalName,
LocalName, Colon, Int,
NoEllipsis, DefaultArg,
Comma);
ASSERT_EQ(ExternalName.getRaw(), Param.getFirstName()->getRaw());
ASSERT_EQ(LocalName.getRaw(), Param.getSecondName()->getRaw());
ASSERT_EQ(Colon.getRaw(), Param.getColon()->getRaw());
auto GottenType = Param.getType();
auto GottenType2 = Param.getType();
ASSERT_TRUE(GottenType->hasSameIdentityAs(*GottenType2));
ASSERT_EQ(DefaultArg.getRaw(), Param.getDefaultArgument()->getRaw());
auto GottenDefaultValue = Param.getDefaultArgument()->getValue();
auto GottenDefaultValue2 = Param.getDefaultArgument()->getValue();
ASSERT_TRUE(GottenDefaultValue.hasSameIdentityAs(GottenDefaultValue2));
ASSERT_EQ(Comma.getRaw(), Param.getTrailingComma()->getRaw());
// Test that llvm::None is returned for non-token missing children:
auto Decimated = Param
.withType(llvm::None)
.withDefaultArgument(llvm::None);
ASSERT_FALSE(Decimated.getType().hasValue());
ASSERT_FALSE(Decimated.getDefaultArgument().hasValue());
}
TEST(DeclSyntaxTests, FunctionParameterWithAPIs) {
auto ExternalName = SyntaxFactory::makeIdentifier("for", {},
Trivia::spaces(1));
auto LocalName = SyntaxFactory::makeIdentifier("integer", {}, {});
auto Colon = SyntaxFactory::makeColonToken(Trivia::spaces(1),
Trivia::spaces(1));
auto Int = SyntaxFactory::makeTypeIdentifier("Int", {},
Trivia::spaces(1));
auto Equal = SyntaxFactory::makeEqualToken({}, Trivia::spaces(1));
auto NoSign = TokenSyntax::missingToken(tok::oper_prefix, "");
auto OneDigits = SyntaxFactory::makeIntegerLiteral("1", {}, {});
auto One = SyntaxFactory::makeIntegerLiteralExpr(OneDigits);
auto DefaultArg = SyntaxFactory::makeInitializerClause(Equal, One);
auto Comma = SyntaxFactory::makeCommaToken({}, {});
{
SmallString<48> Scratch;
llvm::raw_svector_ostream OS(Scratch);
getCannedFunctionParameter()
.withFirstName(ExternalName)
.withSecondName(LocalName)
.withColon(Colon)
.withType(Int)
.withDefaultArgument(DefaultArg)
.withTrailingComma(Comma)
.print(OS);
ASSERT_EQ(OS.str().str(), "for integer : Int = 1,");
}
{
SmallString<48> Scratch;
llvm::raw_svector_ostream OS(Scratch);
getCannedFunctionParameter()
.withType(llvm::None)
.withDefaultArgument(llvm::None)
.print(OS);
ASSERT_EQ(OS.str().str(), "with radius: , ");
}
}
TEST(DeclSyntaxTests, FunctionParameterWithEllipsis) {
auto ExternalName = SyntaxFactory::makeIdentifier("for", {},
Trivia::spaces(1));
auto LocalName = SyntaxFactory::makeIdentifier("integer", {}, {});
auto Colon = SyntaxFactory::makeColonToken(Trivia::spaces(1),
Trivia::spaces(1));
auto Int = SyntaxFactory::makeTypeIdentifier("Int", {},
Trivia::spaces(0));
auto Ellipsis = SyntaxFactory::makeEllipsisToken({},
Trivia::spaces(1));
auto Comma = SyntaxFactory::makeCommaToken({}, {});
{
SmallString<48> Scratch;
llvm::raw_svector_ostream OS(Scratch);
getCannedFunctionParameter()
.withFirstName(ExternalName)
.withSecondName(LocalName)
.withColon(Colon)
.withType(Int)
.withEllipsis(Ellipsis)
.withDefaultArgument(llvm::None)
.withTrailingComma(Comma)
.print(OS);
ASSERT_EQ(OS.str().str(), "for integer : Int... ,");
}
}
#pragma mark - parameter-list
TEST(DeclSyntaxTests, FunctionParameterListMakeAPIs) {
{
SmallString<1> Scratch;
llvm::raw_svector_ostream OS(Scratch);
SyntaxFactory::makeBlankFunctionParameterList().print(OS);
ASSERT_EQ(OS.str().str(), "");
}
{
SmallString<48> Scratch;
llvm::raw_svector_ostream OS(Scratch);
auto Param = getCannedFunctionParameter();
std::vector<FunctionParameterSyntax> Params { Param, Param, Param };
SyntaxFactory::makeFunctionParameterList(Params).print(OS);
ASSERT_EQ(OS.str().str(),
"with radius: Int = -1, with radius: Int = -1, with radius: Int = -1, ");
}
}
#pragma mark - function-signature
FunctionSignatureSyntax getCannedFunctionSignature() {
auto LParen = SyntaxFactory::makeLeftParenToken({}, {});
auto Param = getCannedFunctionParameter();
auto List = SyntaxFactory::makeBlankFunctionParameterList()
.appending(Param)
.appending(Param)
.appending(Param)
.castTo<FunctionParameterListSyntax>();
auto RParen = SyntaxFactory::makeRightParenToken({}, Trivia::spaces(1));
auto Parameter = SyntaxFactory::makeParameterClause(LParen, List, RParen);
auto Throws = SyntaxFactory::makeThrowsKeyword({}, Trivia::spaces(1));
auto Arrow = SyntaxFactory::makeArrowToken({}, Trivia::spaces(1));
auto Int = SyntaxFactory::makeTypeIdentifier("Int", {}, Trivia::spaces(1));
auto Return = SyntaxFactory::makeReturnClause(Arrow, Int);
return SyntaxFactory::makeFunctionSignature(Parameter, Throws, Return);
}
TEST(DeclSyntaxTests, FunctionSignatureMakeAPIs) {
{
SmallString<1> Scratch;
llvm::raw_svector_ostream OS(Scratch);
SyntaxFactory::makeBlankFunctionSignature().print(OS);
ASSERT_EQ(OS.str().str(), "");
}
{
SmallString<48> Scratch;
llvm::raw_svector_ostream OS(Scratch);
getCannedFunctionSignature().print(OS);
ASSERT_EQ(OS.str().str(),
"(with radius: Int = -1, "
"with radius: Int = -1, "
"with radius: Int = -1, ) throws -> Int ");
}
}
TEST(DeclSyntaxTests, FunctionSignatureGetAPIs) {
auto LParen = SyntaxFactory::makeLeftParenToken({}, {});
auto Param = getCannedFunctionParameter();
auto List = SyntaxFactory::makeBlankFunctionParameterList()
.appending(Param)
.appending(Param)
.appending(Param)
.castTo<FunctionParameterListSyntax>();
auto RParen = SyntaxFactory::makeRightParenToken({}, Trivia::spaces(1));
auto Throws = SyntaxFactory::makeThrowsKeyword({}, Trivia::spaces(1));
auto Arrow = SyntaxFactory::makeArrowToken({}, Trivia::spaces(1));
auto Int = SyntaxFactory::makeTypeIdentifier("Int", {}, {});
auto Sig = SyntaxFactory::makeFunctionSignature(
SyntaxFactory::makeParameterClause(LParen, List, RParen),
Throws,
SyntaxFactory::makeReturnClause(Arrow, Int));
ASSERT_EQ(LParen.getRaw(), Sig.getInput().getLeftParen().getRaw());
{
SmallString<48> Scratch;
llvm::raw_svector_ostream OS(Scratch);
auto GottenList1 = Sig.getInput().getParameterList();
auto GottenList2 = Sig.getInput().getParameterList();
ASSERT_TRUE(GottenList1.hasSameIdentityAs(GottenList2));
GottenList1.print(OS);
ASSERT_EQ(OS.str().str(),
"with radius: Int = -1, "
"with radius: Int = -1, "
"with radius: Int = -1, ");
}
ASSERT_EQ(RParen.getRaw(), Sig.getInput().getRightParen().getRaw());
ASSERT_EQ(Throws.getRaw(), Sig.getThrowsOrRethrowsKeyword()->getRaw());
ASSERT_EQ(Sig.getThrowsOrRethrowsKeyword()->getTokenKind(), tok::kw_throws);
ASSERT_EQ(Arrow.getRaw(), Sig.getOutput()->getArrow().getRaw());
{
SmallString<3> Scratch;
llvm::raw_svector_ostream OS(Scratch);
auto GottenReturnType1 = Sig.getOutput()->getReturnType();
auto GottenReturnType2 = Sig.getOutput()->getReturnType();
ASSERT_TRUE(GottenReturnType1.hasSameIdentityAs(GottenReturnType2));
GottenReturnType1.print(OS);
ASSERT_EQ(OS.str().str(), "Int");
}
}
TEST(DeclSyntaxTests, FunctionSignatureWithAPIs) {
auto LParen = SyntaxFactory::makeLeftParenToken({}, {});
auto Param = getCannedFunctionParameter();
auto List = SyntaxFactory::makeBlankFunctionParameterList()
.appending(Param)
.appending(Param)
.appending(Param)
.castTo<FunctionParameterListSyntax>();
auto RParen = SyntaxFactory::makeRightParenToken({}, Trivia::spaces(1));
auto Throws = SyntaxFactory::makeThrowsKeyword({}, Trivia::spaces(1));
auto Arrow = SyntaxFactory::makeArrowToken({}, Trivia::spaces(1));
auto Int = SyntaxFactory::makeTypeIdentifier("Int", {}, {});
auto Parameter = SyntaxFactory::makeParameterClause(LParen, List, RParen);
auto Return = SyntaxFactory::makeReturnClause(Arrow, Int);
SmallString<48> Scratch;
llvm::raw_svector_ostream OS(Scratch);
SyntaxFactory::makeBlankFunctionSignature()
.withInput(Parameter)
.withThrowsOrRethrowsKeyword(Throws)
.withOutput(Return)
.print(OS);
ASSERT_EQ(OS.str().str(),
"(with radius: Int = -1, "
"with radius: Int = -1, "
"with radius: Int = -1, ) throws -> Int");
}
#pragma mark - function-declaration
ModifierListSyntax getCannedModifiers() {
auto PublicID = SyntaxFactory::makePublicKeyword({}, Trivia::spaces(1));
auto NoLParen = TokenSyntax::missingToken(tok::l_paren, "(");
auto NoArgument = TokenSyntax::missingToken(tok::identifier, "");
auto NoRParen = TokenSyntax::missingToken(tok::r_paren, ")");
auto Public =
SyntaxFactory::makeDeclModifier(PublicID, NoLParen, NoArgument, NoRParen);
auto StaticKW = SyntaxFactory::makeStaticKeyword({}, Trivia::spaces(1));
auto Static =
SyntaxFactory::makeDeclModifier(StaticKW, NoLParen, NoArgument, NoRParen);
return SyntaxFactory::makeBlankModifierList()
.appending(Public)
.appending(Static);
}
GenericParameterClauseSyntax getCannedGenericParams() {
GenericParameterClauseSyntaxBuilder GB;
auto LAngle = SyntaxFactory::makeLeftAngleToken({}, {});
auto RAngle = SyntaxFactory::makeRightAngleToken({}, {});
auto TType = SyntaxFactory::makeIdentifier("T", {}, {});
auto UType = SyntaxFactory::makeIdentifier("U", {}, {});
auto Comma = SyntaxFactory::makeCommaToken({}, Trivia::spaces(1));
auto T = SyntaxFactory::makeGenericParameter(TType, Comma);
auto U = SyntaxFactory::makeGenericParameter(UType, None);
GB.addGenericParameter(T);
GB.addGenericParameter(U);
GB.useLeftAngleBracket(LAngle);
GB.useRightAngleBracket(RAngle);
return GB.build();
}
CodeBlockSyntax getCannedBody() {
auto NoSign = TokenSyntax::missingToken(tok::oper_prefix, "-");
auto OneDigits = SyntaxFactory::makeIntegerLiteral("1", {}, {});
auto One = SyntaxFactory::makeIntegerLiteralExpr(OneDigits);
auto ReturnKW =
SyntaxFactory::makeReturnKeyword(Trivia::newlines(1) + Trivia::spaces(2),
{});
auto Return = SyntaxFactory::makeReturnStmt(ReturnKW, One);
auto ReturnItem = SyntaxFactory::makeCodeBlockItem(Return, None, None);
auto Stmts = SyntaxFactory::makeCodeBlockItemList({ReturnItem});
auto LBrace = SyntaxFactory::makeLeftBraceToken({}, {});
auto RBrace = SyntaxFactory::makeRightBraceToken(Trivia::newlines(1), {});
return SyntaxFactory::makeCodeBlock(LBrace, Stmts, RBrace);
}
GenericWhereClauseSyntax getCannedWhereClause() {
auto WhereKW = SyntaxFactory::makeWhereKeyword({}, Trivia::spaces(1));
auto T = SyntaxFactory::makeTypeIdentifier("T", {}, Trivia::spaces(1));
auto EqualEqual = SyntaxFactory::makeEqualityOperator({}, Trivia::spaces(1));
auto Int = SyntaxFactory::makeTypeIdentifier("Int", {}, Trivia::spaces(1));
auto SameType = SyntaxFactory::makeSameTypeRequirement(T, EqualEqual, Int);
auto Req = SyntaxFactory::makeGenericRequirement(SameType, None);
auto Requirements = SyntaxFactory::makeBlankGenericRequirementList()
.appending(Req);
return SyntaxFactory::makeBlankGenericWhereClause()
.withWhereKeyword(WhereKW)
.withRequirementList(Requirements);
}
FunctionDeclSyntax getCannedFunctionDecl() {
auto NoAttributes = SyntaxFactory::makeBlankAttributeList();
auto Foo = SyntaxFactory::makeIdentifier("foo", {}, {});
auto FuncKW = SyntaxFactory::makeFuncKeyword({}, Trivia::spaces(1));
auto Modifiers = getCannedModifiers();
auto GenericParams = getCannedGenericParams();
auto GenericWhere = getCannedWhereClause();
auto Signature = getCannedFunctionSignature();
auto Body = getCannedBody();
return SyntaxFactory::makeFunctionDecl(NoAttributes, Modifiers, FuncKW, Foo,
GenericParams, Signature, GenericWhere,
Body);
}
TEST(DeclSyntaxTests, FunctionDeclMakeAPIs) {
{
SmallString<1> Scratch;
llvm::raw_svector_ostream OS(Scratch);
SyntaxFactory::makeBlankFunctionDecl().print(OS);
ASSERT_EQ(OS.str().str(), "");
}
{
SmallString<64> Scratch;
llvm::raw_svector_ostream OS(Scratch);
getCannedFunctionDecl().print(OS);
ASSERT_EQ(OS.str().str(),
"public static func foo<T, U>"
"(with radius: Int = -1, "
"with radius: Int = -1, "
"with radius: Int = -1, ) "
"throws -> Int "
"where T == Int {\n"
" return1\n"
"}");
}
}
TEST(DeclSyntaxTests, FunctionDeclGetAPIs) {
{
SmallString<1> Scratch;
llvm::raw_svector_ostream OS(Scratch);
SyntaxFactory::makeBlankFunctionDecl().print(OS);
ASSERT_EQ(OS.str().str(), "");
}
{
}
}
TEST(DeclSyntaxTests, FunctionDeclWithAPIs) {
}
TEST(DeclSyntaxTests, FunctionDeclBuilderAPIs) {
}