Skip to content

Commit

Permalink
refactor: add reason argument to assert import (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko authored May 3, 2024
1 parent 6b9c317 commit 532295c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/basic/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ declare module "main" {

declare module "xtp:test" {
interface harness {
assert(name: PTR, value: I64);
assert(name: PTR, value: I64, reason: PTR);
call(func: PTR, input: PTR): PTR;
time(func: PTR, input: PTR): I64;
group(name: PTR);
Expand Down
Binary file modified examples/basic/test.wasm
Binary file not shown.
17 changes: 12 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,25 @@ export class Test {
return Test.call(funcName, input).readBuffer();
}

static assert(msg: string, value: boolean) {
static assert(name: string, value: boolean, reason: string) {
// @ts-ignore: Memory
const a = Memory.fromString(msg);
assert(a.offset, !!value);
const a = Memory.fromString(name);
// @ts-ignore: Memory
const b = Memory.fromString(reason);
assert(a.offset, !!value, b.offset);
a.free();
b.free();
}

static assertEqual(msg: string, x: unknown, y: unknown) {
Test.assert(msg, x === y);
const stack = new Error().stack;
stack.trim();
Test.assert(msg, x === y, `Expected ${x} === ${y}\n${stack}`);
}

static assertNotEqual(msg: string, x: unknown, y: unknown) {
Test.assert(msg, x !== y);
const stack = new Error().stack;
stack.trim();
Test.assert(msg, x !== y, `Expected ${x} !== ${y}\n${stack}`);
}
}
2 changes: 1 addition & 1 deletion test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ declare module "main" {

declare module "xtp:test" {
interface harness {
assert(name: PTR, value: I64);
assert(name: PTR, value: I64, reason: PTR);
call(func: PTR, input: PTR): PTR;
time(func: PTR, input: PTR): I64;
group(name: PTR);
Expand Down

0 comments on commit 532295c

Please sign in to comment.