InteropInterfaceExtendTests.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Jint.Runtime;
  2. namespace Jint.Tests.Runtime;
  3. public class InterfaceTests
  4. {
  5. public interface I0
  6. {
  7. string NameI0 { get; }
  8. string OverloadSuperMethod();
  9. string SubPropertySuperMethod();
  10. }
  11. public interface I1 : I0
  12. {
  13. string Name { get; }
  14. string OverloadSuperMethod(int x);
  15. new string SubPropertySuperMethod { get; }
  16. }
  17. public class Super
  18. {
  19. public string Name { get; } = "Super";
  20. }
  21. public class CI1 : Super, I1
  22. {
  23. public new string Name { get; } = "CI1";
  24. public string NameI0 { get; } = "I0.Name";
  25. string I1.Name { get; } = "CI1 as I1";
  26. string I1.SubPropertySuperMethod { get; } = "I1.SubPropertySuperMethod";
  27. public string OverloadSuperMethod()
  28. {
  29. return "I0.OverloadSuperMethod()";
  30. }
  31. public string OverloadSuperMethod(int x)
  32. {
  33. return $"I1.OverloadSuperMethod(int {x})";
  34. }
  35. public string SubPropertySuperMethod()
  36. {
  37. return "I0.SubPropertySuperMethod()";
  38. }
  39. }
  40. public class Indexer<T>
  41. {
  42. private readonly T t;
  43. public Indexer(T t)
  44. {
  45. this.t = t;
  46. }
  47. public T this[int index]
  48. {
  49. get { return t; }
  50. }
  51. }
  52. public class InterfaceHolder
  53. {
  54. public InterfaceHolder()
  55. {
  56. var ci1 = new CI1();
  57. this.ci1 = ci1;
  58. this.i1 = ci1;
  59. this.super = ci1;
  60. this.IndexerCI1 = new Indexer<CI1>(ci1);
  61. this.IndexerI1 = new Indexer<I1>(ci1);
  62. this.IndexerSuper = new Indexer<Super>(ci1);
  63. }
  64. public readonly CI1 ci1;
  65. public readonly I1 i1;
  66. public readonly Super super;
  67. public CI1 CI1 { get => ci1; }
  68. public I1 I1 { get => i1; }
  69. public Super Super { get => super; }
  70. public CI1 GetCI1() => ci1;
  71. public I1 GetI1() => i1;
  72. public Super GetSuper() => super;
  73. public Indexer<CI1> IndexerCI1 { get; }
  74. public Indexer<I1> IndexerI1 { get; }
  75. public Indexer<Super> IndexerSuper { get; }
  76. }
  77. private readonly Engine _engine;
  78. private readonly InterfaceHolder holder;
  79. public InterfaceTests()
  80. {
  81. holder = new InterfaceHolder();
  82. _engine = new Engine(cfg => cfg.AllowClr(
  83. typeof(CI1).Assembly,
  84. typeof(Console).Assembly,
  85. typeof(File).Assembly))
  86. .SetValue("log", new Action<object>(Console.WriteLine))
  87. .SetValue("assert", new Action<bool>(Assert.True))
  88. .SetValue("equal", new Action<object, object>(Assert.Equal))
  89. .SetValue("holder", holder)
  90. ;
  91. }
  92. [Fact]
  93. public void CallSuperPropertyFromInterface()
  94. {
  95. Assert.Equal(holder.I1.NameI0, _engine.Evaluate("holder.I1.NameI0"));
  96. }
  97. [Fact]
  98. public void CallOverloadSuperMethod()
  99. {
  100. Assert.Equal(
  101. holder.I1.OverloadSuperMethod(1),
  102. _engine.Evaluate("holder.I1.OverloadSuperMethod(1)"));
  103. Assert.Equal(
  104. holder.I1.OverloadSuperMethod(),
  105. _engine.Evaluate("holder.I1.OverloadSuperMethod()"));
  106. }
  107. [Fact]
  108. public void CallSubPropertySuperMethod_SubProperty()
  109. {
  110. Assert.Equal(
  111. holder.I1.SubPropertySuperMethod,
  112. _engine.Evaluate("holder.I1.SubPropertySuperMethod"));
  113. }
  114. [Fact]
  115. public void CallSubPropertySuperMethod_SuperMethod()
  116. {
  117. var ex = Assert.Throws<JavaScriptException>(() =>
  118. {
  119. Assert.Equal(
  120. holder.I1.SubPropertySuperMethod(),
  121. _engine.Evaluate("holder.I1.SubPropertySuperMethod()"));
  122. });
  123. Assert.Equal("Property 'SubPropertySuperMethod' of object is not a function", ex.Message);
  124. }
  125. }