InteropDisposeTests.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. namespace Jint.Tests.Runtime;
  2. public class InteropDisposeTests
  3. {
  4. private readonly Engine _engine;
  5. private readonly SyncDisposable _syncDisposable = new();
  6. #if NETCOREAPP
  7. private readonly AsyncDisposable _asyncDisposable = new();
  8. #endif
  9. public InteropDisposeTests()
  10. {
  11. _engine = new Engine();
  12. _engine.SetValue("getSync", () => _syncDisposable);
  13. #if NETCOREAPP
  14. _engine.SetValue("getAsync", () => _asyncDisposable);
  15. #endif
  16. }
  17. [Theory]
  18. [InlineData("{ using temp = getSync(); }")]
  19. [InlineData("(function x() { using temp = getSync(); })()")]
  20. [InlineData("class X { constructor() { using temp = getSync(); } } new X();")]
  21. [InlineData("class X { static { using temp = getSync(); } } new X();")]
  22. [InlineData("for (let i = 0; i < 1; i++) { using temp = getSync(); }")]
  23. public void ShouldSyncDispose(string program)
  24. {
  25. _engine.Execute(program);
  26. _syncDisposable.Disposed.Should().BeTrue();
  27. }
  28. private class SyncDisposable : IDisposable
  29. {
  30. public bool Disposed { get; private set; }
  31. public void Dispose()
  32. {
  33. Disposed = true;
  34. }
  35. }
  36. #if NETCOREAPP
  37. [Theory]
  38. [InlineData("(async function x() { await using temp = getAsync(); })();")]
  39. public void ShouldAsyncDispose(string program)
  40. {
  41. _engine.Evaluate(program).UnwrapIfPromise();
  42. _asyncDisposable.Disposed.Should().BeTrue();
  43. }
  44. private class AsyncDisposable : IAsyncDisposable
  45. {
  46. public bool Disposed { get; private set; }
  47. public ValueTask DisposeAsync()
  48. {
  49. Disposed = true;
  50. return default;
  51. }
  52. }
  53. #endif
  54. }