PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/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
19using System;
20using System.Linq;
21using NUnit.Framework;
22
23namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
24{
25	[TestFixture]
26	public class ForStatementTests
27	{
28		[Test]
29		public void ForeachStatementTest()
30		{
31			ParseUtilCSharp.AssertStatement(
32				"foreach (int i in myColl) {} ",
33				new ForeachStatement {
34					VariableType = new PrimitiveType("int"),
35					VariableName = "i",
36					InExpression = new IdentifierExpression("myColl"),
37					EmbeddedStatement = new BlockStatement()
38				});
39		}
40		
41		[Test]
42		public void EmptyForStatementTest()
43		{
44			ForStatement forStmt = ParseUtilCSharp.ParseStatement<ForStatement>("for (;;) ;");
45			Assert.AreEqual(0, forStmt.Initializers.Count());
46			Assert.AreEqual(0, forStmt.Iterators.Count());
47			Assert.IsTrue(forStmt.Condition.IsNull);
48			Assert.IsTrue(forStmt.EmbeddedStatement is EmptyStatement);
49		}
50		
51		[Test]
52		public void ForStatementTest()
53		{
54			ForStatement forStmt = ParseUtilCSharp.ParseStatement<ForStatement>("for (int i = 5; i < 6; ++i) {} ");
55			var init = (VariableDeclarationStatement)forStmt.Initializers.Single();
56			Assert.AreEqual("i", init.Variables.Single().Name);
57			
58			Assert.IsTrue(forStmt.Condition is BinaryOperatorExpression);
59			
60			var inc = (ExpressionStatement)forStmt.Iterators.Single();
61			Assert.IsTrue(inc.Expression is UnaryOperatorExpression);
62		}
63		
64		[Test]
65		public void ForStatementTestMultipleInitializers()
66		{
67			ForStatement forStmt = ParseUtilCSharp.ParseStatement<ForStatement>("for (i = 0, j = 1; i < 6; ++i) {} ");
68			Assert.AreEqual(2, forStmt.Initializers.Count());
69			Assert.IsTrue(forStmt.Iterators.All(i => i is ExpressionStatement));
70		}
71		
72		[Test]
73		public void ForStatementTestMultipleIterators()
74		{
75			ForStatement forStmt = ParseUtilCSharp.ParseStatement<ForStatement>("for (int i = 5, j = 10; i < 6; ++i, j--) {} ");
76			Assert.AreEqual(1, forStmt.Initializers.Count());
77			Assert.AreEqual(2, ((VariableDeclarationStatement)forStmt.Initializers.Single()).Variables.Count());
78			Assert.AreEqual(2, forStmt.Iterators.Count());
79			Assert.IsTrue(forStmt.Iterators.All(i => i is ExpressionStatement));
80		}
81	}
82}