PageRenderTime 23ms CodeModel.GetById 15ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/NRefactory/ICSharpCode.NRefactory.VB.Tests/Parser/Expressions/ObjectCreateExpressionTests.cs

http://github.com/icsharpcode/ILSpy
C# | 114 lines | 92 code | 19 blank | 3 comment | 1 complexity | b6b33cc36902390045d25778f6cded1d MD5 | raw file
  1// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
  2// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
  3
  4using System;
  5using System.IO;
  6using NUnit.Framework;
  7using ICSharpCode.NRefactory.VB.Parser;
  8using ICSharpCode.NRefactory.VB.Ast;
  9
 10namespace ICSharpCode.NRefactory.VB.Tests.Ast
 11{
 12	[TestFixture]
 13	public class ObjectCreateExpressionTests
 14	{
 15		void CheckSimpleObjectCreateExpression(ObjectCreateExpression oce)
 16		{
 17			Assert.AreEqual("MyObject", oce.CreateType.Type);
 18			Assert.AreEqual(3, oce.Parameters.Count);
 19			Assert.IsTrue(oce.ObjectInitializer.IsNull);
 20			
 21			for (int i = 0; i < oce.Parameters.Count; ++i) {
 22				Assert.IsTrue(oce.Parameters[i] is PrimitiveExpression);
 23			}
 24		}
 25		
 26		Expression CheckPropertyInitializationExpression(Expression e, string name)
 27		{
 28			Assert.IsInstanceOf(typeof(MemberInitializerExpression), e);
 29			Assert.AreEqual(name, ((MemberInitializerExpression)e).Name);
 30			return ((MemberInitializerExpression)e).Expression;
 31		}
 32		
 33		[Test]
 34		public void VBNetAnonymousType()
 35		{
 36			ObjectCreateExpression oce = ParseUtil.ParseExpression<ObjectCreateExpression>(
 37				"New With {.Id = 1, .Name= \"Bill Gates\" }");
 38			
 39			Assert.IsTrue(oce.CreateType.IsNull);
 40			Assert.AreEqual(0, oce.Parameters.Count);
 41			Assert.AreEqual(2, oce.ObjectInitializer.CreateExpressions.Count);
 42			
 43			Assert.IsInstanceOf(typeof(PrimitiveExpression), CheckPropertyInitializationExpression(oce.ObjectInitializer.CreateExpressions[0], "Id"));
 44			Assert.IsInstanceOf(typeof(MemberInitializerExpression), oce.ObjectInitializer.CreateExpressions[1]);
 45		}
 46		
 47		[Test]
 48		public void VBNetAnonymousTypeWithoutProperty()
 49		{
 50			ObjectCreateExpression oce = ParseUtil.ParseExpression<ObjectCreateExpression>("New With { c }");
 51			
 52			Assert.IsTrue(oce.CreateType.IsNull);
 53			Assert.AreEqual(0, oce.Parameters.Count);
 54			Assert.AreEqual(1, oce.ObjectInitializer.CreateExpressions.Count);
 55			
 56			Assert.IsInstanceOf(typeof(SimpleNameExpression), oce.ObjectInitializer.CreateExpressions[0]);
 57			Assert.AreEqual("c", (oce.ObjectInitializer.CreateExpressions[0] as SimpleNameExpression).Identifier);
 58		}
 59		
 60		[Test]
 61		public void VBNetSimpleObjectCreateExpressionTest()
 62		{
 63			CheckSimpleObjectCreateExpression(ParseUtil.ParseExpression<ObjectCreateExpression>("New MyObject(1, 2, 3)"));
 64		}
 65		
 66		[Test]
 67		public void VBNetInvalidTypeArgumentListObjectCreateExpressionTest()
 68		{
 69			// this test was written because this bug caused the AbstractDomVisitor to crash
 70			
 71			InvocationExpression expr = ParseUtil.ParseExpression<InvocationExpression>("WriteLine(New SomeGenericType(Of Integer, )())", true);
 72			Assert.IsTrue(expr.TargetObject is SimpleNameExpression);
 73			Assert.AreEqual("WriteLine", ((SimpleNameExpression)expr.TargetObject).Identifier);
 74			Assert.AreEqual(1, expr.Arguments.Count); // here a second null parameter was added incorrectly
 75			
 76			Assert.IsTrue(expr.Arguments[0] is ObjectCreateExpression);
 77			TypeReference typeRef = ((ObjectCreateExpression)expr.Arguments[0]).CreateType;
 78			Assert.AreEqual("SomeGenericType", typeRef.Type);
 79			Assert.AreEqual(1, typeRef.GenericTypes.Count);
 80			Assert.AreEqual("System.Int32", typeRef.GenericTypes[0].Type);
 81		}
 82		
 83		[Test]
 84		public void VBNetMemberInitializationTest()
 85		{
 86			ObjectCreateExpression oce = ParseUtil.ParseExpression<ObjectCreateExpression>("new Contact() With { .FirstName = \"Bill\", .LastName = \"Gates\" }");
 87			Assert.AreEqual(2, oce.ObjectInitializer.CreateExpressions.Count);
 88			
 89			Assert.AreEqual("FirstName", ((MemberInitializerExpression)oce.ObjectInitializer.CreateExpressions[0]).Name);
 90			Assert.AreEqual("LastName", ((MemberInitializerExpression)oce.ObjectInitializer.CreateExpressions[1]).Name);
 91			
 92			Assert.IsInstanceOf(typeof(PrimitiveExpression), ((MemberInitializerExpression)oce.ObjectInitializer.CreateExpressions[0]).Expression);
 93			Assert.IsInstanceOf(typeof(PrimitiveExpression), ((MemberInitializerExpression)oce.ObjectInitializer.CreateExpressions[1]).Expression);
 94		}
 95		
 96		[Test]
 97		public void VBNetNullableObjectCreateExpressionTest()
 98		{
 99			ObjectCreateExpression oce = ParseUtil.ParseExpression<ObjectCreateExpression>("New Integer?");
100			Assert.AreEqual("System.Nullable", oce.CreateType.Type);
101			Assert.AreEqual(1, oce.CreateType.GenericTypes.Count);
102			Assert.AreEqual("System.Int32", oce.CreateType.GenericTypes[0].Type);
103		}
104
105		[Test]
106		public void VBNetNullableObjectArrayCreateExpressionTest()
107		{
108			ObjectCreateExpression oce = ParseUtil.ParseExpression<ObjectCreateExpression>("New Integer?()");
109			Assert.AreEqual("System.Nullable", oce.CreateType.Type);
110			Assert.AreEqual(1, oce.CreateType.GenericTypes.Count);
111			Assert.AreEqual("System.Int32", oce.CreateType.GenericTypes[0].Type);
112		}
113	}
114}