MethodAmbiguityTests.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using Jint.Native;
  2. using System.Dynamic;
  3. using Jint.Runtime.Interop;
  4. namespace Jint.Tests.Runtime;
  5. public class MethodAmbiguityTests : IDisposable
  6. {
  7. private readonly Engine _engine;
  8. public MethodAmbiguityTests()
  9. {
  10. _engine = new Engine(cfg => cfg
  11. .AllowOperatorOverloading())
  12. .SetValue("log", new Action<object>(Console.WriteLine))
  13. .SetValue("throws", new Func<Action, Exception>(Assert.Throws<Exception>))
  14. .SetValue("assert", new Action<bool>(Assert.True))
  15. .SetValue("assertFalse", new Action<bool>(Assert.False))
  16. .SetValue("equal", new Action<object, object>(Assert.Equal))
  17. .SetValue("TestClass", typeof(TestClass))
  18. .SetValue("ChildTestClass", typeof(ChildTestClass))
  19. ;
  20. }
  21. void IDisposable.Dispose()
  22. {
  23. }
  24. private void RunTest(string source)
  25. {
  26. _engine.Execute(source);
  27. }
  28. public class TestClass
  29. {
  30. public string this[int index] => "int";
  31. public string this[string index] => "string";
  32. public int TestMethod(double a, string b, double c) => 0;
  33. public int TestMethod(double a, double b, double c) => 1;
  34. public int TestMethod(TestClass a, string b, double c) => 2;
  35. public int TestMethod(TestClass a, TestClass b, double c) => 3;
  36. public int TestMethod(TestClass a, TestClass b, TestClass c) => 4;
  37. public int TestMethod(TestClass a, double b, string c) => 5;
  38. public int TestMethod(ChildTestClass a, double b, string c) => 6;
  39. public int TestMethod(ChildTestClass a, string b, JsValue c) => 7;
  40. public static implicit operator TestClass(double i) => new TestClass();
  41. public static implicit operator double(TestClass tc) => 0;
  42. public static explicit operator string(TestClass tc) => "";
  43. }
  44. public class ChildTestClass : TestClass { }
  45. [Fact]
  46. public void BestMatchingMethodShouldBeCalled()
  47. {
  48. RunTest(@"
  49. var tc = new TestClass();
  50. var cc = new ChildTestClass();
  51. equal(0, tc.TestMethod(0, '', 0));
  52. equal(1, tc.TestMethod(0, 0, 0));
  53. equal(2, tc.TestMethod(tc, '', 0));
  54. equal(3, tc.TestMethod(tc, tc, 0));
  55. equal(4, tc.TestMethod(tc, tc, tc));
  56. equal(5, tc.TestMethod(tc, tc, ''));
  57. equal(5, tc.TestMethod(0, 0, ''));
  58. equal(6, tc.TestMethod(cc, 0, ''));
  59. equal(1, tc.TestMethod(cc, 0, 0));
  60. equal(6, tc.TestMethod(cc, cc, ''));
  61. equal(6, tc.TestMethod(cc, 0, tc));
  62. equal(7, tc.TestMethod(cc, '', {}));
  63. ");
  64. }
  65. [Fact]
  66. public void IndexerCachesMethodsCorrectly()
  67. {
  68. RunTest(@"
  69. var tc = new TestClass();
  70. equal('string:int', tc['Whistler'] + ':' + tc[10]);
  71. equal('int:string', tc[10] + ':' + tc['Whistler']);
  72. ");
  73. }
  74. [Fact]
  75. public void ShouldFavorOtherOverloadsOverObjectParameter()
  76. {
  77. var engine = new Engine(cfg => cfg.AllowOperatorOverloading());
  78. engine.SetValue("Class1", TypeReference.CreateTypeReference<Class1>(engine));
  79. engine.SetValue("Class2", TypeReference.CreateTypeReference<Class2>(engine));
  80. Assert.Equal("Class1.Double[]", engine.Evaluate("Class1.Print([ 1, 2 ]);"));
  81. Assert.Equal("Class1.ExpandoObject", engine.Evaluate("Class1.Print({ x: 1, y: 2 });"));
  82. Assert.Equal("Class1.Int32", engine.Evaluate("Class1.Print(5);"));
  83. Assert.Equal("Class2.Double[]", engine.Evaluate("Class2.Print([ 1, 2 ]); "));
  84. Assert.Equal("Class2.ExpandoObject", engine.Evaluate("Class2.Print({ x: 1, y: 2 });"));
  85. Assert.Equal("Class2.Int32", engine.Evaluate("Class2.Print(5);"));
  86. Assert.Equal("Class2.Object", engine.Evaluate("Class2.Print(() => '');"));
  87. }
  88. [Fact]
  89. public void ShouldMatchCorrectConstructors()
  90. {
  91. Engine engine = new Engine();
  92. engine.SetValue("MyClass", TypeReference.CreateTypeReference(engine, typeof(Class3)));
  93. engine.Execute(@"
  94. const a = new MyClass();
  95. a.Print(1); // Works
  96. a.Print([1, 2]); // Works
  97. const b = new MyClass(1); // Works
  98. const c = new MyClass([1, 2]); // Throws 'an object must implement IConvertible' exception
  99. ");
  100. }
  101. private struct Class1
  102. {
  103. public static string Print(ExpandoObject eo) => nameof(Class1) + "." + nameof(ExpandoObject);
  104. public static string Print(double[] a) => nameof(Class1) + "." + nameof(Double) + "[]";
  105. public static string Print(int i) => nameof(Class1) + "." + nameof(Int32);
  106. }
  107. private struct Class2
  108. {
  109. public static string Print(ExpandoObject eo) => nameof(Class2) + "." + nameof(ExpandoObject);
  110. public static string Print(double[] a) => nameof(Class2) + "." + nameof(Double) + "[]";
  111. public static string Print(int i) => nameof(Class2) + "." + nameof(Int32);
  112. public static string Print(object o) => nameof(Class2) + "." + nameof(Object);
  113. }
  114. public class Class3
  115. {
  116. public Class3() { }
  117. public Class3(int a) => Console.WriteLine("Class1(int a): " + a);
  118. public Class3(object a) => Console.WriteLine("Class1(object a): " + a);
  119. public void Print(int a) => Console.WriteLine("Print(int a): " + a);
  120. public void Print(object a) => Console.WriteLine("Print(object a): " + a);
  121. }
  122. }