Skip to content
Browse files

Squashed 'NRefactory/' changes from 2200240..a92606c

a92606c Some improvements to output visitor.
136fd88 Fix some parser issues.
41b4385 Port NRefactory to .NET 3.5.
3b6fe6f Adjust resolver to AST changes and disable failing unit tests.
93e9d1e Use implicit conversion operator to convert from Pattern to AST nodes.
b37ea77 Re-enabled the unit tests that were commented out.
defd426 AST bugfixes.
60d4fe9 Add pattern matching for query expressions. Pattern matching should now work for the complete C# AST.
b51f5a6 Add pattern matching for MemberDeclarations.

git-subtree-dir: NRefactory
git-subtree-split: a92606cb97ef6739d59b30a278d017cf64677f88
  • Loading branch information...
1 parent 7ac091e commit 39fcd7d72f73d386455d00d63446601c99eafb83 @dgrunwald dgrunwald committed
Showing with 2,055 additions and 1,038 deletions.
  1. +15 −0 ICSharpCode.NRefactory.Tests/CSharp/AstStructureTests.cs
  2. +27 −11 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/AliasReferenceExpressionTests.cs
  3. +1 −0 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/AnonymousMethodTests.cs
  4. +36 −16 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/ArrayObjectCreateExpressionTests.cs
  5. +88 −63 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/CastExpressionTests.cs
  6. +43 −17 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/DefaultValueExpressionTests.cs
  7. +18 −10 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/IdentifierExpressionTests.cs
  8. +97 −80 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/InvocationExpressionTests.cs
  9. +8 −7 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/IsExpressionTests.cs
  10. +68 −49 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/LambdaExpressionTests.cs
  11. +55 −42 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/MemberReferenceExpressionTests.cs
  12. +15 −3 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/PointerReferenceExpressionTests.cs
  13. +174 −58 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/QueryExpressionTests.cs
  14. +2 −2 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/SizeOfExpressionTests.cs
  15. +6 −3 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/StackAllocExpressionTests.cs
  16. +62 −35 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/TypeOfExpressionTests.cs
  17. +3 −3 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/UnaryOperatorExpressionTests.cs
  18. +27 −41 ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/DelegateDeclarationTests.cs
  19. +117 −81 ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/TypeDeclarationTests.cs
  20. +1 −1 ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/UsingDeclarationTests.cs
  21. +40 −1 ICSharpCode.NRefactory.Tests/CSharp/Parser/ParseUtil.cs
  22. +13 −2 ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/FixedStatementTests.cs
  23. +14 −6 ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/ForStatementTests.cs
  24. +41 −15 ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/TryCatchStatementTests.cs
  25. +1 −2 ICSharpCode.NRefactory.Tests/CSharp/Parser/Statements/YieldStatementTests.cs
  26. +59 −54 ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeMembers/EventDeclarationTests.cs
  27. +51 −11 ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeMembers/FieldDeclarationTests.cs
  28. +24 −13 ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeMembers/IndexerDeclarationTests.cs
  29. +153 −113 ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeMembers/MethodDeclarationTests.cs
  30. +8 −8 ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeMembers/OperatorDeclarationTests.cs
  31. +4 −6 ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeMembers/PropertyDeclarationTests.cs
  32. +1 −1 ICSharpCode.NRefactory.Tests/CSharp/Resolver/InvocationTests.cs
  33. +5 −5 ICSharpCode.NRefactory.Tests/CSharp/Resolver/NameLookupTests.cs
  34. +1 −1 ICSharpCode.NRefactory.Tests/CSharp/Resolver/UnsafeCodeTests.cs
  35. +1 −0 ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj
  36. +1 −0 ICSharpCode.NRefactory.VB.Tests/ICSharpCode.NRefactory.VB.Tests.csproj
  37. +1 −0 ICSharpCode.NRefactory.VB/ICSharpCode.NRefactory.VB.csproj
  38. +3 −3 ICSharpCode.NRefactory/CSharp/Ast/AstLocation.cs
  39. +5 −0 ICSharpCode.NRefactory/CSharp/Ast/AstNode.cs
  40. +6 −12 ICSharpCode.NRefactory/CSharp/Ast/AstNodeCollection.cs
  41. +1 −1 ICSharpCode.NRefactory/CSharp/Ast/AstType.cs
  42. +6 −0 ICSharpCode.NRefactory/CSharp/Ast/CSharpModifierToken.cs
  43. +1 −1 ICSharpCode.NRefactory/CSharp/Ast/CSharpTokenNode.cs
  44. +1 −1 ICSharpCode.NRefactory/CSharp/Ast/Expressions/ArrayCreateExpression.cs
  45. +54 −7 ICSharpCode.NRefactory/CSharp/Ast/Expressions/QueryExpression.cs
  46. +9 −0 ICSharpCode.NRefactory/CSharp/Ast/GeneralScope/AttributeSection.cs
  47. +3 −2 ICSharpCode.NRefactory/CSharp/Ast/GeneralScope/Constraint.cs
  48. +14 −5 ICSharpCode.NRefactory/CSharp/Ast/GeneralScope/DelegateDeclaration.cs
  49. +9 −0 ICSharpCode.NRefactory/CSharp/Ast/GeneralScope/TypeDeclaration.cs
  50. +6 −1 ICSharpCode.NRefactory/CSharp/Ast/GeneralScope/TypeParameterDeclaration.cs
  51. +6 −1 ICSharpCode.NRefactory/CSharp/Ast/Identifier.cs
  52. +1 −1 ICSharpCode.NRefactory/CSharp/Ast/MemberType.cs
  53. +10 −15 ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Pattern.cs
  54. +52 −16 ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Placeholder.cs
  55. +1 −1 ICSharpCode.NRefactory/CSharp/Ast/SimpleType.cs
  56. +1 −1 ICSharpCode.NRefactory/CSharp/Ast/Statements/BlockStatement.cs
  57. +1 −1 ICSharpCode.NRefactory/CSharp/Ast/Statements/FixedStatement.cs
  58. +2 −1 ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/Accessor.cs
  59. +2 −2 ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/AttributedNode.cs
  60. +13 −1 ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/ConstructorDeclaration.cs
  61. +7 −1 ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/DestructorDeclaration.cs
  62. +9 −2 ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/EnumMemberDeclaration.cs
  63. +24 −1 ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/EventDeclaration.cs
  64. +19 −3 ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/FieldDeclaration.cs
  65. +31 −3 ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/IndexerDeclaration.cs
  66. +6 −0 ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/MemberDeclaration.cs
  67. +11 −3 ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/MethodDeclaration.cs
  68. +24 −3 ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/OperatorDeclaration.cs
  69. +13 −1 ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/ParameterDeclaration.cs
  70. +9 −2 ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/PropertyDeclaration.cs
  71. +57 −20 ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs
  72. +26 −20 ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs
  73. +42 −41 ICSharpCode.NRefactory/CSharp/Parser/TypeSystemConvertVisitor.cs
  74. +19 −9 ICSharpCode.NRefactory/CSharp/Resolver/CSharpResolver.cs
  75. +5 −5 ICSharpCode.NRefactory/CSharp/Resolver/MemberLookup.cs
  76. +1 −1 ICSharpCode.NRefactory/CSharp/Resolver/MemberTypeOrNamespaceReference.cs
  77. +1 −1 ICSharpCode.NRefactory/CSharp/Resolver/OverloadResolution.cs
  78. +31 −18 ICSharpCode.NRefactory/CSharp/Resolver/ResolveVisitor.cs
  79. +1 −1 ICSharpCode.NRefactory/CSharp/Resolver/SimpleTypeOrNamespaceReference.cs
  80. +1 −1 ICSharpCode.NRefactory/CSharp/Resolver/TypeInference.cs
  81. +12 −5 ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj
  82. +1 −1 ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs
  83. +5 −3 ICSharpCode.NRefactory/TypeSystem/ExtensionMethods.cs
  84. +4 −0 ICSharpCode.NRefactory/TypeSystem/IAttribute.cs
  85. +4 −0 ICSharpCode.NRefactory/TypeSystem/IConstantValue.cs
  86. +4 −0 ICSharpCode.NRefactory/TypeSystem/IEntity.cs
  87. +4 −0 ICSharpCode.NRefactory/TypeSystem/IExplicitInterfaceImplementation.cs
  88. +4 −0 ICSharpCode.NRefactory/TypeSystem/IField.cs
  89. +4 −0 ICSharpCode.NRefactory/TypeSystem/IFreezable.cs
  90. +4 −0 ICSharpCode.NRefactory/TypeSystem/IInterningProvider.cs
  91. +4 −0 ICSharpCode.NRefactory/TypeSystem/IMember.cs
  92. +4 −7 ICSharpCode.NRefactory/TypeSystem/IMethod.cs
  93. +4 −0 ICSharpCode.NRefactory/TypeSystem/INamedElement.cs
  94. +4 −0 ICSharpCode.NRefactory/TypeSystem/IParameter.cs
  95. +4 −0 ICSharpCode.NRefactory/TypeSystem/IParameterizedMember.cs
  96. +4 −0 ICSharpCode.NRefactory/TypeSystem/IProjectContent.cs
  97. +4 −0 ICSharpCode.NRefactory/TypeSystem/ISupportsInterning.cs
  98. +4 −0 ICSharpCode.NRefactory/TypeSystem/IType.cs
  99. +4 −0 ICSharpCode.NRefactory/TypeSystem/ITypeDefinition.cs
  100. +4 −0 ICSharpCode.NRefactory/TypeSystem/ITypeParameter.cs
  101. +4 −0 ICSharpCode.NRefactory/TypeSystem/ITypeReference.cs
  102. +4 −0 ICSharpCode.NRefactory/TypeSystem/ITypeResolveContext.cs
  103. +4 −0 ICSharpCode.NRefactory/TypeSystem/IVariable.cs
  104. +1 −3 ICSharpCode.NRefactory/TypeSystem/Implementation/AbstractType.cs
  105. +0 −9 ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultParameter.cs
  106. +6 −17 ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultTypeDefinition.cs
  107. +18 −6 ICSharpCode.NRefactory/TypeSystem/Implementation/SimpleInterningProvider.cs
  108. +58 −0 ICSharpCode.NRefactory/Utils/DotNet35Compat.cs
  109. +4 −4 NRefactory.sln
  110. +23 −2 README
  111. +1 −0 VBDomGenerator/VBDomGenerator.csproj
  112. +21 −12 doc/TODO
View
15 ICSharpCode.NRefactory.Tests/CSharp/AstStructureTests.cs
@@ -27,5 +27,20 @@ public void RolesAreStaticReadOnly()
}
}
}
+
+ [Test]
+ public void AstNodesDoNotDeriveFromEachOther()
+ {
+ // Ast nodes should derive only from abstract classes; not from concrete types.
+ // For example, we want to avoid that an AST consumer doing "if (node is PropertyDeclaration)"
+ // unknowingly also handles IndexerDeclarations.
+ foreach (Type type in typeof(AstNode).Assembly.GetExportedTypes()) {
+ if (type == typeof(CSharpModifierToken)) // CSharpModifierToken is the exception (though I'm not too happy about that)
+ continue;
+ if (type.IsSubclassOf(typeof(AstNode))) {
+ Assert.IsTrue(type.BaseType.IsAbstract, type.FullName);
+ }
+ }
+ }
}
}
View
38 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/AliasReferenceExpressionTests.cs
@@ -8,27 +8,43 @@
namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
{
- [TestFixture]
+ [TestFixture, Ignore("Aliases not yet implemented")]
public class AliasReferenceExpressionTests
{
- [Test, Ignore]
+ [Test]
public void GlobalReferenceExpressionTest()
{
CSharpParser parser = new CSharpParser();
- parser.ParseTypeReference(new StringReader("global::System"));
- //Assert.IsTrue(tre.TypeReference.IsGlobal);
- //Assert.AreEqual("System", tre.TypeReference.Type);
- throw new NotImplementedException();
+ AstType type = parser.ParseTypeReference(new StringReader("global::System"));
+ Assert.IsNotNull(
+ new MemberType {
+ Target = new SimpleType("global"),
+ IsDoubleColon = true,
+ MemberName = "System"
+ }.Match(type)
+ );
}
- [Test, Ignore]
+ [Test]
public void GlobalTypeDeclaration()
{
VariableDeclarationStatement lvd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("global::System.String a;");
- //TypeReference typeRef = lvd.GetTypeForVariable(0);
- //Assert.IsTrue(typeRef.IsGlobal);
- //Assert.AreEqual("System.String", typeRef.Type);
- throw new NotImplementedException();
+ Assert.IsNotNull(
+ new VariableDeclarationStatement {
+ Type = new MemberType {
+ Target = new MemberType {
+ Target = new SimpleType("global"),
+ IsDoubleColon = true,
+ MemberName = "System"
+ },
+ IsDoubleColon = false,
+ MemberName = "String",
+ },
+ Variables = {
+ new VariableInitializer("a")
+ }
+ }.Match(lvd)
+ );
}
// TODO: add tests for aliases other than 'global'
View
1 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/AnonymousMethodTests.cs
@@ -47,6 +47,7 @@ public void EmptyAnonymousMethod()
public void SimpleAnonymousMethod()
{
AnonymousMethodExpression ame = Parse("delegate(int a, int b) { return a + b; }");
+ Assert.IsTrue(ame.HasParameterList);
Assert.AreEqual(2, ame.Parameters.Count());
Assert.AreEqual(1, ame.Body.Statements.Count());
Assert.IsTrue(ame.Body.Children.First() is ReturnStatement);
View
52 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/ArrayObjectCreateExpressionTests.cs
@@ -6,31 +6,51 @@
namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
{
- [TestFixture, Ignore("Needs to be ported to new NRefactory")]
+ [TestFixture]
public class ArrayObjectCreateExpressionTests
{
[Test]
public void ArrayCreateExpressionTest1()
{
- /*
- ArrayCreateExpression ace = ParseUtilCSharp.ParseExpression<ArrayCreateExpression>("new int[5]");
- Assert.AreEqual("System.Int32", ace.CreateType.Type);
- Assert.IsTrue(ace.CreateType.IsKeyword);
- Assert.AreEqual(1, ace.Arguments.Count);
- Assert.AreEqual(new int[] {0}, ace.CreateType.RankSpecifier);
- */
- throw new NotImplementedException();
+ ParseUtilCSharp.AssertExpression(
+ "new int[5]",
+ new ArrayCreateExpression {
+ Type = new PrimitiveType("int"),
+ Arguments = { new PrimitiveExpression(5) }
+ });
}
- [Test]
+ [Test, Ignore("AdditionalArraySpecifiers not yet implemented")]
+ public void MultidimensionalNestedArray()
+ {
+ ParseUtilCSharp.AssertExpression(
+ "new int[5,2][,,][]",
+ new ArrayCreateExpression {
+ Type = new PrimitiveType("int"),
+ Arguments = { new PrimitiveExpression(5), new PrimitiveExpression(2) },
+ AdditionalArraySpecifiers = {
+ new ArraySpecifier(3),
+ new ArraySpecifier(1)
+ }
+ });
+ }
+
+ [Test, Ignore("Array initializers not yet implemented")]
public void ImplicitlyTypedArrayCreateExpression()
{
- /*
- ArrayCreateExpression ace = ParseUtilCSharp.ParseExpression<ArrayCreateExpression>("new[] { 1, 10, 100, 1000 }");
- Assert.AreEqual("", ace.CreateType.Type);
- Assert.AreEqual(0, ace.Arguments.Count);
- Assert.AreEqual(4, ace.ArrayInitializer.CreateExpressions.Count);*/
- throw new NotImplementedException();
+ ParseUtilCSharp.AssertExpression(
+ "new[] { 1, 10, 100, 1000 }",
+ new ArrayCreateExpression {
+ Initializer = new ArrayInitializerExpression {
+ Elements = {
+ new PrimitiveExpression(1),
+ new PrimitiveExpression(10),
+ new PrimitiveExpression(100),
+ new PrimitiveExpression(1000)
+ }
+ }
+ });
+
}
}
}
View
151 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/CastExpressionTests.cs
@@ -9,132 +9,157 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
[TestFixture, Ignore("Port unit tests to new DOM")]
public class CastExpressionTests
{
- /*
[Test]
public void SimpleCastExpression()
{
- CastExpression ce = ParseUtilCSharp.ParseExpression<CastExpression>("(MyObject)o");
- Assert.AreEqual("MyObject", ce.CastTo.Type);
- Assert.IsTrue(ce.Expression is IdentifierExpression);
- Assert.AreEqual(CastType.Cast, ce.CastType);
+ ParseUtilCSharp.AssertExpression(
+ "(MyObject)o",
+ new CastExpression {
+ Type = new SimpleType("MyObject"),
+ Expression = new IdentifierExpression("o")
+ });
}
[Test]
public void ArrayCastExpression()
{
- CastExpression ce = ParseUtilCSharp.ParseExpression<CastExpression>("(MyType[])o");
- Assert.AreEqual("MyType", ce.CastTo.Type);
- Assert.AreEqual(new int[] { 0 }, ce.CastTo.RankSpecifier);
- Assert.IsTrue(ce.Expression is IdentifierExpression);
- Assert.AreEqual(CastType.Cast, ce.CastType);
+ ParseUtilCSharp.AssertExpression(
+ "(MyType[])o",
+ new CastExpression {
+ Type = new SimpleType("MyType").MakeArrayType(1),
+ Expression = new IdentifierExpression("o")
+ });
}
[Test]
public void NullablePrimitiveCastExpression()
{
- CastExpression ce = ParseUtilCSharp.ParseExpression<CastExpression>("(int?)o");
- Assert.AreEqual("System.Nullable", ce.CastTo.Type);
- Assert.AreEqual("System.Int32", ce.CastTo.GenericTypes[0].Type);
- Assert.IsTrue(ce.Expression is IdentifierExpression);
- Assert.AreEqual(CastType.Cast, ce.CastType);
+ ParseUtilCSharp.AssertExpression(
+ "(int?)o",
+ new CastExpression {
+ Type = new ComposedType { BaseType = new PrimitiveType("int"), HasNullableSpecifier = true },
+ Expression = new IdentifierExpression("o")
+ });
}
[Test]
public void NullableCastExpression()
{
- CastExpression ce = ParseUtilCSharp.ParseExpression<CastExpression>("(MyType?)o");
- Assert.AreEqual("System.Nullable", ce.CastTo.Type);
- Assert.AreEqual("MyType", ce.CastTo.GenericTypes[0].Type);
- Assert.IsTrue(ce.Expression is IdentifierExpression);
- Assert.AreEqual(CastType.Cast, ce.CastType);
+ ParseUtilCSharp.AssertExpression(
+ "(MyType?)o",
+ new CastExpression {
+ Type = new ComposedType { BaseType = new SimpleType("MyType"), HasNullableSpecifier = true },
+ Expression = new IdentifierExpression("o")
+ });
}
[Test]
public void NullableTryCastExpression()
{
- CastExpression ce = ParseUtilCSharp.ParseExpression<CastExpression>("o as int?");
- Assert.AreEqual("System.Nullable", ce.CastTo.Type);
- Assert.IsTrue(ce.CastTo.IsKeyword);
- Assert.AreEqual("System.Int32", ce.CastTo.GenericTypes[0].Type);
- Assert.IsTrue(ce.Expression is IdentifierExpression);
- Assert.AreEqual(CastType.TryCast, ce.CastType);
+ ParseUtilCSharp.AssertExpression(
+ "o as int?",
+ new AsExpression {
+ Type = new ComposedType { BaseType = new PrimitiveType("int"), HasNullableSpecifier = true },
+ Expression = new IdentifierExpression("o")
+ });
}
[Test]
public void GenericCastExpression()
{
- CastExpression ce = ParseUtilCSharp.ParseExpression<CastExpression>("(List<string>)o");
- Assert.AreEqual("List", ce.CastTo.Type);
- Assert.AreEqual("System.String", ce.CastTo.GenericTypes[0].Type);
- Assert.IsTrue(ce.Expression is IdentifierExpression);
- Assert.AreEqual(CastType.Cast, ce.CastType);
+ ParseUtilCSharp.AssertExpression(
+ "(List<string>)o",
+ new CastExpression {
+ Type = new SimpleType("List") { TypeArguments = { new PrimitiveType("string") } },
+ Expression = new IdentifierExpression("o")
+ });
}
[Test]
public void GenericArrayCastExpression()
{
- CastExpression ce = ParseUtilCSharp.ParseExpression<CastExpression>("(List<string>[])o");
- Assert.AreEqual("List", ce.CastTo.Type);
- Assert.AreEqual("System.String", ce.CastTo.GenericTypes[0].Type);
- Assert.AreEqual(new int[] { 0 }, ce.CastTo.RankSpecifier);
- Assert.IsTrue(ce.Expression is IdentifierExpression);
- Assert.AreEqual(CastType.Cast, ce.CastType);
+ ParseUtilCSharp.AssertExpression(
+ "(List<string>[])o",
+ new CastExpression {
+ Type = new ComposedType {
+ BaseType = new SimpleType("List") { TypeArguments = { new PrimitiveType("string") } },
+ ArraySpecifiers = { new ArraySpecifier(1) }
+ },
+ Expression = new IdentifierExpression("o")
+ });
}
[Test]
public void GenericArrayAsCastExpression()
{
- CastExpression ce = ParseUtilCSharp.ParseExpression<CastExpression>("o as List<string>[]");
- Assert.AreEqual("List", ce.CastTo.Type);
- Assert.AreEqual("System.String", ce.CastTo.GenericTypes[0].Type);
- Assert.AreEqual(new int[] { 0 }, ce.CastTo.RankSpecifier);
- Assert.IsTrue(ce.Expression is IdentifierExpression);
- Assert.AreEqual(CastType.TryCast, ce.CastType);
+ ParseUtilCSharp.AssertExpression(
+ "o as List<string>[]",
+ new AsExpression {
+ Type = new ComposedType {
+ BaseType = new SimpleType("List") { TypeArguments = { new PrimitiveType("string") } },
+ ArraySpecifiers = { new ArraySpecifier(1) }
+ },
+ Expression = new IdentifierExpression("o")
+ });
}
[Test]
public void CastMemberReferenceOnParenthesizedExpression()
{
- // yes, we really wanted to evaluate .Member on expr and THEN cast the result to MyType
- CastExpression ce = ParseUtilCSharp.ParseExpression<CastExpression>("(MyType)(expr).Member");
- Assert.AreEqual("MyType", ce.CastTo.Type);
- Assert.IsTrue(ce.Expression is MemberReferenceExpression);
- Assert.AreEqual(CastType.Cast, ce.CastType);
+ // yes, we really want to evaluate .Member on expr and THEN cast the result to MyType
+ ParseUtilCSharp.AssertExpression(
+ "(MyType)(expr).Member",
+ new CastExpression {
+ Type = new SimpleType("MyType"),
+ Expression = new ParenthesizedExpression { Expression = new IdentifierExpression("expr") }.Member("Member")
+ });
}
[Test]
public void TryCastParenthesizedExpression()
{
- CastExpression ce = ParseUtilCSharp.ParseExpression<CastExpression>("(o) as string");
- Assert.AreEqual("System.String", ce.CastTo.ToString());
- Assert.IsTrue(ce.Expression is ParenthesizedExpression);
- Assert.AreEqual(CastType.TryCast, ce.CastType);
+ ParseUtilCSharp.AssertExpression(
+ "(o) as string",
+ new AsExpression {
+ Expression = new ParenthesizedExpression { Expression = new IdentifierExpression("o") },
+ Type = new PrimitiveType("string")
+ });
}
[Test]
public void CastNegation()
{
- CastExpression ce = ParseUtilCSharp.ParseExpression<CastExpression>("(uint)-negativeValue");
- Assert.AreEqual("System.UInt32", ce.CastTo.ToString());
- Assert.IsTrue(ce.Expression is UnaryOperatorExpression);
- Assert.AreEqual(CastType.Cast, ce.CastType);
+ ParseUtilCSharp.AssertExpression(
+ "(uint)-negativeValue",
+ new CastExpression {
+ Type = new PrimitiveType("uint"),
+ Expression = new UnaryOperatorExpression(
+ UnaryOperatorType.Minus,
+ new IdentifierExpression("negativeValue")
+ )});
}
- */
[Test]
public void SubtractionIsNotCast()
{
- BinaryOperatorExpression boe = ParseUtilCSharp.ParseExpression<BinaryOperatorExpression>("(BigInt)-negativeValue");
- Assert.IsTrue(boe.Left is ParenthesizedExpression);
- Assert.IsTrue(boe.Right is IdentifierExpression);
+ ParseUtilCSharp.AssertExpression(
+ "(BigInt)-negativeValue",
+ new BinaryOperatorExpression {
+ Left = new ParenthesizedExpression { Expression = new IdentifierExpression("BigInt") },
+ Operator = BinaryOperatorType.Subtract,
+ Right = new IdentifierExpression("negativeValue")
+ });
}
[Test]
public void IntMaxValueToBigInt()
{
- CastExpression ce = ParseUtilCSharp.ParseExpression<CastExpression>("(BigInt)int.MaxValue");
- Assert.AreEqual("BigInt", ce.Type.ToString());
- Assert.IsTrue(ce.Expression is MemberReferenceExpression);
+ ParseUtilCSharp.AssertExpression(
+ "(BigInt)int.MaxValue",
+ new CastExpression {
+ Type = new SimpleType("BigInt"),
+ Expression = new PrimitiveExpression("int").Member("MaxValue")
+ });
}
}
}
View
60 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/DefaultValueExpressionTests.cs
@@ -6,48 +6,74 @@
namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
{
- [TestFixture, Ignore("tests need to be ported")]
+ [TestFixture, Ignore("Aliases not yet implemented")]
public class DefaultValueExpressionTests
{
[Test]
public void SimpleDefaultValue()
{
DefaultValueExpression toe = ParseUtilCSharp.ParseExpression<DefaultValueExpression>("default(T)");
- Assert.AreEqual("T", toe.Type);
+ Assert.AreEqual("T", ((SimpleType)toe.Type).Identifier);
}
- /*
[Test]
public void FullQualifiedDefaultValue()
{
- DefaultValueExpression toe = ParseUtilCSharp.ParseExpression<DefaultValueExpression>("default(global::MyNamespace.N1.MyType)");
- Assert.IsTrue(toe.TypeReference.IsGlobal);
- Assert.AreEqual("MyNamespace.N1.MyType", toe.TypeReference.Type);
+ ParseUtilCSharp.AssertExpression(
+ "default(global::MyNamespace.N1.MyType)",
+ new DefaultValueExpression {
+ Type = new MemberType {
+ Target = new MemberType {
+ Target = new MemberType {
+ Target = new SimpleType("global"),
+ IsDoubleColon = true,
+ MemberName = "MyNamespace"
+ },
+ MemberName = "N1"
+ },
+ MemberName = "MyType"
+ }
+ });
}
[Test]
public void GenericDefaultValue()
{
- DefaultValueExpression toe = ParseUtilCSharp.ParseExpression<DefaultValueExpression>("default(MyNamespace.N1.MyType<string>)");
- Assert.AreEqual("MyNamespace.N1.MyType", toe.TypeReference.Type);
- Assert.AreEqual("System.String", toe.TypeReference.GenericTypes[0].Type);
+ ParseUtilCSharp.AssertExpression(
+ "default(MyNamespace.N1.MyType<string>)",
+ new DefaultValueExpression {
+ Type = new MemberType {
+ Target = new MemberType {
+ Target = new SimpleType("MyNamespace"),
+ MemberName = "N1"
+ },
+ MemberName = "MyType",
+ TypeArguments = { new PrimitiveType("string") }
+ }
+ });
}
[Test]
public void DefaultValueAsIntializer()
{
- // This test is failing because we need a resolver for the "default:" / "default(" conflict.
- LocalVariableDeclaration lvd = ParseUtilCSharp.ParseStatement<LocalVariableDeclaration>("T a = default(T);");
- DefaultValueExpression dve = (DefaultValueExpression)lvd.Variables[0].Initializer;
- Assert.AreEqual("T", dve.TypeReference.Type);
+ // This test was problematic (in old NRefactory) because we need a resolver for the "default:" / "default(" conflict.
+ ParseUtilCSharp.AssertStatement(
+ "T a = default(T);",
+ new VariableDeclarationStatement {
+ Type = new SimpleType("T"),
+ Variables = {
+ new VariableInitializer("a", new DefaultValueExpression { Type = new SimpleType("T") })
+ }});
}
[Test]
public void DefaultValueInReturnStatement()
{
- ReturnStatement rs = ParseUtilCSharp.ParseStatement<ReturnStatement>("return default(T);");
- DefaultValueExpression dve = (DefaultValueExpression)rs.Expression;
- Assert.AreEqual("T", dve.TypeReference.Type);
- }*/
+ ParseUtilCSharp.AssertStatement(
+ "return default(T);",
+ new ReturnStatement {
+ Expression = new DefaultValueExpression { Type = new SimpleType("T") }
+ });
+ }
}
}
View
28 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/IdentifierExpressionTests.cs
@@ -39,7 +39,7 @@ public void TestIdentifierContainingEscapeSequence()
CheckIdentifier(@"l\U00000065xer", "lexer");
}
- [Test]
+ [Test, Ignore("The @ should not be part of IdentifierExpression.Identifier")]
public void TestKeyWordAsIdentifier()
{
CheckIdentifier("@int", "int");
@@ -51,28 +51,36 @@ public void TestKeywordWithEscapeSequenceIsIdentifier()
CheckIdentifier(@"i\u006et", "int");
}
- [Test]
+ [Test, Ignore("The @ should not be part of IdentifierExpression.Identifier")]
public void TestKeyWordAsIdentifierStartingWithUnderscore()
{
CheckIdentifier("@_int", "_int");
}
- [Test, Ignore]
+ [Test]
public void GenericMethodReference()
{
IdentifierExpression ident = ParseUtilCSharp.ParseExpression<IdentifierExpression>("M<int>");
- Assert.AreEqual("M", ident.Identifier);
- //Assert.AreEqual(1, ident.TypeArguments.Count);
- throw new NotImplementedException();
+ Assert.IsNotNull(
+ new IdentifierExpression {
+ Identifier = "M" ,
+ TypeArguments = {
+ new PrimitiveType("int")
+ }
+ }.Match(ident));
}
- [Test, Ignore]
+ [Test]
public void GenericMethodReference2()
{
IdentifierExpression ident = ParseUtilCSharp.ParseExpression<IdentifierExpression>("TargetMethod<string>");
- Assert.AreEqual("TargetMethod", ident.Identifier);
- //Assert.AreEqual(1, ident.TypeArguments.Count);
- throw new NotImplementedException();
+ Assert.IsNotNull(
+ new IdentifierExpression {
+ Identifier = "TargetMethod" ,
+ TypeArguments = {
+ new PrimitiveType("string")
+ }
+ }.Match(ident));
}
}
}
View
177 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/InvocationExpressionTests.cs
@@ -7,7 +7,7 @@
namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
{
- [TestFixture, Ignore("Port unit tests to new DOM")]
+ [TestFixture]
public class InvocationExpressionTests
{
[Test]
@@ -19,55 +19,71 @@ public void SimpleInvocationExpressionTest()
Assert.AreEqual("myMethod", ((IdentifierExpression)ie.Target).Identifier);
}
- /* TODO port unit tests to new DOM
[Test]
public void GenericInvocationExpressionTest()
{
- var expr = ParseUtilCSharp.ParseExpression<InvocationExpression>("myMethod<char>('a')");
- Assert.AreEqual(1, expr.Arguments.Count());
- Assert.IsTrue(expr.TargetObject is IdentifierExpression);
- IdentifierExpression ident = (IdentifierExpression)expr.TargetObject;
- Assert.AreEqual("myMethod", ident.Identifier);
- Assert.AreEqual(1, ident.TypeArguments.Count);
- Assert.AreEqual("System.Char", ident.TypeArguments[0].Type);
+ ParseUtilCSharp.AssertExpression(
+ "myMethod<char>('a')",
+ new InvocationExpression {
+ Target = new IdentifierExpression {
+ Identifier = "myMethod",
+ TypeArguments = { new PrimitiveType("char") }
+ },
+ Arguments = { new PrimitiveExpression('a') }
+ }
+ );
}
[Test]
public void GenericInvocation2ExpressionTest()
{
- var expr = ParseUtilCSharp.ParseExpression<InvocationExpression>("myMethod<T,bool>()");
- Assert.AreEqual(0, expr.Arguments.Count);
- Assert.IsTrue(expr.TargetObject is IdentifierExpression);
- IdentifierExpression ident = (IdentifierExpression)expr.TargetObject;
- Assert.AreEqual("myMethod", ident.Identifier);
- Assert.AreEqual(2, ident.TypeArguments.Count);
- Assert.AreEqual("T", ident.TypeArguments[0].Type);
- Assert.IsFalse(ident.TypeArguments[0].IsKeyword);
- Assert.AreEqual("System.Boolean", ident.TypeArguments[1].Type);
- Assert.IsTrue(ident.TypeArguments[1].IsKeyword);
+ ParseUtilCSharp.AssertExpression(
+ "myMethod<T,bool>()",
+ new InvocationExpression {
+ Target = new IdentifierExpression {
+ Identifier = "myMethod",
+ TypeArguments = {
+ new SimpleType("T"),
+ new PrimitiveType("bool")
+ }
+ }
+ }
+ );
}
[Test]
public void AmbiguousGrammarGenericMethodCall()
{
- InvocationExpression ie = ParseUtilCSharp.ParseExpression<InvocationExpression>("F(G<A,B>(7))");
- Assert.IsTrue(ie.TargetObject is IdentifierExpression);
- Assert.AreEqual(1, ie.Arguments.Count);
- ie = (InvocationExpression)ie.Arguments[0];
- Assert.AreEqual(1, ie.Arguments.Count);
- Assert.IsTrue(ie.Arguments[0] is PrimitiveExpression);
- IdentifierExpression ident = (IdentifierExpression)ie.TargetObject;
- Assert.AreEqual("G", ident.Identifier);
- Assert.AreEqual(2, ident.TypeArguments.Count);
+ ParseUtilCSharp.AssertExpression(
+ "F(G<A,B>(7))",
+ new InvocationExpression {
+ Target = new IdentifierExpression("F"),
+ Arguments = {
+ new InvocationExpression {
+ Target = new IdentifierExpression {
+ Identifier = "G",
+ TypeArguments = { new SimpleType("A"), new SimpleType("B") }
+ },
+ Arguments = { new PrimitiveExpression(7) }
+ }}});
}
- [Test]
+ [Test, Ignore("Mono Parser Bug???")]
public void AmbiguousGrammarNotAGenericMethodCall()
{
- BinaryOperatorExpression boe = ParseUtilCSharp.ParseExpression<BinaryOperatorExpression>("F<A>+y");
- Assert.AreEqual(BinaryOperatorType.GreaterThan, boe.Op);
- Assert.IsTrue(boe.Left is BinaryOperatorExpression);
- Assert.IsTrue(boe.Right is UnaryOperatorExpression);
+ ParseUtilCSharp.AssertExpression(
+ "F<A>+y",
+ new BinaryOperatorExpression {
+ Left = new BinaryOperatorExpression {
+ Left = new IdentifierExpression("F"),
+ Operator = BinaryOperatorType.LessThan,
+ Right = new IdentifierExpression("A")
+ },
+ Operator = BinaryOperatorType.GreaterThan,
+ Right = new UnaryOperatorExpression {
+ Operator = UnaryOperatorType.Plus,
+ Expression = new IdentifierExpression("y")
+ }});
}
[Test]
@@ -76,80 +92,81 @@ public void InvalidNestedInvocationExpressionTest()
// this test was written because this bug caused the AbstractASTVisitor to crash
InvocationExpression expr = ParseUtilCSharp.ParseExpression<InvocationExpression>("WriteLine(myMethod(,))", true);
- Assert.IsTrue(expr.TargetObject is IdentifierExpression);
- Assert.AreEqual("WriteLine", ((IdentifierExpression)expr.TargetObject).Identifier);
+ Assert.IsTrue(expr.Target is IdentifierExpression);
+ Assert.AreEqual("WriteLine", ((IdentifierExpression)expr.Target).Identifier);
Assert.AreEqual(1, expr.Arguments.Count); // here a second null parameter was added incorrectly
- Assert.IsTrue(expr.Arguments[0] is InvocationExpression);
- CheckSimpleInvoke((InvocationExpression)expr.Arguments[0]);
+ Assert.IsTrue(expr.Arguments.Single() is InvocationExpression);
}
- [Test]
+ [Test, Ignore("Positions not yet accurate when parsing expression only (because class/method is added around it)")]
public void NestedInvocationPositions()
{
InvocationExpression expr = ParseUtilCSharp.ParseExpression<InvocationExpression>("a.B().C(args)");
- Assert.AreEqual(new Location(8, 1), expr.StartLocation);
- Assert.AreEqual(new Location(14, 1), expr.EndLocation);
- MemberReferenceExpression mre = (MemberReferenceExpression)expr.TargetObject;
- Assert.AreEqual(new Location(6, 1), mre.StartLocation);
- Assert.AreEqual(new Location(8, 1), mre.EndLocation);
+ Assert.AreEqual(new AstLocation(1, 8), expr.StartLocation);
+ Assert.AreEqual(new AstLocation(1, 14), expr.EndLocation);
+ MemberReferenceExpression mre = (MemberReferenceExpression)expr.Target;
+ Assert.AreEqual(new AstLocation(1, 6), mre.StartLocation);
+ Assert.AreEqual(new AstLocation(1, 8), mre.EndLocation);
- Assert.AreEqual(new Location(4, 1), mre.TargetObject.StartLocation);
- Assert.AreEqual(new Location(6, 1), mre.TargetObject.EndLocation);
+ Assert.AreEqual(new AstLocation(1, 4), mre.Target.StartLocation);
+ Assert.AreEqual(new AstLocation(1, 6), mre.Target.EndLocation);
}
[Test]
public void InvocationOnGenericType()
{
- InvocationExpression expr = ParseUtilCSharp.ParseExpression<InvocationExpression>("A<T>.Foo()");
- MemberReferenceExpression mre = (MemberReferenceExpression)expr.TargetObject;
- Assert.AreEqual("Foo", mre.MemberName);
- TypeReferenceExpression tre = (TypeReferenceExpression)mre.TargetObject;
- Assert.AreEqual("A", tre.TypeReference.Type);
- Assert.AreEqual("T", tre.TypeReference.GenericTypes[0].Type);
+ ParseUtilCSharp.AssertExpression(
+ "A<T>.Foo()",
+ new IdentifierExpression {
+ Identifier = "A",
+ TypeArguments = { new SimpleType("T") }
+ }.Invoke("Foo")
+ );
}
[Test]
public void InvocationOnInnerClassInGenericType()
{
- InvocationExpression expr = ParseUtilCSharp.ParseExpression<InvocationExpression>("A<T>.B.Foo()");
- MemberReferenceExpression mre = (MemberReferenceExpression)expr.TargetObject;
- Assert.AreEqual("Foo", mre.MemberName);
- MemberReferenceExpression mre2 = (MemberReferenceExpression)mre.TargetObject;
- Assert.AreEqual("B", mre2.MemberName);
- TypeReferenceExpression tre = (TypeReferenceExpression)mre2.TargetObject;
- Assert.AreEqual("A", tre.TypeReference.Type);
- Assert.AreEqual("T", tre.TypeReference.GenericTypes[0].Type);
+ ParseUtilCSharp.AssertExpression(
+ "A<T>.B.Foo()",
+ new IdentifierExpression {
+ Identifier = "A",
+ TypeArguments = { new SimpleType("T") }
+ }.Member("B").Invoke("Foo")
+ );
}
[Test]
public void InvocationOnGenericInnerClassInGenericType()
{
- InvocationExpression expr = ParseUtilCSharp.ParseExpression<InvocationExpression>("A<T>.B.C<U>.Foo()");
- MemberReferenceExpression mre = (MemberReferenceExpression)expr.TargetObject;
- Assert.AreEqual("Foo", mre.MemberName);
- TypeReferenceExpression tre = (TypeReferenceExpression)mre.TargetObject;
- InnerClassTypeReference ictr = (InnerClassTypeReference)tre.TypeReference;
- Assert.AreEqual("B.C", ictr.Type);
- Assert.AreEqual(1, ictr.GenericTypes.Count);
- Assert.AreEqual("U", ictr.GenericTypes[0].Type);
-
- Assert.AreEqual("A", ictr.BaseType.Type);
- Assert.AreEqual(1, ictr.BaseType.GenericTypes.Count);
- Assert.AreEqual("T", ictr.BaseType.GenericTypes[0].Type);
+ ParseUtilCSharp.AssertExpression(
+ "A<T>.B.C<U>.Foo()",
+ new MemberReferenceExpression {
+ Target = new IdentifierExpression {
+ Identifier = "A",
+ TypeArguments = { new SimpleType("T") }
+ }.Member("B"),
+ MemberName = "C",
+ TypeArguments = { new SimpleType("U") }
+ }.Invoke("Foo"));
}
- [Test]
+ [Test, Ignore("named arguments not yet supported")]
public void InvocationWithNamedArgument()
{
- InvocationExpression expr = ParseUtilCSharp.ParseExpression<InvocationExpression>("a(arg: ref v)");
- Assert.AreEqual(1, expr.Arguments.Count);
- NamedArgumentExpression nae = (NamedArgumentExpression)expr.Arguments[0];
- Assert.AreEqual("arg", nae.Name);
- DirectionExpression dir = (DirectionExpression)nae.Expression;
- Assert.AreEqual(FieldDirection.Ref, dir.FieldDirection);
- Assert.IsInstanceOf<IdentifierExpression>(dir.Expression);
- }*/
+ ParseUtilCSharp.AssertExpression(
+ "a(arg: ref v)",
+ new InvocationExpression {
+ Target = new IdentifierExpression("a"),
+ Arguments = {
+ new NamedArgumentExpression {
+ Identifier = "arg",
+ Expression = new DirectionExpression {
+ FieldDirection = FieldDirection.Ref,
+ Expression = new IdentifierExpression("v")
+ }}}});
+ }
}
}
View
15 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/IsExpressionTests.cs
@@ -9,15 +9,16 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
[TestFixture]
public class IsExpressionTests
{
- [Test, Ignore]
+ [Test]
public void GenericArrayIsExpression()
{
- /* TODO
- TypeOfIsExpression ce = ParseUtilCSharp.ParseExpression<TypeOfIsExpression>("o is List<string>[]");
- Assert.AreEqual("List", ce.TypeReference.Type);
- Assert.AreEqual("System.String", ce.TypeReference.GenericTypes[0].Type);
- Assert.AreEqual(new int[] { 0 }, ce.TypeReference.RankSpecifier);
- Assert.IsTrue(ce.Expression is IdentifierExpression);*/
+ ParseUtilCSharp.AssertExpression(
+ "o is List<string>[]",
+ new IsExpression {
+ Expression = new IdentifierExpression("o"),
+ Type = new SimpleType("List") { TypeArguments = { new PrimitiveType("string") } }.MakeArrayType(1)
+ }
+ );
}
[Test]
View
117 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/LambdaExpressionTests.cs
@@ -6,84 +6,103 @@
namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
{
- [TestFixture, Ignore("Port unit tests")]
+ [TestFixture]
public class LambdaExpressionTests
{
- static LambdaExpression ParseCSharp(string program)
- {
- return ParseUtilCSharp.ParseExpression<LambdaExpression>(program);
- }
-
- [Test]
+ [Test, Ignore("Lambdas with expression body not yet supported")]
public void ImplicitlyTypedExpressionBody()
{
- /*
- LambdaExpression e = ParseCSharp("(x) => x + 1");
- Assert.AreEqual("x", e.Parameters[0].ParameterName);
- Assert.IsTrue(e.Parameters[0].TypeReference.IsNull);
- Assert.IsTrue(e.ExpressionBody is BinaryOperatorExpression);
- Assert.IsTrue(e.ReturnType.IsNull);*/
- throw new NotImplementedException();
+ ParseUtilCSharp.AssertExpression(
+ "(x) => x + 1",
+ new LambdaExpression {
+ Parameters = { new ParameterDeclaration { Name = "x" } },
+ Body = new BinaryOperatorExpression(new IdentifierExpression("x"), BinaryOperatorType.Add, new PrimitiveExpression(1))
+ });
}
- /* TODO Port unit tests
- [Test]
+ [Test, Ignore("Lambdas with expression body not yet supported")]
public void ImplicitlyTypedExpressionBodyWithoutParenthesis()
{
- LambdaExpression e = ParseCSharp("x => x + 1");
- Assert.AreEqual("x", e.Parameters[0].ParameterName);
- Assert.IsTrue(e.Parameters[0].TypeReference.IsNull);
- Assert.IsTrue(e.ExpressionBody is BinaryOperatorExpression);
- Assert.IsTrue(e.ReturnType.IsNull);
+ ParseUtilCSharp.AssertExpression(
+ "x => x + 1",
+ new LambdaExpression {
+ Parameters = { new ParameterDeclaration { Name = "x" } },
+ Body = new BinaryOperatorExpression(new IdentifierExpression("x"), BinaryOperatorType.Add, new PrimitiveExpression(1))
+ });
}
[Test]
public void ImplicitlyTypedStatementBody()
{
- LambdaExpression e = ParseCSharp("(x) => { return x + 1; }");
- Assert.AreEqual("x", e.Parameters[0].ParameterName);
- Assert.IsTrue(e.Parameters[0].TypeReference.IsNull);
- Assert.IsTrue(e.StatementBody.Children[0] is ReturnStatement);
- Assert.IsTrue(e.ReturnType.IsNull);
+ ParseUtilCSharp.AssertExpression(
+ "(x) => { return x + 1; }",
+ new LambdaExpression {
+ Parameters = { new ParameterDeclaration { Name = "x" } },
+ Body = new BlockStatement {
+ new ReturnStatement {
+ Expression = new BinaryOperatorExpression(
+ new IdentifierExpression("x"), BinaryOperatorType.Add, new PrimitiveExpression(1))
+ }}});
}
[Test]
public void ImplicitlyTypedStatementBodyWithoutParenthesis()
{
- LambdaExpression e = ParseCSharp("x => { return x + 1; }");
- Assert.AreEqual("x", e.Parameters[0].ParameterName);
- Assert.IsTrue(e.Parameters[0].TypeReference.IsNull);
- Assert.IsTrue(e.StatementBody.Children[0] is ReturnStatement);
- Assert.IsTrue(e.ReturnType.IsNull);
+ ParseUtilCSharp.AssertExpression(
+ "x => { return x + 1; }",
+ new LambdaExpression {
+ Parameters = { new ParameterDeclaration { Name = "x" } },
+ Body = new BlockStatement {
+ new ReturnStatement {
+ Expression = new BinaryOperatorExpression(
+ new IdentifierExpression("x"), BinaryOperatorType.Add, new PrimitiveExpression(1))
+ }}});
}
[Test]
public void ExplicitlyTypedStatementBody()
{
- LambdaExpression e = ParseCSharp("(int x) => { return x + 1; }");
- Assert.AreEqual("x", e.Parameters[0].ParameterName);
- Assert.AreEqual("System.Int32", e.Parameters[0].TypeReference.Type);
- Assert.IsTrue(e.StatementBody.Children[0] is ReturnStatement);
- Assert.IsTrue(e.ReturnType.IsNull);
+ ParseUtilCSharp.AssertExpression(
+ "(int x) => { return x + 1; }",
+ new LambdaExpression {
+ Parameters = { new ParameterDeclaration { Type = new PrimitiveType("int"), Name = "x" } },
+ Body = new BlockStatement {
+ new ReturnStatement {
+ Expression = new BinaryOperatorExpression(
+ new IdentifierExpression("x"), BinaryOperatorType.Add, new PrimitiveExpression(1))
+ }}});
}
- [Test]
- public void ExplicitlyTypedStatementBodyWithRefParameter()
+ [Test, Ignore("Lambdas with expression body not yet supported")]
+ public void ExplicitlyTypedWithRefParameter()
{
- LambdaExpression e = ParseCSharp("(ref int i) => i = 1");
- Assert.AreEqual("i", e.Parameters[0].ParameterName);
- Assert.IsTrue((e.Parameters[0].ParamModifier & ParameterModifiers.Ref) == ParameterModifiers.Ref);
- Assert.AreEqual("System.Int32", e.Parameters[0].TypeReference.Type);
- Assert.IsTrue(e.ReturnType.IsNull);
+ ParseUtilCSharp.AssertExpression(
+ "(ref int i) => i = 1",
+ new LambdaExpression {
+ Parameters = {
+ new ParameterDeclaration {
+ ParameterModifier = ParameterModifier.Ref,
+ Type = new PrimitiveType("int"),
+ Name = "x"
+ }
+ },
+ Body = new AssignmentExpression(new IdentifierExpression("i"), new PrimitiveExpression(1))
+ });
}
- [Test]
+ [Test, Ignore("Lambdas with expression body not yet supported")]
public void LambdaExpressionContainingConditionalExpression()
{
- LambdaExpression e = ParseCSharp("rr => rr != null ? rr.ResolvedType : null");
- Assert.AreEqual("rr", e.Parameters[0].ParameterName);
- Assert.IsTrue(e.ExpressionBody is ConditionalExpression);
- Assert.IsTrue(e.ReturnType.IsNull);
- }*/
+ ParseUtilCSharp.AssertExpression(
+ "rr => rr != null ? rr.ResolvedType : null",
+ new LambdaExpression {
+ Parameters = { new ParameterDeclaration { Name = "rr" } },
+ Body = new ConditionalExpression {
+ Condition = new BinaryOperatorExpression(
+ new IdentifierExpression("rr"), BinaryOperatorType.InEquality, new NullReferenceExpression()),
+ TrueExpression = new IdentifierExpression("rr").Member("ResolvedType"),
+ FalseExpression = new NullReferenceExpression()
+ }});
+ }
}
}
View
97 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/MemberReferenceExpressionTests.cs
@@ -6,71 +6,84 @@
namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
{
- [TestFixture, Ignore]
+ [TestFixture]
public class MemberReferenceExpressionTests
{
[Test]
public void SimpleFieldReferenceExpressionTest()
{
- MemberReferenceExpression fre = ParseUtilCSharp.ParseExpression<MemberReferenceExpression>("myTargetObject.myField");
- //Assert.AreEqual("myField", fre.MemberName);
- //Assert.IsTrue(fre.TargetObject is IdentifierExpression);
- //Assert.AreEqual("myTargetObject", ((IdentifierExpression)fre.TargetObject).Identifier);
- throw new NotImplementedException();
+ ParseUtilCSharp.AssertExpression(
+ "myTargetObject.myField",
+ new IdentifierExpression("myTargetObject").Member("myField")
+ );
+ }
+
+ [Test, Ignore("parser is broken and produces IdentifierExpression instead of PrimitiveType")]
+ public void ShortMaxValueTest()
+ {
+ ParseUtilCSharp.AssertExpression(
+ "short.MaxValue",
+ new PrimitiveType("short").Member("MaxValue")
+ );
+ }
+
+ [Test, Ignore("Parsing of @-identifiers is broken")]
+ public void IdentShortMaxValueTest()
+ {
+ ParseUtilCSharp.AssertExpression(
+ "@short.MaxValue",
+ new IdentifierExpression("short").Member("MaxValue")
+ );
}
- /* TODO port unit tests
[Test]
public void GenericFieldReferenceExpressionTest()
{
- MemberReferenceExpression fre = ParseUtilCSharp.ParseExpression<MemberReferenceExpression>("SomeClass<string>.myField");
- Assert.AreEqual("myField", fre.MemberName);
- Assert.IsTrue(fre.TargetObject is TypeReferenceExpression);
- TypeReference tr = ((TypeReferenceExpression)fre.TargetObject).TypeReference;
- Assert.AreEqual("SomeClass", tr.Type);
- Assert.AreEqual(1, tr.GenericTypes.Count);
- Assert.AreEqual("System.String", tr.GenericTypes[0].Type);
+ ParseUtilCSharp.AssertExpression(
+ "SomeClass<string>.myField",
+ new IdentifierExpression("SomeClass") { TypeArguments = { new PrimitiveType("string") } }.Member("myField")
+ );
}
[Test]
public void FullNamespaceGenericFieldReferenceExpressionTest()
{
- MemberReferenceExpression fre = ParseUtilCSharp.ParseExpression<MemberReferenceExpression>("Namespace.Subnamespace.SomeClass<string>.myField");
- Assert.AreEqual("myField", fre.MemberName);
- Assert.IsTrue(fre.TargetObject is TypeReferenceExpression);
- TypeReference tr = ((TypeReferenceExpression)fre.TargetObject).TypeReference;
- Assert.AreEqual("Namespace.Subnamespace.SomeClass", tr.Type);
- Assert.AreEqual(1, tr.GenericTypes.Count);
- Assert.AreEqual("System.String", tr.GenericTypes[0].Type);
+ ParseUtilCSharp.AssertExpression(
+ "Namespace.Subnamespace.SomeClass<string>.myField",
+ new MemberReferenceExpression {
+ Target = new IdentifierExpression("Namespace").Member("Subnamespace"),
+ TypeArguments = { new PrimitiveType("string") }
+ }.Member("myField")
+ );
}
- [Test]
+ [Test, Ignore("Aliases not yet implemented")]
public void GlobalFullNamespaceGenericFieldReferenceExpressionTest()
{
- MemberReferenceExpression fre = ParseUtilCSharp.ParseExpression<MemberReferenceExpression>("global::Namespace.Subnamespace.SomeClass<string>.myField");
- Assert.AreEqual("myField", fre.MemberName);
- Assert.IsTrue(fre.TargetObject is TypeReferenceExpression);
- TypeReference tr = ((TypeReferenceExpression)fre.TargetObject).TypeReference;
- Assert.IsFalse(tr is InnerClassTypeReference);
- Assert.AreEqual("Namespace.Subnamespace.SomeClass", tr.Type);
- Assert.AreEqual(1, tr.GenericTypes.Count);
- Assert.AreEqual("System.String", tr.GenericTypes[0].Type);
- Assert.IsTrue(tr.IsGlobal);
+ ParseUtilCSharp.AssertExpression(
+ "global::Namespace.Subnamespace.SomeClass<string>.myField",
+ new MemberReferenceExpression {
+ Target = new MemberType {
+ Target = new SimpleType("global"),
+ IsDoubleColon = true,
+ MemberName = "Namespace"
+ }.Member("Subnamespace"),
+ TypeArguments = { new PrimitiveType("string") }
+ }.Member("myField")
+ );
}
[Test]
public void NestedGenericFieldReferenceExpressionTest()
{
- MemberReferenceExpression fre = ParseUtilCSharp.ParseExpression<MemberReferenceExpression>("MyType<string>.InnerClass<int>.myField");
- Assert.AreEqual("myField", fre.MemberName);
- Assert.IsTrue(fre.TargetObject is TypeReferenceExpression);
- InnerClassTypeReference ic = (InnerClassTypeReference)((TypeReferenceExpression)fre.TargetObject).TypeReference;
- 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);
- }*/
+ ParseUtilCSharp.AssertExpression(
+ "MyType<string>.InnerClass<int>.myField",
+ new MemberReferenceExpression {
+ Target = new IdentifierExpression("MyType") { TypeArguments = { new PrimitiveType("string") } },
+ MemberName = "InnerClass",
+ TypeArguments = { new PrimitiveType("int") }
+ }.Member("myField")
+ );
+ }
}
}
View
18 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/PointerReferenceExpressionTests.cs
@@ -9,13 +9,25 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
[TestFixture]
public class PointerReferenceExpressionTests
{
- [Test, Ignore("where did PointerReferenceExpression.MemberName go?")]
+ [Test, Ignore("Parser bug!")]
public void PointerReferenceExpressionTest()
{
PointerReferenceExpression pre = ParseUtilCSharp.ParseExpression<PointerReferenceExpression>("myObj.field->b");
Assert.IsTrue(pre.Target is MemberReferenceExpression);
- //Assert.AreEqual("b", pre.MemberName);
- throw new NotImplementedException();
+ Assert.AreEqual("b", pre.MemberName);
+ }
+
+ [Test, Ignore("Parser bug!")]
+ public void PointerReferenceGenericMethodTest()
+ {
+ ParseUtilCSharp.AssertExpression(
+ "ptr->M<string>();",
+ new InvocationExpression {
+ Target = new PointerReferenceExpression {
+ Target = new IdentifierExpression("ptr"),
+ MemberName = "M",
+ TypeArguments = { new PrimitiveType("string") }
+ }});
}
}
}
View
232 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/QueryExpressionTests.cs
@@ -6,104 +6,220 @@
namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
{
- [TestFixture, Ignore]
+ [TestFixture, Ignore("Query expressions not yet implemented")]
public class QueryExpressionTests
{
[Test]
public void SimpleExpression()
{
- /*
- QueryExpression qe = ParseUtilCSharp.ParseExpression<QueryExpression>(
- "from c in customers where c.City == \"London\" select c"
- );
- Assert.AreEqual("c", qe.FromClause.Sources.First().Identifier);
- Assert.AreEqual("customers", ((IdentifierExpression)qe.FromClause.Sources.First().Expression).Identifier);
- Assert.AreEqual(1, qe.MiddleClauses.Count);
- Assert.IsInstanceOf(typeof(QueryExpressionWhereClause), qe.MiddleClauses[0]);
- QueryExpressionWhereClause wc = (QueryExpressionWhereClause)qe.MiddleClauses[0];
- Assert.IsInstanceOf(typeof(BinaryOperatorExpression), wc.Condition);
- Assert.IsInstanceOf(typeof(QueryExpressionSelectClause), qe.SelectOrGroupClause);*/
- throw new NotImplementedException();
+ ParseUtilCSharp.AssertExpression(
+ "from c in customers where c.City == \"London\" select c",
+ new QueryExpression {
+ Clauses = {
+ new QueryFromClause {
+ Identifier = "c",
+ Expression = new IdentifierExpression("customers")
+ },
+ new QueryWhereClause {
+ Condition = new BinaryOperatorExpression {
+ Left = new IdentifierExpression("c").Member("City"),
+ Operator = BinaryOperatorType.Equality,
+ Right = new PrimitiveExpression("London")
+ }
+ },
+ new QuerySelectClause {
+ Expression = new IdentifierExpression("c")
+ }
+ }});
}
- /* TODO port unit tests
[Test]
public void ExpressionWithType1()
{
- QueryExpression qe = ParseUtilCSharp.ParseExpression<QueryExpression>(
- "from Customer c in customers select c"
- );
- Assert.AreEqual("c", qe.FromClause.Sources.First().Identifier);
- Assert.AreEqual("Customer", qe.FromClause.Sources.First().Type.ToString());
- Assert.AreEqual("customers", ((IdentifierExpression)qe.FromClause.Sources.First().Expression).Identifier);
- Assert.IsInstanceOf(typeof(QueryExpressionSelectClause), qe.SelectOrGroupClause);
+ ParseUtilCSharp.AssertExpression(
+ "from Customer c in customers select c",
+ new QueryExpression {
+ Clauses = {
+ new QueryFromClause {
+ Type = new SimpleType("Customer"),
+ Identifier = "c",
+ Expression = new IdentifierExpression("customers")
+ },
+ new QuerySelectClause {
+ Expression = new IdentifierExpression("c")
+ }
+ }});
}
[Test]
public void ExpressionWithType2()
{
- QueryExpression qe = ParseUtilCSharp.ParseExpression<QueryExpression>(
- "from int c in customers select c"
- );
- Assert.AreEqual("c", qe.FromClause.Sources.First().Identifier);
- Assert.AreEqual("System.Int32", qe.FromClause.Sources.First().Type.Type);
- Assert.AreEqual("customers", ((IdentifierExpression)qe.FromClause.Sources.First().Expression).Identifier);
- Assert.IsInstanceOf(typeof(QueryExpressionSelectClause), qe.SelectOrGroupClause);
+ ParseUtilCSharp.AssertExpression(
+ "from int c in customers select c",
+ new QueryExpression {
+ Clauses = {
+ new QueryFromClause {
+ Type = new PrimitiveType("int"),
+ Identifier = "c",
+ Expression = new IdentifierExpression("customers")
+ },
+ new QuerySelectClause {
+ Expression = new IdentifierExpression("c")
+ }
+ }});
}
[Test]
public void ExpressionWithType3()
{
- QueryExpression qe = ParseUtilCSharp.ParseExpression<QueryExpression>(
- "from S<int[]>? c in customers select c"
- );
- Assert.AreEqual("c", qe.FromClause.Sources.First().Identifier);
- Assert.AreEqual("System.Nullable<S<System.Int32[]>>", qe.FromClause.Sources.First().Type.ToString());
- Assert.AreEqual("customers", ((IdentifierExpression)qe.FromClause.Sources.First().Expression).Identifier);
- Assert.IsInstanceOf(typeof(QueryExpressionSelectClause), qe.SelectOrGroupClause);
+ ParseUtilCSharp.AssertExpression(
+ "from S<int[]>? c in customers select c",
+ new QueryExpression {
+ Clauses = {
+ new QueryFromClause {
+ Type = new ComposedType {
+ BaseType = new SimpleType {
+ Identifier = "S",
+ TypeArguments = {
+ new PrimitiveType("int").MakeArrayType()
+ }
+ },
+ HasNullableSpecifier = true
+ },
+ Identifier = "c",
+ Expression = new IdentifierExpression("customers")
+ },
+ new QuerySelectClause {
+ Expression = new IdentifierExpression("c")
+ }
+ }});
}
[Test]
public void MultipleGenerators()
{
- QueryExpression qe = ParseUtilCSharp.ParseExpression<QueryExpression>(@"
+ ParseUtilCSharp.AssertExpression(
+ @"
from c in customers
where c.City == ""London""
from o in c.Orders
where o.OrderDate.Year == 2005
-select new { c.Name, o.OrderID, o.Total }");
- Assert.AreEqual(3, qe.MiddleClauses.Count);
- Assert.IsInstanceOf(typeof(QueryExpressionWhereClause), qe.MiddleClauses[0]);
- Assert.IsInstanceOf(typeof(QueryExpressionFromClause), qe.MiddleClauses[1]);
- Assert.IsInstanceOf(typeof(QueryExpressionWhereClause), qe.MiddleClauses[2]);
-
- Assert.IsInstanceOf(typeof(QueryExpressionSelectClause), qe.SelectOrGroupClause);
+select new { c.Name, o.OrderID, o.Total }",
+ new QueryExpression {
+ Clauses = {
+ new QueryFromClause {
+ Identifier = "c",
+ Expression = new IdentifierExpression("customers")
+ },
+ new QueryWhereClause {
+ Condition = new BinaryOperatorExpression {
+ Left = new IdentifierExpression("c").Member("City"),
+ Operator = BinaryOperatorType.Equality,
+ Right = new PrimitiveExpression("London")
+ }
+ },
+ new QueryFromClause {
+ Identifier = "o",
+ Expression = new IdentifierExpression("c").Member("Orders")
+ },
+ new QueryWhereClause {
+ Condition = new BinaryOperatorExpression {
+ Left = new IdentifierExpression("c").Member("OrderDate").Member("Year"),
+ Operator = BinaryOperatorType.Equality,
+ Right = new PrimitiveExpression(2005)
+ }
+ },
+ new QuerySelectClause {
+ Expression = new ObjectCreateExpression {
+ Initializer = new ArrayInitializerExpression {
+ Elements = {
+ new IdentifierExpression("c").Member("Name"),
+ new IdentifierExpression("o").Member("OrderID"),
+ new IdentifierExpression("o").Member("Total")
+ }
+ }
+ }
+ }
+ }});
}
[Test]
public void ExpressionWithOrderBy()
{
- QueryExpression qe = ParseUtilCSharp.ParseExpression<QueryExpression>(
- "from c in customers orderby c.Name select c"
- );
- Assert.AreEqual("c", qe.FromClause.Sources.First().Identifier);
- Assert.AreEqual("customers", ((IdentifierExpression)qe.FromClause.Sources.First().Expression).Identifier);
- Assert.IsInstanceOf(typeof(QueryExpressionOrderClause), qe.MiddleClauses[0]);
- Assert.IsInstanceOf(typeof(QueryExpressionSelectClause), qe.SelectOrGroupClause);
+ ParseUtilCSharp.AssertExpression(
+ "from c in customers orderby c.Name select c",
+ new QueryExpression {
+ Clauses = {
+ new QueryFromClause {
+ Identifier = "c",
+ Expression = new IdentifierExpression("customers")
+ },
+ new QueryOrderClause {
+ Orderings = {
+ new QueryOrdering {
+ Expression = new IdentifierExpression("c").Member("Name")
+ }
+ }
+ },
+ new QuerySelectClause {
+ Expression = new IdentifierExpression("c")
+ }
+ }});
}
[Test]
public void ExpressionWithOrderByAndLet()
{
- QueryExpression qe = ParseUtilCSharp.ParseExpression<QueryExpression>(
- "from c in customers orderby c.Name let x = c select x"
+ ParseUtilCSharp.AssertExpression(
+ "from c in customers orderby c.Name descending let x = c select x",
+ new QueryExpression {
+ Clauses = {
+ new QueryFromClause {
+ Identifier = "c",
+ Expression = new IdentifierExpression("customers")
+ },
+ new QueryOrderClause {
+ Orderings = {
+ new QueryOrdering {
+ Expression = new IdentifierExpression("c").Member("Name"),
+ Direction = QueryOrderingDirection.Descending
+ }
+ }
+ },
+ new QueryLetClause {
+ Identifier = "x",
+ Expression = new IdentifierExpression("c")
+ },
+ new QuerySelectClause {
+ Expression = new IdentifierExpression("x")
+ }
+ }});
+ }
+
+ [Test]
+ public void QueryContinuation()
+ {
+ ParseUtilCSharp.AssertExpression(
+ "from a in b select c into d select e",
+ new QueryExpression {
+ Clauses = {
+ new QueryContinuationClause {
+ PrecedingQuery = new QueryExpression {
+ Clauses = {
+ new QueryFromClause {
+ Identifier = "a",
+ Expression = new IdentifierExpression("b")
+ },
+ new QuerySelectClause { Expression = new IdentifierExpression("c") }
+ }
+ },
+ Identifier = "d"
+ },
+ new QuerySelectClause { Expression = new IdentifierExpression("e") }
+ }
+ }
);
- Assert.AreEqual("c", qe.FromClause.Sources.First().Identifier);
- Assert.AreEqual("customers", ((IdentifierExpression)qe.FromClause.Sources.First().Expression).Identifier);
- Assert.IsInstanceOf(typeof(QueryExpressionOrderClause), qe.MiddleClauses[0]);
- Assert.IsInstanceOf(typeof(QueryExpressionLetClause), qe.MiddleClauses[1]);
- Assert.IsInstanceOf(typeof(QueryExpressionSelectClause), qe.SelectOrGroupClause);
- }*/
+ }
}
}
View
4 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/SizeOfExpressionTests.cs
@@ -9,11 +9,11 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
[TestFixture]
public class SizeOfExpressionTests
{
- [Test, Ignore("type references not implemented yet")]
+ [Test]
public void SizeOfExpressionTest()
{
SizeOfExpression soe = ParseUtilCSharp.ParseExpression<SizeOfExpression>("sizeof(MyType)");
- Assert.AreEqual("MyType", soe.Type);
+ Assert.AreEqual("MyType", ((SimpleType)soe.Type).Identifier);
}
}
}
View
9 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/StackAllocExpressionTests.cs
@@ -2,6 +2,7 @@
// 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.Expression
@@ -9,11 +10,13 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
[TestFixture]
public class StackAllocExpressionTests
{
- [Test, Ignore]
+ [Test]
public void StackAllocExpressionTest()
{
- var sae = ParseUtilCSharp.ParseExpression<StackAllocExpression>("stackalloc int[100]");
- throw new NotImplementedException(); // TODO: verify type + length expression
+ var vd = ParseUtilCSharp.ParseStatement<VariableDeclarationStatement>("int* a = stackalloc int[100];");
+ StackAllocExpression sae = (StackAllocExpression)vd.Variables.Single().Initializer;
+ Assert.AreEqual("int", ((PrimitiveType)sae.Type).Keyword);
+ Assert.AreEqual(100, ((PrimitiveExpression)sae.CountExpression).Value);
}
}
}
View
97 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/TypeOfExpressionTests.cs
@@ -6,86 +6,113 @@
namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
{
- [TestFixture, Ignore]
+ [TestFixture]
public class TypeOfExpressionTests
{
[Test]
public void SimpleTypeOfExpressionTest()
{
- //TypeOfExpression toe = ParseUtilCSharp.ParseExpression<TypeOfExpression>("typeof(MyNamespace.N1.MyType)");
- //Assert.AreEqual("MyNamespace.N1.MyType", toe.TypeReference.Type);
- throw new NotImplementedException();
+ ParseUtilCSharp.AssertExpression(
+ "typeof(MyNamespace.N1.MyType)",
+ new TypeOfExpression {
+ Type = new MemberType {
+ Target = new MemberType {
+ Target = new SimpleType("MyNamespace"),
+ MemberName = "N1"
+ },
+ MemberName = "MyType"
+ }});
}
- /* TODO
- [Test]
+ [Test, Ignore("Aliases not yet implemented")]
public void GlobalTypeOfExpressionTest()
{
- TypeOfExpression toe = ParseUtilCSharp.ParseExpression<TypeOfExpression>("typeof(global::System.Console)");
- Assert.AreEqual("System.Console", toe.TypeReference.Type);
+ ParseUtilCSharp.AssertExpression(
+ "typeof(global::System.Console)",
+ new TypeOfExpression {
+ Type = new MemberType {
+ Target = new MemberType {
+ Target = new SimpleType("global"),
+ IsDoubleColon = true,
+ MemberName = "System"
+ },
+ MemberName = "Console"
+ }});
}
[Test]
public void PrimitiveTypeOfExpressionTest()
{
TypeOfExpression toe = ParseUtilCSharp.ParseExpression<TypeOfExpression>("typeof(int)");
- Assert.AreEqual("System.Int32", toe.TypeReference.Type);
+ Assert.AreEqual("int", ((PrimitiveType)toe.Type).Keyword);
}
[Test]
public void VoidTypeOfExpressionTest()
{
TypeOfExpression toe = ParseUtilCSharp.ParseExpression<TypeOfExpression>("typeof(void)");
- Assert.AreEqual("System.Void", toe.TypeReference.Type);
+ Assert.AreEqual("void", ((PrimitiveType)toe.Type).Keyword);
}
[Test]
public void ArrayTypeOfExpressionTest()
{
- TypeOfExpression toe = ParseUtilCSharp.ParseExpression<TypeOfExpression>("typeof(MyType[])");
- Assert.AreEqual("MyType", toe.TypeReference.Type);
- Assert.AreEqual(new int[] {0}, toe.TypeReference.RankSpecifier);
+ ParseUtilCSharp.AssertExpression(
+ "typeof(MyType[])",
+ new TypeOfExpression {
+ Type = new SimpleType("MyType").MakeArrayType()
+ });
}
[Test]
public void GenericTypeOfExpressionTest()
{
- TypeOfExpression toe = ParseUtilCSharp.ParseExpression<TypeOfExpression>("typeof(MyNamespace.N1.MyType<string>)");
- Assert.AreEqual("MyNamespace.N1.MyType", toe.TypeReference.Type);
- Assert.AreEqual("System.String", toe.TypeReference.GenericTypes[0].Type);
+ ParseUtilCSharp.AssertExpression(
+ "typeof(MyNamespace.N1.MyType<string>)",
+ new TypeOfExpression {
+ Type = new MemberType {
+ Target = new MemberType {
+ Target = new SimpleType("MyNamespace"),
+ MemberName = "N1"
+ },
+ MemberName = "MyType",
+ TypeArguments = { new PrimitiveType("string") }
+ }});
}
[Test]
public void NestedGenericTypeOfExpressionTest()
{
- TypeOfExpression toe = ParseUtilCSharp.ParseExpression<TypeOfExpression>("typeof(MyType<string>.InnerClass<int>.InnerInnerClass)");
- InnerClassTypeReference ic = (InnerClassTypeReference)toe.TypeReference;
- 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);
+ ParseUtilCSharp.AssertExpression(
+ "typeof(MyType<string>.InnerClass<int>.InnerInnerClass)",
+ new TypeOfExpression {
+ Type = new MemberType {
+ Target = new MemberType {
+ Target = new SimpleType("MyType") { TypeArguments = { new PrimitiveType("string") } },
+ MemberName = "InnerClass",
+ TypeArguments = { new PrimitiveType("int") }
+ },
+ MemberName = "InnerInnerClass"
+ }});
}
[Test]
public void NullableTypeOfExpressionTest()
{
- TypeOfExpression toe = ParseUtilCSharp.ParseExpression<TypeOfExpression>("typeof(MyStruct?)");
- Assert.AreEqual("System.Nullable", toe.TypeReference.Type);
- Assert.AreEqual("MyStruct", toe.TypeReference.GenericTypes[0].Type);
+ ParseUtilCSharp.AssertExpression(
+ "typeof(MyStruct?)",
+ new TypeOfExpression {
+ Type = new ComposedType {
+ BaseType = new SimpleType("MyType"),
+ HasNullableSpecifier = true
+ }});
}
- [Test]
+ [Test, Ignore("How do we represent unbound types in the AST?")]
public void UnboundTypeOfExpressionTest()
{
TypeOfExpression toe = ParseUtilCSharp.ParseExpression<TypeOfExpression>("typeof(MyType<,>)");
- Assert.AreEqual("MyType", toe.TypeReference.Type);
- Assert.IsTrue(toe.TypeReference.GenericTypes[0].IsNull);
- Assert.IsTrue(toe.TypeReference.GenericTypes[1].IsNull);
- }*/
+ throw new NotImplementedException("How do we represent unbound types in the AST?");
+ }
}
}
View
6 ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/UnaryOperatorExpressionTests.cs
@@ -84,9 +84,9 @@ public void DereferenceAfterCast()
Assert.AreEqual(UnaryOperatorType.Dereference, uoe.Operator);
ParenthesizedExpression pe = (ParenthesizedExpression)uoe.Expression;
CastExpression ce = (CastExpression)pe.Expression;
- //Assert.AreEqual("SomeType", ce.CastTo.Type);
- //Assert.AreEqual(1, ce.CastTo.PointerNestingLevel);
- Assert.Ignore("need to check target type"); // TODO
+ ComposedType type = (ComposedType)ce.Type;
+ Assert.AreEqual("SomeType", ((SimpleType)type.BaseType).Identifier);
+ Assert.AreEqual(1, type.PointerRank);
UnaryOperatorExpression adrOf = (UnaryOperatorExpression)ce.Expression;
Assert.AreEqual(UnaryOperatorType.AddressOf, adrOf.Operator);
View
68 ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/DelegateDeclarationTests.cs
@@ -7,55 +7,41 @@
namespace ICSharpCode.NRefactory.CSharp.Parser.GeneralScope
{
- [TestFixture, Ignore("delegates are completely broken at the moment")]
+ [TestFixture]
public class DelegateDeclarationTests
{
- void TestParameters(DelegateDeclaration dd)
- {
- Assert.AreEqual(3, dd.Parameters.Count());
-
- Assert.AreEqual("a", ((ParameterDeclaration)dd.Parameters.ElementAt(0)).Name);
- //Assert.AreEqual("System.Int32", ((ParameterDeclaration)dd.Parameters.ElementAt(0)).TypeReference.Type);
- Assert.Ignore("check types"); // TODO
- Assert.AreEqual("secondParam", ((ParameterDeclaration)dd.Parameters.ElementAt(1)).Name);
- //Assert.AreEqual("System.Int32", ((ParameterDeclaration)dd.Parameters.ElementAt(1)).TypeReference.Type);
-
- Assert.AreEqual("lastParam", ((ParameterDeclaration)dd.Parameters.ElementAt(2)).Name);
- //Assert.AreEqual("MyObj", ((ParameterDeclaration)dd.Parameters.ElementAt(2)).TypeReference.Type);
- }
-
[Test]
public void SimpleCSharpDelegateDeclarationTest()
{
- string program = "public delegate void MyDelegate(int a, int secondParam, MyObj lastParam);\n";
- DelegateDeclaration dd = ParseUtilCSharp.ParseGlobal<DelegateDeclaration>(program);
- Assert.AreEqual("MyDelegate", dd.Name);
- //Assert.AreEqual("System.Void", dd.ReturnType.Type);
- TestParameters(dd);
+ ParseUtilCSharp.AssertGlobal(
+ "public delegate void MyDelegate(int a, int secondParam, MyObj lastParam);",
+ new DelegateDeclaration {
+ Modifiers = Modifiers.Public,
+ ReturnType = new PrimitiveType("void"),
+ Name = "MyDelegate",
+ Parameters = {
+ new ParameterDeclaration(new PrimitiveType("int"), "a"),
+ new ParameterDeclaration(new PrimitiveType("int"), "secondParam"),
+ new ParameterDeclaration(new SimpleType("MyObj"), "lastParam")
+ }});
}
- [Test, Ignore]
- public void DelegateWithoutNameDeclarationTest()
- {
- string program = "public delegate void(int a, int secondParam, MyObj lastParam);\n";
- DelegateDeclaration dd = ParseUtilCSharp.ParseGlobal<DelegateDeclaration>(program, true);
- //Assert.AreEqual("System.Void", dd.ReturnType.Type);
- //Assert.AreEqual("?", dd.Name);
- TestParameters(dd);
- }
-
- [Test, Ignore]
+ [Test, Ignore("Generics not yet supported")]
public void GenericDelegateDeclarationTest()
{
- string program = "public delegate T CreateObject<T>() where T : ICloneable;\n";
- DelegateDeclaration dd = ParseUtilCSharp.ParseGlobal<DelegateDeclaration>(program);
- Assert.AreEqual("CreateObject", dd.Name);
- //Assert.AreEqual("T", dd.ReturnType.Type);
- Assert.AreEqual(0, dd.Parameters.Count());
- /*Assert.AreEqual(1, dd.Templates.Count);
- Assert.AreEqual("T", dd.Templates[0].Name);
- Assert.AreEqual(1, dd.Templates[0].Bases.Count);
- Assert.AreEqual("ICloneable", dd.Templates[0].Bases[0].Type);*/ throw new NotImplementedException();
+ ParseUtilCSharp.AssertGlobal(
+ "public delegate T CreateObject<T>() where T : ICloneable;",
+ new DelegateDeclaration {
+ Modifiers = Modifiers.Public,
+ ReturnType = new SimpleType("T"),
+ Name = "CreateObject",
+ TypeParameters = { new TypeParameterDeclaration { Name = "T" } },
+ Constraints = {
+ new Constraint {
+ TypeParameter = "T",
+ BaseTypes = { new SimpleType("ICloneable") }
+ }
+ }});
}
[Test]
@@ -66,7 +52,7 @@ public void DelegateDeclarationInNamespace()
Assert.AreEqual("MyDelegate", ((DelegateDeclaration)nd.Members.Single()).Name);
}
- [Test, Ignore("inner classes not yet implemented")]
+ [Test]
public void DelegateDeclarationInClass()
{
string program = "class Outer { delegate void Inner(); }";
View
198 ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/TypeDeclarationTests.cs
@@ -70,85 +70,111 @@ public void SimpleStaticClassTypeDeclarationTest()
Assert.AreEqual(Modifiers.Static, td.Modifiers);
}
- [Test, Ignore]
+ [Test, Ignore("Generics not yet supported")]
public void GenericClassTypeDeclarationTest()
{
- TypeDeclaration td = ParseUtilCSharp.ParseGlobal<TypeDeclaration>("public class G<T> {}");
-
- Assert.AreEqual(ClassType.Class, td.ClassType);
- Assert.AreEqual("G", td.Name);
- Assert.AreEqual(Modifiers.Public, td.Modifiers);
- /*Assert.AreEqual(0, td.BaseTypes.Count);
- Assert.AreEqual(1, td.TypeArguments.Count());
- Assert.AreEqual("T", td.TypeArguments.Single().Name);*/ throw new NotImplementedException();
+ ParseUtilCSharp.AssertGlobal(
+ "public class G<T> {}",
+ new TypeDeclaration {
+ Modifiers = Modifiers.Public,
+ ClassType = ClassType.Class,
+ Name = "G",
+ TypeParameters = { new TypeParameterDeclaration { Name = "T" } }
+ });
}
-
- [Test, Ignore]
+ [Test, Ignore("Constraints not yet supported")]
public void GenericClassWithWhere()
{
- string declr = @"
-public class Test<T> where T : IMyInterface
-{
-}
-";
- TypeDeclaration td = ParseUtilCSharp.ParseGlobal<TypeDeclaration>(declr);
-
- Assert.AreEqual(ClassType.Class, td.ClassType);
- Assert.AreEqual("Test", td.Name);
-
- /*Assert.AreEqual(1, td.Templates.Count);
- Assert.AreEqual("T", td.Templates[0].Name);
- Assert.AreEqual("IMyInterface", td.Templates[0].Bases[0].Type);*/ throw new NotImplementedException();
+ ParseUtilCSharp.AssertGlobal(
+ @"public class Test<T> where T : IMyInterface { }",
+ new TypeDeclaration {
+ Modifiers = Modifiers.Public,
+ ClassType = ClassType.Class,
+ Name = "Test",
+ TypeParameters = { new TypeParameterDeclaration { Name = "T" } },
+ Constraints = {
+ new Constraint {
+ TypeParameter = "T",
+ BaseTypes = { new SimpleType("IMyInterface") }
+ }
+ }});
}
- [Test, Ignore]
+ [Test, Ignore("Generic classes not yet supported")]
public void ComplexGenericClassTypeDeclarationTest()
{
- string declr = @"
-public class Generic<T, S> : System.IComparable where S : G<T[]> where T : MyNamespace.IMyInterface
-{
-}
-";
- TypeDeclaration td = ParseUtilCSharp.ParseGlobal<TypeDeclaration>(declr);
-
- Assert.AreEqual(ClassType.Class, td.ClassType);
- Assert.AreEqual("Generic", td.Name);
- Assert.AreEqual(Modifiers.Public, td.Modifiers);
- /*Assert.AreEqual(1, td.BaseTypes.Count);
- Assert.AreEqual("System.IComparable", td.BaseTypes[0].Type);
-
- Assert.AreEqual(2, td.Templates.Count);
- Assert.AreEqual("T", td.Templates[0].Name);
- Assert.AreEqual("MyNamespace.IMyInterface", td.Templates[0].Bases[0].Type);
-
- Assert.AreEqual("S", td.Templates[1].Name);
- Assert.AreEqual("G", td.Templates[1].Bases[0].Type);
- Assert.AreEqual(1, td.Templates[1].Bases[0].GenericTypes.Count);
- Assert.IsTrue(td.Templates[1].Bases[0].GenericTypes[0].IsArrayType);
- Assert.AreEqual("T", td.Templates[1].Bases[0].GenericTypes[0].Type);
- Assert.AreEqual(new int[] {0}, td.Templates[1].Bases[0].GenericTypes[0].RankSpecifier);*/ throw new NotImplementedException();
+ ParseUtilCSharp.AssertGlobal(
+ "public class Generic<in T, out S> : System.IComparable where S : G<T[]>, new() where T : MyNamespace.IMyInterface",
+ new TypeDeclaration {
+ Modifiers = Modifiers.Public,
+ ClassType = ClassType.Class,
+ Name = "Generic",
+ TypeParameters = {
+ new TypeParameterDeclaration { Variance = VarianceModifier.Contravariant, Name = "T" },
+ new TypeParameterDeclaration { Variance = VarianceModifier.Covariant, Name = "S" }
+ },
+ BaseTypes = {
+ new MemberType {
+ Target = new SimpleType("System"),
+ MemberName = "IComparable"
+ }
+ },
+ Constraints = {
+ new Constraint {
+ TypeParameter = "S",
+ BaseTypes = {
+ new SimpleType {
+ Identifier = "G",
+ TypeArguments = { new SimpleType("T").MakeArrayType() }
+ },
+ new PrimitiveType("new")
+ }
+ },
+ new Constraint {
+ TypeParameter = "T",
+ BaseTypes = {
+ new MemberType {
+ Target = new SimpleType("MyNamespace"),
+ MemberName = "IMyInterface"
+ }
+ }
+ }
+ }
+ });
}
- [Test, Ignore]
+ [Test, Ignore("Base types not yet implemented")]
public void ComplexClassTypeDeclarationTest()
{
- string declr = @"
+ ParseUtilCSharp.AssertGlobal(
+ @"
[MyAttr()]
public abstract class MyClass : MyBase, Interface1, My.Test.Interface2
{
-}
-";
- TypeDeclaration td = ParseUtilCSharp.ParseGlobal<TypeDeclaration>(declr);
-
- Assert.AreEqual(ClassType.Class, td.ClassType);
- Assert.AreEqual("MyClass", td.Name);
- Assert.AreEqual(Modifiers.Public | Modifiers.Abstract, td.Modifiers);
- Assert.AreEqual(1, td.Attributes.Count());
- /* Assert.AreEqual(3, td.BaseTypes.Count);
- Assert.AreEqual("MyBase", td.BaseTypes[0].Type);
- Assert.AreEqual("Interface1", td.BaseTypes[1].Type);
- Assert.AreEqual("My.Test.Interface2", td.BaseTypes[2].Type);*/ throw new NotImplementedException();
+}",
+ new TypeDeclaration {
+ Attributes = {
+ new AttributeSection {
+ Attributes = {
+ new Attribute { Type = new SimpleType("MyAttr") }
+ }
+ }
+ },
+ Modifiers = Modifiers.Public | Modifiers.Abstract,
+ ClassType = ClassType.Class,
+ Name = "MyClass",
+ BaseTypes = {
+ new SimpleType("MyBase"),
+ new SimpleType("Interface1"),
+ new MemberType {
+ Target = new MemberType {
+ Target = new SimpleType("My"),
+ MemberName = "Test"
+ },
+ MemberName = "Interface2"
+ }
+ }});
}
[Test]
@@ -178,27 +204,37 @@ public void SimpleEnumTypeDeclarationTest()
Assert.AreEqual("MyEnum", td.Name);
}
- [Test, Ignore]
+ [Test, Ignore("Mono parser bug?")]
public void ContextSensitiveKeywordTest()
{
- TypeDeclaration td = ParseUtilCSharp.ParseGlobal<TypeDeclaration>("partial class partial<[partial: where] where> where where : partial<where> { }");
-
- Assert.AreEqual(Modifiers.Partial, td.Modifiers);
- Assert.AreEqual("partial", td.Name);
-
- /*
- Assert.AreEqual(1, td.Templates.Count);
- TemplateDefinition tp = td.Templates[0];
- Assert.AreEqual("where", tp.Name);
-
- Assert.AreEqual(1, tp.Attributes.Count);
- Assert.AreEqual("partial", tp.Attributes[0].AttributeTarget);
- Assert.AreEqual(1, tp.Attributes[0].Attributes.Count);
- Assert.AreEqual("where", tp.Attributes[0].Attributes[0].Name);
-
- Assert.AreEqual(1, tp.Bases.Count);
- Assert.AreEqual("partial", tp.Bases[0].Type);
- Assert.AreEqual("where", tp.Bases[0].GenericTypes[0].Type);*/ throw new NotImplementedException();
+ ParseUtilCSharp.AssertGlobal(
+ "partial class partial<[partial: where] where> where where : partial<where> { }",
+ new TypeDeclaration {
+ Modifiers = Modifiers.Partial,
+ ClassType = ClassType.Class,
+ Name = "partial",
+ TypeParameters = {
+ new TypeParameterDeclaration {
+ Attributes = {
+ new AttributeSection {
+ AttributeTarget = AttributeTarget.Unknown,
+ Attributes = { new Attribute { Type = new SimpleType("where") } }
+ }
+ },
+ Name = "where"
+ }
+ },
+ Constraints = {
+ new Constraint {
+ TypeParameter = "where",
+ BaseTypes = {
+ new SimpleType {
+ Identifier = "partial",
+ TypeArguments = { new SimpleType("where") }
+ }
+ }
+ }
+ }});
}
[Test]
View
2 ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/UsingDeclarationTests.cs
@@ -43,7 +43,7 @@ public void DeclarationTest()
Assert.AreEqual("My.Name.Space", ud.Namespace);
}
- [Test]
+ [Test, Ignore("Aliases to generic types not yet supported")]
public void UsingAliasDeclarationTest()
{
string program = "using TESTME=System;\n" +
View
41 ICSharpCode.NRefactory.Tests/CSharp/Parser/ParseUtil.cs
@@ -11,7 +11,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser
/// <summary>
/// Helper methods for parser unit tests.
/// </summary>
- public class ParseUtilCSharp
+ public static class ParseUtilCSharp
{
public static T ParseGlobal<T>(string code, bool expectErrors = false) where T : AstNode
{
@@ -26,6 +26,14 @@ public class ParseUtilCSharp
return (T)node;
}
+ public static void AssertGlobal(string code, AstNode expectedNode)
+ {
+ var node = ParseGlobal<AstNode>(code);
+ if (expectedNode.Match(node) == null) {
+ Assert.Fail("Expected '{0}' but was '{1}'", ToCSharp(expectedNode), ToCSharp(node));
+ }
+ }
+
public static T ParseStatement<T>(string stmt, bool expectErrors = false) where T : AstNode
{
CSharpParser parser = new CSharpParser();
@@ -39,6 +47,14 @@ public class ParseUtilCSharp
return (T)statement;
}
+ public static void AssertStatement(string code, CSharp.Statement expectedStmt)
+ {
+ var stmt = ParseStatement<CSharp.Statement>(code);
+ if (expectedStmt.Match(stmt) == null) {
+ Assert.Fail("Expected '{0}' but was '{1}'", ToCSharp(expectedStmt), ToCSharp(stmt));
+ }
+ }
+
public static T ParseExpression<T>(string expr, bool expectErrors = false) where T : AstNode
{
if (expectErrors) Assert.Ignore("errors not yet implemented");
@@ -53,6 +69,14 @@ public class ParseUtilCSharp
return (T)parsedExpression;
}
+ public static void AssertExpression(string code, CSharp.Expression expectedExpr)
+ {
+ var expr = ParseExpression<CSharp.Expression>(code);
+ if (expectedExpr.Match(expr) == null) {
+ Assert.Fail("Expected '{0}' but was '{1}'", ToCSharp(expectedExpr), ToCSharp(expr));
+ }
+ }
+
public static T ParseTypeMember<T>(string expr, bool expectErrors = false) where T : AttributedNode
{
if (expectErrors) Assert.Ignore("errors not yet implemented");
@@ -67,5 +91,20 @@ public class ParseUtilCSharp
Assert.IsTrue(type.IsAssignableFrom(m.GetType()), String.Format("Parsed member was {0} instead of {1} ({2})", m.GetType(), type, m));
return (T)m;
}
+
+ public static void AssertTypeMember(string code, CSharp.AttributedNode expectedMember)