// Copyright (c) AlphaSierraPapa for the SharpDevelop Team // // Permission is hereby granted, free of charge, to any person obtaining a copy of this // software and associated documentation files (the "Software"), to deal in the Software // without restriction, including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons // to whom the Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. using System; using ICSharpCode.NRefactory.TypeSystem; using NUnit.Framework; namespace ICSharpCode.NRefactory.CSharp.Resolver { [TestFixture] public class ObjectCreationTests : ResolverTestBase { [Test] public void GenericObjectCreation() { string program = @"using System.Collections.Generic; class A { static void Main() { var a = $new List()$; } } "; InvocationResolveResult result = Resolve(program); Assert.AreEqual("System.Collections.Generic.List..ctor", result.Member.FullName); Assert.AreEqual("System.Collections.Generic.List`1[[System.String]]", result.Type.ReflectionName); } [Test] public void NonExistingClass() { string program = @"class A { void Method() { var a = $new ThisClassDoesNotExist()$; } } "; ResolveResult result = Resolve(program); Assert.AreSame(SharedTypes.UnknownType, result.Type); } [Test] public void NonExistingClassTypeName() { string program = @"class A { void Method() { var a = new $ThisClassDoesNotExist$(); } } "; UnknownIdentifierResolveResult result = Resolve(program); Assert.AreEqual("ThisClassDoesNotExist", result.Identifier); Assert.AreSame(SharedTypes.UnknownType, result.Type); } [Test] public void CTorOverloadLookupTest() { string program = @"class A { void Method() { $; } static A() {} A() {} A(int intVal) {} A(double dblVal) {} } "; InvocationResolveResult result = Resolve(program.Replace("$", "$new A()$")); Assert.IsFalse(result.Member.IsStatic, "new A() is static"); Assert.AreEqual(0, result.Member.Parameters.Count, "new A() parameter count"); Assert.AreEqual("A", result.Type.FullName); result = Resolve(program.Replace("$", "$new A(10)$")); Assert.AreEqual(1, result.Member.Parameters.Count, "new A(10) parameter count"); Assert.AreEqual("intVal", result.Member.Parameters[0].Name, "new A(10) parameter"); result = Resolve(program.Replace("$", "$new A(11.1)$")); Assert.AreEqual(1, result.Member.Parameters.Count, "new A(11.1) parameter count"); Assert.AreEqual("dblVal", result.Member.Parameters[0].Name, "new A(11.1) parameter"); } [Test] public void DefaultCTorOverloadLookupTest() { string program = @"class A { void Method() { $new A()$; } } "; InvocationResolveResult result = Resolve(program); Assert.AreEqual("A", result.Type.ReflectionName); Assert.AreEqual(0, result.Member.Parameters.Count); } [Test, Ignore("Parser returns incorrect positions")] public void ChainedConstructorCall() { string program = @"using System; class A { public A(int a) {} } class B : A { public B(int b) : base(b) {} } class C : B { public C(int c) : base(c) {} public C() : this(0) {} } "; InvocationResolveResult mrr = Resolve(program, "base(b)"); Assert.AreEqual("A..ctor", mrr.Member.FullName); mrr = Resolve(program, "base(c)"); Assert.AreEqual("B..ctor", mrr.Member.FullName); mrr = Resolve(program, "this(0)"); Assert.AreEqual("C..ctor", mrr.Member.FullName); } } }