Skip to content
This repository has been archived by the owner on Sep 24, 2020. It is now read-only.

Commit

Permalink
Re-enabled the unit tests that were commented out.
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrunwald committed Feb 28, 2011
1 parent defd426 commit b37ea77
Show file tree
Hide file tree
Showing 29 changed files with 1,179 additions and 734 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,40 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
[TestFixture]
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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
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]
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)
}
}
});

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
});
}
}
}
Loading

0 comments on commit b37ea77

Please sign in to comment.