MethodAmbiguityTests.cs 5.8 KB

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