Skip to content

Commit

Permalink
push
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jul 17, 2024
1 parent 12c24f2 commit 208aebe
Show file tree
Hide file tree
Showing 18 changed files with 940 additions and 717 deletions.
127 changes: 70 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ You'll also need to install `visitor-as`

`npm i visitor-as --save-dev`

## Templates

I provide two templates for reference

[WASI](https://github.com/JairusSW/as-test/tree/template/wasi)

[Node/Bun/Deno](https://github.com/JairusSW/as-test/tree/template/node-bun-deno)

View the docs: https://docs.jairus.dev/as-test

## Usage
Expand All @@ -39,79 +31,100 @@ Note: You can use either `ast` or `as-test` in the terminal.

Next, create a test file

`assembly/__tests__/test.spec.ts`
`assembly/__tests__/example.spec.ts`

```js
import {
describe,
expect,
test,
beforeAll,
afterAll,
beforeEach,
afterEach,
mockFn,
log,
run
describe,
expect,
test,
beforeAll,
afterAll,
mockFn,
log,
run,
it
} from "as-test";

beforeAll(() => {
log("Setting up test environment...");
log("Setting up test environment...");
});

afterAll(() => {
log("Tearing down test environment...");
log("Tearing down test environment...");
});

// Mock/override the function console.log
mockFn<void>("console.log", (data: string): void => {
console.log("[MOCKED]: " + data + "\n");
console.log("[MOCKED]: " + data + "\\n");
});

describe("Should sleep", () => {
test("1ms", () => {
const start = Date.now();
sleep(1);
expect(Date.now() - start).toBeGreaterOrEqualTo(1);
});
test("10ms", () => {
const start = Date.now();
sleep(10);
expect(Date.now() - start).toBeGreaterOrEqualTo(10);
});
test("1s", () => {
const start = Date.now();
sleep(1000);
expect(Date.now() - start).toBeGreaterOrEqualTo(1000);
});
test("5s", () => {
const start = Date.now();
log("Sleeping...");
sleep(5000);
log("Done!");
expect(Date.now() - start).toBeGreaterOrEqualTo(5000);
});
});

describe("Math operations", () => {
beforeEach(() => {
log("Initializing test...");
});

afterEach(() => {
log("Cleaning up after test...");
});

test("Addition", () => {
expect(1 + 2).toBe(3);
});

test("Comparison", () => {
expect(5).toBeGreaterThan(3);
expect(2).toBeLessThan(4);
});

test("Type checking", () => {
expect("hello").toBeString();
expect(true).toBeBoolean();
expect(10.5).toBeNumber();
});
test("Addition", () => {
expect(1 + 2).toBe(3);
});

test("Subtraction", () => {
expect(1 - 2).toBe(-1);
});

test("Comparison", () => {
expect(5).toBeGreaterThan(3);
expect(2).toBeLessThan(4);
});

test("Type checking", () => {
expect("hello").toBeString();
expect(true).toBeBoolean();
expect(10.5).toBeNumber();
});
});

let myArray: i32[] = [];
let myArray: i32[] = [1, 2, 3];

describe("Array manipulation", () => {
beforeAll(() => {
myArray = [1, 2, 3];
});
test("Array length", () => {
expect(myArray).toHaveLength(3);
});

test("Array length", () => {
expect(myArray).toHaveLength(3);
});
test("Array inclusion", () => {
expect(myArray).toContain(2);
});

test("Array inclusion", () => {
expect(myArray).toContain(2);
});
it("should be empty", () => { });
});

run({
log: true
});
run();

function sleep(ms: i64): void {
const target = Date.now() + ms;
while (target > Date.now()) { }
}
```

Build and run it using as-test
Expand Down
6 changes: 4 additions & 2 deletions as-test.config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"input": ["./assembly/__tests__/*.spec.ts"],
"input": [
"./assembly/__tests__/*.spec.ts"
],
"outDir": "./build",
"logs": "./logs",
"config": "none",
Expand All @@ -16,4 +18,4 @@
"run": "wasmtime <file>"
}
}
}
}
29 changes: 0 additions & 29 deletions asconfig.json

This file was deleted.

27 changes: 20 additions & 7 deletions assembly/util/term.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// @ts-ignore
@external("env", "process.stdout.write")
declare function process_stdout_write(data: string): void;

export class TermLine {
public start: i32 = 0;
public end: i32 = 0;
Expand All @@ -10,7 +14,7 @@ export class TermLine {
while (--end >= this.start) {
term.clearLn(end);
}
process.stdout.write(data);
writeRaw(data);
term.resetCursor();
return new TermLine(this.end);
}
Expand All @@ -27,16 +31,25 @@ export namespace term {
const code = data.charCodeAt(i);
if (code === 10) term.lines++;
}
process.stdout.write(data);
writeRaw(data);
return new TermLine(start, term.lines);
}
export function clearLn(line: i32): void {
process.stdout.write(`\u001B[${term.lines - line}A`);
process.stdout.write("\x1B[2K");
process.stdout.write("\x1B[0G");
writeRaw(`\u001B[${term.lines - line}A`);
writeRaw("\x1B[2K");
writeRaw("\x1B[0G");
}
export function resetCursor(): void {
process.stdout.write("\x1B[999B");
process.stdout.write("\x1B[0G");
writeRaw("\x1B[999B");
writeRaw("\x1B[0G");
}
}

export function writeRaw(data: string): void {
// @ts-ignore
if (isDefined(ASC_WASI)) {
process.stdout.write(data);
} else {
process_stdout_write(data);
}
}
Loading

0 comments on commit 208aebe

Please sign in to comment.