FormatTestCSharp.cpp
26.9 KB
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
//===- unittest/Format/FormatTestCSharp.cpp - Formatting tests for CSharp -===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "FormatTestUtils.h"
#include "clang/Format/Format.h"
#include "llvm/Support/Debug.h"
#include "gtest/gtest.h"
#define DEBUG_TYPE "format-test"
namespace clang {
namespace format {
class FormatTestCSharp : public ::testing::Test {
protected:
static std::string format(llvm::StringRef Code, unsigned Offset,
unsigned Length, const FormatStyle &Style) {
LLVM_DEBUG(llvm::errs() << "---\n");
LLVM_DEBUG(llvm::errs() << Code << "\n\n");
std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
tooling::Replacements Replaces = reformat(Style, Code, Ranges);
auto Result = applyAllReplacements(Code, Replaces);
EXPECT_TRUE(static_cast<bool>(Result));
LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
return *Result;
}
static std::string
format(llvm::StringRef Code,
const FormatStyle &Style = getMicrosoftStyle(FormatStyle::LK_CSharp)) {
return format(Code, 0, Code.size(), Style);
}
static FormatStyle getStyleWithColumns(unsigned ColumnLimit) {
FormatStyle Style = getMicrosoftStyle(FormatStyle::LK_CSharp);
Style.ColumnLimit = ColumnLimit;
return Style;
}
static void verifyFormat(
llvm::StringRef Code,
const FormatStyle &Style = getMicrosoftStyle(FormatStyle::LK_CSharp)) {
EXPECT_EQ(Code.str(), format(Code, Style)) << "Expected code is not stable";
EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
}
};
TEST_F(FormatTestCSharp, CSharpClass) {
verifyFormat("public class SomeClass\n"
"{\n"
" void f()\n"
" {\n"
" }\n"
" int g()\n"
" {\n"
" return 0;\n"
" }\n"
" void h()\n"
" {\n"
" while (true)\n"
" f();\n"
" for (;;)\n"
" f();\n"
" if (true)\n"
" f();\n"
" }\n"
"}");
// Ensure that small and empty classes are handled correctly with condensed
// (Google C++-like) brace-breaking style.
FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
Style.BreakBeforeBraces = FormatStyle::BS_Attach;
verifyFormat("public class SomeEmptyClass {}", Style);
verifyFormat("public class SomeTinyClass {\n"
" int X;\n"
"}",
Style);
verifyFormat("private class SomeTinyClass {\n"
" int X;\n"
"}",
Style);
verifyFormat("protected class SomeTinyClass {\n"
" int X;\n"
"}",
Style);
verifyFormat("internal class SomeTinyClass {\n"
" int X;\n"
"}",
Style);
}
TEST_F(FormatTestCSharp, AccessModifiers) {
verifyFormat("public String toString()\n"
"{\n"
"}");
verifyFormat("private String toString()\n"
"{\n"
"}");
verifyFormat("protected String toString()\n"
"{\n"
"}");
verifyFormat("internal String toString()\n"
"{\n"
"}");
verifyFormat("public override String toString()\n"
"{\n"
"}");
verifyFormat("private override String toString()\n"
"{\n"
"}");
verifyFormat("protected override String toString()\n"
"{\n"
"}");
verifyFormat("internal override String toString()\n"
"{\n"
"}");
verifyFormat("internal static String toString()\n"
"{\n"
"}");
}
TEST_F(FormatTestCSharp, NoStringLiteralBreaks) {
verifyFormat("foo("
"\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaa\");");
}
TEST_F(FormatTestCSharp, CSharpVerbatiumStringLiterals) {
verifyFormat("foo(@\"aaaaaaaa\\abc\\aaaa\");");
// @"ABC\" + ToString("B") - handle embedded \ in literal string at
// the end
//
/*
* After removal of Lexer change we are currently not able
* To handle these cases
verifyFormat("string s = @\"ABC\\\" + ToString(\"B\");");
verifyFormat("string s = @\"ABC\"\"DEF\"\"GHI\"");
verifyFormat("string s = @\"ABC\"\"DEF\"\"\"");
verifyFormat("string s = @\"ABC\"\"DEF\"\"\" + abc");
*/
}
TEST_F(FormatTestCSharp, CSharpInterpolatedStringLiterals) {
verifyFormat("foo($\"aaaaaaaa{aaa}aaaa\");");
verifyFormat("foo($\"aaaa{A}\");");
verifyFormat(
"foo($\"aaaa{A}"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\");");
verifyFormat("Name = $\"{firstName} {lastName}\";");
// $"ABC\" + ToString("B") - handle embedded \ in literal string at
// the end
verifyFormat("string s = $\"A{abc}BC\" + ToString(\"B\");");
verifyFormat("$\"{domain}\\\\{user}\"");
verifyFormat(
"var verbatimInterpolated = $@\"C:\\Users\\{userName}\\Documents\\\";");
}
TEST_F(FormatTestCSharp, CSharpFatArrows) {
verifyFormat("Task serverTask = Task.Run(async() => {");
verifyFormat("public override string ToString() => \"{Name}\\{Age}\";");
}
TEST_F(FormatTestCSharp, CSharpConditionalExpressions) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
// conditional expression is not seen as a NullConditional.
verifyFormat("var y = A < B ? -1 : 1;", Style);
}
TEST_F(FormatTestCSharp, CSharpNullConditional) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
verifyFormat(
"public Person(string firstName, string lastName, int? age = null)");
verifyFormat("foo () {\n"
" switch (args?.Length) {}\n"
"}",
Style);
verifyFormat("switch (args?.Length) {}", Style);
verifyFormat("public static void Main(string[] args)\n"
"{\n"
" string dirPath = args?[0];\n"
"}");
Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
verifyFormat("switch(args?.Length) {}", Style);
}
TEST_F(FormatTestCSharp, Attributes) {
verifyFormat("[STAThread]\n"
"static void Main(string[] args)\n"
"{\n"
"}");
verifyFormat("[TestMethod]\n"
"private class Test\n"
"{\n"
"}");
verifyFormat("[TestMethod]\n"
"protected class Test\n"
"{\n"
"}");
verifyFormat("[TestMethod]\n"
"internal class Test\n"
"{\n"
"}");
verifyFormat("[TestMethod]\n"
"class Test\n"
"{\n"
"}");
verifyFormat("[TestMethod]\n"
"[DeploymentItem(\"Test.txt\")]\n"
"public class Test\n"
"{\n"
"}");
verifyFormat("[System.AttributeUsage(System.AttributeTargets.Method)]\n"
"[System.Runtime.InteropServices.ComVisible(true)]\n"
"public sealed class STAThreadAttribute : Attribute\n"
"{\n"
"}");
verifyFormat("[Verb(\"start\", HelpText = \"Starts the server listening on "
"provided port\")]\n"
"class Test\n"
"{\n"
"}");
verifyFormat("[TestMethod]\n"
"public string Host { set; get; }");
verifyFormat("[TestMethod(\"start\", HelpText = \"Starts the server "
"listening on provided host\")]\n"
"public string Host { set; get; }");
verifyFormat(
"[DllImport(\"Hello\", EntryPoint = \"hello_world\")]\n"
"// The const char* returned by hello_world must not be deleted.\n"
"private static extern IntPtr HelloFromCpp();)");
// Class attributes go on their own line and do not affect layout of
// interfaces. Line wrapping decisions previously caused each interface to be
// on its own line.
verifyFormat("[SomeAttribute]\n"
"[SomeOtherAttribute]\n"
"public class A : IShape, IAnimal, IVehicle\n"
"{\n"
" int X;\n"
"}");
// Attributes in a method declaration do not cause line wrapping.
verifyFormat("void MethodA([In][Out] ref double x)\n"
"{\n"
"}");
// [] in an attribute do not cause premature line wrapping or indenting.
verifyFormat(R"(//
public class A
{
[SomeAttribute(new[] { RED, GREEN, BLUE }, -1.0f, 1.0f)]
[DoNotSerialize]
public Data MemberVariable;
})");
// Unwrappable lines go on a line of their own.
// 'target:' is not treated as a label.
// Modify Style to enforce a column limit.
FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
Style.ColumnLimit = 10;
verifyFormat(R"([assembly:InternalsVisibleTo(
"SomeAssembly, PublicKey=SomePublicKeyThatExceedsTheColumnLimit")])",
Style);
}
TEST_F(FormatTestCSharp, CSharpUsing) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
verifyFormat("public void foo () {\n"
" using (StreamWriter sw = new StreamWriter (filenameA)) {}\n"
" using () {}\n"
"}",
Style);
// Ensure clang-format affects top-level snippets correctly.
verifyFormat("using (StreamWriter sw = new StreamWriter (filenameB)) {}",
Style);
Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
verifyFormat("public void foo() {\n"
" using(StreamWriter sw = new StreamWriter(filenameB)) {}\n"
" using() {}\n"
"}",
Style);
// Ensure clang-format affects top-level snippets correctly.
verifyFormat("using(StreamWriter sw = new StreamWriter(filenameB)) {}",
Style);
Style.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements;
verifyFormat("public void foo() {\n"
" using (StreamWriter sw = new StreamWriter(filenameA)) {}\n"
" using () {}\n"
"}",
Style);
// Ensure clang-format affects top-level snippets correctly.
verifyFormat("using (StreamWriter sw = new StreamWriter(filenameB)) {}",
Style);
Style.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
verifyFormat("public void foo() {\n"
" using (StreamWriter sw = new StreamWriter (filenameA)) {}\n"
" using() {}\n"
"}",
Style);
// Ensure clang-format affects top-level snippets correctly.
verifyFormat("using (StreamWriter sw = new StreamWriter (filenameB)) {}",
Style);
}
TEST_F(FormatTestCSharp, CSharpRegions) {
verifyFormat("#region aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaa "
"aaaaaaaaaaaaaaa long region");
}
TEST_F(FormatTestCSharp, CSharpKeyWordEscaping) {
verifyFormat("public enum var\n"
"{\n"
" none,\n"
" @string,\n"
" bool,\n"
" @enum\n"
"}");
}
TEST_F(FormatTestCSharp, CSharpNullCoalescing) {
verifyFormat("var test = ABC ?? DEF");
verifyFormat("string myname = name ?? \"ABC\";");
verifyFormat("return _name ?? \"DEF\";");
}
TEST_F(FormatTestCSharp, AttributesIndentation) {
FormatStyle Style = getMicrosoftStyle(FormatStyle::LK_CSharp);
Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
verifyFormat("[STAThread]\n"
"static void Main(string[] args)\n"
"{\n"
"}",
Style);
verifyFormat("[STAThread]\n"
"void "
"veryLooooooooooooooongFunctionName(string[] args)\n"
"{\n"
"}",
Style);
verifyFormat("[STAThread]\n"
"veryLoooooooooooooooooooongReturnType "
"veryLooooooooooooooongFunctionName(string[] args)\n"
"{\n"
"}",
Style);
verifyFormat("[SuppressMessage(\"A\", \"B\", Justification = \"C\")]\n"
"public override X Y()\n"
"{\n"
"}\n",
Style);
verifyFormat("[SuppressMessage]\n"
"public X Y()\n"
"{\n"
"}\n",
Style);
verifyFormat("[SuppressMessage]\n"
"public override X Y()\n"
"{\n"
"}\n",
Style);
verifyFormat("public A(B b) : base(b)\n"
"{\n"
" [SuppressMessage]\n"
" public override X Y()\n"
" {\n"
" }\n"
"}\n",
Style);
verifyFormat("public A : Base\n"
"{\n"
"}\n"
"[Test]\n"
"public Foo()\n"
"{\n"
"}\n",
Style);
verifyFormat("namespace\n"
"{\n"
"public A : Base\n"
"{\n"
"}\n"
"[Test]\n"
"public Foo()\n"
"{\n"
"}\n"
"}\n",
Style);
}
TEST_F(FormatTestCSharp, CSharpSpaceBefore) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
verifyFormat("List<string> list;", Style);
verifyFormat("Dictionary<string, string> dict;", Style);
verifyFormat("for (int i = 0; i < size (); i++) {\n"
"}",
Style);
verifyFormat("foreach (var x in y) {\n"
"}",
Style);
verifyFormat("switch (x) {}", Style);
verifyFormat("do {\n"
"} while (x);",
Style);
Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
verifyFormat("List<string> list;", Style);
verifyFormat("Dictionary<string, string> dict;", Style);
verifyFormat("for(int i = 0; i < size(); i++) {\n"
"}",
Style);
verifyFormat("foreach(var x in y) {\n"
"}",
Style);
verifyFormat("switch(x) {}", Style);
verifyFormat("do {\n"
"} while(x);",
Style);
}
TEST_F(FormatTestCSharp, CSharpSpaceAfterCStyleCast) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
verifyFormat("(int)x / y;", Style);
Style.SpaceAfterCStyleCast = true;
verifyFormat("(int) x / y;", Style);
}
TEST_F(FormatTestCSharp, CSharpEscapedQuotesInVerbatimStrings) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
verifyFormat(R"(string str = @"""";)", Style);
verifyFormat(R"(string str = @"""Hello world""";)", Style);
verifyFormat(R"(string str = $@"""Hello {friend}""";)", Style);
}
TEST_F(FormatTestCSharp, CSharpQuotesInInterpolatedStrings) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
verifyFormat(R"(string str1 = $"{null ?? "null"}";)", Style);
verifyFormat(R"(string str2 = $"{{{braceCount} braces";)", Style);
verifyFormat(R"(string str3 = $"{braceCount}}} braces";)", Style);
}
TEST_F(FormatTestCSharp, CSharpNewlinesInVerbatimStrings) {
// Use MS style as Google Style inserts a line break before multiline strings.
// verifyFormat does not understand multiline C# string-literals
// so check the format explicitly.
FormatStyle Style = getMicrosoftStyle(FormatStyle::LK_CSharp);
std::string Code = R"(string s1 = $@"some code:
class {className} {{
{className}() {{}}
}}";)";
EXPECT_EQ(Code, format(Code, Style));
// Multiline string in the middle of a function call.
Code = R"(
var x = foo(className, $@"some code:
class {className} {{
{className}() {{}}
}}",
y);)"; // y aligned with `className` arg.
EXPECT_EQ(Code, format(Code, Style));
// Interpolated string with embedded multiline string.
Code = R"(Console.WriteLine($"{string.Join(@",
", values)}");)";
EXPECT_EQ(Code, format(Code, Style));
}
TEST_F(FormatTestCSharp, CSharpLambdas) {
FormatStyle GoogleStyle = getGoogleStyle(FormatStyle::LK_CSharp);
FormatStyle MicrosoftStyle = getMicrosoftStyle(FormatStyle::LK_CSharp);
verifyFormat(R"(//
class MyClass {
Action<string> greet = name => {
string greeting = $"Hello {name}!";
Console.WriteLine(greeting);
};
})",
GoogleStyle);
// Microsoft Style:
// https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions#statement-lambdas
verifyFormat(R"(//
class MyClass
{
Action<string> greet = name =>
{
string greeting = $"Hello {name}!";
Console.WriteLine(greeting);
};
})",
MicrosoftStyle);
}
TEST_F(FormatTestCSharp, CSharpObjectInitializers) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
// Start code fragments with a comment line so that C++ raw string literals
// as seen are identical to expected formatted code.
verifyFormat(R"(//
Shape[] shapes = new[] {
new Circle {
Radius = 2.7281,
Colour = Colours.Red,
},
new Square {
Side = 101.1,
Colour = Colours.Yellow,
},
};)",
Style);
// Omitted final `,`s will change the formatting.
verifyFormat(R"(//
Shape[] shapes = new[] { new Circle { Radius = 2.7281, Colour = Colours.Red },
new Square { Side = 101.1, Colour = Colours.Yellow } };)",
Style);
// Lambdas can be supplied as initialiser arguments.
verifyFormat(R"(//
private Transformer _transformer = new X.Y {
Filler = (Shape shape) => { return new Transform.Fill(shape, RED); },
Scaler = (Shape shape) => { return new Transform.Resize(shape, 0.1); },
};)",
Style);
// Dictionary initialisation.
verifyFormat(R"(//
var myDict = new Dictionary<string, string> {
["name"] = _donald,
["age"] = Convert.ToString(DateTime.Today.Year - 1934),
["type"] = _duck,
};)",
Style);
}
TEST_F(FormatTestCSharp, CSharpArrayInitializers) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
verifyFormat(R"(//
private MySet<Node>[] setPoints = {
new Point<Node>(),
new Point<Node>(),
};)",
Style);
}
TEST_F(FormatTestCSharp, CSharpNamedArguments) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
verifyFormat(R"(//
PrintOrderDetails(orderNum: 31, productName: "Red Mug", sellerName: "Gift Shop");)",
Style);
// Ensure that trailing comments do not cause problems.
verifyFormat(R"(//
PrintOrderDetails(orderNum: 31, productName: "Red Mug", // comment
sellerName: "Gift Shop");)",
Style);
verifyFormat(R"(foreach (var tickCount in task.Begin(seed: 0)) {)", Style);
}
TEST_F(FormatTestCSharp, CSharpPropertyAccessors) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
verifyFormat("int Value { get }", Style);
verifyFormat("int Value { get; }", Style);
verifyFormat("int Value { internal get; }", Style);
verifyFormat("int Value { get; } = 0", Style);
verifyFormat("int Value { set }", Style);
verifyFormat("int Value { set; }", Style);
verifyFormat("int Value { internal set; }", Style);
verifyFormat("int Value { set; } = 0", Style);
verifyFormat("int Value { get; set }", Style);
verifyFormat("int Value { set; get }", Style);
verifyFormat("int Value { get; private set; }", Style);
verifyFormat("int Value { get; set; }", Style);
verifyFormat("int Value { get; set; } = 0", Style);
verifyFormat("int Value { internal get; internal set; }", Style);
// Do not wrap expression body definitions.
verifyFormat(R"(//
public string Name {
get => _name;
set => _name = value;
})",
Style);
// Examples taken from
// https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties
verifyFormat(R"(
// Expression body definitions
public class SaleItem {
public decimal Price {
get => _cost;
set => _cost = value;
}
})",
Style);
verifyFormat(R"(
// Properties with backing fields
class TimePeriod {
public double Hours {
get { return _seconds / 3600; }
set {
if (value < 0 || value > 24)
throw new ArgumentOutOfRangeException($"{nameof(value)} must be between 0 and 24.");
_seconds = value * 3600;
}
}
})",
Style);
verifyFormat(R"(
// Auto-implemented properties
public class SaleItem {
public decimal Price { get; set; }
})",
Style);
// Add column limit to wrap long lines.
Style.ColumnLimit = 100;
// Examples with assignment to default value.
verifyFormat(R"(
// Long assignment to default value
class MyClass {
public override VeryLongNamedTypeIndeed VeryLongNamedValue { get; set } =
VeryLongNamedTypeIndeed.Create(DefaultFirstArgument, DefaultSecondArgument,
DefaultThirdArgument);
})",
Style);
verifyFormat(R"(
// Long assignment to default value with expression body
class MyClass {
public override VeryLongNamedTypeIndeed VeryLongNamedValue {
get => veryLongNamedField;
set => veryLongNamedField = value;
} = VeryLongNamedTypeIndeed.Create(DefaultFirstArgument, DefaultSecondArgument,
DefaultThirdArgument);
})",
Style);
// Brace wrapping and single-lining of accessor can be controlled by config.
Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
Style.BraceWrapping.AfterFunction = true;
verifyFormat(R"(//
class TimePeriod {
public double Hours
{
get {
return _seconds / 3600;
}
set {
_seconds = value * 3600;
}
}
})",
Style);
// Microsoft style trivial property accessors have no line break before the
// opening brace.
auto MicrosoftStyle = getMicrosoftStyle(FormatStyle::LK_CSharp);
verifyFormat(R"(//
public class SaleItem
{
public decimal Price { get; set; }
})",
MicrosoftStyle);
}
TEST_F(FormatTestCSharp, CSharpSpaces) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
Style.SpaceBeforeSquareBrackets = false;
Style.SpacesInSquareBrackets = false;
Style.SpaceBeforeCpp11BracedList = true;
Style.Cpp11BracedListStyle = false;
Style.SpacesInContainerLiterals = false;
Style.SpaceAfterCStyleCast = false;
verifyFormat(R"(new Car { "Door", 0.1 })", Style);
verifyFormat(R"(new Car { 0.1, "Door" })", Style);
verifyFormat(R"(new string[] { "A" })", Style);
verifyFormat(R"(new string[] {})", Style);
verifyFormat(R"(new Car { someVariableName })", Style);
verifyFormat(R"(new Car { someVariableName })", Style);
verifyFormat(R"(new Dictionary<string, string> { ["Key"] = "Value" };)",
Style);
verifyFormat(R"(Apply(x => x.Name, x => () => x.ID);)", Style);
verifyFormat(R"(bool[] xs = { true, true };)", Style);
verifyFormat(R"(taskContext.Factory.Run(async () => doThing(args);)", Style);
verifyFormat(R"(catch (TestException) when (innerFinallyExecuted))", Style);
verifyFormat(R"(private float[,] Values;)", Style);
verifyFormat(R"(Result this[Index x] => Foo(x);)", Style);
verifyFormat(R"(char[,,] rawCharArray = MakeCharacterGrid();)", Style);
verifyFormat(R"(var (key, value))", Style);
// `&&` is not seen as a reference.
verifyFormat(R"(A == typeof(X) && someBool)", Style);
// Not seen as a C-style cast.
verifyFormat(R"(//
foreach ((A a, B b) in someList) {
})",
Style);
// space after lock in `lock (processes)`.
verifyFormat("lock (process)", Style);
Style.SpacesInSquareBrackets = true;
verifyFormat(R"(private float[ , ] Values;)", Style);
verifyFormat(R"(string dirPath = args?[ 0 ];)", Style);
verifyFormat(R"(char[ ,, ] rawCharArray = MakeCharacterGrid();)", Style);
// Method returning tuple
verifyFormat(R"(public (string name, int age) methodTuple() {})", Style);
verifyFormat(R"(private (string name, int age) methodTuple() {})", Style);
verifyFormat(R"(protected (string name, int age) methodTuple() {})", Style);
verifyFormat(R"(virtual (string name, int age) methodTuple() {})", Style);
verifyFormat(R"(extern (string name, int age) methodTuple() {})", Style);
verifyFormat(R"(static (string name, int age) methodTuple() {})", Style);
verifyFormat(R"(internal (string name, int age) methodTuple() {})", Style);
verifyFormat(R"(abstract (string name, int age) methodTuple() {})", Style);
verifyFormat(R"(sealed (string name, int age) methodTuple() {})", Style);
verifyFormat(R"(override (string name, int age) methodTuple() {})", Style);
verifyFormat(R"(async (string name, int age) methodTuple() {})", Style);
verifyFormat(R"(unsafe (string name, int age) methodTuple() {})", Style);
}
TEST_F(FormatTestCSharp, CSharpNullableTypes) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
Style.SpacesInSquareBrackets = false;
verifyFormat(R"(//
public class A {
void foo() {
int? value = some.bar();
}
})",
Style); // int? is nullable not a conditional expression.
verifyFormat(R"(void foo(int? x, int? y, int? z) {})",
Style); // Nullables in function definitions.
verifyFormat(R"(public float? Value;)", Style); // no space before `?`.
verifyFormat(R"(int?[] arr = new int?[10];)",
Style); // An array of a nullable type.
verifyFormat(R"(var x = (int?)y;)", Style); // Cast to a nullable type.
verifyFormat(R"(var x = new MyContainer<int?>();)", Style); // Generics.
}
TEST_F(FormatTestCSharp, CSharpArraySubscripts) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
// Do not format array subscript operators as attributes.
verifyFormat(R"(//
if (someThings[index].Contains(myThing)) {
})",
Style);
verifyFormat(R"(//
if (someThings[i][j][k].Contains(myThing)) {
})",
Style);
}
TEST_F(FormatTestCSharp, CSharpGenericTypeConstraints) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
verifyFormat(R"(//
class ItemFactory<T>
where T : new() {})",
Style);
verifyFormat(R"(//
class Dictionary<TKey, TVal>
where TKey : IComparable<TKey>
where TVal : IMyInterface {
public void MyMethod<T>(T t)
where T : IMyInterface {
doThing();
}
})",
Style);
verifyFormat(R"(//
class ItemFactory<T>
where T : new(), IAnInterface<T>, IAnotherInterface<T>, IAnotherInterfaceStill<T> {})",
Style);
Style.ColumnLimit = 50; // Force lines to be wrapped.
verifyFormat(R"(//
class ItemFactory<T, U>
where T : new(),
IAnInterface<T>,
IAnotherInterface<T, U>,
IAnotherInterfaceStill<T, U> {})",
Style);
// In other languages `where` can be used as a normal identifier.
// This example is in C++!
verifyFormat(R"(//
class A {
int f(int where) {}
};)",
getGoogleStyle(FormatStyle::LK_Cpp));
}
} // namespace format
} // end namespace clang