2
// TestFormattingVisitor.cs
5
// Mike KrĆ¼ger <mkrueger@novell.com>
7
// Copyright (c) 2010 Novell, Inc (http://www.novell.com)
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29
using NUnit.Framework;
30
using ICSharpCode.NRefactory.CSharp;
32
namespace ICSharpCode.NRefactory.CSharp.FormattingTests
35
public class TestSpacingVisitor : TestBase
38
public void TestFieldSpacesBeforeComma1()
40
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
41
policy.ClassBraceStyle = BraceStyle.EndOfLine;
42
policy.SpaceBeforeFieldDeclarationComma = false;
43
policy.SpaceAfterFieldDeclarationComma = false;
45
Test(policy, @"class Test {
54
public void TestFieldSpacesBeforeComma2 ()
56
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
57
policy.ClassBraceStyle = BraceStyle.EndOfLine;
58
policy.SpaceBeforeFieldDeclarationComma = true;
59
policy.SpaceAfterFieldDeclarationComma = true;
61
Test (policy, @"class Test {
70
public void TestFixedFieldSpacesBeforeComma ()
72
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
73
policy.ClassBraceStyle = BraceStyle.EndOfLine;
74
policy.SpaceAfterFieldDeclarationComma = true;
75
policy.SpaceBeforeFieldDeclarationComma = true;
77
Test (policy, @"class Test {
78
fixed int a[10] , b[10], c[10];
81
fixed int a[10] , b[10] , c[10];
86
public void TestConstFieldSpacesBeforeComma ()
88
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
89
policy.ClassBraceStyle = BraceStyle.EndOfLine;
90
policy.SpaceAfterFieldDeclarationComma = false;
91
policy.SpaceBeforeFieldDeclarationComma = false;
93
Test (policy, @"class Test {
94
const int a = 1 , b = 2, c = 3;
97
const int a = 1,b = 2,c = 3;
102
public void TestBeforeMethodDeclarationParentheses ()
104
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
105
policy.SpaceBeforeMethodDeclarationParentheses = true;
107
Test (policy, @"public abstract class Test
109
public abstract Test TestMethod();
111
@"public abstract class Test
113
public abstract Test TestMethod ();
118
public void TestBeforeConstructorDeclarationParenthesesDestructorCase ()
120
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
121
policy.SpaceBeforeConstructorDeclarationParentheses = true;
123
Test (policy, @"class Test
137
static void TestBinaryOperator (CSharpFormattingOptions policy, string op)
139
var result = GetResult (policy, "class Test { void TestMe () { result = left" + op + "right; } }");
141
int i1 = result.Text.IndexOf ("left");
142
int i2 = result.Text.IndexOf ("right") + "right".Length;
143
if (i1 < 0 || i2 < 0)
144
Assert.Fail ("text invalid:" + result.Text);
145
Assert.AreEqual ("left " + op + " right", result.GetText (i1, i2 - i1));
149
public void TestSpacesAroundMultiplicativeOperator ()
151
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
152
policy.SpaceAroundMultiplicativeOperator = true;
154
TestBinaryOperator (policy, "*");
155
TestBinaryOperator (policy, "/");
159
public void TestSpacesAroundShiftOperator ()
161
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
162
policy.SpaceAroundShiftOperator = true;
163
TestBinaryOperator (policy, "<<");
164
TestBinaryOperator (policy, ">>");
168
public void TestSpacesAroundAdditiveOperator ()
170
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
171
policy.SpaceAroundAdditiveOperator = true;
173
TestBinaryOperator (policy, "+");
174
TestBinaryOperator (policy, "-");
178
public void TestSpacesAroundBitwiseOperator ()
180
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
181
policy.SpaceAroundBitwiseOperator = true;
183
TestBinaryOperator (policy, "&");
184
TestBinaryOperator (policy, "|");
185
TestBinaryOperator (policy, "^");
189
public void TestSpacesAroundRelationalOperator ()
191
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
192
policy.SpaceAroundRelationalOperator = true;
194
TestBinaryOperator (policy, "<");
195
TestBinaryOperator (policy, "<=");
196
TestBinaryOperator (policy, ">");
197
TestBinaryOperator (policy, ">=");
201
public void TestSpacesAroundEqualityOperator ()
203
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
204
policy.SpaceAroundEqualityOperator = true;
206
TestBinaryOperator (policy, "==");
207
TestBinaryOperator (policy, "!=");
211
public void TestSpacesAroundLogicalOperator ()
213
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
214
policy.SpaceAroundLogicalOperator = true;
216
TestBinaryOperator (policy, "&&");
217
TestBinaryOperator (policy, "||");
221
public void TestConditionalOperator ()
223
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
224
policy.SpaceBeforeConditionalOperatorCondition = true;
225
policy.SpaceAfterConditionalOperatorCondition = true;
226
policy.SpaceBeforeConditionalOperatorSeparator = true;
227
policy.SpaceAfterConditionalOperatorSeparator = true;
229
var result = GetResult (policy, @"class Test {
232
result = condition?trueexpr:falseexpr;
235
int i1 = result.Text.IndexOf ("condition");
236
int i2 = result.Text.IndexOf ("falseexpr") + "falseexpr".Length;
237
Assert.AreEqual (@"condition ? trueexpr : falseexpr", result.GetText (i1, i2 - i1));
240
policy.SpaceBeforeConditionalOperatorCondition = false;
241
policy.SpaceAfterConditionalOperatorCondition = false;
242
policy.SpaceBeforeConditionalOperatorSeparator = false;
243
policy.SpaceAfterConditionalOperatorSeparator = false;
245
result = GetResult (policy, @"class Test {
248
result = true ? trueexpr : falseexpr;
251
i1 = result.Text.IndexOf ("true");
252
i2 = result.Text.IndexOf ("falseexpr") + "falseexpr".Length;
253
Assert.AreEqual (@"true?trueexpr:falseexpr", result.GetText (i1, i2 - i1));
257
public void TestBeforeMethodCallParenthesesSpace ()
259
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
260
policy.SpaceBeforeMethodCallParentheses = true;
262
var result = GetResult (policy, @"class Test {
269
int i1 = result.Text.IndexOf ("MethodCall");
270
int i2 = result.Text.IndexOf (";") + ";".Length;
271
Assert.AreEqual (@"MethodCall ();", result.GetText (i1, i2 - i1));
274
result = GetResult (policy, @"class Test {
280
policy.SpaceBeforeMethodCallParentheses = false;
282
result = GetResult (policy, result.Text);
283
i1 = result.Text.IndexOf ("MethodCall");
284
i2 = result.Text.IndexOf (";") + ";".Length;
285
Assert.AreEqual (@"MethodCall();", result.GetText (i1, i2 - i1));
289
public void TestWithinMethodCallParenthesesSpace ()
291
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
292
policy.SpaceWithinMethodCallParentheses = true;
294
var result = GetResult (policy, @"class Test {
300
int i1 = result.Text.LastIndexOf ("(");
301
int i2 = result.Text.LastIndexOf (")") + ")".Length;
302
Assert.AreEqual (@"( true )", result.GetText (i1, i2 - i1));
305
policy.SpaceWithinMethodCallParentheses = false;
306
result = GetResult (policy, @"class Test {
313
i1 = result.Text.LastIndexOf ("(");
314
i2 = result.Text.LastIndexOf (")") + ")".Length;
315
Assert.AreEqual (@"(true)", result.GetText (i1, i2 - i1));
319
public void TestBeforeIfParenthesesSpace ()
321
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
322
policy.SpaceBeforeIfParentheses = true;
324
var result = GetResult (policy, @"class Test {
330
int i1 = result.Text.IndexOf ("if");
331
int i2 = result.Text.LastIndexOf (")") + ")".Length;
332
Assert.AreEqual (@"if (true)", result.GetText (i1, i2 - i1));
336
public void TestWithinIfParenthesesSpace ()
338
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
339
policy.SpacesWithinIfParentheses = true;
341
var result = GetResult (policy, @"class Test {
347
int i1 = result.Text.LastIndexOf ("(");
348
int i2 = result.Text.LastIndexOf (")") + ")".Length;
349
Assert.AreEqual (@"( true )", result.GetText (i1, i2 - i1));
353
public void TestBeforeWhileParenthesesSpace ()
355
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
356
policy.SpaceBeforeWhileParentheses = true;
358
var result = GetResult (policy, @"class Test {
364
int i1 = result.Text.IndexOf ("while");
365
int i2 = result.Text.LastIndexOf (")") + ")".Length;
366
Assert.AreEqual (@"while (true)", result.GetText (i1, i2 - i1));
370
public void TestWithinWhileParenthesesSpace ()
372
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
373
policy.SpacesWithinWhileParentheses = true;
375
var result = GetResult (policy, @"class Test {
382
int i1 = result.Text.LastIndexOf ("(");
383
int i2 = result.Text.LastIndexOf (")") + ")".Length;
384
Assert.AreEqual (@"( true )", result.GetText (i1, i2 - i1));
388
public void TestBeforeForParenthesesSpace ()
390
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
391
policy.SpaceBeforeForParentheses = true;
393
var result = GetResult (policy, @"class Test {
399
int i1 = result.Text.IndexOf ("for");
400
int i2 = result.Text.LastIndexOf ("(") + "(".Length;
401
Assert.AreEqual (@"for (", result.GetText (i1, i2 - i1));
405
public void TestWithinForParenthesesSpace ()
407
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
408
policy.SpacesWithinForParentheses = true;
410
var result = GetResult (policy, @"class Test {
416
int i1 = result.Text.LastIndexOf ("(");
417
int i2 = result.Text.LastIndexOf (")") + ")".Length;
418
Assert.AreEqual (@"( ;; )", result.GetText (i1, i2 - i1));
422
public void TestBeforeForeachParenthesesSpace ()
424
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
425
policy.SpaceBeforeForeachParentheses = true;
427
var result = GetResult (policy, @"class Test {
430
foreach(var o in list);
433
int i1 = result.Text.IndexOf ("foreach");
434
int i2 = result.Text.LastIndexOf (")") + ")".Length;
435
Assert.AreEqual (@"foreach (var o in list)", result.GetText (i1, i2 - i1));
439
public void TestWithinForeachParenthesesSpace ()
441
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
442
policy.SpacesWithinForeachParentheses = true;
444
var result = GetResult (policy, @"class Test {
447
foreach(var o in list);
450
int i1 = result.Text.LastIndexOf ("(");
451
int i2 = result.Text.LastIndexOf (")") + ")".Length;
452
Assert.AreEqual (@"( var o in list )", result.GetText (i1, i2 - i1));
456
public void TestBeforeCatchParenthesesSpace ()
458
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
459
policy.SpaceBeforeCatchParentheses = true;
461
var result = GetResult (policy, @"class Test {
464
try {} catch(Exception) {}
467
int i1 = result.Text.IndexOf ("catch");
468
int i2 = result.Text.LastIndexOf (")") + ")".Length;
469
Assert.AreEqual (@"catch (Exception)", result.GetText (i1, i2 - i1));
473
public void TestWithinCatchParenthesesSpace ()
475
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
476
policy.SpacesWithinCatchParentheses = true;
478
var result = GetResult (policy, @"class Test {
481
try {} catch(Exception) {}
484
int i1 = result.Text.LastIndexOf ("(");
485
int i2 = result.Text.LastIndexOf (")") + ")".Length;
486
Assert.AreEqual (@"( Exception )", result.GetText (i1, i2 - i1));
490
public void TestBeforeLockParenthesesSpace ()
492
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
493
policy.SpaceBeforeLockParentheses = true;
495
var result = GetResult (policy, @"class Test {
501
int i1 = result.Text.IndexOf ("lock");
502
int i2 = result.Text.LastIndexOf (")") + ")".Length;
503
Assert.AreEqual (@"lock (this)", result.GetText (i1, i2 - i1));
507
public void TestWithinLockParenthesesSpace ()
509
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
510
policy.SpacesWithinLockParentheses = true;
512
var result = GetResult (policy, @"class Test {
518
int i1 = result.Text.LastIndexOf ("(");
519
int i2 = result.Text.LastIndexOf (")") + ")".Length;
520
Assert.AreEqual (@"( this )", result.GetText (i1, i2 - i1));
524
public void TestSpacesAfterForSemicolon ()
526
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
527
policy.SpaceAfterForSemicolon = true;
529
var result = GetResult (policy, @"class Test {
532
for (int i;true;i++) ;
535
int i1 = result.Text.LastIndexOf ("for");
536
int i2 = result.Text.LastIndexOf (")") + ")".Length;
538
Assert.AreEqual (@"for (int i; true; i++)", result.GetText (i1, i2 - i1));
542
public void TestSpacesBeforeForSemicolon ()
544
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
545
policy.SpaceBeforeForSemicolon = true;
546
policy.SpaceAfterForSemicolon = false;
548
var result = GetResult (policy, @"class Test {
551
for (int i;true;i++) ;
554
int i1 = result.Text.LastIndexOf ("for");
555
int i2 = result.Text.LastIndexOf (")") + ")".Length;
557
Assert.AreEqual (@"for (int i ;true ;i++)", result.GetText (i1, i2 - i1));
561
public void TestSpacesAfterTypecast ()
563
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
564
policy.SpaceAfterTypecast = true;
566
var result = GetResult (policy, @"class Test {
572
int i1 = result.Text.LastIndexOf ("return");
573
int i2 = result.Text.LastIndexOf ("null") + "null".Length;
575
Assert.AreEqual (@"return (Test) null", result.GetText (i1, i2 - i1));
579
public void TestBeforeUsingParenthesesSpace ()
581
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
582
policy.SpaceBeforeUsingParentheses = true;
584
var result = GetResult (policy, @"class Test {
590
int i1 = result.Text.IndexOf ("using");
591
int i2 = result.Text.LastIndexOf ("(") + "(".Length;
592
Assert.AreEqual (@"using (", result.GetText (i1, i2 - i1));
596
public void TestWithinUsingParenthesesSpace ()
598
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
599
policy.SpacesWithinUsingParentheses = true;
601
var result = GetResult (policy, @"class Test {
607
int i1 = result.Text.LastIndexOf ("(");
608
int i2 = result.Text.LastIndexOf (")") + ")".Length;
609
Assert.AreEqual (@"( a )", result.GetText (i1, i2 - i1));
612
static void TestAssignmentOperator (CSharpFormattingOptions policy, string op)
614
var result = GetResult (policy, "class Test { void TestMe () { left" + op + "right; } }");
616
int i1 = result.Text.IndexOf ("left");
617
int i2 = result.Text.IndexOf ("right") + "right".Length;
618
if (i1 < 0 || i2 < 0)
619
Assert.Fail ("text invalid:" + result.Text);
620
Assert.AreEqual ("left " + op + " right", result.GetText (i1, i2 - i1));
624
public void TestAroundAssignmentSpace ()
626
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
627
policy.SpaceAroundAssignment = true;
629
TestAssignmentOperator (policy, "=");
630
TestAssignmentOperator (policy, "*=");
631
TestAssignmentOperator (policy, "/=");
632
TestAssignmentOperator (policy, "+=");
633
TestAssignmentOperator (policy, "%=");
634
TestAssignmentOperator (policy, "-=");
635
TestAssignmentOperator (policy, "<<=");
636
TestAssignmentOperator (policy, ">>=");
637
TestAssignmentOperator (policy, "&=");
638
TestAssignmentOperator (policy, "|=");
639
TestAssignmentOperator (policy, "^=");
643
public void TestAroundAssignmentSpaceInDeclarations ()
645
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
646
policy.SpaceAroundAssignment = true;
647
var result = GetResult (policy, @"class Test {
654
int i1 = result.Text.LastIndexOf ("left");
655
int i2 = result.Text.LastIndexOf ("right") + "right".Length;
656
Assert.AreEqual (@"left = right", result.GetText (i1, i2 - i1));
660
public void TestBeforeSwitchParenthesesSpace ()
662
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
663
policy.SpaceBeforeSwitchParentheses = true;
665
var result = GetResult (policy, @"class Test {
668
switch (test) { default: break; }
671
int i1 = result.Text.IndexOf ("switch");
672
int i2 = result.Text.LastIndexOf ("(") + "(".Length;
673
Assert.AreEqual (@"switch (", result.GetText (i1, i2 - i1));
677
public void TestWithinSwitchParenthesesSpace ()
679
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
680
policy.SpacesWithinSwitchParentheses = true;
682
var result = GetResult (policy, @"class Test {
685
switch (test) { default: break; }
688
int i1 = result.Text.LastIndexOf ("(");
689
int i2 = result.Text.LastIndexOf (")") + ")".Length;
690
Assert.AreEqual (@"( test )", result.GetText (i1, i2 - i1));
694
public void TestWithinParenthesesSpace ()
696
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
697
policy.SpacesWithinParentheses = true;
699
var result = GetResult (policy, @"class Test {
705
int i1 = result.Text.LastIndexOf ("(");
706
int i2 = result.Text.LastIndexOf (")") + ")".Length;
707
Assert.AreEqual (@"( test )", result.GetText (i1, i2 - i1));
711
public void TestWithinMethodDeclarationParenthesesSpace ()
713
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
714
policy.SpaceWithinMethodDeclarationParentheses = true;
716
var result = GetResult (policy, @"class Test {
721
int i1 = result.Text.LastIndexOf ("(");
722
int i2 = result.Text.LastIndexOf (")") + ")".Length;
723
Assert.AreEqual (@"( int a )", result.GetText (i1, i2 - i1));
727
public void TestWithinCastParenthesesSpace ()
729
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
730
policy.SpacesWithinCastParentheses = true;
732
var result = GetResult (policy, @"class Test {
738
int i1 = result.Text.LastIndexOf ("(");
739
int i2 = result.Text.LastIndexOf (")") + ")".Length;
740
Assert.AreEqual (@"( int )", result.GetText (i1, i2 - i1));
744
public void TestWithinSizeOfParenthesesSpace ()
746
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
747
policy.SpacesWithinSizeOfParentheses = true;
749
var result = GetResult (policy, @"class Test {
755
int i1 = result.Text.LastIndexOf ("(");
756
int i2 = result.Text.LastIndexOf (")") + ")".Length;
757
Assert.AreEqual (@"( int )", result.GetText (i1, i2 - i1));
761
public void TestBeforeSizeOfParentheses ()
763
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
764
policy.SpaceBeforeSizeOfParentheses = true;
766
var result = GetResult (policy, @"class Test {
772
int i1 = result.Text.LastIndexOf ("sizeof");
773
int i2 = result.Text.LastIndexOf ("(") + "(".Length;
774
Assert.AreEqual (@"sizeof (", result.GetText (i1, i2 - i1));
778
public void TestWithinTypeOfParenthesesSpace ()
780
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
781
policy.SpacesWithinTypeOfParentheses = true;
783
var result = GetResult (policy, @"class Test {
789
int i1 = result.Text.LastIndexOf ("(");
790
int i2 = result.Text.LastIndexOf (")") + ")".Length;
791
Assert.AreEqual (@"( int )", result.GetText (i1, i2 - i1));
795
public void TestBeforeTypeOfParentheses ()
797
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
798
policy.SpaceBeforeTypeOfParentheses = true;
800
var result = GetResult (policy, @"class Test {
807
int i1 = result.Text.LastIndexOf ("typeof");
808
int i2 = result.Text.LastIndexOf ("(") + "(".Length;
809
Assert.AreEqual (@"typeof (", result.GetText (i1, i2 - i1));
813
public void TestWithinCheckedExpressionParanthesesSpace ()
815
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
816
policy.SpacesWithinCheckedExpressionParantheses = true;
818
var result = GetResult (policy, @"class Test {
824
int i1 = result.Text.LastIndexOf ("(");
825
int i2 = result.Text.LastIndexOf (")") + ")".Length;
826
Assert.AreEqual (@"( a + b )", result.GetText (i1, i2 - i1));
828
result = GetResult (policy, @"class Test {
831
a = unchecked(a + b);
835
result = GetResult (policy, result.Text);
836
i1 = result.Text.LastIndexOf ("(");
837
i2 = result.Text.LastIndexOf (")") + ")".Length;
838
Assert.AreEqual (@"( a + b )", result.GetText (i1, i2 - i1));
842
public void TestSpaceBeforeNewParentheses ()
844
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
845
policy.SpaceBeforeNewParentheses = true;
847
var result = GetResult (policy, @"class Test {
853
int i1 = result.Text.LastIndexOf ("new");
854
int i2 = result.Text.LastIndexOf (";") + ";".Length;
855
Assert.AreEqual (@"new Test ();", result.GetText (i1, i2 - i1));
859
public void TestWithinNewParentheses ()
861
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
862
policy.SpacesWithinNewParentheses = true;
864
var result = GetResult (policy, @"class Test {
870
int i1 = result.Text.LastIndexOf ("new");
871
int i2 = result.Text.LastIndexOf (";") + ";".Length;
872
Assert.AreEqual (@"new Test ( 1 );", result.GetText (i1, i2 - i1));
876
public void TestBetweenEmptyNewParentheses ()
878
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
879
policy.SpacesBetweenEmptyNewParentheses = true;
881
var result = GetResult (policy, @"class Test {
887
int i1 = result.Text.LastIndexOf ("new");
888
int i2 = result.Text.LastIndexOf (";") + ";".Length;
889
Assert.AreEqual (@"new Test ( );", result.GetText (i1, i2 - i1));
893
public void TestBeforeNewParameterComma ()
895
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
896
policy.SpaceBeforeNewParameterComma = true;
897
policy.SpaceAfterNewParameterComma = false;
899
var result = GetResult (policy, @"class Test {
905
int i1 = result.Text.LastIndexOf ("new");
906
int i2 = result.Text.LastIndexOf (";") + ";".Length;
907
Assert.AreEqual (@"new Test (1 ,2);", result.GetText (i1, i2 - i1));
911
public void TestAfterNewParameterComma ()
913
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
914
policy.SpaceAfterNewParameterComma = true;
916
var result = GetResult (policy, @"class Test {
922
int i1 = result.Text.LastIndexOf ("new");
923
int i2 = result.Text.LastIndexOf (";") + ";".Length;
924
Assert.AreEqual (@"new Test (1, 2);", result.GetText (i1, i2 - i1));
928
public void TestFieldDeclarationComma ()
930
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
931
policy.SpaceBeforeFieldDeclarationComma = false;
932
policy.SpaceAfterFieldDeclarationComma = true;
934
var result = GetResult (policy, @"class Test {
937
int i1 = result.Text.LastIndexOf ("int");
938
int i2 = result.Text.LastIndexOf (";") + ";".Length;
939
Assert.AreEqual (@"int a, b, c;", result.GetText (i1, i2 - i1));
940
policy.SpaceBeforeFieldDeclarationComma = true;
942
result = GetResult (policy, result.Text);
943
i1 = result.Text.LastIndexOf ("int");
944
i2 = result.Text.LastIndexOf (";") + ";".Length;
945
Assert.AreEqual (@"int a , b , c;", result.GetText (i1, i2 - i1));
947
policy.SpaceBeforeFieldDeclarationComma = false;
948
policy.SpaceAfterFieldDeclarationComma = false;
949
result = GetResult (policy, result.Text);
950
i1 = result.Text.LastIndexOf ("int");
951
i2 = result.Text.LastIndexOf (";") + ";".Length;
952
Assert.AreEqual (@"int a,b,c;", result.GetText (i1, i2 - i1));
956
public void TestBeforeMethodDeclarationParameterComma ()
958
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
959
policy.SpaceBeforeMethodDeclarationParameterComma = true;
960
policy.SpaceAfterMethodDeclarationParameterComma = false;
962
var result = GetResult (policy, @"class Test {
963
public void Foo (int a,int b,int c) {}
965
int i1 = result.Text.LastIndexOf ("(");
966
int i2 = result.Text.LastIndexOf (")") + ")".Length;
967
Assert.AreEqual (@"(int a ,int b ,int c)", result.GetText (i1, i2 - i1));
969
policy.SpaceBeforeMethodDeclarationParameterComma = false;
970
result = GetResult (policy, result.Text);
971
i1 = result.Text.LastIndexOf ("(");
972
i2 = result.Text.LastIndexOf (")") + ")".Length;
973
Assert.AreEqual (@"(int a,int b,int c)", result.GetText (i1, i2 - i1));
977
public void TestAfterMethodDeclarationParameterComma ()
979
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
980
policy.SpaceBeforeMethodDeclarationParameterComma = false;
981
policy.SpaceAfterMethodDeclarationParameterComma = true;
983
var result = GetResult (policy, @"class Test {
984
public void Foo (int a,int b,int c) {}
986
int i1 = result.Text.LastIndexOf ("(");
987
int i2 = result.Text.LastIndexOf (")") + ")".Length;
988
Assert.AreEqual (@"(int a, int b, int c)", result.GetText (i1, i2 - i1));
990
policy.SpaceAfterMethodDeclarationParameterComma = false;
991
result = GetResult (policy, result.Text);
992
i1 = result.Text.LastIndexOf ("(");
993
i2 = result.Text.LastIndexOf (")") + ")".Length;
994
Assert.AreEqual (@"(int a,int b,int c)", result.GetText (i1, i2 - i1));
998
public void TestSpacesInLambdaExpression ()
1000
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1001
policy.SpacesWithinWhileParentheses = true;
1003
var result = GetResult (policy, @"class Test {
1009
int i1 = result.Text.IndexOf ("x");
1010
int i2 = result.Text.LastIndexOf ("null") + "null".Length;
1011
Assert.AreEqual (@"x => x != null", result.GetText (i1, i2 - i1));
1015
public void TestBeforeLocalVariableDeclarationComma ()
1017
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1018
policy.SpaceBeforeLocalVariableDeclarationComma = true;
1019
policy.SpaceAfterLocalVariableDeclarationComma = false;
1021
var result = GetResult (policy, @"class Test {
1027
int i1 = result.Text.IndexOf ("int");
1028
int i2 = result.Text.IndexOf (";") + ";".Length;
1029
Assert.AreEqual (@"int a ,b ,c;", result.GetText (i1, i2 - i1));
1031
result = GetResult (policy, result.Text);
1033
policy.SpaceBeforeLocalVariableDeclarationComma = false;
1035
result = GetResult (policy, result.Text);
1036
i1 = result.Text.IndexOf ("int");
1037
i2 = result.Text.IndexOf (";") + ";".Length;
1038
Assert.AreEqual (@"int a,b,c;", result.GetText (i1, i2 - i1));
1042
public void TestLocalVariableDeclarationComma ()
1044
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1045
policy.SpaceBeforeLocalVariableDeclarationComma = true;
1046
policy.SpaceAfterLocalVariableDeclarationComma = true;
1048
var result = GetResult (policy, @"class Test {
1054
int i1 = result.Text.IndexOf ("int");
1055
int i2 = result.Text.IndexOf (";") + ";".Length;
1056
Assert.AreEqual (@"int a = 5 , b = 6 , c;", result.GetText (i1, i2 - i1));
1058
result = GetResult (policy, result.Text);
1060
policy.SpaceBeforeLocalVariableDeclarationComma = false;
1061
policy.SpaceAfterLocalVariableDeclarationComma = false;
1063
result = GetResult (policy, result.Text);
1064
i1 = result.Text.IndexOf ("int");
1065
i2 = result.Text.IndexOf (";") + ";".Length;
1066
Assert.AreEqual (@"int a = 5,b = 6,c;", result.GetText (i1, i2 - i1));
1070
public void TestLocalVariableWithGenerics ()
1072
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1073
policy.SpaceBeforeLocalVariableDeclarationComma = true;
1074
policy.SpaceAfterLocalVariableDeclarationComma = true;
1076
var result = GetResult (policy, @"class Test {
1082
int i1 = result.Text.IndexOf ("List");
1083
int i2 = result.Text.IndexOf (";") + ";".Length;
1084
Assert.AreEqual (@"List<Test> a;", result.GetText (i1, i2 - i1));
1088
#region Constructors
1091
public void TestBeforeConstructorDeclarationParentheses ()
1093
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1094
policy.SpaceBeforeConstructorDeclarationParentheses = true;
1096
var result = GetResult (policy, @"class Test
1103
Assert.AreEqual (NormalizeNewlines(@"class Test
1112
public void TestBeforeConstructorDeclarationParameterComma ()
1114
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1115
policy.SpaceBeforeConstructorDeclarationParameterComma = true;
1116
policy.SpaceAfterConstructorDeclarationParameterComma = false;
1118
var result = GetResult (policy, @"class Test {
1119
public Test (int a,int b,int c) {}
1122
int i1 = result.Text.LastIndexOf ("(");
1123
int i2 = result.Text.LastIndexOf (")") + ")".Length;
1124
Assert.AreEqual (@"(int a ,int b ,int c)", result.GetText (i1, i2 - i1));
1126
policy.SpaceBeforeConstructorDeclarationParameterComma = false;
1127
result = GetResult (policy, result.Text);
1129
i1 = result.Text.LastIndexOf ("(");
1130
i2 = result.Text.LastIndexOf (")") + ")".Length;
1131
Assert.AreEqual (@"(int a,int b,int c)", result.GetText (i1, i2 - i1));
1135
public void TestAfterConstructorDeclarationParameterComma ()
1137
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1138
policy.SpaceBeforeConstructorDeclarationParameterComma = false;
1139
policy.SpaceAfterConstructorDeclarationParameterComma = true;
1141
var result = GetResult (policy, @"class Test {
1142
public Test (int a,int b,int c) {}
1144
int i1 = result.Text.LastIndexOf ("(");
1145
int i2 = result.Text.LastIndexOf (")") + ")".Length;
1146
Assert.AreEqual (@"(int a, int b, int c)", result.GetText (i1, i2 - i1));
1148
policy.SpaceAfterConstructorDeclarationParameterComma = false;
1149
result = GetResult (policy, result.Text);
1150
i1 = result.Text.LastIndexOf ("(");
1151
i2 = result.Text.LastIndexOf (")") + ")".Length;
1152
Assert.AreEqual (@"(int a,int b,int c)", result.GetText (i1, i2 - i1));
1156
public void TestWithinConstructorDeclarationParentheses ()
1158
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1159
policy.SpaceWithinConstructorDeclarationParentheses = true;
1161
var result = GetResult (policy, @"class Test {
1166
int i1 = result.Text.LastIndexOf ("(");
1167
int i2 = result.Text.LastIndexOf (")") + ")".Length;
1168
Assert.AreEqual (@"( int a )", result.GetText (i1, i2 - i1));
1172
public void TestBetweenEmptyConstructorDeclarationParentheses ()
1174
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1175
policy.SpaceBetweenEmptyConstructorDeclarationParentheses = true;
1177
var result = GetResult (policy, @"class Test {
1182
int i1 = result.Text.LastIndexOf ("(");
1183
int i2 = result.Text.LastIndexOf (")") + ")".Length;
1184
Assert.AreEqual (@"( )", result.GetText (i1, i2 - i1));
1191
public void TestBeforeDelegateDeclarationParentheses ()
1193
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1194
policy.SpaceBeforeDelegateDeclarationParentheses = true;
1196
var result = GetResult (policy, @"delegate void Test();");
1198
Assert.AreEqual (@"delegate void Test ();", result.Text);
1202
public void TestBeforeDelegateDeclarationParenthesesComplex ()
1204
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1205
policy.SpaceBeforeDelegateDeclarationParentheses = true;
1207
var result = GetResult (policy, "delegate void TestDelegate\t\t\t();");
1209
Assert.AreEqual (@"delegate void TestDelegate ();", result.Text);
1213
public void TestBeforeDelegateDeclarationParameterComma ()
1215
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1216
policy.SpaceBeforeDelegateDeclarationParameterComma = true;
1217
policy.SpaceAfterDelegateDeclarationParameterComma = false;
1219
var result = GetResult (policy, @"delegate void Test (int a,int b,int c);");
1221
int i1 = result.Text.LastIndexOf ("(");
1222
int i2 = result.Text.LastIndexOf (")") + ")".Length;
1223
Assert.AreEqual (@"(int a ,int b ,int c)", result.GetText (i1, i2 - i1));
1225
policy.SpaceBeforeDelegateDeclarationParameterComma = false;
1226
result = GetResult (policy, result.Text);
1227
i1 = result.Text.LastIndexOf ("(");
1228
i2 = result.Text.LastIndexOf (")") + ")".Length;
1229
Assert.AreEqual (@"(int a,int b,int c)", result.GetText (i1, i2 - i1));
1233
public void TestAfterDelegateDeclarationParameterComma ()
1235
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1236
policy.SpaceBeforeDelegateDeclarationParameterComma = false;
1237
policy.SpaceAfterDelegateDeclarationParameterComma = true;
1239
var result = GetResult (policy, @"delegate void Test (int a,int b,int c);");
1241
int i1 = result.Text.LastIndexOf ("(");
1242
int i2 = result.Text.LastIndexOf (")") + ")".Length;
1243
Assert.AreEqual (@"(int a, int b, int c)", result.GetText (i1, i2 - i1));
1245
policy.SpaceAfterDelegateDeclarationParameterComma = false;
1246
result = GetResult (policy, result.Text);
1247
i1 = result.Text.LastIndexOf ("(");
1248
i2 = result.Text.LastIndexOf (")") + ")".Length;
1249
Assert.AreEqual (@"(int a,int b,int c)", result.GetText (i1, i2 - i1));
1253
public void TestWithinDelegateDeclarationParentheses ()
1255
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1256
policy.SpaceWithinDelegateDeclarationParentheses = true;
1257
var result = GetResult (policy, @"delegate void Test (int a);");
1259
int i1 = result.Text.LastIndexOf ("(");
1260
int i2 = result.Text.LastIndexOf (")") + ")".Length;
1261
Assert.AreEqual (@"( int a )", result.GetText (i1, i2 - i1));
1265
public void TestBetweenEmptyDelegateDeclarationParentheses ()
1267
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1268
policy.SpaceBetweenEmptyDelegateDeclarationParentheses = true;
1269
var result = GetResult (policy, @"delegate void Test();");
1271
int i1 = result.Text.LastIndexOf ("(");
1272
int i2 = result.Text.LastIndexOf (")") + ")".Length;
1273
Assert.AreEqual (@"( )", result.GetText (i1, i2 - i1));
1278
#region Method invocations
1280
public void TestBeforeMethodCallParentheses ()
1282
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1283
policy.SpaceBeforeMethodCallParentheses = true;
1285
var result = GetResult (policy, @"class FooBar
1293
Assert.AreEqual (NormalizeNewlines(@"class FooBar
1303
public void TestBeforeMethodCallParameterComma ()
1305
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1306
policy.SpaceBeforeMethodCallParameterComma = true;
1307
policy.SpaceAfterMethodCallParameterComma = false;
1309
var result = GetResult (policy, @"class FooBar
1316
int i1 = result.Text.LastIndexOf ("(");
1317
int i2 = result.Text.LastIndexOf (")") + ")".Length;
1318
Assert.AreEqual (@"(a ,b ,c)", result.GetText (i1, i2 - i1));
1320
policy.SpaceBeforeMethodCallParameterComma = false;
1321
result = GetResult (policy, result.Text);
1322
i1 = result.Text.LastIndexOf ("(");
1323
i2 = result.Text.LastIndexOf (")") + ")".Length;
1324
Assert.AreEqual (@"(a,b,c)", result.GetText (i1, i2 - i1));
1328
public void TestAfterMethodCallParameterComma ()
1330
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1331
policy.SpaceBeforeMethodCallParameterComma = false;
1332
policy.SpaceAfterMethodCallParameterComma = true;
1334
var result = GetResult (policy, @"class FooBar
1341
int i1 = result.Text.LastIndexOf ("(");
1342
int i2 = result.Text.LastIndexOf (")") + ")".Length;
1343
Assert.AreEqual (@"(a, b, c)", result.GetText (i1, i2 - i1));
1345
policy.SpaceAfterMethodCallParameterComma = false;
1346
result = GetResult (policy, result.Text);
1347
i1 = result.Text.LastIndexOf ("(");
1348
i2 = result.Text.LastIndexOf (")") + ")".Length;
1349
Assert.AreEqual (@"(a,b,c)", result.GetText (i1, i2 - i1));
1353
public void TestWithinMethodCallParentheses ()
1355
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1356
policy.SpaceWithinMethodCallParentheses = true;
1358
var result = GetResult (policy, @"class FooBar
1365
int i1 = result.Text.LastIndexOf ("(");
1366
int i2 = result.Text.LastIndexOf (")") + ")".Length;
1367
Assert.AreEqual (@"( a )", result.GetText (i1, i2 - i1));
1371
public void TestBetweenEmptyMethodCallParentheses ()
1373
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1374
policy.SpaceBetweenEmptyMethodCallParentheses = true;
1376
var result = GetResult (policy, @"class FooBar
1383
int i1 = result.Text.LastIndexOf ("(");
1384
int i2 = result.Text.LastIndexOf (")") + ")".Length;
1385
Assert.AreEqual (@"( )", result.GetText (i1, i2 - i1));
1390
#region Indexer declarations
1392
public void TestBeforeIndexerDeclarationBracket ()
1395
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1396
policy.SpaceBeforeIndexerDeclarationBracket = true;
1398
var result = GetResult (policy, @"class FooBar
1400
public int this[int a, int b] {
1406
Assert.AreEqual (NormalizeNewlines(@"class FooBar
1408
public int this [int a, int b] {
1417
public void TestBeforeIndexerDeclarationParameterComma ()
1419
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1420
policy.SpaceBeforeIndexerDeclarationParameterComma = true;
1421
policy.SpaceAfterIndexerDeclarationParameterComma = false;
1423
var result = GetResult (policy, @"class FooBar
1425
public int this[int a,int b] {
1431
int i1 = result.Text.LastIndexOf ("[");
1432
int i2 = result.Text.LastIndexOf ("]") + "]".Length;
1433
Assert.AreEqual (@"[int a ,int b]", result.GetText (i1, i2 - i1));
1438
public void TestAfterIndexerDeclarationParameterComma ()
1440
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1441
policy.SpaceAfterIndexerDeclarationParameterComma = true;
1443
var result = GetResult (policy, @"class FooBar
1445
public int this[int a,int b] {
1451
int i1 = result.Text.LastIndexOf ("[");
1452
int i2 = result.Text.LastIndexOf ("]") + "]".Length;
1453
Assert.AreEqual (@"[int a, int b]", result.GetText (i1, i2 - i1));
1457
public void TestWithinIndexerDeclarationBracket ()
1459
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1460
policy.SpaceWithinIndexerDeclarationBracket = true;
1462
var result = GetResult (policy, @"class FooBar
1464
public int this[int a, int b] {
1470
int i1 = result.Text.LastIndexOf ("[");
1471
int i2 = result.Text.LastIndexOf ("]") + "]".Length;
1472
Assert.AreEqual (@"[ int a, int b ]", result.GetText (i1, i2 - i1));
1480
public void TestSpacesWithinBrackets ()
1482
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1483
policy.SpacesWithinBrackets = true;
1484
policy.SpacesBeforeBrackets = false;
1486
var result = GetResult (policy, @"class Test
1493
Assert.AreEqual (NormalizeNewlines(@"class Test
1505
public void TestSpacesBeforeBrackets ()
1507
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1508
policy.SpacesBeforeBrackets = true;
1510
var result = GetResult (policy, @"class Test
1517
Assert.AreEqual (NormalizeNewlines(@"class Test
1529
public void TestBeforeBracketComma ()
1531
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1532
policy.SpaceBeforeBracketComma = true;
1533
policy.SpaceAfterBracketComma = false;
1535
var result = GetResult (policy, @"class Test {
1542
int i1 = result.Text.LastIndexOf ("[");
1543
int i2 = result.Text.LastIndexOf ("]") + "]".Length;
1544
Assert.AreEqual (@"[1 ,2 ,3]", result.GetText (i1, i2 - i1));
1548
public void TestAfterBracketComma ()
1550
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1551
policy.SpaceAfterBracketComma = true;
1553
var result = GetResult (policy, @"class Test {
1560
int i1 = result.Text.LastIndexOf ("[");
1561
int i2 = result.Text.LastIndexOf ("]") + "]".Length;
1562
Assert.AreEqual (@"[1, 2, 3]", result.GetText (i1, i2 - i1));
1568
public void TestSpacesBeforeArrayDeclarationBrackets ()
1570
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1571
policy.SpaceBeforeArrayDeclarationBrackets = true;
1573
var result = GetResult (policy, @"class Test
1579
Assert.AreEqual (NormalizeNewlines(@"class Test
1589
public void TestRemoveWhitespacesBeforeSemicolon()
1591
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1592
var result = GetResult(policy, @"class Test {
1598
int i1 = result.Text.IndexOf("Foo");
1599
int i2 = result.Text.LastIndexOf(";") + ";".Length;
1600
Assert.AreEqual(@"Foo ();", result.GetText(i1, i2 - i1));
1604
public void TestSpaceInNamedArgumentAfterDoubleColon()
1606
var policy = FormattingOptionsFactory.CreateMono ();
1607
policy.SpaceInNamedArgumentAfterDoubleColon = true;
1608
var result = GetResult(policy, @"class Test {
1614
int i1 = result.Text.IndexOf("Foo");
1615
int i2 = result.Text.LastIndexOf(";") + ";".Length;
1616
Assert.AreEqual(@"Foo (bar: expr);", result.GetText(i1, i2 - i1));
1620
public void TestSpaceInNamedArgumentAfterDoubleColon2()
1622
var policy = FormattingOptionsFactory.CreateMono ();
1623
policy.SpaceInNamedArgumentAfterDoubleColon = false;
1624
var result = GetResult(policy, @"class Test {
1630
int i1 = result.Text.IndexOf("Foo");
1631
int i2 = result.Text.LastIndexOf(";") + ";".Length;
1632
Assert.AreEqual(@"Foo (bar:expr);", result.GetText(i1, i2 - i1));