as-test 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Jairus Tanaka <jairus.v.tanaka@outlook.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,54 @@
1
+ <h5 align="center">
2
+ <pre>
3
+ _____ _____ _____ _____ _____ _____
4
+ | _ | __|___|_ _| __| __|_ _|
5
+ | |__ |___| | | | __|__ | | |
6
+ |__|__|_____| |_| |_____|_____| |_|
7
+ v0.0.0
8
+ </pre>
9
+ </h5>
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ npm install as-test
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```js
20
+ import {
21
+ describe,
22
+ expect,
23
+ run
24
+ } from "as-test";
25
+
26
+ describe("Should create suite", () => {
27
+ expect("foo").toBe("foo");
28
+ expect(3.14).toBeGreaterThan(0.0);
29
+ expect("a").toBe("b");
30
+ });
31
+
32
+ run();
33
+ ```
34
+
35
+ If you use this project in your codebase, consider dropping a [⭐ HERE](https://github.com/JairusSW/as-test). I would really appreciate it!
36
+
37
+ ## Notes
38
+
39
+ This library is in the EARLY STAGES OF DEVELOPMENT!
40
+ If you want a feature, drop an issue (and again, maybe a star). I'll likely add it in less than 7 days.
41
+
42
+ ## Contact
43
+
44
+ Contact me at:
45
+
46
+ Email: `me@jairus.dev`
47
+
48
+ GitHub: `JairusSW`
49
+
50
+ Discord: `jairussw`
51
+
52
+ ## Issues
53
+
54
+ Please submit an issue to https://github.com/JairusSW/as-test/issues if you find anything wrong with this library
@@ -0,0 +1,5 @@
1
+ import { describe, expect } from ".";
2
+
3
+ describe("Should create suite successfully", () => {
4
+ expect("Should compare correctly").toBe("adafasfa");
5
+ })
@@ -0,0 +1,82 @@
1
+ import { rainbow } from "as-rainbow";
2
+ import { TestGroup } from "./src/group";
3
+ import { Expectation } from "./src/expectation";
4
+ import { convertSeconds } from "as-convert-seconds/assembly";
5
+ import { Verdict } from "./src/result";
6
+ import { formatTime } from "./util";
7
+
8
+ // Globals
9
+ let current_group: TestGroup | null = null;
10
+ let groups: TestGroup[] = [];
11
+
12
+ /**
13
+ * Creates a test group containing multiple test cases
14
+ *
15
+ * @param {string} description - The name of the test group
16
+ * @param callback - The block containing the test cases for this group
17
+ *
18
+ * @example
19
+ *
20
+ * ```ts
21
+ * describe("my test suite", () => {
22
+ * // Tests go here
23
+ * });
24
+ * ```
25
+ */
26
+ export function describe(description: string, callback: () => void): void {
27
+ const group = new TestGroup(description, callback);
28
+
29
+ current_group = group;
30
+ groups.push(group);
31
+ }
32
+
33
+ export function expect<T>(value: T): Expectation<T> {
34
+ const result = new Expectation<T>(value);
35
+
36
+ current_group!.addExpectation(result);
37
+
38
+ //if (!result.tested) {
39
+ //
40
+ //}
41
+
42
+ return result;
43
+ }
44
+
45
+ export function run(): void {
46
+ console.log(rainbow.boldMk(rainbow.blue(
47
+ ` _____ _____ _____ _____ _____ _____
48
+ | _ | __|___|_ _| __| __|_ _|
49
+ | |__ |___| | | | __|__ | | |
50
+ |__|__|_____| |_| |_____|_____| |_| `)));
51
+ console.log(rainbow.dimMk("\n-----------------------------------------\n"));
52
+ const suites = groups.length;
53
+ let failed = 0;
54
+ let tests = 0;
55
+ let failed_tests = 0;
56
+ const start = performance.now();
57
+ for (let i = 0; i < groups.length; i++) {
58
+ const suite = unchecked(groups[i]);
59
+ console.log(rainbow.boldMk(`Running test suite: ${suite.description}...`));
60
+ suite.run();
61
+ for (let i = 0; i < suite.results.length; i++) {
62
+ const expectation = unchecked(suite.results[i]);
63
+ const verdict = expectation.verdict;
64
+ tests++;
65
+ if (verdict == Verdict.Ok) {
66
+ suite.passed++;
67
+ } else if (verdict == Verdict.Fail) {
68
+ suite.verdict = Verdict.Fail;
69
+ suite.failed++;
70
+ failed_tests++;
71
+ }
72
+ }
73
+ if (suite.verdict == Verdict.Unreachable) suite.verdict = Verdict.Ok;
74
+ else failed++;
75
+ }
76
+ const ms = performance.now() - start;
77
+ console.log(rainbow.dimMk("\n-----------------------------------------\n"));
78
+ console.log(rainbow.boldMk("Test Suites: ") + rainbow.boldMk(rainbow.red(failed.toString() + " failed")) + ", " + suites.toString() + " total");
79
+ console.log(rainbow.boldMk("Tests: ") + rainbow.boldMk(rainbow.red(failed_tests.toString() + " failed")) + ", " + tests.toString() + " total");
80
+ console.log(rainbow.boldMk("Snapshots: ") + "0 total");
81
+ console.log(rainbow.boldMk("Time: ") + formatTime(ms))
82
+ }
@@ -0,0 +1,142 @@
1
+ import { Verdict } from "./result";
2
+ import { rainbow } from "as-rainbow";
3
+ import { diff, visualize } from "../util";
4
+
5
+ export class Expectation<T> {
6
+ public verdict: Verdict = Verdict.Unreachable;
7
+ public left: T;
8
+ public right!: T;
9
+ private _not: boolean = false;
10
+ constructor(left: T) {
11
+ this.left = left;
12
+ }
13
+ get not(): Expectation<T> {
14
+ this._not = true;
15
+ return this;
16
+ }
17
+ toBeNull(): Expectation<T> {
18
+ this.verdict = (isNullable<T>() && changetype<usize>(this.left)) ? Verdict.Ok : Verdict.Fail;
19
+
20
+ // @ts-ignore
21
+ this.right = null;
22
+
23
+ const report = this.report();
24
+ if (report) console.log(report);
25
+
26
+ return this;
27
+ }
28
+ /**
29
+ * Tests if a > b
30
+ * @param number equals - The value to test
31
+ * @returns - Expectation
32
+ */
33
+ toBeGreaterThan(value: T): Expectation<T> {
34
+ if (!isInteger<T>() && !isFloat<T>()) throw new Error("toBeGreaterThan() can only be used on number types. Received " + nameof<T>() + " instead!");
35
+
36
+ this.verdict = this.left > value ? Verdict.Ok : Verdict.Fail;
37
+ this.right = value;
38
+
39
+ const report = this.report(">");
40
+ if (report) console.log(report);
41
+
42
+ return this;
43
+ }
44
+ /**
45
+ * Tests if a >= b
46
+ * @param number equals - The value to test
47
+ * @returns - Expectation
48
+ */
49
+ toBeGreaterOrEqualTo(value: T): Expectation<T> {
50
+ if (!isInteger<T>() && !isFloat<T>()) throw new Error("toBeGreaterOrEqualTo() can only be used on number types. Received " + nameof<T>() + " instead!");
51
+
52
+ this.verdict = this.left >= value ? Verdict.Ok : Verdict.Fail;
53
+ this.right = value;
54
+
55
+ const report = this.report(">=");
56
+ if (report) console.log(report);
57
+
58
+ return this;
59
+ }
60
+ /**
61
+ * Tests if a < b
62
+ * @param number equals - The value to test
63
+ * @returns - Expectation
64
+ */
65
+ toBeLessThan(value: T): Expectation<T> {
66
+ if (!isInteger<T>() && !isFloat<T>()) throw new Error("toBeLessThan() can only be used on number types. Received " + nameof<T>() + " instead!");
67
+
68
+ this.verdict = this.left < value ? Verdict.Ok : Verdict.Fail;
69
+ this.right = value;
70
+
71
+ const report = this.report("<");
72
+ if (report) console.log(report);
73
+
74
+ return this;
75
+ }
76
+ /**
77
+ * Tests if a <= b
78
+ * @param number equals - The value to test
79
+ * @returns - Expectation
80
+ */
81
+ toBeLessThanOrEqualTo(value: T): Expectation<T> {
82
+ if (!isInteger<T>() && !isFloat<T>()) throw new Error("toBeLessThanOrEqualTo() can only be used on number types. Received " + nameof<T>() + " instead!");
83
+
84
+ this.verdict = this.left <= value ? Verdict.Ok : Verdict.Fail;
85
+ this.right = value;
86
+
87
+ const report = this.report("<=");
88
+ if (report) console.log(report);
89
+
90
+ return this;
91
+ }
92
+ /**
93
+ * Tests for equality
94
+ * @param any equals - The value to test
95
+ * @returns - Expectation
96
+ */
97
+ toBe(equals: T): Expectation<T> {
98
+ this.right = equals;
99
+ if (isBoolean<T>()) {
100
+ this.verdict = this.left === this.right
101
+ ? Verdict.Ok
102
+ : Verdict.Fail;
103
+
104
+ } else if (isString<T>()) {
105
+ this.verdict = this.left === this.right
106
+ ? Verdict.Ok
107
+ : Verdict.Fail;
108
+ } else if (isInteger<T>() || isFloat<T>()) {
109
+ this.verdict = this.left === this.right
110
+ ? Verdict.Ok
111
+ : Verdict.Fail;
112
+ } else if (isArray<T>()) {
113
+ // getArrayDepth<T>();
114
+ } else {
115
+ this.verdict = Verdict.Unreachable;
116
+ }
117
+
118
+ const report = this.report();
119
+ if (report) console.log(report);
120
+
121
+ return this;
122
+ }
123
+
124
+ report(op: string = "="): string | null {
125
+ if (!this._not && this.verdict === Verdict.Ok) return null;
126
+
127
+ const left = visualize(this.left);
128
+ const right = visualize(this.right);
129
+
130
+ if (this._not) {
131
+ if (this.verdict === Verdict.Fail) return null;
132
+ const dif = diff(left, right, true);
133
+ return rainbow.red(" - Test failed") + "\n" + rainbow.italicMk(` ${rainbow.dimMk("(expected) ->")} ${dif.left.toString()}\n ${rainbow.dimMk("[ !" + op + " ]")}\n ${rainbow.dimMk("(recieved) ->")} ${dif.right.toString()}`);
134
+ }
135
+
136
+ if (left == right) return null;
137
+
138
+ const dif = diff(left, right);
139
+
140
+ return rainbow.red(" - Test failed") + "\n" + rainbow.italicMk(` ${rainbow.dimMk("(expected) ->")} ${dif.left.toString()}\n ${rainbow.dimMk("[ " + op + " ]")}\n ${rainbow.dimMk("(recieved) ->")} ${dif.right.toString()}`);
141
+ }
142
+ }
@@ -0,0 +1,26 @@
1
+ import { Expectation } from "./expectation";
2
+ import { Verdict } from "./result";
3
+ export class TestGroup {
4
+ public results: Expectation<usize>[] = [];
5
+
6
+ public description: string;
7
+ public executed: boolean = false;
8
+ public verdict: Verdict = Verdict.Unreachable;
9
+
10
+ public passed: i32 = 0;
11
+ public failed: i32 = 0;
12
+
13
+ public callback: () => void;
14
+ constructor(description: string, callback: () => void) {
15
+ this.description = description;
16
+ this.callback = callback;
17
+ }
18
+
19
+ addExpectation<T extends Expectation<unknown>>(test: T): void {
20
+ this.results.push(changetype<Expectation<usize>>(test));
21
+ }
22
+
23
+ run(): void {
24
+ this.callback();
25
+ }
26
+ }
@@ -0,0 +1,90 @@
1
+ import { Variant } from "as-variant/assembly";
2
+ import { Verdict } from "./result";
3
+ import { rainbow } from "as-rainbow";
4
+ import { visualize } from "../util";
5
+ import { StringSink } from "as-string-sink/assembly";
6
+
7
+ export class It {
8
+ public verdict: Verdict = Verdict.Unreachable;
9
+ public left: Variant;
10
+ public right!: Variant;
11
+ private _not: boolean = false;
12
+ constructor(left: Variant) {
13
+ this.left = left;
14
+ }
15
+ not(): It {
16
+ this._not = true;
17
+ return this;
18
+ }
19
+ /**
20
+ * Tests for strict equality
21
+ * @param any equals - The value to test
22
+ * @returns - Expectation
23
+ */
24
+ toBe<T>(equals: T): It {
25
+ this.right = Variant.from(equals);
26
+ if (this.left.id !== this.right.id) throw "cannot compare different types";
27
+
28
+ if (isBoolean<T>()) {
29
+ this.verdict = this.left.getUnchecked<T>() === this.right.getUnchecked<T>()
30
+ ? Verdict.Ok
31
+ : Verdict.Fail;
32
+
33
+ } else if (isString<T>()) {
34
+ this.verdict = this.left.getUnchecked<T>() === this.right.getUnchecked<T>()
35
+ ? Verdict.Ok
36
+ : Verdict.Fail;
37
+ } else if (isInteger<T>() || isFloat<T>()) {
38
+ this.verdict = this.left.getUnchecked<T>() === this.right.getUnchecked<T>()
39
+ ? Verdict.Ok
40
+ : Verdict.Fail;
41
+ } else if (isArray<T>()) {
42
+ // getArrayDepth<T>();
43
+ } else {
44
+ this.verdict = Verdict.Unreachable;
45
+ }
46
+
47
+ console.log(this.report<T>());
48
+
49
+ return this;
50
+ }
51
+
52
+ report<T>(): string {
53
+ if (!this.not && this.verdict === Verdict.Ok) {
54
+ return rainbow.green(" - Test completed successfully");
55
+ }
56
+
57
+ const left = visualize<T>(this.left.getUnchecked<T>());
58
+ const right = visualize<T>(this.right.getUnchecked<T>());
59
+
60
+ if (this._not) {
61
+ if (this.verdict === Verdict.Fail) return rainbow.green(" - Test completed successfully");
62
+ return rainbow.red(" - Test failed") + "\n" + rainbow.italicMk(` ${rainbow.dimMk("(expected) ->")} ${rainbow.bgGreen(left.toString())}\n ${rainbow.dimMk("(recieved) ->")} ${rainbow.bgRed(right.toString())}`);
63
+ }
64
+
65
+ let leftDiff = StringSink.withCapacity(left.length);
66
+ let rightDiff = StringSink.withCapacity(right.length);
67
+
68
+ let i = 0
69
+
70
+ for (; i < min(left.length, right.length); i++) {
71
+ const lChar = left.charAt(i);
72
+ const rChar = right.charAt(i);
73
+ if (lChar != rChar) {
74
+ leftDiff.write(rainbow.bgGreen(lChar));
75
+ rightDiff.write(rainbow.bgRed(rChar));
76
+ } else {
77
+ leftDiff.write(lChar);
78
+ rightDiff.write(rChar);
79
+ }
80
+ }
81
+
82
+ for (; i < left.length; i++) {
83
+ leftDiff.write(rainbow.bgGreen(left.charAt(i)));
84
+ rightDiff.write(rainbow.bgRed(" "));
85
+ }
86
+ for (; i < right.length; i++) rightDiff.write(rainbow.bgRed(right.charAt(i)));
87
+
88
+ return rainbow.red(" - Test failed") + "\n" + rainbow.italicMk(` ${rainbow.dimMk("(expected) ->")} ${leftDiff.toString()}\n ${rainbow.dimMk("(recieved) ->")} ${rightDiff.toString()}`);
89
+ }
90
+ }
@@ -0,0 +1,23 @@
1
+ import { Variant } from "as-variant/assembly";
2
+
3
+ export enum Verdict {
4
+ Unreachable,
5
+ Ok,
6
+ Fail,
7
+ }
8
+
9
+ export class TestResult {
10
+ public verdict: Verdict = Verdict.Unreachable;
11
+ public left: Variant;
12
+ public right!: Variant;
13
+ constructor(left: Variant) {
14
+ this.left = left;
15
+ }
16
+ toBe<T>(equals: T): TestResult {
17
+ return this;
18
+ }
19
+
20
+ report(): string {
21
+
22
+ }
23
+ }
@@ -0,0 +1,13 @@
1
+ import { describe, expect, run } from ".";
2
+
3
+ describe("Should create suite successfully", () => {
4
+ expect("foo joe momma joe mommmmma").toBe("booq2132132312");
5
+ expect("abcdefg").not.toBe("abcdefg");
6
+ expect("hello").toBe("hello");
7
+ expect<Nullable | null>(null).toBeNull();
8
+ expect(5).toBeGreaterOrEqualTo(9)
9
+ });
10
+
11
+ class Nullable { }
12
+
13
+ run();
@@ -0,0 +1,99 @@
1
+ {
2
+ "extends": "assemblyscript/std/assembly.json",
3
+ "include": [
4
+ "./**/*.ts"
5
+ ],
6
+ "compilerOptions": {
7
+ /* Visit https://aka.ms/tsconfig to read more about this file */
8
+ /* Projects */
9
+ // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
10
+ // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
11
+ // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
12
+ // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
13
+ // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
14
+ // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
15
+ /* Language and Environment */
16
+ "target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
17
+ // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
18
+ // "jsx": "preserve", /* Specify what JSX code is generated. */
19
+ "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
20
+ "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
21
+ // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
22
+ // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
23
+ // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
24
+ // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
25
+ // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
26
+ // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
27
+ // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
28
+ /* Modules */
29
+ "module": "commonjs" /* Specify what module code is generated. */,
30
+ // "rootDir": "./", /* Specify the root folder within your source files. */
31
+ // "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
32
+ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
33
+ // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
34
+ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
35
+ // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
36
+ // "types": [], /* Specify type package names to be included without being referenced in a source file. */
37
+ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
38
+ // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
39
+ // "resolveJsonModule": true, /* Enable importing .json files. */
40
+ // "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
41
+ /* JavaScript Support */
42
+ // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
43
+ // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
44
+ // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
45
+ /* Emit */
46
+ // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
47
+ // "declarationMap": true, /* Create sourcemaps for d.ts files. */
48
+ // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
49
+ // "sourceMap": true, /* Create source map files for emitted JavaScript files. */
50
+ // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
51
+ // "outDir": "./", /* Specify an output folder for all emitted files. */
52
+ // "removeComments": true, /* Disable emitting comments. */
53
+ // "noEmit": true, /* Disable emitting files from a compilation. */
54
+ // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
55
+ // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
56
+ // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
57
+ // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
58
+ // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
59
+ // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
60
+ // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
61
+ // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
62
+ // "newLine": "crlf", /* Set the newline character for emitting files. */
63
+ // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
64
+ // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
65
+ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
66
+ // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
67
+ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */
68
+ // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
69
+ /* Interop Constraints */
70
+ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
71
+ // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
72
+ "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
73
+ // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
74
+ "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
75
+ /* Type Checking */
76
+ "strict": false/* Enable all strict type-checking options. */,
77
+ // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
78
+ "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
79
+ // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
80
+ // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
81
+ // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
82
+ // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
83
+ // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
84
+ // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
85
+ "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
86
+ "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
87
+ // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
88
+ // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
89
+ // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
90
+ // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
91
+ // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
92
+ // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
93
+ // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
94
+ // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
95
+ /* Completeness */
96
+ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
97
+ "skipLibCheck": true /* Skip type checking all .d.ts files. */
98
+ }
99
+ }
@@ -0,0 +1,110 @@
1
+ import { rainbow } from "as-rainbow";
2
+
3
+ export function visualize<T>(value: T): string {
4
+ if (isNullable<T>() && changetype<usize>(value) == <usize>0) {
5
+ return "null";
6
+ } else if (isString<T>()) {
7
+ return value as string;
8
+ } else if (isBoolean<T>()) {
9
+ // @ts-ignore
10
+ return value.toString();
11
+ } else if (isInteger<T>() || isFloat<T>()) {
12
+ // @ts-ignore
13
+ return value.toString();
14
+ }
15
+
16
+ return unreachable();
17
+ }
18
+
19
+ export function isTruthy<T>(value: T): boolean {
20
+ if (isNullable<T>() && changetype<usize>(value) === <usize>0) {
21
+ return false;
22
+ } else if (isString<T>()) {
23
+ return (value as string).length != 0;
24
+ } else if (isBoolean<T>()) {
25
+ return value as boolean;
26
+ // @ts-ignore
27
+ } else if ((isInteger<T>() || isFloat<T>()) && isNaN(value)) {
28
+ return false;
29
+ }
30
+ return true;
31
+ }
32
+
33
+ export function isFalsy<T>(value: T): boolean {
34
+ return !isTruthy(value);
35
+ }
36
+
37
+ class Diff { left: string; right: string; }
38
+ export function diff(left: string, right: string, not: boolean = false): Diff {
39
+ let rDiff = "";
40
+ let lDiff = "";
41
+
42
+ let i = 0;
43
+
44
+ for (; i < min(left.length, right.length); i++) {
45
+ const lChar = left.charAt(i);
46
+ const rChar = right.charAt(i);
47
+ if (not) {
48
+ if (lChar == rChar) {
49
+ lDiff += rainbow.bgGreen(rChar);
50
+ rDiff += rainbow.bgRed(lChar);
51
+ } else {
52
+ lDiff += rChar;
53
+ rDiff += lChar;
54
+ }
55
+ } else {
56
+ if (lChar != rChar) {
57
+ lDiff += rainbow.bgGreen(rChar);
58
+ rDiff += rainbow.bgRed(lChar);
59
+ } else {
60
+ lDiff += rChar;
61
+ rDiff += lChar;
62
+ }
63
+ }
64
+ }
65
+
66
+ if (!not) {
67
+ for (; i < left.length; i++) {
68
+ rDiff += rainbow.bgRed(left.charAt(i));
69
+ }
70
+ for (; i < right.length; i++) lDiff += rainbow.bgRed(right.charAt(i));
71
+ }
72
+
73
+ return {
74
+ left: lDiff,
75
+ right: rDiff
76
+ }
77
+ }
78
+
79
+ class Unit {
80
+ name: string;
81
+ divisor: number;
82
+ }
83
+
84
+ export function formatTime(ms: number): string {
85
+ if (ms < 0) {
86
+ throw new Error("Time should be a non-negative number.");
87
+ }
88
+
89
+ // Convert milliseconds to microseconds
90
+ const us = ms * 1000;
91
+
92
+ const units: Unit[] = [
93
+ { name: 'μs', divisor: 1 },
94
+ { name: 'ms', divisor: 1000 },
95
+ { name: 's', divisor: 1000 * 1000 },
96
+ { name: 'm', divisor: 60 * 1000 * 1000 },
97
+ { name: 'h', divisor: 60 * 60 * 1000 * 1000 },
98
+ { name: 'd', divisor: 24 * 60 * 60 * 1000 * 1000 }
99
+ ];
100
+
101
+ for (let i = units.length - 1; i >= 0; i--) {
102
+ const unit = units[i];
103
+ if (us >= unit.divisor) {
104
+ const value = Math.round((us / unit.divisor) * 1000) / 1000;
105
+ return `${value}${unit.name}`;
106
+ }
107
+ }
108
+
109
+ return `${us}us`;
110
+ }
package/index.ts ADDED
File without changes
package/jest.test.js ADDED
@@ -0,0 +1,5 @@
1
+ describe("Should create suite successfully", () => {
2
+ expect("foo joe momma joe mommmmma").toBe("booq2132132312");
3
+ expect("abcdefg").not.toBe("abcdefg");
4
+ expect("hello").toBe("hello")
5
+ });
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "as-test",
3
+ "version": "0.0.0",
4
+ "description": "Testing framework for AssemblyScript. Compatible with WASI or Bindings ",
5
+ "types": "assembly/index.ts",
6
+ "author": "Jairus Tanaka",
7
+ "contributors": [],
8
+ "license": "MIT",
9
+ "scripts": {
10
+ "build:test": "asc assembly/test.ts -o build/test.wasm --bindings esm --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json",
11
+ "build:bench": "asc assembly/bench/bench.ts -o build/bench.wasm --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json --optimizeLevel 3 --converge --exportRuntime --runtime stub",
12
+ "bench:wasmtime": "wasmtime ./build/bench.wasm",
13
+ "build:transform": "tsc -p ./transform",
14
+ "test:wasmtime": "wasmtime ./build/test.wasm",
15
+ "prettier": "as-prettier -w ."
16
+ },
17
+ "devDependencies": {
18
+ "@assemblyscript/wasi-shim": "^0.1.0",
19
+ "assemblyscript": "^0.27.22",
20
+ "assemblyscript-prettier": "^3.0.1",
21
+ "jest": "^29.7.0",
22
+ "typescript": "^5.3.3",
23
+ "visitor-as": "^0.11.4"
24
+ },
25
+ "dependencies": { "as-convert-seconds": "^1.0.0", "as-rainbow": "^0.1.0", "as-string-sink": "^0.5.3", "as-variant": "^0.4.1", "json-as": "^0.9.6" },
26
+ "overrides": {
27
+ "assemblyscript": "$assemblyscript"
28
+ },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git+https://github.com/JairusSW/as-test.git"
32
+ },
33
+ "keywords": [
34
+ "assemblyscript"
35
+ ],
36
+ "bugs": {
37
+ "url": "https://github.com/JairusSW/as-test/issues"
38
+ },
39
+ "homepage": "https://github.com/JairusSW/as-test#readme",
40
+ "type": "module",
41
+ "publishConfig": {
42
+ "@JairusSW:registry": "https://npm.pkg.github.com"
43
+ }
44
+ }