2
0

MethodAmbiguityTests.cs 4.9 KB

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