#develop (short for SharpDevelop) is a free IDE for .NET programming languages.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

201 lines
5.9 KiB

// 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.Semantics;
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<string>()$;
}
}
";
var result = Resolve<CSharpInvocationResolveResult>(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<ErrorResolveResult>(program);
Assert.AreSame(SharedTypes.UnknownType, result.Type);
}
[Test]
public void NonExistingClassTypeName()
{
string program = @"class A {
void Method() {
var a = new $ThisClassDoesNotExist$();
}
}
";
UnknownIdentifierResolveResult result = Resolve<UnknownIdentifierResolveResult>(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) {}
}
";
var result = Resolve<CSharpInvocationResolveResult>(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<CSharpInvocationResolveResult>(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<CSharpInvocationResolveResult>(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()$;
}
}
";
var result = Resolve<CSharpInvocationResolveResult>(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<InvocationResolveResult>(program.Replace("base(b)", "$base(b)$"));
Assert.AreEqual("A..ctor", mrr.Member.FullName);
mrr = Resolve<InvocationResolveResult>(program.Replace("base(c)", "$base(c)$"));
Assert.AreEqual("B..ctor", mrr.Member.FullName);
mrr = Resolve<InvocationResolveResult>(program.Replace("this(0)", "$this(0)$"));
Assert.AreEqual("C..ctor", mrr.Member.FullName);
}
[Test]
public void FieldReferenceInObjectInitializer()
{
string program = @"class A {
public int Property;
}
class B {
void Method() {
var x = new A() { $Property = 0$ };
}
}";
MemberResolveResult result = Resolve<MemberResolveResult>(program);
Assert.AreEqual("A.Property", result.Member.FullName);
}
[Test]
public void FieldReferenceInNestedObjectInitializer()
{
string program = @"class Point { public float X, Y; }
class Rect { public Point TopLeft, BottomRight; }
class B {
void Method() {
var x = new Rect() { TopLeft = { $X = 1$ } };
}
}";
MemberResolveResult result = Resolve<MemberResolveResult>(program);
Assert.AreEqual("Point.X", result.Member.FullName);
}
[Test, Ignore("Parser returns incorrect positions")]
public void CollectionInitializerTest()
{
string program = @"using System.Collections.Generic;
class B {
void Method() {
var x = new List<int>() { ${ 0 }$ };
}
}";
InvocationResolveResult result = Resolve<InvocationResolveResult>(program);
Assert.AreEqual("System.Collections.Generic.List.Add", result.Member.FullName);
}
[Test, Ignore("Parser returns incorrect positions")]
public void DictionaryInitializerTest()
{
string program = @"using System.Collections.Generic;
class B {
void Method() {
var x = new Dictionary<char, int>() { ${ 'a', 0 }$ };
}
}";
InvocationResolveResult result = Resolve<InvocationResolveResult>(program);
Assert.AreEqual("System.Collections.Generic.Dictionary.Add", result.Member.FullName);
}
}
}