-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from yantrajs/feature/disposable
Feature/disposable
- Loading branch information
Showing
28 changed files
with
723 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using Microsoft.Threading; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using YantraJS.Core.Clr; | ||
using YantraJS.Utils; | ||
|
||
namespace YantraJS.Core.Tests.ClrObjects | ||
{ | ||
[TestClass] | ||
public class DisposableTests | ||
{ | ||
class DisposableFile : IDisposable | ||
{ | ||
public bool Open = true; | ||
|
||
public string Value = ""; | ||
|
||
[JSExport("add")] | ||
public void Add(string text) | ||
{ | ||
this.Value += text + "\r\n"; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
this.Open = false; | ||
} | ||
} | ||
|
||
class AsyncDisposableFile : IAsyncDisposable | ||
{ | ||
public bool Open = true; | ||
|
||
public string Value = ""; | ||
|
||
[JSExport("add")] | ||
public void Add(string text) | ||
{ | ||
this.Value += text + "\r\n"; | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
await Task.Delay(10); | ||
this.Open = false; | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void SyncDispose() | ||
{ | ||
var c = new JSTestContext(); | ||
|
||
c["DisposableFile"] = ClrType.From(typeof(DisposableFile)); | ||
|
||
|
||
var a = c.Eval(@" | ||
function use(d) { | ||
using f = d; | ||
f.add('a'); | ||
} | ||
var a = new DisposableFile(); | ||
use(a); | ||
return a; | ||
"); | ||
|
||
a.ConvertTo<DisposableFile>(out var d); | ||
Assert.IsFalse(d.Open); | ||
} | ||
|
||
[TestMethod] | ||
public void AsyncDispose() | ||
{ | ||
AsyncPump.Run(async () => | ||
{ | ||
var c = new JSTestContext(); | ||
c["AsyncDisposableFile"] = ClrType.From(typeof(AsyncDisposableFile)); | ||
var a = await c.ExecuteAsync(@" | ||
(async function() { | ||
async function use(d) { | ||
await using f = d; | ||
f.add('a'); | ||
} | ||
var a = new AsyncDisposableFile(); | ||
await use(a); | ||
return a; | ||
})(); | ||
"); | ||
a.ConvertTo<AsyncDisposableFile>(out var d); | ||
Assert.IsFalse(d.Open); | ||
}); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
YantraJS.Core.Tests/Generator/Files/es/2023/disposables/async-disposable.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
function sleep(n) { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, n); | ||
}); | ||
} | ||
|
||
class File { | ||
|
||
lines = []; | ||
|
||
constructor() { | ||
this.open = true; | ||
} | ||
|
||
add(line) { | ||
this.lines.push(line); | ||
} | ||
|
||
async disposeAsync() { | ||
await sleep(1); | ||
this.open = false; | ||
} | ||
|
||
[Symbol.asyncDispose]() { | ||
return this.disposeAsync(); | ||
} | ||
} | ||
|
||
async function runTest(f) { | ||
await using file = f; | ||
f.add("1"); | ||
f.add("2"); | ||
} | ||
|
||
var f1 = new File(); | ||
assert.strictEqual(true, f1.open); | ||
await runTest(f1); | ||
assert.strictEqual(false, f1.open); | ||
assert.strictEqual("1,2", f1.lines.toString()); | ||
|
||
|
||
class CorruptFile extends File { | ||
[Symbol.asyncDispose]() { | ||
throw new Error("File corrupt"); | ||
} | ||
} | ||
|
||
var f2 = new CorruptFile(); | ||
assert.strictEqual(true, f2.open); | ||
try { | ||
await runTest(f2); | ||
} catch (error) { | ||
console.log(error); | ||
console.log(Object.getPrototypeOf(error).constructor.name); | ||
assert.strictEqual(true, error instanceof SuppressedError); | ||
} |
45 changes: 45 additions & 0 deletions
45
YantraJS.Core.Tests/Generator/Files/es/2023/disposables/disposable.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
class File { | ||
|
||
lines = []; | ||
|
||
constructor() { | ||
this.open = true; | ||
} | ||
|
||
add(line) { | ||
this.lines.push(line); | ||
} | ||
|
||
[Symbol.dispose]() { | ||
this.open = false; | ||
} | ||
} | ||
|
||
function runTest(f) { | ||
using file = f; | ||
f.add("1"); | ||
f.add("2"); | ||
} | ||
|
||
var f1 = new File(); | ||
assert.strictEqual(true, f1.open); | ||
runTest(f1); | ||
assert.strictEqual(false, f1.open); | ||
assert.strictEqual("1,2", f1.lines.toString()); | ||
|
||
|
||
class CorruptFile extends File { | ||
[Symbol.dispose]() { | ||
throw new Error("File corrupt"); | ||
} | ||
} | ||
|
||
var f2 = new CorruptFile(); | ||
assert.strictEqual(true, f2.open); | ||
try { | ||
runTest(f2); | ||
} catch (error) { | ||
console.log(error); | ||
console.log(Object.getPrototypeOf(error).constructor.name); | ||
assert.strictEqual(true, error instanceof SuppressedError); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.