Skip to content

Commit

Permalink
feat: arrays can be tested
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Oct 8, 2024
1 parent e8c1981 commit 8c96862
Show file tree
Hide file tree
Showing 16 changed files with 72 additions and 43 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ v0.3.0 - Pass metadata through terminal - Support for multiple files - Better re
v0.3.1 - Add screenshot of completed tests to readme
v0.3.2 - Add `mockImport` to override imported functions
v0.3.3 - Allow `mockImport`'s return type to be any
v0.3.4 - Fix: import functions were not received after visitSource()
v0.3.4 - Fix: import functions were not received after visitSource
v0.3.5 - Fix: allow arrays to be used in `.toBe()`
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
| _ || __| ___|_ _|| __|| __||_ _|
| ||__ ||___| | | | __||__ | | |
|__|__||_____| |_| |_____||_____| |_|
v0.3.4
v0.3.5
</pre>
</h5>

Expand Down
20 changes: 13 additions & 7 deletions assembly/__tests__/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@ import { describe, expect, test, run, it } from "..";
const myArray: i32[] = [1, 2, 3];

describe("Array manipulation", () => {
test("Array length", () => {
expect(myArray).toHaveLength(3);
});
// test("Array length", () => {
// expect("foo").toBe("foo")
// });

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

test("Array check", () => {
const a = [1,2,3];
const b = [1,2,3];
expect(a).toBe(b);
})

it("should be empty", () => {});
});

run({
log: false,
log: true,
});
2 changes: 1 addition & 1 deletion assembly/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export class CoverPoint {
public hash: string = "";
public line: i32 = 0;
public column: i32 = 0;
public type!: string;
public type: string = "";
public executed: boolean = false;
}

Expand Down
34 changes: 21 additions & 13 deletions assembly/src/expectation.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { visualize } from "../util/helpers";
import { Tests } from "./tests";
import { after_each_callback, before_each_callback } from "..";
import { after_each_callback, before_each_callback, log } from "..";
import { JSON } from "json-as";


@json
export class Expectation<T> extends Tests {
public verdict: string = "none";
public right: JSON.Raw = "";
public left: JSON.Raw = "";
private _left: T;
// @ts-ignore
private _right: u64 = 0;
Expand Down Expand Up @@ -340,33 +343,38 @@ export class Expectation<T> extends Tests {
* @returns - void
*/
toBe(equals: T): void {
store<T>(
changetype<usize>(this),
equals,
offsetof<Expectation<T>>("_right"),
);
if (isBoolean<T>()) {
if (isArray<T>()) {
// @ts-ignore
this.verdict = arrayEquals(this._left, equals) ? "ok" : "fail";
} else if (isBoolean<T>()) {
this.verdict = this._left === equals ? "ok" : "fail";
} else if (isString<T>()) {
this.verdict = this._left === equals ? "ok" : "fail";
} else if (isInteger<T>() || isFloat<T>()) {
this.verdict = this._left === equals ? "ok" : "fail";
} else if (isArray<T>()) {
// getArrayDepth<T>();
} else {
this.verdict = "none";
}

this.instr = "toBe";

this.left = visualize<T>(this._left);
this.right = visualize<T>(
load<T>(changetype<usize>(this), offsetof<Expectation<T>>("_right")),
);
this.left = JSON.stringify<T>(this._left);
this.right = JSON.stringify<T>(equals);

// @ts-ignore
if (after_each_callback) after_each_callback();
// @ts-ignore
if (before_each_callback) before_each_callback();

// store<T>(
// changetype<usize>(this),
// equals,
// offsetof<Expectation<T>>("_right"),
// );
}
}

function arrayEquals<T extends any[]>(a: T, b: T): boolean {
if (a.length != b.length) return false;
return JSON.stringify(a) == JSON.stringify(b);
}
7 changes: 4 additions & 3 deletions assembly/src/tests.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { JSON } from "json-as";

@json
export class Tests {
public order: i32 = 0;
public type!: string;
public type: string = "";
public verdict: string = "none";
public left: string = "";
public right: string = "";
public left: JSON.Raw = "";
public right: JSON.Raw = "";
public instr: string = "";
}
13 changes: 13 additions & 0 deletions assembly/util/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { rainbow } from "as-rainbow";
import { JSON } from "json-as";

export function visualize<T>(value: T): string {
if (isNullable<T>() && changetype<usize>(value) == <usize>0) {
Expand All @@ -11,6 +12,18 @@ export function visualize<T>(value: T): string {
} else if (isInteger<T>() || isFloat<T>()) {
// @ts-ignore
return value.toString();
} else if (isArray<T>()) {
// @ts-ignore
if (!value.length) return "[]";
let out = "[";
// @ts-ignore
for (let i = 0; i < value.length - 1; i++) {
// @ts-ignore
out += visualize<valueof<T>>(value[i]);
}
// @ts-ignore
out += visualize<valueof<T>>(value[value.length - 1]);
return out;
}

return unreachable();
Expand Down
2 changes: 1 addition & 1 deletion bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const _args = process.argv.slice(2);
const flags = [];
const args = [];
const COMMANDS = ["run", "build", "test", "init"];
const version = "0.3.4";
const version = "0.3.5";
for (const arg of _args) {
if (arg.startsWith("-")) flags.push(arg);
else args.push(arg);
Expand Down
4 changes: 2 additions & 2 deletions bin/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function init(args) {
input: process.stdin,
output: process.stdout,
});
console.log(chalk.bold("as-test init v0.3.4") + "\n");
console.log(chalk.bold("as-test init v0.3.5") + "\n");
console.log(chalk.dim("[1/3]") + " select a target [wasi/bindings]");
const target = await ask(chalk.dim(" -> "), rl);
if (!TARGETS.includes(target)) {
Expand Down Expand Up @@ -182,7 +182,7 @@ const exports = instantiate(module, {});`,
}
if (!pkg["devDependencies"]) pkg["devDependencies"] = {};
if (!pkg["devDependencies"]["as-test"])
pkg["devDependencies"]["as-test"] = "^0.3.4";
pkg["devDependencies"]["as-test"] = "^0.3.5";
if (target == "bindings") {
pkg["type"] = "module";
}
Expand Down
8 changes: 4 additions & 4 deletions bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function run() {
chalk.bold.blueBright(`|__|__||_____| |_| |_____||_____| |_| `),
);
console.log(
chalk.dim("\n------------------- v0.3.4 -------------------\n"),
chalk.dim("\n------------------- v0.3.5 -------------------\n"),
);
}
for (const plugin of Object.keys(config.plugins)) {
Expand Down Expand Up @@ -114,10 +114,10 @@ export async function run() {
);
for (const test of failed.tests) {
const diffResult = diff(
JSON.stringify(test._left),
JSON.stringify(test._right),
JSON.stringify(test.left),
JSON.stringify(test.right),
);
let expected = chalk.dim(JSON.stringify(test._left));
let expected = chalk.dim(JSON.stringify(test.left));
let received = "";
for (const res of diffResult.diff) {
switch (res.type) {
Expand Down
2 changes: 1 addition & 1 deletion cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const args: string[] = [];

const COMMANDS: string[] = ["run", "build", "test", "init"];

const version = "0.3.4";
const version = "0.3.5";

for (const arg of _args) {
if (arg.startsWith("-")) flags.push(arg);
Expand Down
4 changes: 2 additions & 2 deletions cli/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function init(args: string[]) {
input: process.stdin,
output: process.stdout,
});
console.log(chalk.bold("as-test init v0.3.4") + "\n");
console.log(chalk.bold("as-test init v0.3.5") + "\n");
console.log(chalk.dim("[1/3]") + " select a target [wasi/bindings]");
const target = await ask(chalk.dim(" -> "), rl);
if (!TARGETS.includes(target)) {
Expand Down Expand Up @@ -192,7 +192,7 @@ const exports = instantiate(module, {});`,
}
if (!pkg["devDependencies"]) pkg["devDependencies"] = {};
if (!pkg["devDependencies"]["as-test"])
pkg["devDependencies"]["as-test"] = "^0.3.4";
pkg["devDependencies"]["as-test"] = "^0.3.5";
if (target == "bindings") {
pkg["type"] = "module";
}
Expand Down
6 changes: 3 additions & 3 deletions cli/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export async function run() {
chalk.bold.blueBright(`|__|__||_____| |_| |_____||_____| |_| `),
);
console.log(
chalk.dim("\n------------------- v0.3.4 -------------------\n"),
chalk.dim("\n------------------- v0.3.5 -------------------\n"),
);
}

Expand Down Expand Up @@ -128,8 +128,8 @@ export async function run() {
);
for (const test of failed.tests) {
const diffResult = diff(
JSON.stringify(test._left),
JSON.stringify(test._right),
JSON.stringify(test.left),
JSON.stringify(test.right),
);
let expected = chalk.dim(JSON.stringify(test._left));
let received = "";
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "as-test",
"version": "0.3.4",
"version": "0.3.5",
"description": "Testing framework for AssemblyScript. Compatible with WASI or Bindings ",
"types": "assembly/index.ts",
"author": "Jairus Tanaka",
Expand Down Expand Up @@ -31,7 +31,7 @@
"as-variant": "^0.4.1",
"chalk": "^5.3.0",
"glob": "^11.0.0",
"json-as": "^0.9.14",
"json-as": "^0.9.21",
"typer-diff": "^1.1.1"
},
"overrides": {
Expand Down
2 changes: 1 addition & 1 deletion run/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@as-test/run",
"version": "0.3.4",
"version": "0.3.5",
"description": "Testing framework for AssemblyScript. Compatible with WASI or Bindings ",
"main": "./lib/index.js",
"author": "Jairus Tanaka",
Expand Down
2 changes: 1 addition & 1 deletion transform/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@as-test/transform",
"version": "0.3.4",
"version": "0.3.5",
"description": "Testing framework for AssemblyScript. Compatible with WASI or Bindings ",
"main": "./lib/index.js",
"author": "Jairus Tanaka",
Expand Down

0 comments on commit 8c96862

Please sign in to comment.