summaryrefslogtreecommitdiff
path: root/cli/tests/unit/opcall_test.ts
blob: fb269fc3239359e34b16cead0663843f1f77e02d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { assertEquals } from "../../../test_util/std/testing/asserts.ts";
import { assert, assertStringIncludes, unreachable } from "./test_util.ts";

Deno.test(async function sendAsyncStackTrace() {
  try {
    await core.ops.op_error_async();
    unreachable();
  } catch (error) {
    assert(error instanceof Error);
    const s = error.stack?.toString();
    assert(s);
    assertStringIncludes(s, "opcall_test.ts");
    assertStringIncludes(s, "sendAsyncStackTrace");
    assert(
      !s.includes("ext:core"),
      "opcall stack traces should NOT include ext:core internals such as unwrapOpResult",
    );
  }
});

Deno.test(async function sendAsyncStackTraceDeferred() {
  try {
    await core.ops.op_error_async_deferred();
    unreachable();
  } catch (error) {
    assert(error instanceof Error);
    const s = error.stack?.toString();
    assert(s);
    assertStringIncludes(s, "opcall_test.ts");
    assertStringIncludes(s, "sendAsyncStackTraceDeferred");
    assert(
      !s.includes("ext:core"),
      "opcall stack traces should NOT include ext:core internals such as unwrapOpResult",
    );
  }
});

Deno.test(function syncAdd() {
  assertEquals(30, core.ops.op_add(10, 20));
});

Deno.test(async function asyncAdd() {
  assertEquals(30, await core.ops.op_add_async(10, 20));
});

// @ts-ignore This is not publicly typed namespace, but it's there for sure.
const core = Deno[Deno.internal].core;

Deno.test(async function opsAsyncBadResource() {
  try {
    const nonExistingRid = 9999;
    await core.read(
      nonExistingRid,
      new Uint8Array(0),
    );
  } catch (e) {
    if (!(e instanceof Deno.errors.BadResource)) {
      throw e;
    }
  }
});

Deno.test(function opsSyncBadResource() {
  try {
    const nonExistingRid = 9999;
    core.ops.op_read_sync(
      nonExistingRid,
      new Uint8Array(0),
    );
  } catch (e) {
    if (!(e instanceof Deno.errors.BadResource)) {
      throw e;
    }
  }
});