Browse Source

Add statement-level parser tests.

newNRvisualizers
Daniel Grunwald 16 years ago
parent
commit
f52338f265
  1. 16
      ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/DelegateDeclarationTests.cs
  2. 2
      ICSharpCode.NRefactory.Tests/CSharp/Parser/ParseUtil.cs
  3. 41
      ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/BlockStatementTests.cs
  4. 45
      ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/CheckedStatementTests.cs
  5. 18
      ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/EmptyStatementTests.cs
  6. 26
      ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/ExpressionStatementTests.cs
  7. 19
      ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/FixedStatementTests.cs
  8. 37
      ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/ForStatementTests.cs
  9. 48
      ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/GotoStatementTests.cs
  10. 39
      ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/IfElseStatementTests.cs
  11. 26
      ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/LabelStatementTests.cs
  12. 19
      ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/LockStatementTests.cs
  13. 33
      ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/ReturnStatementTests.cs
  14. 20
      ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/SwitchStatementTests.cs
  15. 26
      ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/ThrowStatementTests.cs
  16. 47
      ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/TryCatchStatementTests.cs
  17. 19
      ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/UnsafeStatementTests.cs
  18. 19
      ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/UsingStatementTests.cs
  19. 226
      ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/VariableDeclarationStatementTests.cs
  20. 30
      ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/WhileStatementTests.cs
  21. 41
      ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/YieldStatementTests.cs
  22. 20
      ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj
  23. 6
      ICSharpCode.NRefactory/CSharp/Dom/Statements/CheckedStatement.cs
  24. 7
      ICSharpCode.NRefactory/CSharp/Dom/Statements/GotoStatement.cs
  25. 6
      ICSharpCode.NRefactory/CSharp/Dom/Statements/UncheckedStatement.cs
  26. 3
      ICSharpCode.NRefactory/CSharp/Dom/Statements/YieldStatement.cs
  27. 4
      ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs

16
ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/DelegateDeclarationTests.cs

@ -57,5 +57,21 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.GeneralScope @@ -57,5 +57,21 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.GeneralScope
Assert.AreEqual(1, dd.Templates[0].Bases.Count);
Assert.AreEqual("ICloneable", dd.Templates[0].Bases[0].Type);*/ throw new NotImplementedException();
}
[Test]
public void DelegateDeclarationInNamespace()
{
string program = "namespace N { delegate void MyDelegate(); }";
NamespaceDeclaration nd = ParseUtilCSharp.ParseGlobal<NamespaceDeclaration>(program);
Assert.AreEqual("MyDelegate", ((DelegateDeclaration)nd.Members.Single()).Name);
}
[Test, Ignore("inner classes not yet implemented")]
public void DelegateDeclarationInClass()
{
string program = "class Outer { delegate void Inner(); }";
TypeDeclaration td = ParseUtilCSharp.ParseGlobal<TypeDeclaration>(program);
Assert.AreEqual("Inner", ((DelegateDeclaration)td.Members.Single()).Name);
}
}
}

2
ICSharpCode.NRefactory.Tests/CSharp/Parser/ParseUtil.cs

@ -28,7 +28,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser @@ -28,7 +28,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser
public static T ParseStatement<T>(string stmt, bool expectErrors = false) where T : INode
{
Assert.Ignore("ParseExpression not yet implemented");
Assert.Ignore("ParseStatement not yet implemented");
CSharpParser parser = new CSharpParser();
BlockStatement parsedBlock = parser.ParseBlock(new StringReader(stmt));

41
ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/BlockStatementTests.cs

@ -0,0 +1,41 @@ @@ -0,0 +1,41 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using System.Linq;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
{
[TestFixture]
public class BlockStatementTests
{
[Test]
public void CSharpBlockStatementTest()
{
BlockStatement blockStmt = ParseUtilCSharp.ParseStatement<BlockStatement>("{}");
Assert.AreEqual(0, blockStmt.Statements.Count());
}
[Test]
public void CSharpComplexBlockStatementPositionTest()
{
string code = @"{
WebClient wc = new WebClient();
wc.Test();
wc.UploadStringCompleted += delegate {
output.BeginInvoke((MethodInvoker)delegate {
output.Text += newText;
});
};
}";
BlockStatement blockStmt = ParseUtilCSharp.ParseStatement<BlockStatement>(code);
Assert.AreEqual(1, blockStmt.StartLocation.Column);
Assert.AreEqual(1, blockStmt.StartLocation.Line);
Assert.AreEqual(2, blockStmt.EndLocation.Column);
Assert.AreEqual(9, blockStmt.EndLocation.Line);
Assert.AreEqual(3, blockStmt.Statements.Count());
}
}
}

45
ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/CheckedStatementTests.cs

@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using System.Linq;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
{
[TestFixture]
public class CheckedStatementTests
{
[Test]
public void CheckedStatementTest()
{
CheckedStatement checkedStatement = ParseUtilCSharp.ParseStatement<CheckedStatement>("checked { }");
Assert.IsNotNull(checkedStatement.Block);
}
[Test]
public void CheckedStatementAndExpressionTest()
{
CheckedStatement checkedStatement = ParseUtilCSharp.ParseStatement<CheckedStatement>("checked { checked(++i); }");
ExpressionStatement es = (ExpressionStatement)checkedStatement.Block.Statements.Single();
CheckedExpression ce = (CheckedExpression)es.Expression;
Assert.IsTrue(ce.Expression is UnaryOperatorExpression);
}
[Test]
public void UncheckedStatementTest()
{
UncheckedStatement uncheckedStatement = ParseUtilCSharp.ParseStatement<UncheckedStatement>("unchecked { }");
Assert.IsNotNull(uncheckedStatement.Block);
}
[Test]
public void UncheckedStatementAndExpressionTest()
{
UncheckedStatement uncheckedStatement = ParseUtilCSharp.ParseStatement<UncheckedStatement>("unchecked { unchecked(++i); }");
ExpressionStatement es = (ExpressionStatement)uncheckedStatement.Block.Statements.Single();
CheckedExpression ce = (CheckedExpression)es.Expression;
Assert.IsTrue(ce.Expression is UnaryOperatorExpression);
}
}
}

18
ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/EmptyStatementTests.cs

@ -0,0 +1,18 @@ @@ -0,0 +1,18 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
{
[TestFixture]
public class EmptyStatementTests
{
[Test]
public void CSharpEmptyStatementTest()
{
EmptyStatement emptyStmt = ParseUtilCSharp.ParseStatement<EmptyStatement>(";");
}
}
}

26
ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/ExpressionStatementTests.cs

@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
{
[TestFixture]
public class ExpressionStatementTests
{
[Test]
public void StatementExpressionTest()
{
ExpressionStatement stmtExprStmt = ParseUtilCSharp.ParseStatement<ExpressionStatement>("my.Obj.PropCall;");
Assert.IsTrue(stmtExprStmt.Expression is MemberReferenceExpression);
}
[Test]
public void StatementExpressionTest1()
{
ExpressionStatement stmtExprStmt = ParseUtilCSharp.ParseStatement<ExpressionStatement>("yield.yield();");
Assert.IsTrue(stmtExprStmt.Expression is InvocationExpression);
}
}
}

19
ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/FixedStatementTests.cs

@ -0,0 +1,19 @@ @@ -0,0 +1,19 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
{
[TestFixture]
public class FixedStatementTests
{
[Test]
public void CSharpFixedStatementTest()
{
FixedStatement fixedStmt = ParseUtilCSharp.ParseStatement<FixedStatement>("fixed (int* ptr = &myIntArr) { }");
// TODO : Extend test.
}
}
}

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

@ -0,0 +1,37 @@ @@ -0,0 +1,37 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using System.Linq;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
{
[TestFixture]
public class ForStatementTests
{
[Test]
public void CSharpForeachStatementTest()
{
ForeachStatement foreachStmt = ParseUtilCSharp.ParseStatement<ForeachStatement>("foreach (int i in myColl) {} ");
// TODO : Extend test.
}
[Test]
public void CSharpEmptyForStatementTest()
{
ForStatement forStmt = ParseUtilCSharp.ParseStatement<ForStatement>("for (;;) ;");
Assert.AreEqual(0, forStmt.Initializers.Count());
Assert.AreEqual(0, forStmt.Iterators.Count());
Assert.IsNull(forStmt.Condition);
Assert.IsTrue(forStmt.EmbeddedStatement is EmptyStatement);
}
[Test]
public void CSharpForStatementTest()
{
ForStatement forStmt = ParseUtilCSharp.ParseStatement<ForStatement>("for (int i = 5; i < 6; ++i) {} ");
// TODO : Extend test.
}
}
}

48
ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/GotoStatementTests.cs

@ -0,0 +1,48 @@ @@ -0,0 +1,48 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
{
[TestFixture]
public class GotoStatementTests
{
[Test]
public void GotoStatementTest()
{
GotoStatement gotoStmt = ParseUtilCSharp.ParseStatement<GotoStatement>("goto myLabel;");
Assert.AreEqual(GotoType.Label, gotoStmt.GotoType);
Assert.AreEqual("myLabel", gotoStmt.Label);
}
[Test]
public void GotoDefaultStatementTest()
{
GotoStatement gotoCaseStmt = ParseUtilCSharp.ParseStatement<GotoStatement>("goto default;");
Assert.AreEqual(GotoType.CaseDefault, gotoCaseStmt.GotoType);
}
[Test]
public void GotoCaseStatementTest()
{
GotoStatement gotoCaseStmt = ParseUtilCSharp.ParseStatement<GotoStatement>("goto case 6;");
Assert.AreEqual(GotoType.Case, gotoCaseStmt.GotoType);
Assert.IsTrue(gotoCaseStmt.LabelExpression is PrimitiveExpression);
}
[Test]
public void BreakStatementTest()
{
BreakStatement breakStmt = ParseUtilCSharp.ParseStatement<BreakStatement>("break;");
}
[Test]
public void ContinueStatementTest()
{
ContinueStatement continueStmt = ParseUtilCSharp.ParseStatement<ContinueStatement>("continue;");
}
}
}

39
ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/IfElseStatementTests.cs

@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
{
[TestFixture]
public class IfElseStatementTests
{
[Test]
public void SimpleIfStatementTest()
{
IfElseStatement ifElseStatement = ParseUtilCSharp.ParseStatement<IfElseStatement>("if (true) { }");
Assert.IsTrue(ifElseStatement.Condition is PrimitiveExpression);
Assert.IsTrue(ifElseStatement.TrueEmbeddedStatement is BlockStatement);
Assert.IsNull(ifElseStatement.FalseEmbeddedStatement);
}
[Test]
public void SimpleIfElseStatementTest()
{
IfElseStatement ifElseStatement = ParseUtilCSharp.ParseStatement<IfElseStatement>("if (true) { } else { }");
Assert.IsTrue(ifElseStatement.Condition is PrimitiveExpression);
Assert.IsTrue(ifElseStatement.TrueEmbeddedStatement is BlockStatement);
Assert.IsTrue(ifElseStatement.FalseEmbeddedStatement is BlockStatement);
}
[Test]
public void IfElseIfStatementTest()
{
IfElseStatement ifElseStatement = ParseUtilCSharp.ParseStatement<IfElseStatement>("if (1) { } else if (2) { } else if (3) { } else { }");
Assert.IsTrue(ifElseStatement.Condition is PrimitiveExpression);
Assert.IsTrue(ifElseStatement.TrueEmbeddedStatement is BlockStatement);
Assert.IsTrue(ifElseStatement.FalseEmbeddedStatement is IfElseStatement);
}
}
}

26
ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/LabelStatementTests.cs

@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
{
[TestFixture]
public class LabelStatementTests
{
[Test]
public void LabelStatementTest()
{
LabelStatement labelStmt = ParseUtilCSharp.ParseStatement<LabelStatement>("myLabel: ; ");
Assert.AreEqual("myLabel", labelStmt.Label);
}
[Test]
public void Label2StatementTest()
{
LabelStatement labelStmt = ParseUtilCSharp.ParseStatement<LabelStatement>("yield: ; ");
Assert.AreEqual("yield", labelStmt.Label);
}
}
}

19
ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/LockStatementTests.cs

@ -0,0 +1,19 @@ @@ -0,0 +1,19 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
{
[TestFixture]
public class LockStatementTests
{
[Test]
public void CSharpLockStatementTest()
{
LockStatement lockStmt = ParseUtilCSharp.ParseStatement<LockStatement>("lock (myObj) {}");
// TODO : Extend test.
}
}
}

33
ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/ReturnStatementTests.cs

@ -0,0 +1,33 @@ @@ -0,0 +1,33 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
{
[TestFixture]
public class ReturnStatementTests
{
[Test]
public void EmptyReturnStatementTest()
{
ReturnStatement returnStatement = ParseUtilCSharp.ParseStatement<ReturnStatement>("return;");
Assert.IsNull(returnStatement.Expression);
}
[Test]
public void ReturnStatementTest()
{
ReturnStatement returnStatement = ParseUtilCSharp.ParseStatement<ReturnStatement>("return 5;");
Assert.IsTrue(returnStatement.Expression is PrimitiveExpression);
}
[Test]
public void ReturnStatementTest1()
{
ReturnStatement returnStatement = ParseUtilCSharp.ParseStatement<ReturnStatement>("return yield;");
Assert.IsTrue(returnStatement.Expression is IdentifierExpression);
}
}
}

20
ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/SwitchStatementTests.cs

@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
{
[TestFixture]
public class SwitchStatementTests
{
[Test]
public void SwitchStatementTest()
{
SwitchStatement switchStmt = ParseUtilCSharp.ParseStatement<SwitchStatement>("switch (a) { case 4: case 5: break; case 6: break; default: break; }");
Assert.AreEqual("a", ((IdentifierExpression)switchStmt.Expression).Identifier);
// TODO: Extend test
}
}
}

26
ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/ThrowStatementTests.cs

@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
{
[TestFixture]
public class ThrowStatementTests
{
[Test]
public void EmptyThrowStatementTest()
{
ThrowStatement throwStmt = ParseUtilCSharp.ParseStatement<ThrowStatement>("throw;");
Assert.IsNull(throwStmt.Expression);
}
[Test]
public void ThrowStatementTest()
{
ThrowStatement throwStmt = ParseUtilCSharp.ParseStatement<ThrowStatement>("throw new Exception();");
Assert.IsTrue(throwStmt.Expression is ObjectCreateExpression);
}
}
}

47
ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/TryCatchStatementTests.cs

@ -0,0 +1,47 @@ @@ -0,0 +1,47 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using System.Linq;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
{
[TestFixture, Ignore]
public class TryCatchStatementTests
{
[Test]
public void CSharpSimpleTryCatchStatementTest()
{
TryCatchStatement tryCatchStatement = ParseUtilCSharp.ParseStatement<TryCatchStatement>("try { } catch { } ");
Assert.IsNull(tryCatchStatement.FinallyBlock);
Assert.AreEqual(1, tryCatchStatement.CatchClauses.Count());
Assert.IsNull(tryCatchStatement.CatchClauses.Single().ReturnType);
Assert.IsNull(tryCatchStatement.CatchClauses.Single().VariableName);
}
/* TODO port tests
[Test]
public void CSharpSimpleTryCatchStatementTest2()
{
TryCatchStatement tryCatchStatement = ParseUtilCSharp.ParseStatement<TryCatchStatement>("try { } catch (Exception e) { } ");
Assert.IsTrue(tryCatchStatement.FinallyBlock.IsNull);
Assert.AreEqual(1, tryCatchStatement.CatchClauses.Count);
Assert.AreEqual("Exception", tryCatchStatement.CatchClauses[0].TypeReference.Type);
Assert.AreEqual("e", tryCatchStatement.CatchClauses[0].VariableName);
}
[Test]
public void CSharpSimpleTryCatchFinallyStatementTest()
{
TryCatchStatement tryCatchStatement = ParseUtilCSharp.ParseStatement<TryCatchStatement>("try { } catch (Exception) { } catch { } finally { } ");
Assert.IsFalse(tryCatchStatement.FinallyBlock.IsNull);
Assert.AreEqual(2, tryCatchStatement.CatchClauses.Count);
Assert.AreEqual("Exception", tryCatchStatement.CatchClauses[0].TypeReference.Type);
Assert.IsEmpty(tryCatchStatement.CatchClauses[0].VariableName);
Assert.IsTrue(tryCatchStatement.CatchClauses[1].TypeReference.IsNull);
Assert.IsEmpty(tryCatchStatement.CatchClauses[1].VariableName);
}
*/
}
}

19
ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/UnsafeStatementTests.cs

@ -0,0 +1,19 @@ @@ -0,0 +1,19 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
{
[TestFixture]
public class UnsafeStatementTests
{
[Test]
public void CSharpUnsafeStatementTest()
{
UnsafeStatement unsafeStatement = ParseUtilCSharp.ParseStatement<UnsafeStatement>("unsafe { }");
Assert.IsNotNull(unsafeStatement.Block);
}
}
}

19
ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/UsingStatementTests.cs

@ -0,0 +1,19 @@ @@ -0,0 +1,19 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
{
[TestFixture]
public class UsingStatementTests
{
[Test]
public void CSharpUsingStatementTest()
{
UsingStatement usingStmt = ParseUtilCSharp.ParseStatement<UsingStatement>("using (MyVar var = new MyVar()) { } ");
// TODO : Extend test.
}
}
}

226
ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/VariableDeclarationStatementTests.cs

@ -0,0 +1,226 @@ @@ -0,0 +1,226 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using System.Linq;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
{
[TestFixture, Ignore]
public class VariableDeclarationStatementTests
{
[Test]
public void VariableDeclarationStatementTest()
{
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("int a = 5;");
Assert.AreEqual(1, lvd.Variables.Count());
/*Assert.AreEqual("a", ((VariableDeclaration)lvd.Variables[0]).Name);
TypeReference type = lvd.GetTypeForVariable(0);
Assert.AreEqual("System.Int32", type.Type);
Assert.AreEqual(5, ((PrimitiveExpression)lvd.Variables[0].Initializer).Value);*/ throw new NotImplementedException();
}
/* TODO port unit tests
[Test]
public void VoidPointerVariableDeclarationTest()
{
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("void *a;");
Assert.AreEqual(1, lvd.Variables.Count);
Assert.AreEqual("a", ((VariableDeclaration)lvd.Variables[0]).Name);
TypeReference type = lvd.GetTypeForVariable(0);
Assert.AreEqual("System.Void", type.Type);
Assert.AreEqual(1, type.PointerNestingLevel);
}
[Test]
public void ComplexGenericVariableDeclarationStatementTest()
{
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("Generic<Namespace.Printable, G<Printable[]> > where = new Generic<Namespace.Printable, G<Printable[]>>();");
Assert.AreEqual(1, lvd.Variables.Count);
Assert.AreEqual("where", ((VariableDeclaration)lvd.Variables[0]).Name);
TypeReference type = lvd.GetTypeForVariable(0);
Assert.AreEqual("Generic", type.Type);
Assert.AreEqual(2, type.GenericTypes.Count);
Assert.AreEqual("Namespace.Printable", type.GenericTypes[0].Type);
Assert.AreEqual(0, type.GenericTypes[0].GenericTypes.Count);
Assert.AreEqual("G", type.GenericTypes[1].Type);
Assert.AreEqual(1, type.GenericTypes[1].GenericTypes.Count);
Assert.AreEqual("Printable", type.GenericTypes[1].GenericTypes[0].Type);
// TODO: Check initializer
}
[Test]
public void NestedGenericVariableDeclarationStatementTest()
{
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("MyType<string>.InnerClass<int>.InnerInnerClass a;");
Assert.AreEqual(1, lvd.Variables.Count);
InnerClassTypeReference ic = (InnerClassTypeReference)lvd.GetTypeForVariable(0);
Assert.AreEqual("InnerInnerClass", ic.Type);
Assert.AreEqual(0, ic.GenericTypes.Count);
ic = (InnerClassTypeReference)ic.BaseType;
Assert.AreEqual("InnerClass", ic.Type);
Assert.AreEqual(1, ic.GenericTypes.Count);
Assert.AreEqual("System.Int32", ic.GenericTypes[0].Type);
Assert.AreEqual("MyType", ic.BaseType.Type);
Assert.AreEqual(1, ic.BaseType.GenericTypes.Count);
Assert.AreEqual("System.String", ic.BaseType.GenericTypes[0].Type);
}
[Test]
public void GenericWithArrayVariableDeclarationStatementTest1()
{
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("G<int>[] a;");
Assert.AreEqual(1, lvd.Variables.Count);
TypeReference type = lvd.GetTypeForVariable(0);
Assert.AreEqual("G", type.Type);
Assert.AreEqual(1, type.GenericTypes.Count);
Assert.AreEqual("System.Int32", type.GenericTypes[0].Type);
Assert.AreEqual(0, type.GenericTypes[0].GenericTypes.Count);
Assert.IsFalse(type.GenericTypes[0].IsArrayType);
Assert.AreEqual(new int[] {0}, type.RankSpecifier);
}
[Test]
public void GenericWithArrayVariableDeclarationStatementTest2()
{
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("G<int[]> a;");
Assert.AreEqual(1, lvd.Variables.Count);
TypeReference type = lvd.GetTypeForVariable(0);
Assert.AreEqual("G", type.Type);
Assert.AreEqual(1, type.GenericTypes.Count);
Assert.AreEqual("System.Int32", type.GenericTypes[0].Type);
Assert.AreEqual(0, type.GenericTypes[0].GenericTypes.Count);
Assert.IsFalse(type.IsArrayType);
Assert.AreEqual(new int[] {0}, type.GenericTypes[0].RankSpecifier);
}
[Test]
public void GenericVariableDeclarationStatementTest2()
{
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("G<G<int> > a;");
Assert.AreEqual(1, lvd.Variables.Count);
TypeReference type = lvd.GetTypeForVariable(0);
Assert.AreEqual("G", type.Type);
Assert.AreEqual(1, type.GenericTypes.Count);
Assert.AreEqual("G", type.GenericTypes[0].Type);
Assert.AreEqual(1, type.GenericTypes[0].GenericTypes.Count);
Assert.AreEqual("System.Int32", type.GenericTypes[0].GenericTypes[0].Type);
}
[Test]
public void GenericVariableDeclarationStatementTest2WithoutSpace()
{
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("G<G<int>> a;");
Assert.AreEqual(1, lvd.Variables.Count);
TypeReference type = lvd.GetTypeForVariable(0);
Assert.AreEqual("G", type.Type);
Assert.AreEqual(1, type.GenericTypes.Count);
Assert.AreEqual("G", type.GenericTypes[0].Type);
Assert.AreEqual(1, type.GenericTypes[0].GenericTypes.Count);
Assert.AreEqual("System.Int32", type.GenericTypes[0].GenericTypes[0].Type);
}
[Test]
public void GenericVariableDeclarationStatementTest()
{
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("G<int> a;");
Assert.AreEqual(1, lvd.Variables.Count);
TypeReference type = lvd.GetTypeForVariable(0);
Assert.AreEqual("G", type.Type);
Assert.AreEqual(1, type.GenericTypes.Count);
Assert.AreEqual("System.Int32", type.GenericTypes[0].Type);
}
[Test]
public void SimpleVariableDeclarationStatementTest()
{
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("MyVar var = new MyVar();");
Assert.AreEqual(1, lvd.Variables.Count);
Assert.AreEqual("var", ((VariableDeclaration)lvd.Variables[0]).Name);
TypeReference type = lvd.GetTypeForVariable(0);
Assert.AreEqual("MyVar", type.Type);
// TODO: Check initializer
}
[Test]
public void SimpleVariableDeclarationStatementTest1()
{
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("yield yield = new yield();");
Assert.AreEqual(1, lvd.Variables.Count);
Assert.AreEqual("yield", ((VariableDeclaration)lvd.Variables[0]).Name);
TypeReference type = lvd.GetTypeForVariable(0);
Assert.AreEqual("yield", type.Type);
// TODO: Check initializer
}
[Test]
public void NullableVariableDeclarationStatementTest1()
{
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("int? a;");
Assert.AreEqual(1, lvd.Variables.Count);
Assert.AreEqual("a", ((VariableDeclaration)lvd.Variables[0]).Name);
TypeReference type = lvd.GetTypeForVariable(0);
Assert.AreEqual("System.Nullable", type.Type);
Assert.AreEqual("System.Int32", type.GenericTypes[0].Type);
}
[Test]
public void NullableVariableDeclarationStatementTest2()
{
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("DateTime? a;");
Assert.AreEqual(1, lvd.Variables.Count);
Assert.AreEqual("a", ((VariableDeclaration)lvd.Variables[0]).Name);
TypeReference type = lvd.GetTypeForVariable(0);
Assert.AreEqual("System.Nullable", type.Type);
Assert.AreEqual("DateTime", type.GenericTypes[0].Type);
}
[Test]
public void NullableVariableDeclarationStatementTest3()
{
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("DateTime?[] a;");
Assert.AreEqual(1, lvd.Variables.Count);
Assert.AreEqual("a", ((VariableDeclaration)lvd.Variables[0]).Name);
TypeReference type = lvd.GetTypeForVariable(0);
Assert.IsTrue(type.IsArrayType);
Assert.AreEqual("System.Nullable", type.Type);
Assert.AreEqual("DateTime", type.GenericTypes[0].Type);
}
[Test]
public void NullableVariableDeclarationStatementTest4()
{
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("SomeStruct<int?>? a;");
Assert.AreEqual(1, lvd.Variables.Count);
Assert.AreEqual("a", ((VariableDeclaration)lvd.Variables[0]).Name);
TypeReference type = lvd.GetTypeForVariable(0);
Assert.AreEqual("System.Nullable", type.Type);
Assert.AreEqual("SomeStruct", type.GenericTypes[0].Type);
Assert.AreEqual("System.Nullable", type.GenericTypes[0].GenericTypes[0].Type);
Assert.AreEqual("System.Int32", type.GenericTypes[0].GenericTypes[0].GenericTypes[0].Type);
}
[Test]
public void PositionTestWithoutModifier()
{
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("\ndouble w = 7;");
Assert.AreEqual(2, lvd.StartLocation.Line);
Assert.AreEqual(1, lvd.StartLocation.Column);
Assert.AreEqual(2, lvd.EndLocation.Line);
Assert.AreEqual(14, lvd.EndLocation.Column);
}
[Test]
public void PositionTestWithModifier()
{
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("\nconst double w = 7;");
Assert.AreEqual(Modifiers.Const, lvd.Modifier);
Assert.AreEqual(2, lvd.StartLocation.Line);
Assert.AreEqual(1, lvd.StartLocation.Column);
Assert.AreEqual(2, lvd.EndLocation.Line);
Assert.AreEqual(20, lvd.EndLocation.Column);
}*/
}
}

30
ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/WhileStatementTests.cs

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
{
[TestFixture]
public class WhileStatementTests
{
[Test]
public void WhileStatementTest()
{
WhileStatement loopStmt = ParseUtilCSharp.ParseStatement<WhileStatement>("while (true) { }");
Assert.AreEqual(WhilePosition.Begin, loopStmt.WhilePosition);
Assert.IsTrue(loopStmt.Condition is PrimitiveExpression);
Assert.IsTrue(loopStmt.EmbeddedStatement is BlockStatement);
}
[Test]
public void DoWhileStatementTest()
{
WhileStatement loopStmt = ParseUtilCSharp.ParseStatement<WhileStatement>("do { } while (true);");
Assert.AreEqual(WhilePosition.End, loopStmt.WhilePosition);
Assert.IsTrue(loopStmt.Condition is PrimitiveExpression);
Assert.IsTrue(loopStmt.EmbeddedStatement is BlockStatement);
}
}
}

41
ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/YieldStatementTests.cs

@ -0,0 +1,41 @@ @@ -0,0 +1,41 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
{
[TestFixture]
public class YieldStatementTests
{
[Test]
public void YieldReturnStatementTest()
{
YieldStatement yieldStmt = ParseUtilCSharp.ParseStatement<YieldStatement>("yield return \"Foo\";");
PrimitiveExpression expr = (PrimitiveExpression)yieldStmt.Expression;
Assert.AreEqual("Foo", expr.Value);
}
[Test]
public void YieldBreakStatementTest()
{
YieldStatement yieldStmt = ParseUtilCSharp.ParseStatement<YieldStatement>("yield break;");
Assert.IsNull(yieldStmt.Expression);
}
[Test]
public void YieldAsVariableTest()
{
ExpressionStatement se = ParseUtilCSharp.ParseStatement<ExpressionStatement>("yield = 3;");
AssignmentExpression ae = se.Expression as AssignmentExpression;
Assert.AreEqual(AssignmentOperatorType.Assign, ae.AssignmentOperatorType);
Assert.IsTrue(ae.Left is IdentifierExpression);
Assert.AreEqual("yield", ((IdentifierExpression)ae.Left).Identifier);
Assert.IsTrue(ae.Right is PrimitiveExpression);
}
}
}

20
ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

@ -62,6 +62,25 @@ @@ -62,6 +62,25 @@
<Compile Include="CSharp\Parser\GeneralScope\NamespaceDeclarationTests.cs" />
<Compile Include="CSharp\Parser\GeneralScope\TypeDeclarationTests.cs" />
<Compile Include="CSharp\Parser\GeneralScope\UsingDeclarationTests.cs" />
<Compile Include="CSharp\Parser\Statements\BlockStatementTests.cs" />
<Compile Include="CSharp\Parser\Statements\CheckedStatementTests.cs" />
<Compile Include="CSharp\Parser\Statements\EmptyStatementTests.cs" />
<Compile Include="CSharp\Parser\Statements\ExpressionStatementTests.cs" />
<Compile Include="CSharp\Parser\Statements\FixedStatementTests.cs" />
<Compile Include="CSharp\Parser\Statements\ForStatementTests.cs" />
<Compile Include="CSharp\Parser\Statements\GotoStatementTests.cs" />
<Compile Include="CSharp\Parser\Statements\IfElseStatementTests.cs" />
<Compile Include="CSharp\Parser\Statements\LabelStatementTests.cs" />
<Compile Include="CSharp\Parser\Statements\LockStatementTests.cs" />
<Compile Include="CSharp\Parser\Statements\ReturnStatementTests.cs" />
<Compile Include="CSharp\Parser\Statements\SwitchStatementTests.cs" />
<Compile Include="CSharp\Parser\Statements\ThrowStatementTests.cs" />
<Compile Include="CSharp\Parser\Statements\TryCatchStatementTests.cs" />
<Compile Include="CSharp\Parser\Statements\UnsafeStatementTests.cs" />
<Compile Include="CSharp\Parser\Statements\UsingStatementTests.cs" />
<Compile Include="CSharp\Parser\Statements\VariableDeclarationStatementTests.cs" />
<Compile Include="CSharp\Parser\Statements\WhileStatementTests.cs" />
<Compile Include="CSharp\Parser\Statements\YieldStatementTests.cs" />
<Compile Include="CSharp\Parser\TypeSystemConvertVisitorTests.cs" />
<Compile Include="CSharp\Parser\Expression\AliasReferenceExpressionTests.cs" />
<Compile Include="CSharp\Parser\Expression\AnonymousMethodTests.cs" />
@ -125,6 +144,7 @@ @@ -125,6 +144,7 @@
<ItemGroup>
<Folder Include="CSharp\Parser\Expression" />
<Folder Include="CSharp\Parser\GeneralScope" />
<Folder Include="CSharp\Parser\Statements" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

6
ICSharpCode.NRefactory/CSharp/Dom/Statements/CheckedStatement.cs

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
//
//
// CheckedStatement.cs
//
// Author:
@ -31,8 +31,8 @@ namespace ICSharpCode.NRefactory.CSharp @@ -31,8 +31,8 @@ namespace ICSharpCode.NRefactory.CSharp
{
public class CheckedStatement : AbstractNode
{
public INode EmbeddedStatement {
get { return (INode)GetChildByRole (Roles.EmbeddedStatement); }
public BlockStatement Block {
get { return (BlockStatement)GetChildByRole (Roles.Body); }
}
public override S AcceptVisitor<T, S> (IDomVisitor<T, S> visitor, T data)

7
ICSharpCode.NRefactory/CSharp/Dom/Statements/GotoStatement.cs

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
//
//
// GotoStatement.cs
//
// Author:
@ -40,7 +40,10 @@ namespace ICSharpCode.NRefactory.CSharp @@ -40,7 +40,10 @@ namespace ICSharpCode.NRefactory.CSharp
}
public string Label {
get { return ((Identifier)LabelExpression).Name; }
get {
Identifier i = this.LabelExpression as Identifier;
return i != null ? i.Name : null;
}
}
public INode LabelExpression {

6
ICSharpCode.NRefactory/CSharp/Dom/Statements/UncheckedStatement.cs

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
//
//
// UncheckedStatement.cs
//
// Author:
@ -31,8 +31,8 @@ namespace ICSharpCode.NRefactory.CSharp @@ -31,8 +31,8 @@ namespace ICSharpCode.NRefactory.CSharp
{
public class UncheckedStatement : AbstractNode
{
public INode EmbeddedStatement {
get { return (INode)GetChildByRole (Roles.EmbeddedStatement); }
public BlockStatement Block {
get { return (BlockStatement)GetChildByRole (Roles.Body); }
}
public override S AcceptVisitor<T, S> (IDomVisitor<T, S> visitor, T data)

3
ICSharpCode.NRefactory/CSharp/Dom/Statements/YieldStatement.cs

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
//
//
// YieldStatement.cs
//
// Author:
@ -34,6 +34,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -34,6 +34,7 @@ namespace ICSharpCode.NRefactory.CSharp
public const int YieldKeywordRole = 100;
public const int ReturnKeywordRole = 101;
public const int BreakKeywordRole = 102;
public INode Expression {
get { return GetChildByRole (Roles.Expression); }
}

4
ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs

@ -1133,7 +1133,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1133,7 +1133,7 @@ namespace ICSharpCode.NRefactory.CSharp
{
var result = new UncheckedStatement ();
result.AddChild (new CSharpTokenNode (Convert (uncheckedStatement.loc), "unchecked".Length), UncheckedStatement.Roles.Keyword);
result.AddChild ((INode)uncheckedStatement.Block.Accept (this), UncheckedStatement.Roles.EmbeddedStatement);
result.AddChild ((INode)uncheckedStatement.Block.Accept (this), UncheckedStatement.Roles.Body);
return result;
}
@ -1142,7 +1142,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1142,7 +1142,7 @@ namespace ICSharpCode.NRefactory.CSharp
{
var result = new CheckedStatement ();
result.AddChild (new CSharpTokenNode (Convert (checkedStatement.loc), "checked".Length), CheckedStatement.Roles.Keyword);
result.AddChild ((INode)checkedStatement.Block.Accept (this), CheckedStatement.Roles.EmbeddedStatement);
result.AddChild ((INode)checkedStatement.Block.Accept (this), CheckedStatement.Roles.Body);
return result;
}

Loading…
Cancel
Save