1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
|
// Copyright (c) 2010-2013 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 System.Linq;
using ICSharpCode.NRefactory.Semantics;
using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.NRefactory.TypeSystem.Implementation;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Resolver
{
[TestFixture]
public class InvocationTests : ResolverTestBase
{
[Test]
public void MethodCallTest()
{
string program = @"class A {
void Method() {
$TargetMethod()$;
}
int TargetMethod() {
return 3;
}
}
";
InvocationResolveResult result = Resolve<CSharpInvocationResolveResult>(program);
Assert.AreEqual("A.TargetMethod", result.Member.FullName);
Assert.AreEqual("System.Int32", result.Type.ReflectionName);
}
[Test]
public void InvalidMethodCall()
{
string program = @"class A {
void Method(string b) {
$b.ThisMethodDoesNotExistOnString(b)$;
}
}
";
UnknownMethodResolveResult result = Resolve<UnknownMethodResolveResult>(program);
Assert.AreEqual("ThisMethodDoesNotExistOnString", result.MemberName);
Assert.AreEqual("System.String", result.TargetType.FullName);
Assert.AreEqual(1, result.Parameters.Count);
Assert.AreEqual("b", result.Parameters[0].Name);
Assert.AreEqual("System.String", result.Parameters[0].Type.ReflectionName);
Assert.AreSame(SpecialType.UnknownType, result.Type);
}
[Test]
public void OverriddenMethodCall()
{
string program = @"class A {
void Method() {
$new B().GetRandomNumber()$;
}
public abstract int GetRandomNumber();
}
class B : A {
public override int GetRandomNumber() {
return 4; // chosen by fair dice roll.
// guaranteed to be random
}
}
";
InvocationResolveResult result = Resolve<CSharpInvocationResolveResult>(program);
Assert.AreEqual("B.GetRandomNumber", result.Member.FullName);
}
[Test]
public void OverriddenMethodCall2()
{
string program = @"class A {
void Method() {
$new B().GetRandomNumber(""x"", this)$;
}
public abstract int GetRandomNumber(string a, A b);
}
class B : A {
public override int GetRandomNumber(string b, A a) {
return 4;
}
}
";
InvocationResolveResult result = Resolve<CSharpInvocationResolveResult>(program);
Assert.AreEqual("B.GetRandomNumber", result.Member.FullName);
}
[Test]
public void ThisMethodCallTest()
{
string program = @"class A {
void Method() {
$this.TargetMethod()$;
}
int TargetMethod() {
return 3;
}
}
";
InvocationResolveResult result = Resolve<CSharpInvocationResolveResult>(program);
Assert.AreEqual("A.TargetMethod", result.Member.FullName);
Assert.AreEqual("System.Int32", result.Type.ReflectionName);
}
[Test]
public void VoidTest()
{
string program = @"using System;
class A {
void TestMethod() {
$TestMethod()$;
}
}
";
Assert.AreEqual("System.Void", Resolve(program).Type.ReflectionName);
}
[Test]
public void EventCallTest()
{
string program = @"using System;
class A {
void Method() {
$TestEvent(this, EventArgs.Empty)$;
}
public event EventHandler TestEvent;
}
";
Assert.AreEqual("System.Void", Resolve(program).Type.ReflectionName);
}
[Test]
public void DelegateCallTest()
{
string program = @"using System; using System.Reflection;
class A {
void Method(ModuleResolveEventHandler eh) {
$eh(this, new ResolveEventArgs())$;
}
}
";
Assert.AreEqual("System.Reflection.Module", Resolve(program).Type.ReflectionName);
}
[Test]
public void DelegateReturnedFromMethodCallTest()
{
string program = @"using System;
class A {
void Method() {
$GetHandler()(abc)$;
}
abstract Predicate<string> GetHandler();
}
";
Assert.AreEqual("System.Boolean", Resolve(program).Type.ReflectionName);
}
/* TODO
[Test]
public void MethodGroupResolveTest()
{
string program = @"class A {
void Method() {
}
void TargetMethod(int a) { }
void TargetMethod<T>(T a) { }
}
";
MethodGroupResolveResult result = Resolve<MethodGroupResolveResult>(program, "TargetMethod", 3);
Assert.AreEqual("TargetMethod", result.Name);
Assert.AreEqual(2, result.Methods.Count);
result = Resolve<MethodGroupResolveResult>(program, "TargetMethod<string>", 3);
Assert.AreEqual("TargetMethod", result.Name);
Assert.AreEqual(1, result.Methods[0].Count);
Assert.AreEqual("System.String", result.GetMethodIfSingleOverload().Parameters[0].ReturnType.FullyQualifiedName);
}
*/
[Test]
public void TestOverloadingByRef()
{
string program = @"using System;
class Program {
public static void Main() {
int a = 42;
T(a);
T(ref a);
}
static void T(int x) {}
static void T(ref int y) {}
}";
InvocationResolveResult mrr = Resolve<CSharpInvocationResolveResult>(program.Replace("T(a)", "$T(a)$"));
Assert.IsFalse(mrr.Member.Parameters[0].IsRef);
mrr = Resolve<CSharpInvocationResolveResult>(program.Replace("T(ref a)", "$T(ref a)$"));
Assert.IsTrue(mrr.Member.Parameters[0].IsRef);
}
[Test]
public void AddedOverload()
{
string program = @"class BaseClass {
static void Main(DerivedClass d) {
$d.Test(3)$;
}
public void Test(int a) { }
}
class DerivedClass : BaseClass {
public void Test(object a) { }
}";
InvocationResolveResult mrr = Resolve<CSharpInvocationResolveResult>(program);
Assert.AreEqual("DerivedClass.Test", mrr.Member.FullName);
}
[Test]
public void AddedOverloadOnInterface()
{
string program = @"
interface IBase { void Method(int a); }
interface IDerived { void Method(object a); }
class Test {
static void Main(IDerived d) {
$d.Method(3)$;
}
}";
InvocationResolveResult mrr = Resolve<CSharpInvocationResolveResult>(program);
Assert.AreEqual("IDerived.Method", mrr.Member.FullName);
}
[Test]
public void AddedNonApplicableOverload()
{
string program = @"class BaseClass {
static void Main(DerivedClass d) {
$d.Test(3)$;
}
public void Test(int a) { }
}
class DerivedClass : BaseClass {
public void Test(string a) { }
}";
InvocationResolveResult mrr = Resolve<CSharpInvocationResolveResult>(program);
Assert.AreEqual("BaseClass.Test", mrr.Member.FullName);
mrr = Resolve<CSharpInvocationResolveResult>(program.Replace("(3)", "(\"3\")"));
Assert.AreEqual("DerivedClass.Test", mrr.Member.FullName);
}
[Test]
public void OverrideShadowed()
{
string program = @"using System;
class BaseClass {
static void Main() {
$new DerivedClass().Test(3)$;
}
public virtual void Test(int a) { }
}
class MiddleClass : BaseClass {
public void Test(object a) { }
}
class DerivedClass : MiddleClass {
public override void Test(int a) { }
}";
InvocationResolveResult mrr = Resolve<CSharpInvocationResolveResult>(program);
Assert.AreEqual("MiddleClass.Test", mrr.Member.FullName);
}
[Test]
public void SubstituteClassAndMethodTypeParametersAtOnce()
{
string program = @"class C<X> { static void M<T>(X a, T b) { $C<T>.M(b, a)$; } }";
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsFalse(rr.IsError);
var m = (IMethod)rr.Member;
Assert.AreEqual("X", m.TypeArguments.Single().Name);
Assert.AreEqual("T", m.Parameters[0].Type.Name);
Assert.AreEqual("X", m.Parameters[1].Type.Name);
}
[Test]
public void MemberHiddenOnOneAccessPath()
{
// If a member is hidden in any access path, it is hidden in all access paths
string program = @"
interface IBase { int F { get; } }
interface ILeft: IBase { new int F { get; } }
interface IRight: IBase { void G(); }
interface IDerived: ILeft, IRight {}
class A {
void Test(IDerived d) { var a = $d.F$; }
}";
var rr = Resolve<MemberResolveResult>(program);
Assert.AreEqual("ILeft.F", rr.Member.FullName);
}
[Test]
public void PropertyClashesWithMethod()
{
string program = @"
interface IList { int Count { get; set; } }
interface ICounter { void Count(int i); }
interface IListCounter: IList, ICounter {}
class A {
void Test(IListCounter x) { var a = $x.Count$; }
}";
var rr = Resolve<MethodGroupResolveResult>(program);
Assert.IsFalse(rr.IsError);
Assert.AreEqual("ICounter.Count", rr.Methods.Single().FullName);
}
[Test]
public void OverloadAmbiguousWithMethodInTwoInterfaces()
{
string program = @"
interface ILeft { void Method(); }
interface IRight { void Method(); }
interface IBoth : ILeft, IRight {}
class A {
void Test(IBoth x) { $x.Method()$; }
}";
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsTrue(rr.IsError);
Assert.AreEqual(OverloadResolutionErrors.AmbiguousMatch, rr.OverloadResolutionErrors);
}
[Test]
public void AddedOverloadInOneInterfaceAndBetterOverloadInOtherInterface1()
{
string program = @"
interface IBase { void Method(int x); }
interface ILeft : IBase { void Method(object x); }
interface IRight { void Method(int x); }
interface IBoth : ILeft, IRight {}
class A {
void Test(IBoth x) { $x.Method(1)$; }
}";
// IBase.Method is "hidden" because ILeft.Method is also applicable,
// so IRight.Method is unambiguously the chosen overload.
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsFalse(rr.IsError);
Assert.AreEqual("IRight.Method", rr.Member.FullName);
}
[Test]
public void AddedOverloadInOneInterfaceAndBetterOverloadInOtherInterface2()
{
// repeat the above test with Left/Right swapped to make sure we aren't order-sensitive
string program = @"
interface IBase { void Method(int x); }
interface ILeft : IBase { void Method(object x); }
interface IRight { void Method(int x); }
interface IBoth : IRight, ILeft {}
class A {
void Test(IBoth x) { $x.Method(1)$; }
}";
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsFalse(rr.IsError);
Assert.AreEqual("IRight.Method", rr.Member.FullName);
}
[Test]
public void AddedOverloadHidesCommonBaseMethod_Generic1()
{
string program = @"
interface IBase<T> {
void Method(int x);
}
interface ILeft : IBase<int> { void Method(object x); }
interface IRight : IBase<int> { }
interface IBoth : ILeft, IRight {}
class A {
void Test(IBoth x) { $x.Method(1)$; }
}";
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsFalse(rr.IsError);
Assert.AreEqual("ILeft.Method", rr.Member.FullName);
}
[Test]
public void AddedOverloadHidesCommonBaseMethod_Generic2()
{
string program = @"
interface IBase<T> {
void Method(int x);
}
interface ILeft : IBase<int> { void Method(object x); }
interface IRight : IBase<int> { }
interface IBoth : ILeft, IRight {}
class A {
void Test(IBoth x) { $x.Method(1)$; }
}";
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsFalse(rr.IsError);
Assert.AreEqual("ILeft.Method", rr.Member.FullName);
}
[Test]
public void AddedOverloadDoesNotHideCommonBaseMethodWithDifferentTypeArgument1()
{
string program = @"
interface IBase<T> {
void Method(int x);
}
interface ILeft : IBase<int> { void Method(object x); }
interface IRight : IBase<long> { }
interface IBoth : IRight, ILeft {}
class A {
void Test(IBoth x) { $x.Method(1)$; }
}";
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsFalse(rr.IsError);
Assert.AreEqual("IBase`1[[System.Int64]]", rr.Member.DeclaringType.ReflectionName);
}
[Test]
public void AddedOverloadDoesNotHideCommonBaseMethodWithDifferentTypeArgument2()
{
string program = @"
interface IBase<T> {
void Method(int x);
}
interface ILeft : IBase<int> { void Method(object x); }
interface IRight : IBase<long> { }
interface IBoth : IRight, ILeft {}
class A {
void Test(IBoth x) { $x.Method(1)$; }
}";
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsFalse(rr.IsError);
Assert.AreEqual("IBase`1[[System.Int64]]", rr.Member.DeclaringType.ReflectionName);
}
[Test]
public void AmbiguityBetweenMemberAndMethodIsNotAnError()
{
string program = @"
interface ILeft { void Method(object x); }
interface IRight { Action<object> Method { get; } }
interface IBoth : ILeft, IRight {}
class A {
void Test(IBoth x) { $x.Method(null)$; }
}";
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsFalse(rr.IsError);
Assert.AreEqual("ILeft.Method", rr.Member.FullName);
}
[Test]
public void AcceptVisitor()
{
string program = @"
interface IVisitor<in T, out S> { }
class Test : IVisitor<object, object> {
void M() {
$Accept(this, null)$;
}
S Accept<T, S>(IVisitor<T, S> v, T input) { }
}";
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsFalse(rr.IsError);
var typeArguments = ((IMethod)rr.Member).TypeArguments;
Assert.AreEqual("System.Object", typeArguments[0].ReflectionName);
Assert.AreEqual("System.Object", typeArguments[1].ReflectionName);
}
[Test]
public void FirstParameterToExtensionMethod()
{
string program = @"
public class X {}
public static class Ex {
public static void F(this X x, int y, int z) {}
}
class C {
public void M() {
X a = null;
int b = 0, c = 0;
$a.F(b, c)$;
}
}";
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsFalse(rr.IsError);
Assert.That(rr.IsExtensionMethodInvocation, Is.True);
Assert.That(rr.Arguments[0], Is.InstanceOf<LocalResolveResult>());
Assert.That(((LocalResolveResult)rr.Arguments[0]).Variable.Name, Is.EqualTo("a"));
Assert.That(rr.Arguments[1], Is.InstanceOf<LocalResolveResult>());
Assert.That(((LocalResolveResult)rr.Arguments[1]).Variable.Name, Is.EqualTo("b"));
Assert.That(rr.Arguments[2], Is.InstanceOf<LocalResolveResult>());
Assert.That(((LocalResolveResult)rr.Arguments[2]).Variable.Name, Is.EqualTo("c"));
Assert.That(rr.TargetResult, Is.InstanceOf<TypeResolveResult>());
}
[Test]
public void BaseInvocation()
{
string program = @"
class B {
public virtual void F(int x, int y) {}
}
class D : B {
public override void F(int x, int y) {}
public void M() {
$base.F(0, 1)$;
}
}";
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsFalse(rr.IsError);
Assert.IsFalse(rr.IsVirtualCall);
}
[Test]
public void NamedArgument()
{
string program = @"
class Test {
public void F(int x) {}
public void Test() {
F($x: 0$);
}
}";
var narr = Resolve<NamedArgumentResolveResult>(program);
Assert.IsInstanceOf<ConstantResolveResult>(narr.Argument);
Assert.AreEqual("x", narr.ParameterName);
Assert.AreEqual("Test.F", narr.Member.FullName);
Assert.AreSame(narr.Member.Parameters.Single(), narr.Parameter);
}
[Test]
public void NamedArgumentInInvocation()
{
string program = @"
class Test {
public void F(int x) {}
public void Test() {
$F(x: 0)$;
}
}";
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsInstanceOf<NamedArgumentResolveResult>(rr.Arguments.Single());
var narr = (NamedArgumentResolveResult)rr.Arguments.Single();
Assert.IsInstanceOf<ConstantResolveResult>(narr.Argument);
Assert.AreEqual("x", narr.ParameterName);
Assert.AreEqual("Test.F", narr.Member.FullName);
Assert.AreSame(narr.Member.Parameters.Single(), narr.Parameter);
// but GetArgumentsForCall() should directly return the constant:
Assert.IsInstanceOf<ConstantResolveResult>(rr.GetArgumentsForCall().Single());
}
[Test]
public void UnknownNamedArgument()
{
string program = @"
class Test {
public void F(int x) {}
public void Test() {
F($y: 0$);
}
}";
var narr = Resolve<NamedArgumentResolveResult>(program);
Assert.IsInstanceOf<ConstantResolveResult>(narr.Argument);
Assert.AreEqual("y", narr.ParameterName);
Assert.IsNull(narr.Parameter);
}
[Test]
public void NamedArgumentInMissingMethod()
{
string program = @"
class Test {
public void Test() {
Missing($x: 0$);
}
}";
var narr = Resolve<NamedArgumentResolveResult>(program);
Assert.IsInstanceOf<ConstantResolveResult>(narr.Argument);
Assert.AreEqual("x", narr.ParameterName);
Assert.IsNull(narr.Parameter);
}
[Test]
public void GenericMethodInvocationWithConstraintMismatch()
{
string program = @"
interface IA
{
}
class Test
{
void F()
{
string o = null;
$M(o)$;
}
void M<T>(T arg) where T : IA
{
}
void M(object arg) {
}
}";
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.AreEqual(OverloadResolutionErrors.MethodConstraintsNotSatisfied, rr.OverloadResolutionErrors);
Assert.IsTrue(rr.IsError);
}
[Test]
public void MethodCanBeInvokedWithNullableTypeArgument1() {
string program = @"
public class C {
static T F<T>() {
return default(T);
}
void M() {
$F<int?>()$;
}
}";
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsFalse(rr.IsError);
}
[Test]
public void MethodCanBeInvokedWithNullableTypeArgument2() {
string program = @"
public class C {
static T F<T>(T t) {
return default(T);
}
void M() {
$F((int?)null)$;
}
}";
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsFalse(rr.IsError);
}
[Test]
public void MethodCanBeInvokedWithNullableTypeArgument3() {
string program = @"
public class C {
static T F<T, U>() where T : U {
return default(T);
}
void M() {
$F<int?, object>()$;
}
}";
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsFalse(rr.IsError);
}
[Test]
public void MethodWithStructContraintCanBeInvokedWithValueType() {
string program = @"
public class C {
static T F<T>() where T : struct {
return default(T);
}
void M() {
$F<int>()$;
}
}";
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsFalse(rr.IsError);
}
[Test]
public void MethodWithStructContraintCannotBeInvokedWithNullableValueType() {
string program = @"
public class C {
static T F<T>() where T : struct {
return default(T);
}
void M() {
$F<int?>()$;
}
}";
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsTrue(rr.IsError);
Assert.AreEqual(OverloadResolutionErrors.MethodConstraintsNotSatisfied, rr.OverloadResolutionErrors);
}
[Test]
public void CanConstructGenericTypeWithNullableTypeArgument() {
string program = @"
public class X<T> {}
public class C {
void M() {
$new X<int?>()$;
}
}";
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsFalse(rr.IsError);
}
[Test]
public void OverloadResolutionIsAmbiguousEvenIfNotDelegateCompatible() {
string program = @"
class Test {
static void M(Func<int> o) {}
static void M(Action o) {}
static int K() { return 0; }
static void Main() {
$M(K)$;
}
}";
// K is only delegate-compatible with one of the overloads; yet we get an invalid match.
// This is because the conversion exists even though it is invalid.
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.AreEqual(OverloadResolutionErrors.AmbiguousMatch, rr.OverloadResolutionErrors);
}
[Test]
public void IndexerWithMoreSpecificParameterTypesIsPreferred()
{
string program = @"
class A {
public static void Test(B<object> b) {
x = $b[4]$;
}
}
public class B<T> {
public string this[T key] {
get { return ""1""; }
}
public int this[object key] {
get { return 2; }
}
}";
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsFalse(rr.IsError);
Assert.AreEqual("System.Int32", rr.Member.ReturnType.FullName);
}
[Test]
public void MethodWithMoreSpecificParameterTypesIsPreferred()
{
string program = @"
class A {
public static void Test(B<object> b) {
$b.M(4)$;
}
}
public class B<T> {
public string M(T key) {
return ""1"";
}
public int M(object key) {
return 2;
}
}";
var rr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsFalse(rr.IsError);
Assert.AreEqual("System.Int32", rr.Member.ReturnType.FullName);
}
}
}
|