1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using ManagedTests.DynamicCSharp.Conformance.dynamic.context.method.regmethod.genclass.genclassregmeth.genclassregmeth; 6 using Xunit; 7 8 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.method.regmethod.genclass.genclassregmeth.genclassregmeth 9 { 10 public class C 11 { 12 } 13 14 public interface I 15 { 16 } 17 18 public class MyClass 19 { 20 public int Field = 0; 21 } 22 23 public struct MyStruct 24 { 25 public int Number; 26 } 27 28 public enum MyEnum 29 { 30 First = 1, 31 Second = 2, 32 Third = 3 33 } 34 35 public class MemberClass<T> 36 { 37 #region Instance methods Method_ReturnBool()38 public bool Method_ReturnBool() 39 { 40 return false; 41 } 42 Method_ReturnsT()43 public T Method_ReturnsT() 44 { 45 return default(T); 46 } 47 Method_ReturnsT(T t)48 public T Method_ReturnsT(T t) 49 { 50 return t; 51 } 52 Method_ReturnsT(out T t)53 public T Method_ReturnsT(out T t) 54 { 55 t = default(T); 56 return t; 57 } 58 Method_ReturnsT(ref T t, T tt)59 public T Method_ReturnsT(ref T t, T tt) 60 { 61 t = default(T); 62 return t; 63 } 64 Method_ReturnsT(params T[] t)65 public T Method_ReturnsT(params T[] t) 66 { 67 return default(T); 68 } 69 Method_ReturnsT(float x, T t)70 public T Method_ReturnsT(float x, T t) 71 { 72 return default(T); 73 } 74 Method_ReturnsInt(T t)75 public int Method_ReturnsInt(T t) 76 { 77 return 1; 78 } 79 Method_ReturnsFloat(T t, dynamic d)80 public float Method_ReturnsFloat(T t, dynamic d) 81 { 82 return 3.4f; 83 } 84 Method_ReturnsFloat(T t, dynamic d, ref decimal dec)85 public float Method_ReturnsFloat(T t, dynamic d, ref decimal dec) 86 { 87 dec = 3m; 88 return 3.4f; 89 } 90 Method_ReturnsDynamic(T t)91 public dynamic Method_ReturnsDynamic(T t) 92 { 93 return t; 94 } 95 Method_ReturnsDynamic(T t, dynamic d)96 public dynamic Method_ReturnsDynamic(T t, dynamic d) 97 { 98 return t; 99 } 100 Method_ReturnsDynamic(T t, int x, dynamic d)101 public dynamic Method_ReturnsDynamic(T t, int x, dynamic d) 102 { 103 return t; 104 } 105 106 // Multiple params 107 // Constraints 108 // Nesting 109 #endregion 110 #region Static methods StaticMethod_ReturnBoolNullable()111 public static bool? StaticMethod_ReturnBoolNullable() 112 { 113 return (bool?)false; 114 } 115 #endregion 116 } 117 118 public class MemberClassMultipleParams<T, U, V> 119 { Method_ReturnsT(V v, U u)120 public T Method_ReturnsT(V v, U u) 121 { 122 return default(T); 123 } 124 } 125 126 public class MemberClassWithClassConstraint<T> 127 where T : class 128 { Method_ReturnsInt()129 public int Method_ReturnsInt() 130 { 131 return 1; 132 } 133 Method_ReturnsT(decimal dec, dynamic d)134 public T Method_ReturnsT(decimal dec, dynamic d) 135 { 136 return null; 137 } 138 } 139 140 public class MemberClassWithNewConstraint<T> 141 where T : new() 142 { Method_ReturnsT()143 public T Method_ReturnsT() 144 { 145 return new T(); 146 } 147 Method_ReturnsDynamic(T t)148 public dynamic Method_ReturnsDynamic(T t) 149 { 150 return new T(); 151 } 152 } 153 154 public class MemberClassWithAnotherTypeConstraint<T, U> 155 where T : U 156 { Method_ReturnsU(dynamic d)157 public U Method_ReturnsU(dynamic d) 158 { 159 return default(U); 160 } 161 Method_ReturnsDynamic(int x, U u, dynamic d)162 public dynamic Method_ReturnsDynamic(int x, U u, dynamic d) 163 { 164 return default(T); 165 } 166 } 167 168 #region Negative tests - you should not be able to construct this with a dynamic object 169 public class MemberClassWithUDClassConstraint<T> 170 where T : C, new() 171 { Method_ReturnsC()172 public C Method_ReturnsC() 173 { 174 return new T(); 175 } 176 } 177 178 public class MemberClassWithStructConstraint<T> 179 where T : struct 180 { Method_ReturnsDynamic(int x)181 public dynamic Method_ReturnsDynamic(int x) 182 { 183 return x; 184 } 185 } 186 187 public class MemberClassWithInterfaceConstraint<T> 188 where T : I 189 { Method_ReturnsDynamic(int x, T v)190 public dynamic Method_ReturnsDynamic(int x, T v) 191 { 192 return default(T); 193 } 194 } 195 #endregion 196 } 197 198 199 200 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.method.regmethod.genclass.genclass001.genclass001 201 { 202 // <Title> Tests generic class regular method used in regular method body.</Title> 203 // <Description> 204 // </Description> 205 // <RelatedBugs></RelatedBugs> 206 //<Expects Status=success></Expects> 207 // <Code> 208 209 public class Test 210 { 211 [Fact] DynamicCSharpRunTest()212 public static void DynamicCSharpRunTest() 213 { 214 Assert.Equal(0, MainMethod()); 215 } 216 MainMethod()217 public static int MainMethod() 218 { 219 Test t = new Test(); 220 if (t.TestMethod() == true) 221 return 1; 222 return 0; 223 } 224 TestMethod()225 public bool TestMethod() 226 { 227 dynamic mc = new MemberClass<string>(); 228 return (bool)mc.Method_ReturnBool(); 229 } 230 } 231 //</Code> 232 } 233 234 235 236 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.method.regmethod.genclass.genclass002.genclass002 237 { 238 // <Title> Tests generic class regular method used in arguments of method invocation.</Title> 239 // <Description> 240 // </Description> 241 // <RelatedBugs></RelatedBugs> 242 //<Expects Status=success></Expects> 243 // <Code> 244 245 public class Test 246 { 247 [Fact] DynamicCSharpRunTest()248 public static void DynamicCSharpRunTest() 249 { 250 Assert.Equal(0, MainMethod()); 251 } 252 MainMethod()253 public static int MainMethod() 254 { 255 Test t = new Test(); 256 dynamic mc1 = new MemberClass<int>(); 257 int result1 = t.TestMethod((int)mc1.Method_ReturnsT()); 258 dynamic mc2 = new MemberClass<string>(); 259 int result2 = Test.TestMethod((string)mc2.Method_ReturnsT()); 260 if (result1 == 0 && result2 == 0) 261 return 0; 262 else 263 return 1; 264 } 265 TestMethod(int i)266 public int TestMethod(int i) 267 { 268 if (i == default(int)) 269 { 270 return 0; 271 } 272 else 273 { 274 return 1; 275 } 276 } 277 TestMethod(string s)278 public static int TestMethod(string s) 279 { 280 if (s == default(string)) 281 { 282 return 0; 283 } 284 else 285 { 286 return 1; 287 } 288 } 289 } 290 //</Code> 291 } 292 293 294 295 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.method.regmethod.genclass.genclass003.genclass003 296 { 297 // <Title> Tests generic class regular method used in static variable.</Title> 298 // <Description> 299 // </Description> 300 // <RelatedBugs></RelatedBugs> 301 //<Expects Status=success></Expects> 302 // <Code> 303 304 public class Test 305 { 306 private static dynamic s_mc = new MemberClass<string>(); 307 private static float s_loc = (float)s_mc.Method_ReturnsFloat(null, 1); 308 [Fact] DynamicCSharpRunTest()309 public static void DynamicCSharpRunTest() 310 { 311 Assert.Equal(0, MainMethod()); 312 } 313 MainMethod()314 public static int MainMethod() 315 { 316 if (s_loc != 3.4f) 317 return 1; 318 return 0; 319 } 320 } 321 //</Code> 322 } 323 324 325 326 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.method.regmethod.genclass.genclass005.genclass005 327 { 328 // <Title> Tests generic class regular method used in unsafe.</Title> 329 // <Description> 330 // </Description> 331 // <RelatedBugs></RelatedBugs> 332 //<Expects Status=success></Expects> 333 // <Code> 334 //using System; 335 //[TestClass]public class Test 336 //{ 337 //[Test][Priority(Priority.Priority1)]public void DynamicCSharpRunTest(){Assert.AreEqual(0, MainMethod());} public static unsafe int MainMethod() 338 //{ 339 //dynamic dy = new MemberClass<int>(); 340 //int value = 1; 341 //int* valuePtr = &value; 342 //int result = dy.Method_ReturnsT(out *valuePtr); 343 //if (result == 0 && value == 0) 344 //return 0; 345 //return 1; 346 //} 347 //} 348 //</Code> 349 } 350 351 352 353 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.method.regmethod.genclass.genclass006.genclass006 354 { 355 // <Title> Tests generic class regular method used in generic method body.</Title> 356 // <Description> 357 // </Description> 358 // <RelatedBugs></RelatedBugs> 359 //<Expects Status=success></Expects> 360 // <Code> 361 362 public class Test 363 { 364 DynamicCSharpRunTest()365 public static void DynamicCSharpRunTest() 366 { 367 Assert.Equal(0, MainMethod()); 368 } 369 MainMethod()370 public static int MainMethod() 371 { 372 Test t = new Test(); 373 string s1 = ""; 374 string s2 = ""; 375 string result = t.TestMethod<string>(ref s1, s2); 376 if (result == null && s1 == null) 377 return 0; 378 return 1; 379 } 380 TestMethod(ref T t1, T t2)381 public T TestMethod<T>(ref T t1, T t2) 382 { 383 dynamic mc = new MemberClass<T>(); 384 return (T)mc.Method_ReturnsT(ref t1, t2); 385 } 386 } 387 //</Code> 388 } 389 390 391 392 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.method.regmethod.genclass.genclass008.genclass008 393 { 394 // <Title> Tests generic class regular method used in extension method body.</Title> 395 // <Description> 396 // </Description> 397 // <RelatedBugs></RelatedBugs> 398 //<Expects Status=success></Expects> 399 // <Code> 400 401 public class Test 402 { 403 public int Field = 10; 404 [Fact] DynamicCSharpRunTest()405 public static void DynamicCSharpRunTest() 406 { 407 Assert.Equal(0, MainMethod()); 408 } 409 MainMethod()410 public static int MainMethod() 411 { 412 Test t = 1.ExReturnTest(); 413 if (t.Field == 1) 414 return 0; 415 return 1; 416 } 417 } 418 419 public static class Extension 420 { ExReturnTest(this int t)421 public static Test ExReturnTest(this int t) 422 { 423 dynamic dy = new MemberClass<int>(); 424 float x = 3.3f; 425 int i = -1; 426 return new Test() 427 { 428 Field = t + (int)dy.Method_ReturnsT(x, i) 429 } 430 431 ; 432 } 433 } 434 //</Code> 435 } 436 437 438 439 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.method.regmethod.genclass.genclass009.genclass009 440 { 441 // <Title> Tests generic class regular method used in for loop.</Title> 442 // <Description> 443 // </Description> 444 // <RelatedBugs></RelatedBugs> 445 //<Expects Status=success></Expects> 446 // <Code> 447 448 public class Test 449 { 450 [Fact] DynamicCSharpRunTest()451 public static void DynamicCSharpRunTest() 452 { 453 Assert.Equal(0, MainMethod()); 454 } 455 MainMethod()456 public static int MainMethod() 457 { 458 dynamic dy = new MemberClass<MyEnum>(); 459 int result = 0; 460 for (int i = (int)dy.Method_ReturnsInt(MyEnum.Third); i < 10; i++) 461 { 462 result += (int)dy.Method_ReturnsInt((MyEnum)(i % 3 + 1)); 463 } 464 465 if (result == 9) 466 return 0; 467 return 1; 468 } 469 } //</Code> 470 } 471 472 473 474 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.method.regmethod.genclass.genclass009a.genclass009a 475 { 476 // <Title> Tests generic class regular method used in for loop.</Title> 477 // <Description> 478 // </Description> 479 // <RelatedBugs></RelatedBugs> 480 //<Expects Status=success></Expects> 481 // <Code> 482 483 public class Test 484 { 485 [Fact] DynamicCSharpRunTest()486 public static void DynamicCSharpRunTest() 487 { 488 Assert.Equal(0, MainMethod()); 489 } 490 MainMethod()491 public static int MainMethod() 492 { 493 dynamic dy = new MemberClass<MyEnum>(); 494 int result = 0; 495 for (int i = dy.Method_ReturnsInt(MyEnum.Third); i < 10; i++) 496 { 497 result += dy.Method_ReturnsInt((MyEnum)(i % 3 + 1)); 498 } 499 500 if (result == 9) 501 return 0; 502 return 1; 503 } 504 } //</Code> 505 } 506 507 508 509 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.method.regmethod.genclass.genclass010.genclass010 510 { 511 // <Title> Tests generic class regular method used in static constructor.</Title> 512 // <Description> 513 // </Description> 514 // <RelatedBugs></RelatedBugs> 515 //<Expects Status=success></Expects> 516 // <Code> 517 518 public class Test : I 519 { 520 private static decimal s_dec = 0M; 521 private static float s_result = 0f; Test()522 static Test() 523 { 524 dynamic dy = new MemberClass<I>(); 525 s_result = (float)dy.Method_ReturnsFloat(new Test(), dy, ref s_dec); 526 } 527 528 DynamicCSharpRunTest()529 public static void DynamicCSharpRunTest() 530 { 531 Assert.Equal(0, MainMethod()); 532 } 533 MainMethod()534 public static int MainMethod() 535 { 536 if (Test.s_dec == 3M && Test.s_result == 3.4f) 537 return 0; 538 return 1; 539 } 540 } //</Code> 541 } 542 543 544 545 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.method.regmethod.genclass.genclass012.genclass012 546 { 547 // <Title> Tests generic class regular method used in lambda.</Title> 548 // <Description> 549 // </Description> 550 // <RelatedBugs></RelatedBugs> 551 //<Expects Status=success></Expects> 552 // <Code> 553 using System; 554 555 public class Test 556 { 557 [Fact] DynamicCSharpRunTest()558 public static void DynamicCSharpRunTest() 559 { 560 Assert.Equal(0, MainMethod()); 561 } 562 MainMethod()563 public static int MainMethod() 564 { 565 dynamic dy = new MemberClass<string>(); 566 dynamic d1 = 3; 567 dynamic d2 = "Test"; 568 Func<string, string, string> fun = (x, y) => (y + x.ToString()); 569 string result = fun((string)dy.Method_ReturnsDynamic("foo", d1), (string)dy.Method_ReturnsDynamic("bar", d2)); 570 if (result == "barfoo") 571 return 0; 572 return 1; 573 } 574 } //</Code> 575 } 576 577 578 579 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.method.regmethod.genclass.genclass013.genclass013 580 { 581 // <Title> Tests generic class regular method used in array initializer list.</Title> 582 // <Description> 583 // </Description> 584 // <RelatedBugs></RelatedBugs> 585 //<Expects Status=success></Expects> 586 // <Code> 587 588 public class Test 589 { 590 [Fact] DynamicCSharpRunTest()591 public static void DynamicCSharpRunTest() 592 { 593 Assert.Equal(0, MainMethod()); 594 } 595 MainMethod()596 public static int MainMethod() 597 { 598 dynamic dy = new MemberClass<string>(); 599 string[] array = new string[] 600 { 601 (string)dy.Method_ReturnsDynamic(string.Empty, 1, dy), (string)dy.Method_ReturnsDynamic(null, 0, dy), (string)dy.Method_ReturnsDynamic("a", -1, dy)} 602 603 ; 604 if (array.Length == 3 && array[0] == string.Empty && array[1] == null && array[2] == "a") 605 return 0; 606 return 1; 607 } 608 } //</Code> 609 } 610 611 612 613 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.method.regmethod.genclass.genclass014.genclass014 614 { 615 // <Title> Tests generic class regular method used in null coalescing operator.</Title> 616 // <Description> 617 // </Description> 618 // <RelatedBugs></RelatedBugs> 619 //<Expects Status=success></Expects> 620 // <Code> 621 622 public class Test 623 { 624 [Fact] DynamicCSharpRunTest()625 public static void DynamicCSharpRunTest() 626 { 627 Assert.Equal(0, MainMethod()); 628 } 629 MainMethod()630 public static int MainMethod() 631 { 632 dynamic dy = MemberClass<string>.StaticMethod_ReturnBoolNullable(); 633 if (!((bool?)dy ?? false)) 634 return 0; 635 return 1; 636 } 637 } //</Code> 638 } 639 640 641 642 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.method.regmethod.genclass.genclass015.genclass015 643 { 644 // <Title> Tests generic class regular method used in static method body.</Title> 645 // <Description> 646 // </Description> 647 // <RelatedBugs></RelatedBugs> 648 //<Expects Status=success></Expects> 649 // <Code> 650 651 public class Test 652 { 653 [Fact] DynamicCSharpRunTest()654 public static void DynamicCSharpRunTest() 655 { 656 Assert.Equal(0, MainMethod()); 657 } 658 MainMethod()659 public static int MainMethod() 660 { 661 dynamic dy = new MemberClassMultipleParams<MyEnum, MyStruct, MyClass>(); 662 MyEnum me = dy.Method_ReturnsT(null, new MyStruct()); 663 return (int)me; 664 } 665 } //</Code> 666 } 667 668 669 670 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.method.regmethod.genclass.genclass016.genclass016 671 { 672 // <Title> Tests generic class regular method used in query expression.</Title> 673 // <Description> 674 // </Description> 675 // <RelatedBugs></RelatedBugs> 676 //<Expects Status=success></Expects> 677 // <Code> 678 using System.Linq; 679 using System.Collections.Generic; 680 681 public class Test 682 { 683 private class M 684 { 685 public int Field1; 686 public int Field2; 687 } 688 689 [Fact] DynamicCSharpRunTest()690 public static void DynamicCSharpRunTest() 691 { 692 Assert.Equal(0, MainMethod()); 693 } 694 MainMethod()695 public static int MainMethod() 696 { 697 dynamic dy = new MemberClassWithClassConstraint<MyClass>(); 698 var list = new List<M>() 699 { 700 new M() 701 { 702 Field1 = 1, Field2 = 2 703 } 704 705 , new M() 706 { 707 Field1 = 2, Field2 = 1 708 } 709 } 710 711 ; 712 return list.Any(p => p.Field1 == (int)dy.Method_ReturnsInt()) ? 0 : 1; 713 } 714 } //</Code> 715 } 716 717 718 719 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.method.regmethod.genclass.genclass017.genclass017 720 { 721 // <Title> Tests generic class regular method used in ternary operator expression.</Title> 722 // <Description> 723 // </Description> 724 // <RelatedBugs></RelatedBugs> 725 //<Expects Status=success></Expects> 726 // <Code> 727 728 public class Test 729 { 730 [Fact] DynamicCSharpRunTest()731 public static void DynamicCSharpRunTest() 732 { 733 Assert.Equal(0, MainMethod()); 734 } 735 MainMethod()736 public static int MainMethod() 737 { 738 dynamic dy = new MemberClassWithClassConstraint<string>(); 739 string result = (string)dy.Method_ReturnsT(decimal.MaxValue, dy) == null ? string.Empty : (string)dy.Method_ReturnsT(decimal.MaxValue, dy); 740 return result == string.Empty ? 0 : 1; 741 } 742 } //</Code> 743 } 744 745 746 747 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.method.regmethod.genclass.genclass018.genclass018 748 { 749 // <Title> Tests generic class regular method used in lock expression.</Title> 750 // <Description> 751 // </Description> 752 // <RelatedBugs></RelatedBugs> 753 //<Expects Status=success></Expects> 754 // <Code> 755 756 public class Test 757 { 758 [Fact] DynamicCSharpRunTest()759 public static void DynamicCSharpRunTest() 760 { 761 Assert.Equal(0, MainMethod()); 762 } 763 MainMethod()764 public static int MainMethod() 765 { 766 dynamic dy = new MemberClassWithNewConstraint<C>(); 767 int result = 1; 768 lock (dy.Method_ReturnsT()) 769 { 770 result = 0; 771 } 772 773 return result; 774 } 775 } //</Code> 776 } 777 778 779 780 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.method.regmethod.genclass.genclass019.genclass019 781 { 782 // <Title> Tests generic class regular method used in switch section statement list.</Title> 783 // <Description> 784 // </Description> 785 // <RelatedBugs></RelatedBugs> 786 //<Expects Status=success></Expects> 787 // <Code> 788 789 public class Test 790 { 791 [Fact] DynamicCSharpRunTest()792 public static void DynamicCSharpRunTest() 793 { 794 Assert.Equal(0, MainMethod()); 795 } 796 MainMethod()797 public static int MainMethod() 798 { 799 dynamic dy1 = new MemberClassWithNewConstraint<C>(); 800 dynamic dy2 = new MemberClassWithAnotherTypeConstraint<int, int>(); 801 C result = new C(); 802 int result2 = -1; 803 switch ((int)dy2.Method_ReturnsU(dy1)) // 0 804 { 805 case 0: 806 result = (C)dy1.Method_ReturnsDynamic(result); // called 807 break; 808 default: 809 result2 = (int)dy2.Method_ReturnsDynamic(0, 0, dy2); // not called 810 break; 811 } 812 813 return (result.GetType() == typeof(C) && result2 == -1) ? 0 : 1; 814 } 815 } //</Code> 816 } 817 818 819 820 namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.method.regmethod.genclass.implicit01.implicit01 821 { 822 // <Title> Tests generic class regular method used in regular method body.</Title> 823 // <RelatedBugs></RelatedBugs> 824 //<Expects Status=success></Expects> 825 // <Code> 826 using System; 827 using System.Collections.Generic; 828 829 public class Ta 830 { 831 public static int i = 2; 832 } 833 834 public class A<T> 835 { operator A<List<string>>(A<T> x)836 public static implicit operator A<List<string>>(A<T> x) 837 { 838 return new A<List<string>>(); 839 } 840 } 841 842 public class B 843 { Foo(A<List<T>> x, string y)844 public static void Foo<T>(A<List<T>> x, string y) 845 { 846 Ta.i--; 847 } 848 Foo(object x, string y)849 public static void Foo<T>(object x, string y) 850 { 851 Ta.i++; 852 } 853 854 [Fact] DynamicCSharpRunTest()855 public static void DynamicCSharpRunTest() 856 { 857 Assert.Equal(0, MainMethod()); 858 } 859 MainMethod()860 public static int MainMethod() 861 { 862 var x = new A<Action<object>>(); 863 Foo<string>(x, ""); 864 Foo<string>(x, (dynamic)""); 865 return Ta.i; 866 } 867 } 868 } 869