Skip to content

Commit

Permalink
fix: globals
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jun 24, 2024
1 parent 8ee0125 commit 212e162
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 94 deletions.
31 changes: 31 additions & 0 deletions assembly/coverage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export class CoverPoint {
public file: string = "";
public hash: string = "";
public line: i32 = 0;
public column: i32 = 0;
public type!: string;
public executed: boolean = false;
}

export class Coverage {
public hashes: Map<string, CoverPoint> = new Map<string, CoverPoint>();
public points: i32 = 0;
static SN: Coverage = new Coverage();
}

export function __REGISTER(point: CoverPoint): void {
Coverage.SN.points++;
Coverage.SN.hashes.set(point.hash, point);
}

export function __COVER(hash: string): void {
if (Coverage.SN.hashes.has(hash)) Coverage.SN.hashes.delete(hash);
}

export function __HASHES(): Map<string, CoverPoint> {
return Coverage.SN.hashes;
}

export function __POINTS(): i32 {
return Coverage.SN.points;
}
26 changes: 10 additions & 16 deletions assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TestGroup } from "./src/group";
import { Expectation } from "./src/expectation";
import { formatTime } from "./util";
import { stringify } from "as-console/assembly";
import { __COVERAGE } from "./src/coverage";
import { __COVER, __HASHES, __POINTS } from "as-test/assembly/coverage";

/**
* Enumeration representing the verdict of a test case.
Expand All @@ -21,12 +21,9 @@ let groups: TestGroup[] = [];
let before_all_callback: (() => void) | null = null;
let after_all_callback: (() => void) | null = null;

// @ts-ignore
@global let before_each_callback: (() => void) | null = null;
// @ts-ignore
@global let after_each_callback: (() => void) | null = null;
// @ts-ignore
@global let __test_options!: RunOptions;
export let before_each_callback: (() => void) | null = null;
export let after_each_callback: (() => void) | null = null;
let __test_options!: RunOptions;

/**
* Creates a test group containing multiple test cases.
Expand Down Expand Up @@ -252,7 +249,7 @@ export function run(options: RunOptions = new RunOptions()): void {
console.log(txt);
}

const report = suite.report();
const report = suite.getLogs();
if (report) {
if (report.passed) console.log(report.passed!);
if (report.failed) failed_suite_logs += report.failed!;
Expand All @@ -266,14 +263,10 @@ export function run(options: RunOptions = new RunOptions()): void {
console.log(failed_suite_logs);
}

// @ts-ignore
const cv = __COVERAGE_STATS();
const COVER_POINTS = cv.hashs;
const COVER_TOTAL_POINTS = cv.points;

if (options.coverage && COVER_POINTS.size) {
if (options.coverage && __HASHES().size) {
console.log(rainbow.dimMk("----------------- [COVERAGE] -----------------\n"));
const points = COVER_POINTS.values();
const points = __HASHES().values();

for (let i = 0; i < points.length; i++) {
const point = unchecked(points[i]);
Expand All @@ -285,10 +278,11 @@ export function run(options: RunOptions = new RunOptions()): void {
console.log(rainbow.dimMk("----------------- [RESULTS] ------------------\n"));
const ms = performance.now() - start;
console.log(rainbow.boldMk("Test Suites: ") + (failed ? rainbow.boldMk(rainbow.red(failed.toString() + " failed")) : rainbow.boldMk(rainbow.green("0 failed"))) + ", " + suites.toString() + " total");
console.log(rainbow.boldMk("Tests: ") + (failed_tests ? rainbow.boldMk(rainbow.red(failed_tests.toString() + " failed")) : rainbow.boldMk(rainbow.green("0 failed"))) + ", " + COVER_TOTAL_POINTS.toString() + " total");
if (options.coverage) console.log(rainbow.boldMk("Coverage: ") + (COVER_POINTS.size ? rainbow.boldMk(rainbow.red(COVER_POINTS.size.toString() + " failed")) : rainbow.boldMk(rainbow.green("0 failed"))) + ", " + COVER_TOTAL_POINTS.toString() + " total");
console.log(rainbow.boldMk("Tests: ") + (failed_tests ? rainbow.boldMk(rainbow.red(failed_tests.toString() + " failed")) : rainbow.boldMk(rainbow.green("0 failed"))) + ", " + tests.toString() + " total");
if (options.coverage) console.log(rainbow.boldMk("Coverage: ") + (__HASHES().size ? rainbow.boldMk(rainbow.red(__HASHES().size.toString() + " failed")) : rainbow.boldMk(rainbow.green("0 failed"))) + ", " + __POINTS().toString() + " total");
console.log(rainbow.boldMk("Snapshots: ") + "0 total");
console.log(rainbow.boldMk("Time: ") + formatTime(ms));
__COVER("joe mom")
if (failed) {
process.exit(1);
}
Expand Down
39 changes: 0 additions & 39 deletions assembly/src/coverage.ts

This file was deleted.

2 changes: 1 addition & 1 deletion assembly/src/expectation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { rainbow } from "as-rainbow";
import { diff, visualize } from "../util";
import { Node } from "./node";
import { Verdict } from "..";
import { Verdict, after_each_callback, before_each_callback } from "..";

export class Expectation<T> extends Node {
public verdict: Verdict = Verdict.Unreachable;
Expand Down
2 changes: 1 addition & 1 deletion assembly/src/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class TestGroup {
this.results.push(test);
}

report(): ReportLogs | null {
getLogs(): ReportLogs {
let passed_logs = "";
let failed_logs = "";
for (let i = 0; i < this.results.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion assembly/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe("Array manipulation", () => {
});
});

export function foo(): void {
function foo(): void {

}

Expand Down
1 change: 0 additions & 1 deletion coverage.ts

This file was deleted.

30 changes: 13 additions & 17 deletions transform/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Transform } from "assemblyscript/dist/transform.js";
import { Source, Tokenizer, BlockStatement, ExpressionStatement, Node } from "assemblyscript/dist/assemblyscript.js";
import { Source, BlockStatement, ExpressionStatement, Node, Tokenizer } from "assemblyscript/dist/assemblyscript.js";
import { BaseVisitor, SimpleParser } from "visitor-as/dist/index.js";
import { isStdlib, toString } from "visitor-as/dist/utils.js";
import { isStdlib } from "visitor-as/dist/utils.js";
import { RangeTransform } from "visitor-as/dist/transformRange.js";
let ENABLED = false;
var CoverType;
Expand Down Expand Up @@ -51,7 +51,7 @@ class CoverageTransform extends BaseVisitor {
hash: "${point.hash}",
line: ${point.line},
column: ${point.column},
type: __COVERTYPES.Expression,
type: "Expression",
executed: false
});`);
replacer.visit(registerStmt);
Expand Down Expand Up @@ -91,7 +91,7 @@ class CoverageTransform extends BaseVisitor {
hash: "${point.hash}",
line: ${point.line},
column: ${point.column},
type: __COVERTYPES.Function,
type: "Function",
executed: false
})`);
replacer.visit(registerStmt);
Expand Down Expand Up @@ -129,7 +129,7 @@ class CoverageTransform extends BaseVisitor {
hash: "${point.hash}",
line: ${point.line},
column: ${point.column},
type: __COVERTYPES.Expression,
type: "Expression",
executed: false
})`);
replacer.visit(registerStmt);
Expand Down Expand Up @@ -167,7 +167,7 @@ class CoverageTransform extends BaseVisitor {
hash: "${point.hash}",
line: ${point.line},
column: ${point.column},
type: __COVERTYPES.Function,
type: "Function",
executed: false
})`);
replacer.visit(registerStmt);
Expand Down Expand Up @@ -227,7 +227,7 @@ class CoverageTransform extends BaseVisitor {
hash: "${point.hash}",
line: ${point.line},
column: ${point.column},
type: __COVERTYPES.Expression,
type: "Expression",
executed: false
})`);
replacer.visit(registerStmt);
Expand All @@ -253,7 +253,7 @@ class CoverageTransform extends BaseVisitor {
hash: "${point.hash}",
line: ${point.line},
column: ${point.column},
type: __COVERTYPES.Expression,
type: "Expression",
executed: false
})`);
replacer.visit(registerStmt);
Expand Down Expand Up @@ -311,7 +311,7 @@ class CoverageTransform extends BaseVisitor {
hash: "${point.hash}",
line: ${point.line},
column: ${point.column},
type: __COVERTYPES.Expression,
type: "Expression",
executed: false
})`);
replacer.visit(registerStmt);
Expand All @@ -335,7 +335,7 @@ class CoverageTransform extends BaseVisitor {
hash: "${point.hash}",
line: ${point.line},
column: ${point.column},
type: __COVERTYPES.Expression,
type: "Expression",
executed: false
})`);
replacer.visit(registerStmt);
Expand Down Expand Up @@ -366,7 +366,7 @@ class CoverageTransform extends BaseVisitor {
hash: "${point.hash}",
line: ${point.line},
column: ${point.column},
type: __COVERTYPES.Block,
type: "Block",
executed: false
})`);
replacer.visit(registerStmt);
Expand Down Expand Up @@ -396,7 +396,7 @@ class CoverageTransform extends BaseVisitor {
hash: "${point.hash}",
line: ${point.line},
column: ${point.column},
type: __COVERTYPES.Block,
type: "Block",
executed: false
})`);
replacer.visit(registerStmt);
Expand Down Expand Up @@ -463,14 +463,10 @@ export default class Transformer extends Transform {
transformer.visit(source);
if (transformer.globalStatements.length) {
source.statements.unshift(...transformer.globalStatements);
const tokenizer = new Tokenizer(new Source(0 /* SourceKind.User */, source.normalizedPath, "import { __REGISTER, __COVER, __COVERTYPES, __COVERAGE_STATS } from \"as-test/coverage\";"));
const tokenizer = new Tokenizer(new Source(0 /* SourceKind.User */, source.normalizedPath, "import { __REGISTER, __COVER } from \"as-test/assembly/coverage\";"));
parser.currentSource = tokenizer.source;
source.statements.unshift(parser.parseTopLevelStatement(tokenizer));
parser.currentSource = source;
// @ts-ignore
if (process && process.env["TEST_DEBUG"]?.toString().toLowerCase() == "all") {
console.log(toString(source));
}
}
}
transformer.globalStatements = [];
Expand Down
Loading

0 comments on commit 212e162

Please sign in to comment.