Skip to content

Commit

Permalink
plugins, reporters, invisible message passing, refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jul 17, 2024
1 parent a5cd7c9 commit c82c916
Show file tree
Hide file tree
Showing 23 changed files with 504 additions and 542 deletions.
1 change: 1 addition & 0 deletions as-test.config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"input": ["./assembly/__tests__/*.spec.ts"],
"outDir": "./build",
"logs": "./logs",
"config": "none",
"plugins": {
"coverage": true
Expand Down
134 changes: 84 additions & 50 deletions assembly/index.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
import { rainbow } from "as-rainbow";
import { Suite, SuiteKind } from "./src/suite";
import { Suite } from "./src/suite";
import { Expectation } from "./src/expectation";
import { stringify } from "as-console/stringify";
import { __COVER, __HASHES, __POINTS } from "as-test/assembly/coverage";
import { JSON } from "json-as";
import { Report, SuiteReport, TestReport, Time } from "../reporters/report";
import { term } from "./util/term";

/**
* Enumeration representing the verdict of a test case.
*/
export type Verdict = string;
export namespace Verdict {
export const None = "none";
export const Ok = "ok";
export const Fail = "fail";
}
import { term, TermLine } from "./util/term";
import { Log } from "./src/log";

let entrySuites: Suite[] = [];

// @ts-ignore
const FILE = isDefined(ENTRY_FILE) ? ENTRY_FILE : "unknown";
// Globals
// @ts-ignore
@global let suites: Suite[] = [];

// @ts-ignore
@global let depth: i32 = -1;

// @ts-ignore
@global let current_suite: Suite | null = null;

// @ts-ignore
let before_all_callback: (() => void) | null = null;
// @ts-ignore
let after_all_callback: (() => void) | null = null;

export let before_each_callback: (() => void) | null = null;
Expand All @@ -50,7 +42,7 @@ let __test_options!: RunOptions;
* ```
*/
export function describe(description: string, callback: () => void): void {
const suite = new Suite(description, callback, SuiteKind.Describe);
const suite = new Suite(description, callback, "describe");

if (depth >= 0) {
const _suite = suites[depth];
Expand All @@ -61,6 +53,7 @@ export function describe(description: string, callback: () => void): void {
suites.push(suite);
}
} else {
suite.file = FILE;
entrySuites.push(suite);
suites.push(suite);
}
Expand All @@ -81,7 +74,7 @@ export function describe(description: string, callback: () => void): void {
* ```
*/
export function test(description: string, callback: () => void): void {
const suite = new Suite(description, callback, SuiteKind.Test);
const suite = new Suite(description, callback, "test");

if (depth >= 0) {
const _suite = suites[depth];
Expand All @@ -92,6 +85,7 @@ export function test(description: string, callback: () => void): void {
suites.push(suite);
}
} else {
suite.file = FILE;
entrySuites.push(suite);
suites.push(suite);
}
Expand All @@ -112,7 +106,7 @@ export function test(description: string, callback: () => void): void {
* ```
*/
export function it(description: string, callback: () => void): void {
const suite = new Suite(description, callback, SuiteKind.It);
const suite = new Suite(description, callback, "it");

if (depth >= 0) {
const _suite = suites[depth];
Expand All @@ -123,6 +117,7 @@ export function it(description: string, callback: () => void): void {
suites.push(suite);
}
} else {
suite.file = FILE;
entrySuites.push(suite);
suites.push(suite);
}
Expand Down Expand Up @@ -175,9 +170,11 @@ export function log<T>(data: T): void {
const lines = formatted.split("\n");
for (let i = 0; i < lines.length; i++) {
const line = unchecked(lines[i]);
console.log(" " + rainbow.bgYellow(" LOG ") + " " + line);
if (current_suite) {
current_suite!.addLog(new Log(line));
}
}
console.log("");
term.write("\n");
}
}

Expand Down Expand Up @@ -272,8 +269,10 @@ class RunOptions {
* ```
*/
export function run(options: RunOptions = new RunOptions()): void {
// const buf = new ArrayBuffer(20);
// const bytes = process.stdin.read(buf);
// const stdinLn = term.write(String.UTF8.decodeUnsafe(changetype<usize>(buf), bytes) + "\n");
__test_options = options;
term.write("\n");
const time = new Time();
const fileLn = term.write(`${rainbow.bgCyanBright(" FILE ")} ${rainbow.dimMk(FILE)}\n`);
term.write("\n");
Expand All @@ -287,45 +286,80 @@ export function run(options: RunOptions = new RunOptions()): void {
depth = -1;
current_suite = null;

const suiteLn = term.write(` ${rainbow.bgBlackBright(" ... ")} ${rainbow.dimMk(suite.description)}\n`);
term.write("\n");
suite.run();

suites = [];
depth = -1;
current_suite = null;
}
time.end = performance.now();
fileLn.edit(`${rainbow.bgCyanBright(" FILE ")} ${rainbow.dimMk(FILE)} ${rainbow.dimMk(time.format())}`);
const reportText = "\x1B[8mSTART_READ" + JSON.stringify(entrySuites) + "END_READ\x1B[0m\n";
term.write(reportText).clear();
}

let suiteNone = true;
for (let ii = 0; ii < suite.suites.length; ii++) {
const _suite = unchecked(suite.suites[ii]);
if (_suite.verdict == Verdict.Fail) {
suite.verdict = Verdict.Fail;
suiteNone = false;
} else if (_suite.verdict == Verdict.Ok) {
suiteNone = false;
}
export class Result {
public name: string;
public arg1: i32;
public arg2: i32;
constructor(name: string, arg1: i32, arg2: i32) {
this.name = name;
this.arg1 = arg1;
this.arg2 = arg2;
}
display(): string {
let out = "";
out += `${rainbow.boldMk(this.name)} `;
if (this.arg1) {
out += `${rainbow.boldMk(rainbow.red(this.arg1.toString() + " " + "failed"))}`;
} else {
out += `${rainbow.boldMk(rainbow.green("0 failed"))}`;
}
out += ` ${this.arg1 + this.arg2} total\n`;
return out;
}
serialize(): string {
return JSON.stringify(this);
}
}

for (let iii = 0; iii < suite.tests.length; iii++) {
const _test = unchecked(suite.tests[iii]);
if (_test.verdict == Verdict.Fail) {
suite.verdict = Verdict.Fail;
}
}
@json
export class Time {
start: f64 = 0;
end: f64 = 0;
format(): string {
return formatTime(this.end - this.start);
}
}

if (!suiteNone && suite.tests.length) {
suite.verdict = Verdict.Ok;
}
class Unit {
name: string;
divisor: number;
}

if (suite.verdict == Verdict.Ok) {
suiteLn.edit(` ${rainbow.bgGreenBright(" PASS ")} ${rainbow.dimMk(suite.description)} ${rainbow.dimMk(suite.time.format())}\n`);
function formatTime(time: f64): string {
if (time < 0) return "0.00μs";

const us = time * 1000;

const units: Unit[] = [
{ name: "μs", divisor: 1 },
{ name: "ms", divisor: 1000 },
{ name: "s", divisor: 1000 * 1000 },
{ name: "m", divisor: 60 * 1000 * 1000 },
{ name: "h", divisor: 60 * 60 * 1000 * 1000 },
{ name: "d", divisor: 24 * 60 * 60 * 1000 * 1000 },
];

for (let i = units.length - 1; i >= 0; i--) {
const unit = units[i];
if (us >= unit.divisor) {
const value = (Math.round((us / unit.divisor) * 100) / 100).toString();
return `${value}${unit.name}`;
}
}
time.end = performance.now();
fileLn.edit(`${rainbow.bgCyanBright(" FILE ")} ${rainbow.dimMk(FILE)} ${rainbow.dimMk(time.format())}`);
}

export function getDepth(): string {
if (depth < 0) return "";
return " ".repeat(depth);
const _us = (Math.round(us * 100) / 100).toString();

return `${_us}μs`;
}
30 changes: 0 additions & 30 deletions assembly/reporters/tap.ts

This file was deleted.

Loading

0 comments on commit c82c916

Please sign in to comment.