Skip to content

Commit

Permalink
terminal features
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jul 16, 2024
1 parent 8ff4ed1 commit a5cd7c9
Show file tree
Hide file tree
Showing 24 changed files with 562 additions and 1,127 deletions.
4 changes: 1 addition & 3 deletions assembly/__tests__/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ describe("Array manipulation", () => {
expect(myArray).toContain(2);
});

it("should be empty", () => {
expect("ag").toBe("asd")
});
it("should be empty", () => {});
});

run({
Expand Down
66 changes: 40 additions & 26 deletions assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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.
Expand All @@ -18,6 +19,8 @@ export namespace Verdict {

let entrySuites: Suite[] = [];

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

Expand Down Expand Up @@ -222,19 +225,19 @@ export function afterEach(callback: () => void): void {
export function mockFn<returnType>(
fn: string,
callback: (...args: any[]) => returnType,
): void {}
): void { }

/**
* Unmock all references to an existing function to instead point to the original function
* @param {string} fn - name of function to override
*/
export function unmockFn(fn: string): void {}
export function unmockFn(fn: string): void { }

/**
* Re-mock all references to an existing function to instead point to the declared function
* @param {string} fn - name of function to override
*/
export function remockFn(fn: string): void {}
export function remockFn(fn: string): void { }

/**
* Class defining options that can be passed to the `run` function.
Expand Down Expand Up @@ -270,10 +273,12 @@ class RunOptions {
*/
export function run(options: RunOptions = new RunOptions()): void {
__test_options = options;
const report = new Report();
report.time.start = performance.now();
term.write("\n");
const time = new Time();
const fileLn = term.write(`${rainbow.bgCyanBright(" FILE ")} ${rainbow.dimMk(FILE)}\n`);
term.write("\n");
time.start = performance.now();
for (let i = 0; i < entrySuites.length; i++) {
const suiteReport = new SuiteReport();
// @ts-ignore
const suite = unchecked(entrySuites[i]);
suites = [suite];
Expand All @@ -282,36 +287,45 @@ export function run(options: RunOptions = new RunOptions()): void {
depth = -1;
current_suite = null;

suiteReport.time.start = performance.now();
const suiteLn = term.write(` ${rainbow.bgBlackBright(" ... ")} ${rainbow.dimMk(suite.description)}\n`);
term.write("\n");
suite.run();
suiteReport.time.end = performance.now();

suites = [];
depth = -1;
current_suite = null;

suiteReport.kind = suite.kind;
suiteReport.verdict = suite.verdict;
let suiteNone = true;
for (let ii = 0; ii < suite.suites.length; ii++) {
const _suite = unchecked(suite.suites[ii]);
if (_suite.verdict == Verdict.Fail) report.verdict = Verdict.Fail;
suiteReport.suites.push(SuiteReport.wrap(_suite));
if (_suite.verdict == Verdict.Fail) {
suite.verdict = Verdict.Fail;
suiteNone = false;
} else if (_suite.verdict == Verdict.Ok) {
suiteNone = false;
}
}

for (let iii = 0; iii < suite.tests.length; iii++) {
const test = unchecked(suite.tests[iii]);
if (test.verdict == Verdict.Fail) report.verdict = Verdict.Fail;
suiteReport.tests.push(TestReport.wrap(test));
const _test = unchecked(suite.tests[iii]);
if (_test.verdict == Verdict.Fail) {
suite.verdict = Verdict.Fail;
}
}

if (!suiteNone && suite.tests.length) {
suite.verdict = Verdict.Ok;
}

if (suite.verdict == Verdict.Ok) {
suiteLn.edit(` ${rainbow.bgGreenBright(" PASS ")} ${rainbow.dimMk(suite.description)} ${rainbow.dimMk(suite.time.format())}\n`);
}
suiteReport.description = suite.description;
report.groups.push(suiteReport);
}
report.time.end = performance.now();
if (report.verdict === Verdict.None) {
if (report.groups.length) report.verdict = Verdict.Ok;
}
// @ts-ignore
report.file = isDefined(ENTRY_FILE) ? ENTRY_FILE : "unknown";
console.log(
"--REPORT-START--\n" + JSON.stringify(report) + "\n--REPORT-END--",
);
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);
}
13 changes: 10 additions & 3 deletions assembly/src/suite.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Verdict } from "..";
import { rainbow } from "as-rainbow";
import { getDepth, Verdict } from "..";
import { Time } from "../../reporters/report";
import { Expectation } from "./expectation";
import { Tests } from "./tests";
import { term } from "../util/term";

export type SuiteKind = string;
export namespace SuiteKind {
Expand All @@ -16,6 +18,7 @@ export class Suite {
public depth: i32 = 0;
public suites: Suite[] = [];
public tests: Tests[] = [];
public logs: string[] = [];
public kind: SuiteKind;

public verdict: Verdict = Verdict.None;
Expand All @@ -39,14 +42,17 @@ export class Suite {
current_suite = this;
// @ts-ignore
depth++;
this.time.start = performance.now();
const suiteDepth = getDepth();
const suiteLn = term.write(`${suiteDepth}${rainbow.bgBlackBright(" ... ")} ${rainbow.dimMk(this.description)}\n`);
term.write("\n");
this.callback();
this.time.end = performance.now();
// @ts-ignore
depth--;
for (let i = 0; i < this.suites.length; i++) {
const suite = unchecked(this.suites[i]);
suite.time.start = performance.now();
suite.run();
suite.time.end = performance.now();
if (suite.verdict === Verdict.Fail) {
this.verdict = Verdict.Fail;
break;
Expand All @@ -63,5 +69,6 @@ export class Suite {
if (this.tests.length) this.verdict = Verdict.Ok;
if (this.suites.length) this.verdict = Verdict.Ok;
}
suiteLn.edit(`${suiteDepth}${rainbow.bgGreenBright(" PASS ")} ${rainbow.dimMk(this.description)} ${rainbow.dimMk(this.time.format())}\n`);
}
}
40 changes: 40 additions & 0 deletions assembly/util/term.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export class Log {
private line: i32 = 0;
constructor(line: i32 = 0) {
this.line = line;
}
edit(data: string): Log {
term.clearLn(this.line);
process.stdout.write(data);
process.stdout.write("\x1B[999B");
process.stdout.write("\x1B[0G");
return new Log(this.line);
}
}

export namespace term {
export let lines: i32 = 0;
export function write(data: string): Log {
process.stdout.write(data);
return new Log(term.lines++);
}
export function writeLn(data: string): void {
for (let i = 0; i < data.length; i++) {
const code = data.charCodeAt(i);
if (code === 10) term.lines++;
}
term.lines++;
process.stdout.write(data + "\n");
}
export function clearLn(line: i32): void {
process.stdout.write(`\u001B[${term.lines - line}A`);
process.stdout.write("\x1B[2K");
process.stdout.write("\x1B[0G");
}
export function editLn(data: string, ln: i32): void {
process.stdout.write(`\u001B[${ln}A`);
process.stdout.write("\x1B[2K");
process.stdout.write("\x1B[0G");
term.writeLn(data);
}
}
135 changes: 0 additions & 135 deletions bin/cli/about.js

This file was deleted.

Loading

0 comments on commit a5cd7c9

Please sign in to comment.