as-test 0.4.4 → 0.5.1

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.
Files changed (67) hide show
  1. package/CHANGELOG.md +46 -0
  2. package/README.md +196 -82
  3. package/as-test.config.schema.json +137 -0
  4. package/assembly/coverage.ts +19 -0
  5. package/assembly/index.ts +172 -85
  6. package/assembly/src/expectation.ts +263 -199
  7. package/assembly/src/log.ts +1 -9
  8. package/assembly/src/suite.ts +61 -25
  9. package/assembly/src/tests.ts +2 -0
  10. package/assembly/util/wipc.ts +286 -0
  11. package/bin/build.js +86 -41
  12. package/bin/index.js +337 -68
  13. package/bin/init.js +441 -183
  14. package/bin/reporter.js +1 -1
  15. package/bin/reporters/default.js +379 -0
  16. package/bin/reporters/types.js +1 -0
  17. package/bin/run.js +882 -194
  18. package/bin/types.js +14 -7
  19. package/bin/util.js +54 -3
  20. package/package.json +34 -16
  21. package/transform/lib/builder.js +169 -169
  22. package/transform/lib/builder.js.map +1 -1
  23. package/transform/lib/coverage.js +47 -1
  24. package/transform/lib/coverage.js.map +1 -1
  25. package/transform/lib/index.js +70 -0
  26. package/transform/lib/index.js.map +1 -1
  27. package/transform/lib/location.js +20 -0
  28. package/transform/lib/location.js.map +1 -0
  29. package/transform/lib/log.js +118 -0
  30. package/transform/lib/log.js.map +1 -0
  31. package/transform/lib/mock.js +2 -2
  32. package/transform/lib/mock.js.map +1 -1
  33. package/transform/lib/util.js +3 -3
  34. package/transform/lib/util.js.map +1 -1
  35. package/.github/workflows/as-test.yml +0 -26
  36. package/.prettierrc +0 -3
  37. package/as-test.config.json +0 -19
  38. package/assembly/__tests__/array.spec.ts +0 -25
  39. package/assembly/__tests__/math.spec.ts +0 -16
  40. package/assembly/__tests__/mock.spec.ts +0 -22
  41. package/assembly/__tests__/mock.ts +0 -7
  42. package/assembly/__tests__/sleep.spec.ts +0 -28
  43. package/assembly/tsconfig.json +0 -97
  44. package/assets/img/screenshot.png +0 -0
  45. package/cli/build.ts +0 -117
  46. package/cli/index.ts +0 -190
  47. package/cli/init.ts +0 -247
  48. package/cli/reporter.ts +0 -1
  49. package/cli/run.ts +0 -286
  50. package/cli/tsconfig.json +0 -9
  51. package/cli/types.ts +0 -29
  52. package/cli/util.ts +0 -65
  53. package/run/package.json +0 -27
  54. package/tests/array.run.js +0 -7
  55. package/tests/math.run.js +0 -7
  56. package/tests/mock.run.js +0 -14
  57. package/tests/sleep.run.js +0 -7
  58. package/transform/src/builder.ts +0 -1474
  59. package/transform/src/coverage.ts +0 -580
  60. package/transform/src/index.ts +0 -73
  61. package/transform/src/linker.ts +0 -41
  62. package/transform/src/mock.ts +0 -163
  63. package/transform/src/range.ts +0 -12
  64. package/transform/src/types.ts +0 -35
  65. package/transform/src/util.ts +0 -81
  66. package/transform/src/visitor.ts +0 -744
  67. package/transform/tsconfig.json +0 -10
@@ -1,163 +0,0 @@
1
- import {
2
- Source,
3
- Statement,
4
- CallExpression,
5
- StringLiteralExpression,
6
- FunctionExpression,
7
- CommonFlags,
8
- Node,
9
- ArrowKind,
10
- IdentifierExpression,
11
- Expression,
12
- FunctionDeclaration,
13
- } from "assemblyscript/dist/assemblyscript.js";
14
- import { Visitor } from "./visitor.js";
15
- import { toString } from "./util.js";
16
- export class MockTransform extends Visitor {
17
- public srcCurrent: Source | null = null;
18
- public globalStatements: Statement[] = [];
19
- public mocked = new Set<string>();
20
- public importFns: FunctionDeclaration[] = [];
21
- public importMocked: Set<string> = new Set<string>();
22
- visitCallExpression(node: CallExpression): void {
23
- super.visitCallExpression(node);
24
- const name = toString(node.expression)
25
- .replaceAll(".", "_")
26
- .replaceAll("[", "_")
27
- .replaceAll("]", "_");
28
-
29
- if (this.mocked.has(name + "_mock")) {
30
- node.expression = Node.createIdentifierExpression(
31
- name + "_mock",
32
- node.expression.range,
33
- );
34
- return;
35
- }
36
-
37
- if (name == "mockImport") {
38
- this.importMocked.add((node.args[0] as StringLiteralExpression).value);
39
- return;
40
- }
41
- if (name != "mockFn") return;
42
- const ov = toString(node.args[0]);
43
- const cb = node.args[1] as FunctionExpression;
44
- const newName = ov
45
- .replaceAll(".", "_")
46
- .replaceAll("[", "_")
47
- .replaceAll("]", "_");
48
-
49
- const newFn = Node.createFunctionDeclaration(
50
- Node.createIdentifierExpression(newName + "_mock", cb.range),
51
- cb.declaration.decorators,
52
- CommonFlags.None,
53
- cb.declaration.typeParameters,
54
- cb.declaration.signature,
55
- cb.declaration.body,
56
- cb.declaration.arrowKind,
57
- cb.range,
58
- );
59
-
60
- const stmts = this.srcCurrent.statements;
61
- let index = -1;
62
- for (let i = 0; i < stmts.length; i++) {
63
- const stmt = stmts[i];
64
- if (stmt.range.start != node.range.start) continue;
65
- index = i;
66
- break;
67
- }
68
- if (index === -1) return;
69
- stmts.splice(index, 1, newFn);
70
- this.mocked.add(newFn.name.text);
71
- }
72
- visitFunctionDeclaration(
73
- node: FunctionDeclaration,
74
- isDefault?: boolean,
75
- ): void {
76
- if (!node.body) this.importFns.push(node);
77
- super.visitFunctionDeclaration(node, isDefault);
78
- }
79
- visitSource(node: Source): void {
80
- this.mocked = new Set<string>();
81
- this.srcCurrent = node;
82
- this.importFns = [];
83
- super.visitSource(node);
84
-
85
- for (const node of this.importFns) {
86
- let path = "";
87
- const dec = node.decorators?.find(
88
- (v) => (v.name as IdentifierExpression).text == "external",
89
- );
90
- if (!dec) {
91
- path = "env." + node.name.text;
92
- } else if (dec.args[0] && dec.args[1])
93
- path = dec.args
94
- .map((v) => (v as StringLiteralExpression).value)
95
- .join(".");
96
- else if (dec.args[0])
97
- path =
98
- this.srcCurrent.simplePath +
99
- "." +
100
- (dec.args[0] as StringLiteralExpression).value;
101
- else path = this.srcCurrent.simplePath + "." + node.name.text;
102
- if (!this.importMocked.has(path)) return;
103
-
104
- let args: Expression[] = [
105
- Node.createCallExpression(
106
- Node.createPropertyAccessExpression(
107
- Node.createIdentifierExpression("__mock_import", node.range),
108
- Node.createIdentifierExpression("get", node.range),
109
- node.range,
110
- ),
111
- null,
112
- [Node.createStringLiteralExpression(path, node.range)],
113
- node.range,
114
- ),
115
- ];
116
-
117
- for (const param of node.signature.parameters) {
118
- args.push(Node.createIdentifierExpression(param.name.text, node.range));
119
- }
120
-
121
- const newFn = Node.createFunctionDeclaration(
122
- node.name,
123
- node.decorators.filter(
124
- (v) => (v.name as IdentifierExpression).text != "external",
125
- ),
126
- node.flags - CommonFlags.Ambient - CommonFlags.Declare,
127
- null,
128
- node.signature,
129
- Node.createBlockStatement(
130
- [
131
- Node.createReturnStatement(
132
- Node.createCallExpression(
133
- Node.createIdentifierExpression("call_indirect", node.range),
134
- null,
135
- args,
136
- node.range,
137
- ),
138
- node.range,
139
- ),
140
- ],
141
- node.range,
142
- ),
143
- ArrowKind.None,
144
- node.range,
145
- );
146
-
147
- const stmts = this.srcCurrent.statements;
148
- let index = -1;
149
- for (let i = 0; i < stmts.length; i++) {
150
- const stmt = stmts[i];
151
- if (
152
- stmt instanceof FunctionDeclaration &&
153
- stmt.name.text === node.name.text
154
- ) {
155
- index = i;
156
- break;
157
- }
158
- }
159
- if (index === -1) return;
160
- stmts.splice(index, 1, newFn);
161
- }
162
- }
163
- }
@@ -1,12 +0,0 @@
1
- import { Node } from "assemblyscript/dist/assemblyscript.js";
2
- import { Visitor } from "./visitor.js";
3
-
4
- export class RangeTransform extends Visitor {
5
- constructor(private node: Node) {
6
- super();
7
- }
8
- _visit(node: Node, ref: Node | null): void {
9
- node.range = this.node.range;
10
- return super._visit(node, ref);
11
- }
12
- }
@@ -1,35 +0,0 @@
1
- import {
2
- ClassDeclaration,
3
- Expression,
4
- FieldDeclaration,
5
- } from "assemblyscript/dist/assemblyscript.js";
6
-
7
- export enum PropertyFlags {
8
- OmitNull,
9
- OmitIf,
10
- Raw,
11
- }
12
-
13
- export class Property {
14
- public name: string = "";
15
- public alias: string | null = null;
16
- public type: string = "";
17
- public value: string | null = null;
18
- public flags: Map<PropertyFlags, Expression | null> = new Map<
19
- PropertyFlags,
20
- Expression | null
21
- >();
22
- public node!: FieldDeclaration;
23
- public byteSize: number = 0;
24
- }
25
-
26
- export class Schema {
27
- public static: boolean = true;
28
- public name: string = "";
29
- public members: Property[] = [];
30
- public parent: Schema | null = null;
31
- public node!: ClassDeclaration;
32
- public needsLink: string | null = null;
33
- public byteSize: number = 0;
34
- public deps: Schema[] = [];
35
- }
@@ -1,81 +0,0 @@
1
- // Taken from https://github.com/as-pect/visitor-as/blob/master/src/simpleParser.ts
2
- import {
3
- Parser,
4
- Tokenizer,
5
- Source,
6
- SourceKind,
7
- Expression,
8
- Statement,
9
- NamespaceDeclaration,
10
- ClassDeclaration,
11
- DeclarationStatement,
12
- Range,
13
- Node,
14
- } from "assemblyscript/dist/assemblyscript.js";
15
- import { ASTBuilder } from "./builder.js";
16
-
17
- export class SimpleParser {
18
- private static get parser(): Parser {
19
- return new Parser();
20
- }
21
-
22
- private static getTokenizer(s: string, file: string = "index.ts"): Tokenizer {
23
- return new Tokenizer(new Source(SourceKind.User, file, s));
24
- }
25
-
26
- static parseExpression(s: string): Expression {
27
- const res = this.parser.parseExpression(this.getTokenizer(s));
28
- if (res == null) {
29
- throw new Error("Failed to parse the expression: '" + s + "'");
30
- }
31
- return res;
32
- }
33
-
34
- static parseStatement(s: string, topLevel = false): Statement {
35
- const res = this.parser.parseStatement(this.getTokenizer(s), topLevel);
36
- if (res == null) {
37
- throw new Error("Failed to parse the statement: '" + s + "'");
38
- }
39
- return res;
40
- }
41
-
42
- static parseTopLevelStatement(
43
- s: string,
44
- namespace?: NamespaceDeclaration | null,
45
- ): Statement {
46
- const res = this.parser.parseTopLevelStatement(
47
- this.getTokenizer(s),
48
- namespace,
49
- );
50
- if (res == null) {
51
- throw new Error("Failed to parse the top level statement: '" + s + "'");
52
- }
53
- return res;
54
- }
55
-
56
- static parseClassMember(
57
- s: string,
58
- _class: ClassDeclaration,
59
- ): DeclarationStatement {
60
- let res = this.parser.parseClassMember(
61
- this.getTokenizer(s, _class.range.source.normalizedPath),
62
- _class,
63
- );
64
- if (res == null) {
65
- throw new Error("Failed to parse the class member: '" + s + "'");
66
- }
67
- return <DeclarationStatement>res;
68
- }
69
- }
70
-
71
- let isStdlibRegex =
72
- /\~lib\/(?:array|arraybuffer|atomics|builtins|crypto|console|compat|dataview|date|diagnostics|error|function|iterator|map|math|number|object|process|reference|regexp|set|staticarray|string|symbol|table|typedarray|vector|rt\/?|bindings\/|shared\/typeinfo)|util\/|uri|polyfills|memory/;
73
-
74
- export function isStdlib(s: Source | { range: Range }): boolean {
75
- let source = s instanceof Source ? s : s.range.source;
76
- return isStdlibRegex.test(source.internalPath);
77
- }
78
-
79
- export function toString(node: Node): string {
80
- return ASTBuilder.build(node);
81
- }