/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/ForStatementTests.cs

http://github.com/icsharpcode/ILSpy · C# · 82 lines · 57 code · 8 blank · 17 comment · 0 complexity · 50a4ce20317028f3040a43a70f0ffe7e MD5 · raw file

  1. // Copyright (c) AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Linq;
  20. using NUnit.Framework;
  21. namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
  22. {
  23. [TestFixture]
  24. public class ForStatementTests
  25. {
  26. [Test]
  27. public void ForeachStatementTest()
  28. {
  29. ParseUtilCSharp.AssertStatement(
  30. "foreach (int i in myColl) {} ",
  31. new ForeachStatement {
  32. VariableType = new PrimitiveType("int"),
  33. VariableName = "i",
  34. InExpression = new IdentifierExpression("myColl"),
  35. EmbeddedStatement = new BlockStatement()
  36. });
  37. }
  38. [Test]
  39. public void EmptyForStatementTest()
  40. {
  41. ForStatement forStmt = ParseUtilCSharp.ParseStatement<ForStatement>("for (;;) ;");
  42. Assert.AreEqual(0, forStmt.Initializers.Count());
  43. Assert.AreEqual(0, forStmt.Iterators.Count());
  44. Assert.IsTrue(forStmt.Condition.IsNull);
  45. Assert.IsTrue(forStmt.EmbeddedStatement is EmptyStatement);
  46. }
  47. [Test]
  48. public void ForStatementTest()
  49. {
  50. ForStatement forStmt = ParseUtilCSharp.ParseStatement<ForStatement>("for (int i = 5; i < 6; ++i) {} ");
  51. var init = (VariableDeclarationStatement)forStmt.Initializers.Single();
  52. Assert.AreEqual("i", init.Variables.Single().Name);
  53. Assert.IsTrue(forStmt.Condition is BinaryOperatorExpression);
  54. var inc = (ExpressionStatement)forStmt.Iterators.Single();
  55. Assert.IsTrue(inc.Expression is UnaryOperatorExpression);
  56. }
  57. [Test]
  58. public void ForStatementTestMultipleInitializers()
  59. {
  60. ForStatement forStmt = ParseUtilCSharp.ParseStatement<ForStatement>("for (i = 0, j = 1; i < 6; ++i) {} ");
  61. Assert.AreEqual(2, forStmt.Initializers.Count());
  62. Assert.IsTrue(forStmt.Iterators.All(i => i is ExpressionStatement));
  63. }
  64. [Test]
  65. public void ForStatementTestMultipleIterators()
  66. {
  67. ForStatement forStmt = ParseUtilCSharp.ParseStatement<ForStatement>("for (int i = 5, j = 10; i < 6; ++i, j--) {} ");
  68. Assert.AreEqual(1, forStmt.Initializers.Count());
  69. Assert.AreEqual(2, ((VariableDeclarationStatement)forStmt.Initializers.Single()).Variables.Count());
  70. Assert.AreEqual(2, forStmt.Iterators.Count());
  71. Assert.IsTrue(forStmt.Iterators.All(i => i is ExpressionStatement));
  72. }
  73. }
  74. }