@ -3,6 +3,7 @@
@@ -3,6 +3,7 @@
using System ;
using System.Linq ;
using ICSharpCode.NRefactory.PatternMatching ;
using NUnit.Framework ;
namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
@ -21,185 +22,137 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
@@ -21,185 +22,137 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
Assert . AreEqual ( 5 , ( ( PrimitiveExpression ) lvd . Variables . First ( ) . Initializer ) . Value ) ;
}
/ * 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 ) ;
Assert . IsTrue ( new VariableDeclarationStatement ( new PrimitiveType ( "void" ) . MakePointerType ( ) , "a" ) . IsMatch ( lvd ) ) ;
}
[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
AstType type = new SimpleType ( "Generic" ) {
TypeArguments = {
new MemberType { Target = new SimpleType ( "Namespace" ) , MemberName = "Printable" } ,
new SimpleType ( "G" ) { TypeArguments = { new SimpleType ( "Printable" ) . MakeArrayType ( ) } }
} } ;
Assert . IsTrue ( new VariableDeclarationStatement ( type , "where" , new ObjectCreateExpression { Type = type . Clone ( ) } ) . IsMatch ( lvd ) ) ;
}
[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 ) ;
AstType type = new MemberType {
Target = new MemberType {
Target = new SimpleType ( "MyType" ) { TypeArguments = { new PrimitiveType ( "string" ) } } ,
MemberName = "InnerClass" ,
TypeArguments = { new PrimitiveType ( "int" ) }
} ,
MemberName = "InnerInnerClass"
} ;
Assert . IsTrue ( new VariableDeclarationStatement ( type , "a" ) . IsMatch ( lvd ) ) ;
}
[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 ) ;
AstType type = new SimpleType ( "G" ) {
TypeArguments = { new PrimitiveType ( "int" ) }
} . MakeArrayType ( ) ;
Assert . IsTrue ( new VariableDeclarationStatement ( type , "a" ) . IsMatch ( lvd ) ) ;
}
[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 ) ;
AstType type = new SimpleType ( "G" ) {
TypeArguments = { new PrimitiveType ( "int" ) . MakeArrayType ( ) }
} ;
Assert . IsTrue ( new VariableDeclarationStatement ( type , "a" ) . IsMatch ( lvd ) ) ;
}
[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 ) ;
AstType type = new SimpleType ( "G" ) {
TypeArguments = {
new SimpleType ( "G" ) { TypeArguments = { new PrimitiveType ( "int" ) } }
}
} ;
Assert . IsTrue ( new VariableDeclarationStatement ( type , "a" ) . IsMatch ( lvd ) ) ;
}
[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 ) ;
AstType type = new SimpleType ( "G" ) {
TypeArguments = {
new SimpleType ( "G" ) { TypeArguments = { new PrimitiveType ( "int" ) } }
}
} ;
Assert . IsTrue ( new VariableDeclarationStatement ( type , "a" ) . IsMatch ( lvd ) ) ;
}
[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 ) ;
AstType type = new SimpleType ( "G" ) {
TypeArguments = { new PrimitiveType ( "int" ) }
} ;
Assert . IsTrue ( new VariableDeclarationStatement ( type , "a" ) . IsMatch ( lvd ) ) ;
}
[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
Assert . IsTrue ( new VariableDeclarationStatement ( new SimpleType ( "MyVar" ) , "var" , new ObjectCreateExpression { Type = new SimpleType ( "MyVar" ) } ) . IsMatch ( lvd ) ) ;
}
[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
Assert . IsTrue ( new VariableDeclarationStatement ( new SimpleType ( "yield" ) , "yield" , new ObjectCreateExpression { Type = new SimpleType ( "yield" ) } ) . IsMatch ( lvd ) ) ;
}
[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 ) ;
Assert . IsTrue ( new VariableDeclarationStatement ( new PrimitiveType ( "int" ) . MakeNullableType ( ) , "a" ) . IsMatch ( lvd ) ) ;
}
[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 ) ;
Assert . IsTrue ( new VariableDeclarationStatement ( new SimpleType ( "DateTime" ) . MakeNullableType ( ) , "a" ) . IsMatch ( lvd ) ) ;
}
[Test]
[Test, Ignore("The parser creates nested ComposedTypes while MakeArrayType() adds the specifier to the existing ComposedType")]
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 ) ;
Assert . IsTrue ( new VariableDeclarationStatement ( new SimpleType ( "DateTime" ) . MakeNullableType ( ) . MakeArrayType ( ) , "a" ) . IsMatch ( lvd ) ) ;
}
[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 ) ;
AstType type = new SimpleType ( "SomeStruct" ) {
TypeArguments = { new PrimitiveType ( "int" ) . MakeNullableType ( ) }
} . MakeNullableType ( ) ;
Assert . IsTrue ( new VariableDeclarationStatement ( type , "a" ) . IsMatch ( lvd ) ) ;
}
[Test]
@ -216,11 +169,39 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
@@ -216,11 +169,39 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.Statements
public void PositionTestWithModifier ( )
{
VariableDeclarationStatement lvd = ParseUtilCSharp . ParseStatement < VariableDeclarationStatement > ( "\nconst double w = 7;" ) ;
Assert . AreEqual ( Modifiers . Const , lvd . Modifier ) ;
Assert . AreEqual ( Modifiers . Const , lvd . Modifiers ) ;
Assert . AreEqual ( 2 , lvd . StartLocation . Line ) ;
Assert . AreEqual ( 1 , lvd . StartLocation . Column ) ;
Assert . AreEqual ( 2 , lvd . EndLocation . Line ) ;
Assert . AreEqual ( 2 0 , lvd . EndLocation . Column ) ;
} * /
}
[Test, Ignore("Nested arrays are broken in the parser")]
public void NestedArray ( )
{
VariableDeclarationStatement lvd = ParseUtilCSharp . ParseStatement < VariableDeclarationStatement > ( "DateTime[,][] a;" ) ;
Assert . IsTrue ( new VariableDeclarationStatement ( new SimpleType ( "DateTime" ) . MakeArrayType ( 1 ) . MakeArrayType ( 2 ) , "a" ) . IsMatch ( lvd ) ) ;
}
[Test, Ignore("Nested pointers are broken in the parser")]
public void NestedPointers ( )
{
VariableDeclarationStatement lvd = ParseUtilCSharp . ParseStatement < VariableDeclarationStatement > ( "DateTime*** a;" ) ;
Assert . IsTrue ( new VariableDeclarationStatement ( new SimpleType ( "DateTime" ) . MakePointerType ( ) . MakePointerType ( ) . MakePointerType ( ) , "a" ) . IsMatch ( lvd ) ) ;
}
[Test, Ignore("The parser creates nested ComposedTypes while MakeArrayType() adds the specifier to the existing ComposedType")]
public void ArrayOfPointers ( )
{
VariableDeclarationStatement lvd = ParseUtilCSharp . ParseStatement < VariableDeclarationStatement > ( "DateTime*[] a;" ) ;
Assert . IsTrue ( new VariableDeclarationStatement ( new SimpleType ( "DateTime" ) . MakePointerType ( ) . MakeArrayType ( ) , "a" ) . IsMatch ( lvd ) ) ;
}
[Test, Ignore("The parser creates nested ComposedTypes while MakeArrayType() adds the specifier to the existing ComposedType")]
public void ArrayOfNullables ( )
{
VariableDeclarationStatement lvd = ParseUtilCSharp . ParseStatement < VariableDeclarationStatement > ( "DateTime?[] a;" ) ;
Assert . IsTrue ( new VariableDeclarationStatement ( new SimpleType ( "DateTime" ) . MakeNullableType ( ) . MakeArrayType ( ) , "a" ) . IsMatch ( lvd ) ) ;
}
}
}