Skip to content

Commit

Permalink
use diff
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jul 17, 2024
1 parent 663aa0b commit 6a349c3
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 12 deletions.
2 changes: 1 addition & 1 deletion assembly/__tests__/sleep.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("Should sleep", () => {
test("1s", () => {
const start = Date.now();
sleep(1000);
expect(Date.now() - start).toBeGreaterOrEqualTo(1000);
expect(Date.now() - start).toBeGreaterOrEqualTo(12345);
});
test("5s", () => {
const start = Date.now();
Expand Down
2 changes: 1 addition & 1 deletion assembly/src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ export class Log {
this.text = text;
}
display(): void {
term.write(" ".repeat(this.depth + 1) + `${rainbow.bgBlackBright(" LOG ")}${rainbow.dimMk(":")} ${this.text}\n`);
term.write(" ".repeat(this.depth + 1) + `${rainbow.bgBlackBright(" LOG ")}${rainbow.dimMk(": " + this.text)}\n`);
}
}
6 changes: 3 additions & 3 deletions assembly/src/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ export class Suite {
}
}

if (!suiteNone || this.tests.length) {
if (this.verdict == "fail") {
suiteLn.edit(`${suiteDepth}${rainbow.bgRed(" FAIL ")} ${rainbow.dimMk(this.description)}\n`);
} else if (!suiteNone || this.tests.length) {
this.verdict = "ok";
suiteLn.edit(`${suiteDepth}${rainbow.bgGreenBright(" PASS ")} ${rainbow.dimMk(this.description)}\n`);
} else if (this.verdict == "fail") {
suiteLn.edit(`${suiteDepth}${rainbow.bgRed(" FAIL ")} ${rainbow.dimMk(this.description)}\n`);
} else {
suiteLn.edit(`${suiteDepth}${rainbow.bgBlackBright(" EMPTY ")} ${rainbow.dimMk(this.description)}\n`);
}
Expand Down
38 changes: 35 additions & 3 deletions bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { glob } from "glob";
import { formatTime, getExec, loadConfig } from "./util.js";
import * as path from "path";
import { existsSync, mkdirSync, writeFileSync } from "fs";
import { diff } from "typer-diff";
const CONFIG_PATH = path.join(process.cwd(), "./as-test.config.json");
const ansi = new RegExp("[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))", "g");
export async function run() {
Expand Down Expand Up @@ -68,14 +69,45 @@ export async function run() {
for (const failed of reporter.failed) {
console.log(`${chalk.bgRed(" FAIL ")} ${chalk.dim(failed.description)}\n`);
for (const test of failed.tests) {
const diffResult = diff(JSON.stringify(test._left), JSON.stringify(test._right));
let expected = chalk.dim(JSON.stringify(test._left));
let received = "";
for (const res of diffResult.diff) {
switch (res.type) {
case "correct": {
received += chalk.dim(res.value);
continue;
}
case "extra": {
received += chalk.red.strikethrough(res.value);
continue;
}
case "missing": {
received += chalk.bgBlack(res.value);
continue;
}
case "wrong": {
received += chalk.bgRed(res.value);
continue;
}
case "untouched": {
//received += chalk.bgBlackBright(res.value);
continue;
}
case "spacer": {
//received += chalk.bgBlackBright(res.value);
continue;
}
}
}
if (test.verdict == "fail") {
console.log(`${chalk.dim("(expected) ->")} ${chalk.bold(test._left.toString())}`);
console.log(`${chalk.dim("(received) ->")} ${chalk.bold(test._right.toString())}\n`);
console.log(`${chalk.dim("(expected) ->")} ${expected}`);
console.log(`${chalk.dim("(received) ->")} ${received}\n`);
}
}
}
}
console.log("----------------- [RESULTS] ------------------\n");
console.log(chalk.dim("----------------- [RESULTS] ------------------\n"));
process.stdout.write(chalk.bold("Files: "));
if (reporter.failedFiles) {
process.stdout.write(chalk.bold.red(reporter.failedFiles + " failed"));
Expand Down
38 changes: 35 additions & 3 deletions cli/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { glob } from "glob";
import { formatTime, getExec, loadConfig } from "./util.js";
import * as path from "path";
import { appendFileSync, existsSync, fstat, mkdirSync, writeFileSync } from "fs";
import { diff } from "typer-diff";

const CONFIG_PATH = path.join(process.cwd(), "./as-test.config.json");

Expand Down Expand Up @@ -82,15 +83,46 @@ export async function run() {
for (const failed of reporter.failed) {
console.log(`${chalk.bgRed(" FAIL ")} ${chalk.dim(failed.description)}\n`);
for (const test of failed.tests) {
const diffResult = diff(JSON.stringify(test._left), JSON.stringify(test._right));
let expected = chalk.dim(JSON.stringify(test._left));
let received = "";
for (const res of diffResult.diff) {
switch (res.type) {
case "correct": {
received += chalk.dim(res.value);
continue;
}
case "extra": {
received += chalk.red.strikethrough(res.value);
continue;
}
case "missing": {
received += chalk.bgBlack(res.value);
continue;
}
case "wrong": {
received += chalk.bgRed(res.value);
continue;
}
case "untouched": {
//received += chalk.bgBlackBright(res.value);
continue;
}
case "spacer": {
//received += chalk.bgBlackBright(res.value);
continue;
}
}
}
if (test.verdict == "fail") {
console.log(`${chalk.dim("(expected) ->")} ${chalk.bold(test._left.toString())}`);
console.log(`${chalk.dim("(received) ->")} ${chalk.bold(test._right.toString())}\n`);
console.log(`${chalk.dim("(expected) ->")} ${expected}`);
console.log(`${chalk.dim("(received) ->")} ${received}\n`);
}
}
}
}

console.log("----------------- [RESULTS] ------------------\n");
console.log(chalk.dim("----------------- [RESULTS] ------------------\n"));

process.stdout.write(chalk.bold("Files: "));
if (reporter.failedFiles) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"chalk": "^5.3.0",
"glob": "^11.0.0",
"jest": "^29.7.0",
"json-as": "^0.9.14"
"json-as": "^0.9.14",
"typer-diff": "^1.1.1"
},
"overrides": {
"assemblyscript": "$assemblyscript",
Expand Down

0 comments on commit 6a349c3

Please sign in to comment.