@ant-lang/wasm 0.8.0 → 0.9.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/README.md CHANGED
@@ -45,6 +45,13 @@ which Vite, webpack and esbuild copy along. Pass `wasm` (a URL, a `Response`, by
45
45
  resolves to `{ exitCode, diagnostics }`. Output reaches `stdout` and `stderr` as `Uint8Array`
46
46
  chunks. Aborting `signal` interrupts the run as Ctrl-C does, once the program waits on
47
47
  something (see [Sandbox](#sandbox)).
48
+ - `test({ files, paths?, run?, skip?, failfast?, onEvent?, env?, stdin?, stdout?, stderr?, signal? })`
49
+ runs the tests of test files as `ant test` does and resolves to `{ exitCode, diagnostics }`.
50
+ `paths` are the test files to run, by default every `*_test.ant` among `files`; `run` and `skip`
51
+ are the patterns of the `-run` and `-skip` flags. `onEvent` gets each event of `ant test -json`
52
+ as an object: `{ action: "run" | "output" | "pass" | "skip" | "fail", file, test, ... }` and a
53
+ last `{ action: "done", passed, failed, skipped, elapsed }`. The files are checked first: when
54
+ the checker reports errors they are the `diagnostics`, and no test runs.
48
55
  - `languageServer(onMessage, { onLog? })` starts the Ant language server and returns
49
56
  `{ send, close }`, carrying JSON-RPC messages as objects. Documents are analyzed when their URI
50
57
  is a `file:` URI. [@ant-lang/monaco](../monaco) connects it to the Monaco editor.
package/ant.wasm CHANGED
Binary file
package/index.d.ts CHANGED
@@ -54,6 +54,51 @@ export interface RunResult {
54
54
  diagnostics: Diagnostic[];
55
55
  }
56
56
 
57
+ export interface TestOptions {
58
+ files: Files;
59
+ /**
60
+ * The test files to run, in order: paths among files whose names end in "_test.ant". The
61
+ * default is every test file among files, in lexical order, as `ant test` finds them.
62
+ */
63
+ paths?: string[];
64
+ /** Runs only the tests whose full name matches, as the -run flag of `ant test`: "parse/0x". */
65
+ run?: string;
66
+ /** Leaves out the tests that match, as the -skip flag. */
67
+ skip?: string;
68
+ /** Starts no test after the first failure. */
69
+ failfast?: boolean;
70
+ /** Called with each event of the run, the last of which is "done". */
71
+ onEvent?: (event: TestEvent) => void;
72
+ /** The whole environment. The default is empty. */
73
+ env?: Record<string, string>;
74
+ /** What the tests read from Os.stdin. */
75
+ stdin?: string;
76
+ /** Called with what the modules write to standard output outside a test, as they initialize. */
77
+ stdout?: (chunk: Uint8Array) => void;
78
+ /** Called with each write to standard error outside a test, the panic of an initializer included. */
79
+ stderr?: (chunk: Uint8Array) => void;
80
+ /** Ends the run, once the tests wait, as RunOptions.signal does. */
81
+ signal?: AbortSignal;
82
+ }
83
+
84
+ /** An event of a test run, as `ant test -json` prints them. `file` is a path of `paths`, `test` a full name such as "parse/0x10", `elapsed` in seconds. */
85
+ export type TestEvent =
86
+ | { action: "run"; file: string; test: string }
87
+ /** The text of one write of a test. */
88
+ | { action: "output"; file: string; test: string; output: string }
89
+ | { action: "pass"; file: string; test: string; elapsed: number }
90
+ | { action: "skip"; file: string; test: string; elapsed: number; reason?: string }
91
+ /** `message` holds the failures, as `ant test` reports them; none when only subtests failed. */
92
+ | { action: "fail"; file: string; test: string; elapsed: number; message?: string }
93
+ | { action: "done"; passed: number; failed: number; skipped: number; elapsed: number };
94
+
95
+ export interface TestResult {
96
+ /** The status `ant test` would exit with: 0, 1 when a test failed or the checker reported an error, 2 when a file did not load, 130 when the signal ended the run, or what Os.exit was given. */
97
+ exitCode: number;
98
+ /** The checker's errors. When there are any, no test ran and there are no events. */
99
+ diagnostics: Diagnostic[];
100
+ }
101
+
57
102
  /** A connection to a language server (spec/lsp.md), carrying JSON-RPC messages as objects. */
58
103
  export interface LanguageServerConnection {
59
104
  send(message: object): void;
@@ -75,6 +120,11 @@ export interface Ant {
75
120
  check(program: Program): Diagnostic[];
76
121
  /** Runs a program. Ant code that blocks, such as on a channel or a timer, does not block the caller. */
77
122
  run(options: RunOptions): Promise<RunResult>;
123
+ /**
124
+ * Runs the tests of test files, as `ant test` does: the files are checked first, then each runs
125
+ * as a program of its own. Rejects when a path is not a test file or a pattern does not compile.
126
+ */
127
+ test(options: TestOptions): Promise<TestResult>;
78
128
  /**
79
129
  * Starts a language server. Documents are analyzed when their URI is a file: URI; their
80
130
  * imports resolve among the open documents, since ant.wasm has no file system.
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // @ant-lang/wasm loads Ant compiled to WebAssembly and wraps what it exposes: format, check,
2
- // run, the language server, and the debug adapter.
2
+ // run, test, the language server, and the debug adapter.
3
3
  import "./wasm_exec.js";
4
4
 
5
5
  let instances = 0;
@@ -65,6 +65,19 @@ function rootOf(files, path) {
65
65
  return paths.length === 1 ? paths[0] : "main.ant";
66
66
  }
67
67
 
68
+ // testFiles are the test files among files, as ant test finds them in a directory: in lexical
69
+ // order, without those in directories whose names begin with a dot.
70
+ function testFiles(files) {
71
+ const inView = (path) =>
72
+ !path
73
+ .split("/")
74
+ .slice(0, -1)
75
+ .some((dir) => dir.startsWith("."));
76
+ return Object.keys(files)
77
+ .filter((path) => path.endsWith("_test.ant") && inView(path))
78
+ .sort();
79
+ }
80
+
68
81
  function wrap(raw) {
69
82
  return {
70
83
  format(source, path = "main.ant") {
@@ -80,6 +93,14 @@ function wrap(raw) {
80
93
  run(options) {
81
94
  return raw.run({ ...options, path: rootOf(options.files, options.path) });
82
95
  },
96
+ test(options) {
97
+ const { onEvent } = options;
98
+ return raw.test({
99
+ ...options,
100
+ paths: options.paths ?? testFiles(options.files),
101
+ onEvent: onEvent && ((json) => onEvent(JSON.parse(json))),
102
+ });
103
+ },
83
104
  languageServer(onMessage, options = {}) {
84
105
  const conn = raw.languageServer((json) => onMessage(JSON.parse(json)), options.onLog);
85
106
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ant-lang/wasm",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "The Ant language compiled to WebAssembly: format, check, and run Ant programs, and serve the Ant language server, in browsers and Node.",
5
5
  "keywords": [
6
6
  "ant",
@@ -40,5 +40,5 @@
40
40
  "engines": {
41
41
  "node": ">=20"
42
42
  },
43
- "ant": "v0.3.1"
43
+ "ant": "v0.3.2"
44
44
  }