/NRefactory/ICSharpCode.NRefactory.Tests/FormattingTests/TestSpacingVisitor.cs

http://github.com/icsharpcode/ILSpy · C# · 1639 lines · 1377 code · 237 blank · 25 comment · 4 complexity · e0bf218106911af02dfb8e1955260455 MD5 · raw file

  1. //
  2. // TestFormattingVisitor.cs
  3. //
  4. // Author:
  5. // Mike Kr?ger <mkrueger@novell.com>
  6. //
  7. // Copyright (c) 2010 Novell, Inc (http://www.novell.com)
  8. //
  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:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  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
  25. // THE SOFTWARE.
  26. using System;
  27. using System.IO;
  28. using NUnit.Framework;
  29. using ICSharpCode.NRefactory.CSharp;
  30. namespace ICSharpCode.NRefactory.CSharp.FormattingTests
  31. {
  32. [TestFixture()]
  33. public class TestSpacingVisitor : TestBase
  34. {
  35. [Test()]
  36. public void TestFieldSpacesBeforeComma1()
  37. {
  38. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  39. policy.ClassBraceStyle = BraceStyle.EndOfLine;
  40. policy.SpaceBeforeFieldDeclarationComma = false;
  41. policy.SpaceAfterFieldDeclarationComma = false;
  42. Test(policy, @"class Test {
  43. int a , b, c;
  44. }",
  45. @"class Test {
  46. int a,b,c;
  47. }");
  48. }
  49. [Test()]
  50. public void TestFieldSpacesBeforeComma2 ()
  51. {
  52. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  53. policy.ClassBraceStyle = BraceStyle.EndOfLine;
  54. policy.SpaceBeforeFieldDeclarationComma = true;
  55. policy.SpaceAfterFieldDeclarationComma = true;
  56. Test (policy, @"class Test {
  57. int a , b, c;
  58. }",
  59. @"class Test {
  60. int a , b , c;
  61. }");
  62. }
  63. [Test()]
  64. public void TestFixedFieldSpacesBeforeComma ()
  65. {
  66. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  67. policy.ClassBraceStyle = BraceStyle.EndOfLine;
  68. policy.SpaceAfterFieldDeclarationComma = true;
  69. policy.SpaceBeforeFieldDeclarationComma = true;
  70. Test (policy, @"class Test {
  71. fixed int a[10] , b[10], c[10];
  72. }",
  73. @"class Test {
  74. fixed int a[10] , b[10] , c[10];
  75. }");
  76. }
  77. [Test()]
  78. public void TestConstFieldSpacesBeforeComma ()
  79. {
  80. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  81. policy.ClassBraceStyle = BraceStyle.EndOfLine;
  82. policy.SpaceAfterFieldDeclarationComma = false;
  83. policy.SpaceBeforeFieldDeclarationComma = false;
  84. Test (policy, @"class Test {
  85. const int a = 1 , b = 2, c = 3;
  86. }",
  87. @"class Test {
  88. const int a = 1,b = 2,c = 3;
  89. }");
  90. }
  91. [Test()]
  92. public void TestBeforeMethodDeclarationParentheses ()
  93. {
  94. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  95. policy.SpaceBeforeMethodDeclarationParentheses = true;
  96. Test (policy, @"public abstract class Test
  97. {
  98. public abstract Test TestMethod();
  99. }",
  100. @"public abstract class Test
  101. {
  102. public abstract Test TestMethod ();
  103. }");
  104. }
  105. [Test()]
  106. public void TestBeforeConstructorDeclarationParenthesesDestructorCase ()
  107. {
  108. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  109. policy.SpaceBeforeConstructorDeclarationParentheses = true;
  110. Test (policy, @"class Test
  111. {
  112. ~Test()
  113. {
  114. }
  115. }",
  116. @"class Test
  117. {
  118. ~Test ()
  119. {
  120. }
  121. }");
  122. }
  123. static void TestBinaryOperator (CSharpFormattingOptions policy, string op)
  124. {
  125. var result = GetResult (policy, "class Test { void TestMe () { result = left" + op + "right; } }");
  126. int i1 = result.Text.IndexOf ("left");
  127. int i2 = result.Text.IndexOf ("right") + "right".Length;
  128. if (i1 < 0 || i2 < 0)
  129. Assert.Fail ("text invalid:" + result.Text);
  130. Assert.AreEqual ("left " + op + " right", result.GetText (i1, i2 - i1));
  131. }
  132. [Test()]
  133. public void TestSpacesAroundMultiplicativeOperator ()
  134. {
  135. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  136. policy.SpaceAroundMultiplicativeOperator = true;
  137. TestBinaryOperator (policy, "*");
  138. TestBinaryOperator (policy, "/");
  139. }
  140. [Test()]
  141. public void TestSpacesAroundShiftOperator ()
  142. {
  143. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  144. policy.SpaceAroundShiftOperator = true;
  145. TestBinaryOperator (policy, "<<");
  146. TestBinaryOperator (policy, ">>");
  147. }
  148. [Test()]
  149. public void TestSpacesAroundAdditiveOperator ()
  150. {
  151. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  152. policy.SpaceAroundAdditiveOperator = true;
  153. TestBinaryOperator (policy, "+");
  154. TestBinaryOperator (policy, "-");
  155. }
  156. [Test()]
  157. public void TestSpacesAroundBitwiseOperator ()
  158. {
  159. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  160. policy.SpaceAroundBitwiseOperator = true;
  161. TestBinaryOperator (policy, "&");
  162. TestBinaryOperator (policy, "|");
  163. TestBinaryOperator (policy, "^");
  164. }
  165. [Test()]
  166. public void TestSpacesAroundRelationalOperator ()
  167. {
  168. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  169. policy.SpaceAroundRelationalOperator = true;
  170. TestBinaryOperator (policy, "<");
  171. TestBinaryOperator (policy, "<=");
  172. TestBinaryOperator (policy, ">");
  173. TestBinaryOperator (policy, ">=");
  174. }
  175. [Test()]
  176. public void TestSpacesAroundEqualityOperator ()
  177. {
  178. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  179. policy.SpaceAroundEqualityOperator = true;
  180. TestBinaryOperator (policy, "==");
  181. TestBinaryOperator (policy, "!=");
  182. }
  183. [Test()]
  184. public void TestSpacesAroundLogicalOperator ()
  185. {
  186. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  187. policy.SpaceAroundLogicalOperator = true;
  188. TestBinaryOperator (policy, "&&");
  189. TestBinaryOperator (policy, "||");
  190. }
  191. [Test()]
  192. public void TestConditionalOperator ()
  193. {
  194. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  195. policy.SpaceBeforeConditionalOperatorCondition = true;
  196. policy.SpaceAfterConditionalOperatorCondition = true;
  197. policy.SpaceBeforeConditionalOperatorSeparator = true;
  198. policy.SpaceAfterConditionalOperatorSeparator = true;
  199. var result = GetResult (policy, @"class Test {
  200. void TestMe ()
  201. {
  202. result = condition?trueexpr:falseexpr;
  203. }
  204. }");
  205. int i1 = result.Text.IndexOf ("condition");
  206. int i2 = result.Text.IndexOf ("falseexpr") + "falseexpr".Length;
  207. Assert.AreEqual (@"condition ? trueexpr : falseexpr", result.GetText (i1, i2 - i1));
  208. policy.SpaceBeforeConditionalOperatorCondition = false;
  209. policy.SpaceAfterConditionalOperatorCondition = false;
  210. policy.SpaceBeforeConditionalOperatorSeparator = false;
  211. policy.SpaceAfterConditionalOperatorSeparator = false;
  212. result = GetResult (policy, @"class Test {
  213. void TestMe ()
  214. {
  215. result = true ? trueexpr : falseexpr;
  216. }
  217. }");
  218. i1 = result.Text.IndexOf ("true");
  219. i2 = result.Text.IndexOf ("falseexpr") + "falseexpr".Length;
  220. Assert.AreEqual (@"true?trueexpr:falseexpr", result.GetText (i1, i2 - i1));
  221. }
  222. [Test()]
  223. public void TestBeforeMethodCallParenthesesSpace ()
  224. {
  225. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  226. policy.SpaceBeforeMethodCallParentheses = true;
  227. var result = GetResult (policy, @"class Test {
  228. void TestMe ()
  229. {
  230. MethodCall();
  231. }
  232. }");
  233. int i1 = result.Text.IndexOf ("MethodCall");
  234. int i2 = result.Text.IndexOf (";") + ";".Length;
  235. Assert.AreEqual (@"MethodCall ();", result.GetText (i1, i2 - i1));
  236. result = GetResult (policy, @"class Test {
  237. void TestMe ()
  238. {
  239. MethodCall ();
  240. }
  241. }");
  242. policy.SpaceBeforeMethodCallParentheses = false;
  243. result = GetResult (policy, result.Text);
  244. i1 = result.Text.IndexOf ("MethodCall");
  245. i2 = result.Text.IndexOf (";") + ";".Length;
  246. Assert.AreEqual (@"MethodCall();", result.GetText (i1, i2 - i1));
  247. }
  248. [Test()]
  249. public void TestWithinMethodCallParenthesesSpace ()
  250. {
  251. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  252. policy.SpaceWithinMethodCallParentheses = true;
  253. var result = GetResult (policy, @"class Test {
  254. void TestMe ()
  255. {
  256. MethodCall(true);
  257. }
  258. }");
  259. int i1 = result.Text.LastIndexOf ("(");
  260. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  261. Assert.AreEqual (@"( true )", result.GetText (i1, i2 - i1));
  262. policy.SpaceWithinMethodCallParentheses = false;
  263. result = GetResult (policy, @"class Test {
  264. void TestMe ()
  265. {
  266. MethodCall( true );
  267. }
  268. }");
  269. i1 = result.Text.LastIndexOf ("(");
  270. i2 = result.Text.LastIndexOf (")") + ")".Length;
  271. Assert.AreEqual (@"(true)", result.GetText (i1, i2 - i1));
  272. }
  273. [Test()]
  274. public void TestBeforeIfParenthesesSpace ()
  275. {
  276. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  277. policy.SpaceBeforeIfParentheses = true;
  278. var result = GetResult (policy, @"class Test {
  279. void TestMe ()
  280. {
  281. if(true);
  282. }
  283. }");
  284. int i1 = result.Text.IndexOf ("if");
  285. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  286. Assert.AreEqual (@"if (true)", result.GetText (i1, i2 - i1));
  287. }
  288. [Test()]
  289. public void TestWithinIfParenthesesSpace ()
  290. {
  291. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  292. policy.SpacesWithinIfParentheses = true;
  293. var result = GetResult (policy, @"class Test {
  294. void TestMe ()
  295. {
  296. if (true);
  297. }
  298. }");
  299. int i1 = result.Text.LastIndexOf ("(");
  300. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  301. Assert.AreEqual (@"( true )", result.GetText (i1, i2 - i1));
  302. }
  303. [Test()]
  304. public void TestBeforeWhileParenthesesSpace ()
  305. {
  306. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  307. policy.SpaceBeforeWhileParentheses = true;
  308. var result = GetResult (policy, @"class Test {
  309. void TestMe ()
  310. {
  311. while(true);
  312. }
  313. }");
  314. int i1 = result.Text.IndexOf ("while");
  315. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  316. Assert.AreEqual (@"while (true)", result.GetText (i1, i2 - i1));
  317. }
  318. [Test()]
  319. public void TestWithinWhileParenthesesSpace ()
  320. {
  321. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  322. policy.SpacesWithinWhileParentheses = true;
  323. var result = GetResult (policy, @"class Test {
  324. void TestMe ()
  325. {
  326. while (true);
  327. }
  328. }");
  329. int i1 = result.Text.LastIndexOf ("(");
  330. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  331. Assert.AreEqual (@"( true )", result.GetText (i1, i2 - i1));
  332. }
  333. [Test()]
  334. public void TestBeforeForParenthesesSpace ()
  335. {
  336. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  337. policy.SpaceBeforeForParentheses = true;
  338. var result = GetResult (policy, @"class Test {
  339. void TestMe ()
  340. {
  341. for(;;);
  342. }
  343. }");
  344. int i1 = result.Text.IndexOf ("for");
  345. int i2 = result.Text.LastIndexOf ("(") + "(".Length;
  346. Assert.AreEqual (@"for (", result.GetText (i1, i2 - i1));
  347. }
  348. [Test()]
  349. public void TestWithinForParenthesesSpace ()
  350. {
  351. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  352. policy.SpacesWithinForParentheses = true;
  353. var result = GetResult (policy, @"class Test {
  354. void TestMe ()
  355. {
  356. for(;;);
  357. }
  358. }");
  359. int i1 = result.Text.LastIndexOf ("(");
  360. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  361. Assert.AreEqual (@"( ;; )", result.GetText (i1, i2 - i1));
  362. }
  363. [Test()]
  364. public void TestBeforeForeachParenthesesSpace ()
  365. {
  366. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  367. policy.SpaceBeforeForeachParentheses = true;
  368. var result = GetResult (policy, @"class Test {
  369. void TestMe ()
  370. {
  371. foreach(var o in list);
  372. }
  373. }");
  374. int i1 = result.Text.IndexOf ("foreach");
  375. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  376. Assert.AreEqual (@"foreach (var o in list)", result.GetText (i1, i2 - i1));
  377. }
  378. [Test()]
  379. public void TestWithinForeachParenthesesSpace ()
  380. {
  381. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  382. policy.SpacesWithinForeachParentheses = true;
  383. var result = GetResult (policy, @"class Test {
  384. void TestMe ()
  385. {
  386. foreach(var o in list);
  387. }
  388. }");
  389. int i1 = result.Text.LastIndexOf ("(");
  390. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  391. Assert.AreEqual (@"( var o in list )", result.GetText (i1, i2 - i1));
  392. }
  393. [Test()]
  394. public void TestBeforeCatchParenthesesSpace ()
  395. {
  396. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  397. policy.SpaceBeforeCatchParentheses = true;
  398. var result = GetResult (policy, @"class Test {
  399. void TestMe ()
  400. {
  401. try {} catch(Exception) {}
  402. }
  403. }");
  404. int i1 = result.Text.IndexOf ("catch");
  405. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  406. Assert.AreEqual (@"catch (Exception)", result.GetText (i1, i2 - i1));
  407. }
  408. [Test()]
  409. public void TestWithinCatchParenthesesSpace ()
  410. {
  411. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  412. policy.SpacesWithinCatchParentheses = true;
  413. var result = GetResult (policy, @"class Test {
  414. void TestMe ()
  415. {
  416. try {} catch(Exception) {}
  417. }
  418. }");
  419. int i1 = result.Text.LastIndexOf ("(");
  420. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  421. Assert.AreEqual (@"( Exception )", result.GetText (i1, i2 - i1));
  422. }
  423. [Test()]
  424. public void TestBeforeLockParenthesesSpace ()
  425. {
  426. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  427. policy.SpaceBeforeLockParentheses = true;
  428. var result = GetResult (policy, @"class Test {
  429. void TestMe ()
  430. {
  431. lock(this) {}
  432. }
  433. }");
  434. int i1 = result.Text.IndexOf ("lock");
  435. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  436. Assert.AreEqual (@"lock (this)", result.GetText (i1, i2 - i1));
  437. }
  438. [Test()]
  439. public void TestWithinLockParenthesesSpace ()
  440. {
  441. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  442. policy.SpacesWithinLockParentheses = true;
  443. var result = GetResult (policy, @"class Test {
  444. void TestMe ()
  445. {
  446. lock(this) {}
  447. }
  448. }");
  449. int i1 = result.Text.LastIndexOf ("(");
  450. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  451. Assert.AreEqual (@"( this )", result.GetText (i1, i2 - i1));
  452. }
  453. [Test()]
  454. public void TestSpacesAfterForSemicolon ()
  455. {
  456. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  457. policy.SpaceAfterForSemicolon = true;
  458. var result = GetResult (policy, @"class Test {
  459. void TestMe ()
  460. {
  461. for (int i;true;i++) ;
  462. }
  463. }");
  464. int i1 = result.Text.LastIndexOf ("for");
  465. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  466. Assert.AreEqual (@"for (int i; true; i++)", result.GetText (i1, i2 - i1));
  467. }
  468. [Test()]
  469. public void TestSpacesBeforeForSemicolon ()
  470. {
  471. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  472. policy.SpaceBeforeForSemicolon = true;
  473. policy.SpaceAfterForSemicolon = false;
  474. var result = GetResult (policy, @"class Test {
  475. void TestMe ()
  476. {
  477. for (int i;true;i++) ;
  478. }
  479. }");
  480. int i1 = result.Text.LastIndexOf ("for");
  481. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  482. Assert.AreEqual (@"for (int i ;true ;i++)", result.GetText (i1, i2 - i1));
  483. }
  484. [Test()]
  485. public void TestSpacesAfterTypecast ()
  486. {
  487. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  488. policy.SpaceAfterTypecast = true;
  489. var result = GetResult (policy, @"class Test {
  490. Test TestMe ()
  491. {
  492. return (Test)null;
  493. }
  494. }");
  495. int i1 = result.Text.LastIndexOf ("return");
  496. int i2 = result.Text.LastIndexOf ("null") + "null".Length;
  497. Assert.AreEqual (@"return (Test) null", result.GetText (i1, i2 - i1));
  498. }
  499. [Test()]
  500. public void TestBeforeUsingParenthesesSpace ()
  501. {
  502. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  503. policy.SpaceBeforeUsingParentheses = true;
  504. var result = GetResult (policy, @"class Test {
  505. void TestMe ()
  506. {
  507. using(a) {}
  508. }
  509. }");
  510. int i1 = result.Text.IndexOf ("using");
  511. int i2 = result.Text.LastIndexOf ("(") + "(".Length;
  512. Assert.AreEqual (@"using (", result.GetText (i1, i2 - i1));
  513. }
  514. [Test()]
  515. public void TestWithinUsingParenthesesSpace ()
  516. {
  517. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  518. policy.SpacesWithinUsingParentheses = true;
  519. var result = GetResult (policy, @"class Test {
  520. void TestMe ()
  521. {
  522. using(a) {}
  523. }
  524. }");
  525. int i1 = result.Text.LastIndexOf ("(");
  526. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  527. Assert.AreEqual (@"( a )", result.GetText (i1, i2 - i1));
  528. }
  529. static void TestAssignmentOperator (CSharpFormattingOptions policy, string op)
  530. {
  531. var result = GetResult (policy, "class Test { void TestMe () { left" + op + "right; } }");
  532. int i1 = result.Text.IndexOf ("left");
  533. int i2 = result.Text.IndexOf ("right") + "right".Length;
  534. if (i1 < 0 || i2 < 0)
  535. Assert.Fail ("text invalid:" + result.Text);
  536. Assert.AreEqual ("left " + op + " right", result.GetText (i1, i2 - i1));
  537. }
  538. [Test()]
  539. public void TestAroundAssignmentSpace ()
  540. {
  541. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  542. policy.SpaceAroundAssignment = true;
  543. TestAssignmentOperator (policy, "=");
  544. TestAssignmentOperator (policy, "*=");
  545. TestAssignmentOperator (policy, "/=");
  546. TestAssignmentOperator (policy, "+=");
  547. TestAssignmentOperator (policy, "%=");
  548. TestAssignmentOperator (policy, "-=");
  549. TestAssignmentOperator (policy, "<<=");
  550. TestAssignmentOperator (policy, ">>=");
  551. TestAssignmentOperator (policy, "&=");
  552. TestAssignmentOperator (policy, "|=");
  553. TestAssignmentOperator (policy, "^=");
  554. }
  555. [Test()]
  556. public void TestAroundAssignmentSpaceInDeclarations ()
  557. {
  558. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  559. policy.SpaceAroundAssignment = true;
  560. var result = GetResult (policy, @"class Test {
  561. void TestMe ()
  562. {
  563. int left=right;
  564. }
  565. }");
  566. int i1 = result.Text.LastIndexOf ("left");
  567. int i2 = result.Text.LastIndexOf ("right") + "right".Length;
  568. Assert.AreEqual (@"left = right", result.GetText (i1, i2 - i1));
  569. }
  570. [Test()]
  571. public void TestBeforeSwitchParenthesesSpace ()
  572. {
  573. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  574. policy.SpaceBeforeSwitchParentheses = true;
  575. var result = GetResult (policy, @"class Test {
  576. void TestMe ()
  577. {
  578. switch (test) { default: break; }
  579. }
  580. }");
  581. int i1 = result.Text.IndexOf ("switch");
  582. int i2 = result.Text.LastIndexOf ("(") + "(".Length;
  583. Assert.AreEqual (@"switch (", result.GetText (i1, i2 - i1));
  584. }
  585. [Test()]
  586. public void TestWithinSwitchParenthesesSpace ()
  587. {
  588. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  589. policy.SpacesWithinSwitchParentheses = true;
  590. var result = GetResult (policy, @"class Test {
  591. void TestMe ()
  592. {
  593. switch (test) { default: break; }
  594. }
  595. }");
  596. int i1 = result.Text.LastIndexOf ("(");
  597. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  598. Assert.AreEqual (@"( test )", result.GetText (i1, i2 - i1));
  599. }
  600. [Test()]
  601. public void TestWithinParenthesesSpace ()
  602. {
  603. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  604. policy.SpacesWithinParentheses = true;
  605. var result = GetResult (policy, @"class Test {
  606. void TestMe ()
  607. {
  608. c = (test);
  609. }
  610. }");
  611. int i1 = result.Text.LastIndexOf ("(");
  612. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  613. Assert.AreEqual (@"( test )", result.GetText (i1, i2 - i1));
  614. }
  615. [Test()]
  616. public void TestWithinMethodDeclarationParenthesesSpace ()
  617. {
  618. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  619. policy.SpaceWithinMethodDeclarationParentheses = true;
  620. var result = GetResult (policy, @"class Test {
  621. void TestMe (int a)
  622. {
  623. }
  624. }");
  625. int i1 = result.Text.LastIndexOf ("(");
  626. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  627. Assert.AreEqual (@"( int a )", result.GetText (i1, i2 - i1));
  628. }
  629. [Test()]
  630. public void TestWithinCastParenthesesSpace ()
  631. {
  632. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  633. policy.SpacesWithinCastParentheses = true;
  634. var result = GetResult (policy, @"class Test {
  635. void TestMe ()
  636. {
  637. a = (int)b;
  638. }
  639. }");
  640. int i1 = result.Text.LastIndexOf ("(");
  641. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  642. Assert.AreEqual (@"( int )", result.GetText (i1, i2 - i1));
  643. }
  644. [Test()]
  645. public void TestWithinSizeOfParenthesesSpace ()
  646. {
  647. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  648. policy.SpacesWithinSizeOfParentheses = true;
  649. var result = GetResult (policy, @"class Test {
  650. void TestMe ()
  651. {
  652. a = sizeof(int);
  653. }
  654. }");
  655. int i1 = result.Text.LastIndexOf ("(");
  656. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  657. Assert.AreEqual (@"( int )", result.GetText (i1, i2 - i1));
  658. }
  659. [Test()]
  660. public void TestBeforeSizeOfParentheses ()
  661. {
  662. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  663. policy.SpaceBeforeSizeOfParentheses = true;
  664. var result = GetResult (policy, @"class Test {
  665. void TestMe ()
  666. {
  667. a = sizeof(int);
  668. }
  669. }");
  670. int i1 = result.Text.LastIndexOf ("sizeof");
  671. int i2 = result.Text.LastIndexOf ("(") + "(".Length;
  672. Assert.AreEqual (@"sizeof (", result.GetText (i1, i2 - i1));
  673. }
  674. [Test()]
  675. public void TestWithinTypeOfParenthesesSpace ()
  676. {
  677. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  678. policy.SpacesWithinTypeOfParentheses = true;
  679. var result = GetResult (policy, @"class Test {
  680. void TestMe ()
  681. {
  682. a = typeof(int);
  683. }
  684. }");
  685. int i1 = result.Text.LastIndexOf ("(");
  686. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  687. Assert.AreEqual (@"( int )", result.GetText (i1, i2 - i1));
  688. }
  689. [Test()]
  690. public void TestBeforeTypeOfParentheses ()
  691. {
  692. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  693. policy.SpaceBeforeTypeOfParentheses = true;
  694. var result = GetResult (policy, @"class Test {
  695. void TestMe ()
  696. {
  697. a = typeof(int);
  698. }
  699. }");
  700. int i1 = result.Text.LastIndexOf ("typeof");
  701. int i2 = result.Text.LastIndexOf ("(") + "(".Length;
  702. Assert.AreEqual (@"typeof (", result.GetText (i1, i2 - i1));
  703. }
  704. [Test()]
  705. public void TestWithinCheckedExpressionParanthesesSpace ()
  706. {
  707. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  708. policy.SpacesWithinCheckedExpressionParantheses = true;
  709. var result = GetResult (policy, @"class Test {
  710. void TestMe ()
  711. {
  712. a = checked(a + b);
  713. }
  714. }");
  715. int i1 = result.Text.LastIndexOf ("(");
  716. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  717. Assert.AreEqual (@"( a + b )", result.GetText (i1, i2 - i1));
  718. result = GetResult (policy, @"class Test {
  719. void TestMe ()
  720. {
  721. a = unchecked(a + b);
  722. }
  723. }");
  724. result = GetResult (policy, result.Text);
  725. i1 = result.Text.LastIndexOf ("(");
  726. i2 = result.Text.LastIndexOf (")") + ")".Length;
  727. Assert.AreEqual (@"( a + b )", result.GetText (i1, i2 - i1));
  728. }
  729. [Test()]
  730. public void TestSpaceBeforeNewParentheses ()
  731. {
  732. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  733. policy.SpaceBeforeNewParentheses = true;
  734. var result = GetResult (policy, @"class Test {
  735. void TestMe ()
  736. {
  737. new Test();
  738. }
  739. }");
  740. int i1 = result.Text.LastIndexOf ("new");
  741. int i2 = result.Text.LastIndexOf (";") + ";".Length;
  742. Assert.AreEqual (@"new Test ();", result.GetText (i1, i2 - i1));
  743. }
  744. [Test()]
  745. public void TestWithinNewParentheses ()
  746. {
  747. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  748. policy.SpacesWithinNewParentheses = true;
  749. var result = GetResult (policy, @"class Test {
  750. void TestMe ()
  751. {
  752. new Test (1);
  753. }
  754. }");
  755. int i1 = result.Text.LastIndexOf ("new");
  756. int i2 = result.Text.LastIndexOf (";") + ";".Length;
  757. Assert.AreEqual (@"new Test ( 1 );", result.GetText (i1, i2 - i1));
  758. }
  759. [Test()]
  760. public void TestBetweenEmptyNewParentheses ()
  761. {
  762. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  763. policy.SpacesBetweenEmptyNewParentheses = true;
  764. var result = GetResult (policy, @"class Test {
  765. void TestMe ()
  766. {
  767. new Test ();
  768. }
  769. }");
  770. int i1 = result.Text.LastIndexOf ("new");
  771. int i2 = result.Text.LastIndexOf (";") + ";".Length;
  772. Assert.AreEqual (@"new Test ( );", result.GetText (i1, i2 - i1));
  773. }
  774. [Test()]
  775. public void TestBeforeNewParameterComma ()
  776. {
  777. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  778. policy.SpaceBeforeNewParameterComma = true;
  779. policy.SpaceAfterNewParameterComma = false;
  780. var result = GetResult (policy, @"class Test {
  781. void TestMe ()
  782. {
  783. new Test (1,2);
  784. }
  785. }");
  786. int i1 = result.Text.LastIndexOf ("new");
  787. int i2 = result.Text.LastIndexOf (";") + ";".Length;
  788. Assert.AreEqual (@"new Test (1 ,2);", result.GetText (i1, i2 - i1));
  789. }
  790. [Test()]
  791. public void TestAfterNewParameterComma ()
  792. {
  793. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  794. policy.SpaceAfterNewParameterComma = true;
  795. var result = GetResult (policy, @"class Test {
  796. void TestMe ()
  797. {
  798. new Test (1,2);
  799. }
  800. }");
  801. int i1 = result.Text.LastIndexOf ("new");
  802. int i2 = result.Text.LastIndexOf (";") + ";".Length;
  803. Assert.AreEqual (@"new Test (1, 2);", result.GetText (i1, i2 - i1));
  804. }
  805. [Test()]
  806. public void TestFieldDeclarationComma ()
  807. {
  808. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  809. policy.SpaceBeforeFieldDeclarationComma = false;
  810. policy.SpaceAfterFieldDeclarationComma = true;
  811. var result = GetResult (policy, @"class Test {
  812. int a,b,c;
  813. }");
  814. int i1 = result.Text.LastIndexOf ("int");
  815. int i2 = result.Text.LastIndexOf (";") + ";".Length;
  816. Assert.AreEqual (@"int a, b, c;", result.GetText (i1, i2 - i1));
  817. policy.SpaceBeforeFieldDeclarationComma = true;
  818. result = GetResult (policy, result.Text);
  819. i1 = result.Text.LastIndexOf ("int");
  820. i2 = result.Text.LastIndexOf (";") + ";".Length;
  821. Assert.AreEqual (@"int a , b , c;", result.GetText (i1, i2 - i1));
  822. policy.SpaceBeforeFieldDeclarationComma = false;
  823. policy.SpaceAfterFieldDeclarationComma = false;
  824. result = GetResult (policy, result.Text);
  825. i1 = result.Text.LastIndexOf ("int");
  826. i2 = result.Text.LastIndexOf (";") + ";".Length;
  827. Assert.AreEqual (@"int a,b,c;", result.GetText (i1, i2 - i1));
  828. }
  829. [Test()]
  830. public void TestBeforeMethodDeclarationParameterComma ()
  831. {
  832. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  833. policy.SpaceBeforeMethodDeclarationParameterComma = true;
  834. policy.SpaceAfterMethodDeclarationParameterComma = false;
  835. var result = GetResult (policy, @"class Test {
  836. public void Foo (int a,int b,int c) {}
  837. }");
  838. int i1 = result.Text.LastIndexOf ("(");
  839. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  840. Assert.AreEqual (@"(int a ,int b ,int c)", result.GetText (i1, i2 - i1));
  841. policy.SpaceBeforeMethodDeclarationParameterComma = false;
  842. result = GetResult (policy, result.Text);
  843. i1 = result.Text.LastIndexOf ("(");
  844. i2 = result.Text.LastIndexOf (")") + ")".Length;
  845. Assert.AreEqual (@"(int a,int b,int c)", result.GetText (i1, i2 - i1));
  846. }
  847. [Test()]
  848. public void TestAfterMethodDeclarationParameterComma ()
  849. {
  850. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  851. policy.SpaceBeforeMethodDeclarationParameterComma = false;
  852. policy.SpaceAfterMethodDeclarationParameterComma = true;
  853. var result = GetResult (policy, @"class Test {
  854. public void Foo (int a,int b,int c) {}
  855. }");
  856. int i1 = result.Text.LastIndexOf ("(");
  857. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  858. Assert.AreEqual (@"(int a, int b, int c)", result.GetText (i1, i2 - i1));
  859. policy.SpaceAfterMethodDeclarationParameterComma = false;
  860. result = GetResult (policy, result.Text);
  861. i1 = result.Text.LastIndexOf ("(");
  862. i2 = result.Text.LastIndexOf (")") + ")".Length;
  863. Assert.AreEqual (@"(int a,int b,int c)", result.GetText (i1, i2 - i1));
  864. }
  865. [Test()]
  866. public void TestSpacesInLambdaExpression ()
  867. {
  868. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  869. policy.SpacesWithinWhileParentheses = true;
  870. var result = GetResult (policy, @"class Test {
  871. void TestMe ()
  872. {
  873. var v = x=>x!=null;
  874. }
  875. }");
  876. int i1 = result.Text.IndexOf ("x");
  877. int i2 = result.Text.LastIndexOf ("null") + "null".Length;
  878. Assert.AreEqual (@"x => x != null", result.GetText (i1, i2 - i1));
  879. }
  880. [Test()]
  881. public void TestBeforeLocalVariableDeclarationComma ()
  882. {
  883. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  884. policy.SpaceBeforeLocalVariableDeclarationComma = true;
  885. policy.SpaceAfterLocalVariableDeclarationComma = false;
  886. var result = GetResult (policy, @"class Test {
  887. void TestMe ()
  888. {
  889. int a,b,c;
  890. }
  891. }");
  892. int i1 = result.Text.IndexOf ("int");
  893. int i2 = result.Text.IndexOf (";") + ";".Length;
  894. Assert.AreEqual (@"int a ,b ,c;", result.GetText (i1, i2 - i1));
  895. result = GetResult (policy, result.Text);
  896. policy.SpaceBeforeLocalVariableDeclarationComma = false;
  897. result = GetResult (policy, result.Text);
  898. i1 = result.Text.IndexOf ("int");
  899. i2 = result.Text.IndexOf (";") + ";".Length;
  900. Assert.AreEqual (@"int a,b,c;", result.GetText (i1, i2 - i1));
  901. }
  902. [Test()]
  903. public void TestLocalVariableDeclarationComma ()
  904. {
  905. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  906. policy.SpaceBeforeLocalVariableDeclarationComma = true;
  907. policy.SpaceAfterLocalVariableDeclarationComma = true;
  908. var result = GetResult (policy, @"class Test {
  909. void TestMe ()
  910. {
  911. int a = 5,b = 6,c;
  912. }
  913. }");
  914. int i1 = result.Text.IndexOf ("int");
  915. int i2 = result.Text.IndexOf (";") + ";".Length;
  916. Assert.AreEqual (@"int a = 5 , b = 6 , c;", result.GetText (i1, i2 - i1));
  917. result = GetResult (policy, result.Text);
  918. policy.SpaceBeforeLocalVariableDeclarationComma = false;
  919. policy.SpaceAfterLocalVariableDeclarationComma = false;
  920. result = GetResult (policy, result.Text);
  921. i1 = result.Text.IndexOf ("int");
  922. i2 = result.Text.IndexOf (";") + ";".Length;
  923. Assert.AreEqual (@"int a = 5,b = 6,c;", result.GetText (i1, i2 - i1));
  924. }
  925. [Test()]
  926. public void TestLocalVariableWithGenerics ()
  927. {
  928. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  929. policy.SpaceBeforeLocalVariableDeclarationComma = true;
  930. policy.SpaceAfterLocalVariableDeclarationComma = true;
  931. var result = GetResult (policy, @"class Test {
  932. void TestMe ()
  933. {
  934. List<Test> a;
  935. }
  936. }");
  937. int i1 = result.Text.IndexOf ("List");
  938. int i2 = result.Text.IndexOf (";") + ";".Length;
  939. Assert.AreEqual (@"List<Test> a;", result.GetText (i1, i2 - i1));
  940. }
  941. #region Constructors
  942. [Test()]
  943. public void TestBeforeConstructorDeclarationParentheses ()
  944. {
  945. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  946. policy.SpaceBeforeConstructorDeclarationParentheses = true;
  947. var result = GetResult (policy, @"class Test
  948. {
  949. Test()
  950. {
  951. }
  952. }");
  953. Assert.AreEqual (NormalizeNewlines(@"class Test
  954. {
  955. Test ()
  956. {
  957. }
  958. }"), result.Text);
  959. }
  960. [Test()]
  961. public void TestBeforeConstructorDeclarationParameterComma ()
  962. {
  963. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  964. policy.SpaceBeforeConstructorDeclarationParameterComma = true;
  965. policy.SpaceAfterConstructorDeclarationParameterComma = false;
  966. var result = GetResult (policy, @"class Test {
  967. public Test (int a,int b,int c) {}
  968. }");
  969. int i1 = result.Text.LastIndexOf ("(");
  970. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  971. Assert.AreEqual (@"(int a ,int b ,int c)", result.GetText (i1, i2 - i1));
  972. policy.SpaceBeforeConstructorDeclarationParameterComma = false;
  973. result = GetResult (policy, result.Text);
  974. i1 = result.Text.LastIndexOf ("(");
  975. i2 = result.Text.LastIndexOf (")") + ")".Length;
  976. Assert.AreEqual (@"(int a,int b,int c)", result.GetText (i1, i2 - i1));
  977. }
  978. [Test()]
  979. public void TestAfterConstructorDeclarationParameterComma ()
  980. {
  981. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  982. policy.SpaceBeforeConstructorDeclarationParameterComma = false;
  983. policy.SpaceAfterConstructorDeclarationParameterComma = true;
  984. var result = GetResult (policy, @"class Test {
  985. public Test (int a,int b,int c) {}
  986. }");
  987. int i1 = result.Text.LastIndexOf ("(");
  988. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  989. Assert.AreEqual (@"(int a, int b, int c)", result.GetText (i1, i2 - i1));
  990. policy.SpaceAfterConstructorDeclarationParameterComma = 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));
  995. }
  996. [Test()]
  997. public void TestWithinConstructorDeclarationParentheses ()
  998. {
  999. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  1000. policy.SpaceWithinConstructorDeclarationParentheses = true;
  1001. var result = GetResult (policy, @"class Test {
  1002. Test (int a)
  1003. {
  1004. }
  1005. }");
  1006. int i1 = result.Text.LastIndexOf ("(");
  1007. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  1008. Assert.AreEqual (@"( int a )", result.GetText (i1, i2 - i1));
  1009. }
  1010. [Test()]
  1011. public void TestBetweenEmptyConstructorDeclarationParentheses ()
  1012. {
  1013. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  1014. policy.SpaceBetweenEmptyConstructorDeclarationParentheses = true;
  1015. var result = GetResult (policy, @"class Test {
  1016. Test ()
  1017. {
  1018. }
  1019. }");
  1020. int i1 = result.Text.LastIndexOf ("(");
  1021. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  1022. Assert.AreEqual (@"( )", result.GetText (i1, i2 - i1));
  1023. }
  1024. #endregion
  1025. #region Delegates
  1026. [Test()]
  1027. public void TestBeforeDelegateDeclarationParentheses ()
  1028. {
  1029. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  1030. policy.SpaceBeforeDelegateDeclarationParentheses = true;
  1031. var result = GetResult (policy, @"delegate void Test();");
  1032. Assert.AreEqual (@"delegate void Test ();", result.Text);
  1033. }
  1034. [Test()]
  1035. public void TestBeforeDelegateDeclarationParenthesesComplex ()
  1036. {
  1037. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  1038. policy.SpaceBeforeDelegateDeclarationParentheses = true;
  1039. var result = GetResult (policy, "delegate void TestDelegate\t\t\t();");
  1040. Assert.AreEqual (@"delegate void TestDelegate ();", result.Text);
  1041. }
  1042. [Test()]
  1043. public void TestBeforeDelegateDeclarationParameterComma ()
  1044. {
  1045. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  1046. policy.SpaceBeforeDelegateDeclarationParameterComma = true;
  1047. policy.SpaceAfterDelegateDeclarationParameterComma = false;
  1048. var result = GetResult (policy, @"delegate void Test (int a,int b,int c);");
  1049. int i1 = result.Text.LastIndexOf ("(");
  1050. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  1051. Assert.AreEqual (@"(int a ,int b ,int c)", result.GetText (i1, i2 - i1));
  1052. policy.SpaceBeforeDelegateDeclarationParameterComma = false;
  1053. result = GetResult (policy, result.Text);
  1054. i1 = result.Text.LastIndexOf ("(");
  1055. i2 = result.Text.LastIndexOf (")") + ")".Length;
  1056. Assert.AreEqual (@"(int a,int b,int c)", result.GetText (i1, i2 - i1));
  1057. }
  1058. [Test()]
  1059. public void TestAfterDelegateDeclarationParameterComma ()
  1060. {
  1061. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  1062. policy.SpaceBeforeDelegateDeclarationParameterComma = false;
  1063. policy.SpaceAfterDelegateDeclarationParameterComma = true;
  1064. var result = GetResult (policy, @"delegate void Test (int a,int b,int c);");
  1065. int i1 = result.Text.LastIndexOf ("(");
  1066. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  1067. Assert.AreEqual (@"(int a, int b, int c)", result.GetText (i1, i2 - i1));
  1068. policy.SpaceAfterDelegateDeclarationParameterComma = false;
  1069. result = GetResult (policy, result.Text);
  1070. i1 = result.Text.LastIndexOf ("(");
  1071. i2 = result.Text.LastIndexOf (")") + ")".Length;
  1072. Assert.AreEqual (@"(int a,int b,int c)", result.GetText (i1, i2 - i1));
  1073. }
  1074. [Test()]
  1075. public void TestWithinDelegateDeclarationParentheses ()
  1076. {
  1077. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  1078. policy.SpaceWithinDelegateDeclarationParentheses = true;
  1079. var result = GetResult (policy, @"delegate void Test (int a);");
  1080. int i1 = result.Text.LastIndexOf ("(");
  1081. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  1082. Assert.AreEqual (@"( int a )", result.GetText (i1, i2 - i1));
  1083. }
  1084. [Test()]
  1085. public void TestBetweenEmptyDelegateDeclarationParentheses ()
  1086. {
  1087. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  1088. policy.SpaceBetweenEmptyDelegateDeclarationParentheses = true;
  1089. var result = GetResult (policy, @"delegate void Test();");
  1090. int i1 = result.Text.LastIndexOf ("(");
  1091. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  1092. Assert.AreEqual (@"( )", result.GetText (i1, i2 - i1));
  1093. }
  1094. #endregion
  1095. #region Method invocations
  1096. [Test()]
  1097. public void TestBeforeMethodCallParentheses ()
  1098. {
  1099. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  1100. policy.SpaceBeforeMethodCallParentheses = true;
  1101. var result = GetResult (policy, @"class FooBar
  1102. {
  1103. public void Foo ()
  1104. {
  1105. Test();
  1106. }
  1107. }");
  1108. Assert.AreEqual (NormalizeNewlines(@"class FooBar
  1109. {
  1110. public void Foo ()
  1111. {
  1112. Test ();
  1113. }
  1114. }"), result.Text);
  1115. }
  1116. [Test()]
  1117. public void TestBeforeMethodCallParameterComma ()
  1118. {
  1119. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  1120. policy.SpaceBeforeMethodCallParameterComma = true;
  1121. policy.SpaceAfterMethodCallParameterComma = false;
  1122. var result = GetResult (policy, @"class FooBar
  1123. {
  1124. public void Foo ()
  1125. {
  1126. Test(a,b,c);
  1127. }
  1128. }");
  1129. int i1 = result.Text.LastIndexOf ("(");
  1130. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  1131. Assert.AreEqual (@"(a ,b ,c)", result.GetText (i1, i2 - i1));
  1132. policy.SpaceBeforeMethodCallParameterComma = false;
  1133. result = GetResult (policy, result.Text);
  1134. i1 = result.Text.LastIndexOf ("(");
  1135. i2 = result.Text.LastIndexOf (")") + ")".Length;
  1136. Assert.AreEqual (@"(a,b,c)", result.GetText (i1, i2 - i1));
  1137. }
  1138. [Test()]
  1139. public void TestAfterMethodCallParameterComma ()
  1140. {
  1141. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  1142. policy.SpaceBeforeMethodCallParameterComma = false;
  1143. policy.SpaceAfterMethodCallParameterComma = true;
  1144. var result = GetResult (policy, @"class FooBar
  1145. {
  1146. public void Foo ()
  1147. {
  1148. Test(a,b,c);
  1149. }
  1150. }");
  1151. int i1 = result.Text.LastIndexOf ("(");
  1152. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  1153. Assert.AreEqual (@"(a, b, c)", result.GetText (i1, i2 - i1));
  1154. policy.SpaceAfterMethodCallParameterComma = false;
  1155. result = GetResult (policy, result.Text);
  1156. i1 = result.Text.LastIndexOf ("(");
  1157. i2 = result.Text.LastIndexOf (")") + ")".Length;
  1158. Assert.AreEqual (@"(a,b,c)", result.GetText (i1, i2 - i1));
  1159. }
  1160. [Test()]
  1161. public void TestWithinMethodCallParentheses ()
  1162. {
  1163. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  1164. policy.SpaceWithinMethodCallParentheses = true;
  1165. var result = GetResult (policy, @"class FooBar
  1166. {
  1167. public void Foo ()
  1168. {
  1169. Test(a);
  1170. }
  1171. }");
  1172. int i1 = result.Text.LastIndexOf ("(");
  1173. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  1174. Assert.AreEqual (@"( a )", result.GetText (i1, i2 - i1));
  1175. }
  1176. [Test()]
  1177. public void TestBetweenEmptyMethodCallParentheses ()
  1178. {
  1179. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  1180. policy.SpaceBetweenEmptyMethodCallParentheses = true;
  1181. var result = GetResult (policy, @"class FooBar
  1182. {
  1183. public void Foo ()
  1184. {
  1185. Test();
  1186. }
  1187. }");
  1188. int i1 = result.Text.LastIndexOf ("(");
  1189. int i2 = result.Text.LastIndexOf (")") + ")".Length;
  1190. Assert.AreEqual (@"( )", result.GetText (i1, i2 - i1));
  1191. }
  1192. #endregion
  1193. #region Indexer declarations
  1194. [Test()]
  1195. public void TestBeforeIndexerDeclarationBracket ()
  1196. {
  1197. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  1198. policy.SpaceBeforeIndexerDeclarationBracket = true;
  1199. var result = GetResult (policy, @"class FooBar
  1200. {
  1201. public int this[int a, int b] {
  1202. get {
  1203. return a + b;
  1204. }
  1205. }
  1206. }");
  1207. Assert.AreEqual (NormalizeNewlines(@"class FooBar
  1208. {
  1209. public int this [int a, int b] {
  1210. get {
  1211. return a + b;
  1212. }
  1213. }
  1214. }"), result.Text);
  1215. }
  1216. [Test()]
  1217. public void TestBeforeIndexerDeclarationParameterComma ()
  1218. {
  1219. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  1220. policy.SpaceBeforeIndexerDeclarationParameterComma = true;
  1221. policy.SpaceAfterIndexerDeclarationParameterComma = false;
  1222. var result = GetResult (policy, @"class FooBar
  1223. {
  1224. public int this[int a,int b] {
  1225. get {
  1226. return a + b;
  1227. }
  1228. }
  1229. }");
  1230. int i1 = result.Text.LastIndexOf ("[");
  1231. int i2 = result.Text.LastIndexOf ("]") + "]".Length;
  1232. Assert.AreEqual (@"[int a ,int b]", result.GetText (i1, i2 - i1));
  1233. }
  1234. [Test()]
  1235. public void TestAfterIndexerDeclarationParameterComma ()
  1236. {
  1237. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  1238. policy.SpaceAfterIndexerDeclarationParameterComma = true;
  1239. var result = GetResult (policy, @"class FooBar
  1240. {
  1241. public int this[int a,int b] {
  1242. get {
  1243. return a + b;
  1244. }
  1245. }
  1246. }");
  1247. int i1 = result.Text.LastIndexOf ("[");
  1248. int i2 = result.Text.LastIndexOf ("]") + "]".Length;
  1249. Assert.AreEqual (@"[int a, int b]", result.GetText (i1, i2 - i1));
  1250. }
  1251. [Test()]
  1252. public void TestWithinIndexerDeclarationBracket ()
  1253. {
  1254. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  1255. policy.SpaceWithinIndexerDeclarationBracket = true;
  1256. var result = GetResult (policy, @"class FooBar
  1257. {
  1258. public int this[int a, int b] {
  1259. get {
  1260. return a + b;
  1261. }
  1262. }
  1263. }");
  1264. int i1 = result.Text.LastIndexOf ("[");
  1265. int i2 = result.Text.LastIndexOf ("]") + "]".Length;
  1266. Assert.AreEqual (@"[ int a, int b ]", result.GetText (i1, i2 - i1));
  1267. }
  1268. #endregion
  1269. #region Brackets
  1270. [Test()]
  1271. public void TestSpacesWithinBrackets ()
  1272. {
  1273. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  1274. policy.SpacesWithinBrackets = true;
  1275. policy.SpacesBeforeBrackets = false;
  1276. var result = GetResult (policy, @"class Test {
  1277. void TestMe ()
  1278. {
  1279. this[0] = 5;
  1280. }
  1281. }");
  1282. Assert.AreEqual (NormalizeNewlines(@"class Test
  1283. {
  1284. void TestMe ()
  1285. {
  1286. this[ 0 ] = 5;
  1287. }
  1288. }"), result.Text);
  1289. }
  1290. [Test()]
  1291. public void TestSpacesBeforeBrackets ()
  1292. {
  1293. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  1294. policy.SpacesBeforeBrackets = true;
  1295. var result = GetResult (policy, @"class Test
  1296. {
  1297. void TestMe ()
  1298. {
  1299. this[0] = 5;
  1300. }
  1301. }");
  1302. Assert.AreEqual (NormalizeNewlines(@"class Test
  1303. {
  1304. void TestMe ()
  1305. {
  1306. this [0] = 5;
  1307. }
  1308. }"), result.Text);
  1309. }
  1310. [Test()]
  1311. public void TestBeforeBracketComma ()
  1312. {
  1313. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  1314. policy.SpaceBeforeBracketComma = true;
  1315. policy.SpaceAfterBracketComma = false;
  1316. var result = GetResult (policy, @"class Test {
  1317. void TestMe ()
  1318. {
  1319. this[1,2,3] = 5;
  1320. }
  1321. }");
  1322. int i1 = result.Text.LastIndexOf ("[");
  1323. int i2 = result.Text.LastIndexOf ("]") + "]".Length;
  1324. Assert.AreEqual (@"[1 ,2 ,3]", result.GetText (i1, i2 - i1));
  1325. }
  1326. [Test()]
  1327. public void TestAfterBracketComma ()
  1328. {
  1329. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  1330. policy.SpaceAfterBracketComma = true;
  1331. var result = GetResult (policy, @"class Test {
  1332. void TestMe ()
  1333. {
  1334. this[1,2,3] = 5;
  1335. }
  1336. }");
  1337. int i1 = result.Text.LastIndexOf ("[");
  1338. int i2 = result.Text.LastIndexOf ("]") + "]".Length;
  1339. Assert.AreEqual (@"[1, 2, 3]", result.GetText (i1, i2 - i1));
  1340. }
  1341. #endregion
  1342. [Test()]
  1343. public void TestSpacesBeforeArrayDeclarationBrackets ()
  1344. {
  1345. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  1346. policy.SpaceBeforeArrayDeclarationBrackets = true;
  1347. var result = GetResult (policy, @"class Test {
  1348. int[] a;
  1349. int[][] b;
  1350. }");
  1351. Assert.AreEqual (NormalizeNewlines(@"class Test
  1352. {
  1353. int [] a;
  1354. int [][] b;
  1355. }"), result.Text);
  1356. }
  1357. [Test()]
  1358. public void TestRemoveWhitespacesBeforeSemicolon()
  1359. {
  1360. CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono ();
  1361. var result = GetResult(policy, @"class Test {
  1362. void TestMe ()
  1363. {
  1364. Foo () ;
  1365. }
  1366. }");
  1367. int i1 = result.Text.IndexOf("Foo");
  1368. int i2 = result.Text.LastIndexOf(";") + ";".Length;
  1369. Assert.AreEqual(@"Foo ();", result.GetText(i1, i2 - i1));
  1370. }
  1371. [Test()]
  1372. public void TestSpaceInNamedArgumentAfterDoubleColon()
  1373. {
  1374. var policy = FormattingOptionsFactory.CreateMono ();
  1375. policy.SpaceInNamedArgumentAfterDoubleColon = true;
  1376. var result = GetResult(policy, @"class Test {
  1377. void TestMe ()
  1378. {
  1379. Foo (bar:expr);
  1380. }
  1381. }");
  1382. int i1 = result.Text.IndexOf("Foo");
  1383. int i2 = result.Text.LastIndexOf(";") + ";".Length;
  1384. Assert.AreEqual(@"Foo (bar: expr);", result.GetText(i1, i2 - i1));
  1385. }
  1386. [Test()]
  1387. public void TestSpaceInNamedArgumentAfterDoubleColon2()
  1388. {
  1389. var policy = FormattingOptionsFactory.CreateMono ();
  1390. policy.SpaceInNamedArgumentAfterDoubleColon = false;
  1391. var result = GetResult(policy, @"class Test {
  1392. void TestMe ()
  1393. {
  1394. Foo (bar: expr);
  1395. }
  1396. }");
  1397. int i1 = result.Text.IndexOf("Foo");
  1398. int i2 = result.Text.LastIndexOf(";") + ";".Length;
  1399. Assert.AreEqual(@"Foo (bar:expr);", result.GetText(i1, i2 - i1));
  1400. }
  1401. }
  1402. }