MethodAmbiguityTests.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using Jint.Native;
  2. using System;
  3. using Xunit;
  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 int TestMethod(double a, string b, double c) => 0;
  32. public int TestMethod(double a, double b, double c) => 1;
  33. public int TestMethod(TestClass a, string b, double c) => 2;
  34. public int TestMethod(TestClass a, TestClass b, double c) => 3;
  35. public int TestMethod(TestClass a, TestClass b, TestClass c) => 4;
  36. public int TestMethod(TestClass a, double b, string c) => 5;
  37. public int TestMethod(ChildTestClass a, double b, string c) => 6;
  38. public int TestMethod(ChildTestClass a, string b, JsValue c) => 7;
  39. public static implicit operator TestClass(double i) => new TestClass();
  40. public static implicit operator double(TestClass tc) => 0;
  41. public static explicit operator string(TestClass tc) => "";
  42. }
  43. public class ChildTestClass : TestClass { }
  44. [Fact]
  45. public void BestMatchingMethodShouldBeCalled()
  46. {
  47. RunTest(@"
  48. var tc = new TestClass();
  49. var cc = new ChildTestClass();
  50. equal(0, tc.TestMethod(0, '', 0));
  51. equal(1, tc.TestMethod(0, 0, 0));
  52. equal(2, tc.TestMethod(tc, '', 0));
  53. equal(3, tc.TestMethod(tc, tc, 0));
  54. equal(4, tc.TestMethod(tc, tc, tc));
  55. equal(5, tc.TestMethod(tc, tc, ''));
  56. equal(5, tc.TestMethod(0, 0, ''));
  57. equal(6, tc.TestMethod(cc, 0, ''));
  58. equal(1, tc.TestMethod(cc, 0, 0));
  59. equal(6, tc.TestMethod(cc, cc, ''));
  60. equal(6, tc.TestMethod(cc, 0, tc));
  61. equal(7, tc.TestMethod(cc, '', {}));
  62. ");
  63. }
  64. }
  65. }