2
// TestStatementIndentation.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 TestStatements : TestBase
38
public void TestInvocationIndentation ()
40
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
41
policy.ClassBraceStyle = BraceStyle.EndOfLine;
59
public void TestIndentBlocks ()
61
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
62
policy.IndentBlocks = true;
64
var adapter = Test (policy,
82
policy.IndentBlocks = false;
83
Continue (policy, adapter, @"class Test
95
public void TestIndentBlocksCase2 ()
97
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
98
policy.IndentBlocks = true;
100
var adapter = Test (policy,
118
policy.IndentBlocks = false;
119
Continue (policy, adapter, @"class Test
131
public void TestBreakIndentation ()
133
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
134
policy.ClassBraceStyle = BraceStyle.EndOfLine;
152
public void TestBreakSemicolon ()
154
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
174
public void TestCheckedIndentation ()
176
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
177
policy.ClassBraceStyle = BraceStyle.EndOfLine;
180
Test (policy, @"class Test {
196
public void TestBaseIndentation ()
198
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
199
policy.ClassBraceStyle = BraceStyle.EndOfLine;
201
Test (policy, @"class Test {
215
public void TestUncheckedIndentation ()
217
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
218
policy.ClassBraceStyle = BraceStyle.EndOfLine;
220
Test (policy, @"class Test {
237
public void TestContinueIndentation ()
239
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
240
policy.ClassBraceStyle = BraceStyle.EndOfLine;
242
Test (policy, @"class Test {
257
public void TestContinueSemicolon ()
259
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
260
Test (policy, @"class Test
277
public void TestEmptyStatementIndentation ()
279
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
280
policy.ClassBraceStyle = BraceStyle.EndOfLine;
282
Test (policy, @"class Test {
296
public void TestFixedStatementIndentation ()
298
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
299
policy.ClassBraceStyle = BraceStyle.EndOfLine;
301
Test (policy, @"class Test {
304
fixed (object* obj = &obj)
311
fixed (object* obj = &obj)
318
public void TestForeachIndentation ()
320
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
321
policy.StatementBraceStyle = BraceStyle.EndOfLine;
323
Test (policy, @"class Test
327
foreach (var obj in col) {
334
foreach (var obj in col) {
341
public void TestForIndentation ()
343
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
345
policy.ClassBraceStyle = BraceStyle.EndOfLine;
347
Test (policy, @"class Test {
363
public void TestGotoIndentation ()
365
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
366
policy.ClassBraceStyle = BraceStyle.EndOfLine;
368
Test (policy, @"class Test {
383
public void TestGotoSemicolon ()
385
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
386
Test (policy, @"class Test
404
public void TestReturnIndentation ()
406
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
407
policy.ClassBraceStyle = BraceStyle.EndOfLine;
409
Test (policy, @"class Test {
424
public void TestReturnSemicolon ()
426
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
427
Test (policy, @"class Test
443
public void TestLockIndentation ()
445
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
446
policy.ClassBraceStyle = BraceStyle.EndOfLine;
448
Test (policy, @"class Test {
465
public void TestThrowIndentation ()
467
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
469
policy.ClassBraceStyle = BraceStyle.EndOfLine;
471
Test (policy, @"class Test {
474
throw new NotSupportedException ();
480
throw new NotSupportedException ();
486
public void TestThrowSemicolon ()
488
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
489
Test (policy, @"class Test
493
throw new NotSupportedException () ;
500
throw new NotSupportedException ();
506
public void TestUnsafeIndentation ()
508
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
510
policy.ClassBraceStyle = BraceStyle.EndOfLine;
512
Test (policy, @"class Test {
529
public void TestUsingIndentation ()
531
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
533
policy.ClassBraceStyle = BraceStyle.EndOfLine;
535
Test (policy, @"class Test {
538
using (var o = new MyObj()) {
544
using (var o = new MyObj()) {
551
public void TestUsingAlignment ()
553
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
554
policy.AlignEmbeddedUsingStatements = true;
555
policy.ClassBraceStyle = BraceStyle.EndOfLine;
557
var adapter = Test (policy, @"class Test {
560
using (var p = new MyObj())
561
using (var o = new MyObj()) {
568
using (var p = new MyObj())
569
using (var o = new MyObj()) {
573
policy.AlignEmbeddedUsingStatements = false;
574
Continue (policy, adapter, @"class Test {
577
using (var p = new MyObj())
578
using (var o = new MyObj()) {
585
public void TestVariableDeclarationIndentation ()
587
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
588
policy.ClassBraceStyle = BraceStyle.EndOfLine;
590
Test (policy, @"class Test {
605
public void TestConstantVariableDeclarationIndentation ()
607
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
608
policy.ClassBraceStyle = BraceStyle.EndOfLine;
610
Test (policy, @"class Test {
625
public void TestYieldReturnIndentation ()
627
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
629
policy.ClassBraceStyle = BraceStyle.EndOfLine;
630
Test (policy, @"class Test {
645
public void TestYieldReturnSemicolon ()
647
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
649
policy.ClassBraceStyle = BraceStyle.EndOfLine;
650
Test (policy, @"class Test {
665
public void TestYieldBreakIndentation ()
667
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
669
policy.ClassBraceStyle = BraceStyle.EndOfLine;
670
Test (policy, @"class Test {
685
public void TestYieldBreakSemicolon ()
687
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
689
policy.ClassBraceStyle = BraceStyle.EndOfLine;
690
Test (policy, @"class Test {
704
public void TestWhileIndentation ()
706
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
708
policy.ClassBraceStyle = BraceStyle.EndOfLine;
710
Test (policy, @"class Test {
727
public void TestDoWhileIndentation ()
729
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
731
policy.ClassBraceStyle = BraceStyle.EndOfLine;
732
Test (policy, @"class Test {
749
public void TestForeachBracketPlacement ()
751
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
753
policy.StatementBraceStyle = BraceStyle.EndOfLine;
754
Test (policy, @"class Test
758
foreach (var obj in col) {}
765
foreach (var obj in col) {
772
public void TestForeachBracketPlacement2 ()
774
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
776
policy.StatementBraceStyle = BraceStyle.NextLineShifted2;
777
Test (policy, @"class Test
781
foreach (var obj in col) {;}
788
foreach (var obj in col)
797
public void TestIfBracketPlacement ()
799
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
801
policy.StatementBraceStyle = BraceStyle.EndOfLine;
803
Test (policy, @"class Test
821
public void TestAllowIfBlockInline ()
823
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
824
policy.StatementBraceStyle = BraceStyle.EndOfLine;
825
policy.AllowIfBlockInline = true;
827
Test (policy, @"class Test
842
Test (policy, @"class Test
846
if (true) { Foo (); }
852
if (true) { Foo (); }
857
Test (policy, @"class Test
872
Test (policy, @"class Test
891
public void TestIfElseBracketPlacement ()
893
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
895
policy.StatementBraceStyle = BraceStyle.EndOfLine;
897
Test (policy, @"class Test
916
public void TestIfAlignment ()
918
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
919
policy.AlignEmbeddedIfStatements = true;
920
policy.ClassBraceStyle = BraceStyle.EndOfLine;
922
var adapter = Test (policy, @"class Test {
938
policy.AlignEmbeddedIfStatements = false;
939
Continue (policy, adapter, @"class Test {
950
public void TestElseOnNewLine()
952
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono();
953
policy.ElseNewLinePlacement = NewLinePlacement.NewLine;
955
Test (policy, @"class Test
981
public void TestElseIfOnNewLine()
983
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono();
984
policy.ElseIfNewLinePlacement = NewLinePlacement.NewLine;
986
Test (policy, @"class Test
1012
public void TestElseOnNewLineOff()
1014
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono();
1015
policy.ElseNewLinePlacement = NewLinePlacement.SameLine;
1017
Test (policy, @"class Test
1043
public void TestSimpleIfElseComment()
1045
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono();
1047
policy.StatementBraceStyle = BraceStyle.EndOfLine;
1048
policy.ElseIfNewLinePlacement = NewLinePlacement.SameLine; // for simple statements it must be new line.
1050
Test (policy, @"class Test
1054
if (true) Call (); else Call ();
1070
public void TestFixedBracketPlacement ()
1072
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1074
policy.StatementBraceStyle = BraceStyle.NextLineShifted;
1076
Test (policy, @"class Test
1080
fixed (object* obj = &obj){
1089
fixed (object* obj = &obj)
1099
public void TestForBracketPlacement ()
1101
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1103
policy.StatementBraceStyle = BraceStyle.EndOfLineWithoutSpace;
1104
Test (policy, @"class Test
1123
public void TestCheckedBracketPlacement ()
1125
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1127
policy.StatementBraceStyle = BraceStyle.EndOfLineWithoutSpace;
1129
Test (policy, @"class Test
1148
public void TestUncheckedBracketPlacement ()
1150
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1152
policy.StatementBraceStyle = BraceStyle.EndOfLineWithoutSpace;
1154
Test (policy, @"class Test
1173
public void TestLockBracketPlacement ()
1175
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1177
policy.StatementBraceStyle = BraceStyle.EndOfLine;
1179
Test (policy, @"class Test
1201
public void TestUnsafeBracketPlacement ()
1203
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1205
policy.StatementBraceStyle = BraceStyle.EndOfLine;
1207
Test (policy, @"class Test
1229
public void TestUsingBracketPlacement ()
1231
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1233
policy.StatementBraceStyle = BraceStyle.EndOfLine;
1235
Test (policy, @"class Test
1239
using (var e = new E())
1249
using (var e = new E()) {
1257
public void TestWhileBracketPlacement ()
1259
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1261
policy.StatementBraceStyle = BraceStyle.EndOfLine;
1263
Test (policy, @"class Test
1285
public void TestDoWhileBracketPlacement ()
1287
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1289
policy.StatementBraceStyle = BraceStyle.EndOfLine;
1291
Test (policy, @"class Test
1313
public void TestSwitchFormatting1 ()
1315
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1317
policy.IndentSwitchBody = true;
1318
policy.IndentCaseBody = true;
1319
policy.IndentBreakStatements = true;
1321
Test (policy, @"class Test
1325
switch (a) { case 1: case 2: DoSomething(); break; default: Foo (); break;}
1346
public void TestSwitchFormatting2 ()
1348
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1350
policy.IndentSwitchBody = false;
1351
policy.IndentCaseBody = false;
1352
policy.IndentBreakStatements = false;
1354
Test (policy, @"class Test
1358
switch (a) { case 1: case 2: DoSomething(); break; default: Foo (); break;}
1380
public void TestSwitchIndentBreak ()
1382
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1384
policy.IndentSwitchBody = true;
1385
policy.IndentBreakStatements = true;
1387
Test (policy, @"class Test
1417
policy.IndentSwitchBody = true;
1418
policy.IndentBreakStatements = false;
1420
Test (policy, @"class Test
1454
public void TestTryCatchBracketPlacement ()
1456
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1458
policy.StatementBraceStyle = BraceStyle.EndOfLine;
1460
Test (policy, @"class Test
1464
try { ; } catch (Exception e) { } finally { }
1473
} catch (Exception e) {
1481
public void TestPlaceCatchOnNewLine()
1483
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono();
1485
policy.CatchNewLinePlacement = NewLinePlacement.NewLine;
1487
Test (policy, @"class Test
1493
} catch (Exception e) {
1505
catch (Exception e) {
1513
public void TestPlaceFinallyOnNewLine()
1515
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono();
1516
policy.FinallyNewLinePlacement = NewLinePlacement.NewLine;
1518
Test (policy, @"class Test
1524
} catch (Exception e) {
1535
} catch (Exception e) {
1544
public void TestPlaceWhileOnNewLine()
1546
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono();
1548
policy.WhileNewLinePlacement = NewLinePlacement.NewLine;
1550
Test (policy, @"class Test
1572
public void TestBlockStatementWithComments ()
1574
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1576
Test (policy, @"class Test
1600
public void TestBlockStatementWithPreProcessorDirective ()
1602
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
1604
Test (policy, @"class Test
1628
public void TestUsingInsideNamespace()
1630
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono();
1631
policy.UsingPlacement = UsingPlacement.InsideNamespace;
1633
Test(policy, @"namespace TestNamespace
1640
}", @"namespace TestNamespace