aboutsummaryrefslogtreecommitdiff
path: root/tests/drivers/format_test.js
diff options
context:
space:
mode:
authorGravatar Moon Flower <moonflower@elhenney.local> 2026-10-14 08:29:46 +0330
committerGravatar Moon Flower <moonflower@elhenney.local> 2026-10-14 08:29:46 +0330
commit1c53304d03414890af0da7432d1aea87e754a14a (patch)
tree97190d86cf548c9bc63f68c86a4d2a4f09809713 /tests/drivers/format_test.js
parenta (diff)
feat: grow memory limits, add icon/actor libraries, overhaul LSP diagnostics
Compiler and VM - make every memory map limit dynamic and adjustable at both build time and runtime, including compiler tables, stacks, and every execution region - reserve and grow tables, operand and return stacks, frame segments, and image-building output without relocating live code or globals - rename i/o and string helpers consistently (puts -> put_s, putn -> put_n, scat -> cat_s) across compiler, runtime, examples, tests, and docs - recast and consume values assigned to globals from inside functions - allow fixed and const together, add typed closure returns (fn:t(...)), multi-include syntax (include rt, run, core;), and recurse type inference - never infer string literals as word, anywhere, as argument, variable, or return type - draft docs/BORROW.md: a borrow checker less intrusive than Rust's but more intelligent LSP and editor integration - require explicit includes and give every toolchain area its own namespace (rt, lsp, run, core) - hover for built-in types, inferred types, values known at hover time, array and pointer lifetime/size, library names in include statements, and inferred lifetimes with explanations - honor /* */ comments as documentation for variables and arguments, and stop misattributing section headers or trailing comments as variable hints - stream diagnostics and cancel stale requests on change - jump to definition and references, including cross-file - quick fixes with automatic library import, and namespace completion Hints and diagnostics - render hints as blue squigglies by default, with hints on/small/off per file - suppress magic-number hints via a preceding /* */ comment, and say so - warn on unused arguments; allow multiple _ discards in parameters, pattern matching, and tagged enums, warning if a _ is actually used - rewrite hint and comment prose in professional compiler-doc register Libraries - add icon.zc: run-time self-modification, find/replace syntax, type and block edits, and step-through inspection of locals and globals - add actor.zc: async actors with Actor<I,O>, implicit this, spawn/recv/send, injected into the compiler machinery at include time rather than built in Tooling and docs - reorganize sources, tests, and designs into subdirectories - unify windows entry points in tinyvm.bat and add a matching tinyvm.sh - add --help across the toolchain - polish the learning guide, stdlib docs, and design notes Tests - add accept, reject, and regression coverage across the compiler, LSP, - runtime, icon, actors, threads, formatting, and the toolchain contracts, plus fuzzers and property tests
Diffstat (limited to 'tests/drivers/format_test.js')
-rw-r--r--tests/drivers/format_test.js88
1 files changed, 88 insertions, 0 deletions
diff --git a/tests/drivers/format_test.js b/tests/drivers/format_test.js
new file mode 100644
index 0000000..94890ae
--- /dev/null
+++ b/tests/drivers/format_test.js
@@ -0,0 +1,88 @@
+// the pretty-printer: it never changes a program's tokens, it is stable (formatting twice is the same as once),
+// it follows .zcformat and the editor's tab settings, and it leaves asm blocks, strings and comments alone.
+// usage: node tests/drivers/format_test.js path/to/zclsp (run from the repository root)
+const assert = require('assert');
+const cp = require('child_process');
+const fs = require('fs');
+const os = require('os');
+const path = require('path');
+const server = process.argv[2];
+const root = path.resolve(__dirname, '..', '..');
+const format = (text, args = []) => cp.spawnSync(server, ['--format', ...args], { input: text, cwd: root, maxBuffer: 1 << 28 }).stdout.toString();
+// tokens of a source, white space dropped; the lines of a block comment are compared without their indent
+function tokens(src) {
+ const out = [];
+ let i = 0;
+ const n = src.length;
+ while (i < n) {
+ const c = src[i];
+ if (/\s/.test(c)) { i++; continue; }
+ if (c === '`') { out.push(src.slice(i)); break; }
+ if (c === '/' && src[i + 1] === '/') { let j = src.indexOf('\n', i); if (j < 0) j = n; out.push(src.slice(i, j).trimEnd()); i = j; continue; }
+ if (c === '/' && src[i + 1] === '*') { let j = src.indexOf('*/', i + 2); j = j < 0 ? n : j + 2; out.push(src.slice(i, j).split('\n').map(l => l.trim()).join('\n')); i = j; continue; }
+ if (c === '"' || c === "'") { let j = i + 1; while (j < n && src[j] !== c && src[j] !== '\n') { if (src[j] === '\\') j++; j++; } out.push(src.slice(i, j + 1)); i = j + 1; continue; }
+ if (/[A-Za-z0-9_\u0080-￿]/.test(c)) { let j = i; while (j < n && /[A-Za-z0-9_\u0080-￿]/.test(src[j])) j++; out.push(src.slice(i, j)); i = j; continue; }
+ out.push(c); i++;
+ }
+ // adjacent symbol characters are compared one by one, so merged or split operators still match
+ return out;
+}
+const files = [];
+for (const dir of ['examples', 'examples/learn', 'tests/programs', 'tests/errors', 'tests/typeerrors', 'tests/langerrors', 'src/lib', 'src/compiler', 'src/runtime', 'src/lsp', 'src/tools'])
+ for (const f of fs.readdirSync(path.join(root, dir))) if (f.endsWith('.zc')) files.push(path.join(dir, f));
+assert.ok(files.length > 100, 'the corpus: ' + files.length);
+let checked = 0;
+for (const f of files) {
+ const src = fs.readFileSync(path.join(root, f), 'utf8');
+ const once = format(src);
+ assert.deepStrictEqual(tokens(once), tokens(src), f + ': formatting changed the tokens');
+ assert.strictEqual(format(once), once, f + ': formatting is not stable');
+ assert.ok(once === '' || once.endsWith('\n'), f + ': ends with a line break');
+ if (!src.includes('`')) assert.ok(!/[ \t]+$/m.test(once), f + ': trailing white space');
+ checked++;
+}
+// the behaviour
+const sample = 'fn main(){\nint x=1+2 ;\n if(x>1){\n put_n( x );\n}\n\n\n\nreturn 0;}\n';
+assert.strictEqual(format(sample), 'fn main() {\n\tint x = 1+2;\n\tif (x>1) {\n\t\tput_n(x);\n\t}\n\n\treturn 0; }\n');
+assert.strictEqual(format('fn f(a,b){return a*b;}\n'), 'fn f(a, b) { return a*b; }\n');
+assert.strictEqual(format('fn main() {\n\tswitch (k) {\n\tcase 1: return 1;\n\tdefault:\n\t\treturn 2;\n\t}\n}\n'), 'fn main() {\n\tswitch (k) {\n\t\tcase 1: return 1;\n\t\tdefault:\n\t\t\treturn 2;\n\t}\n}\n');
+assert.strictEqual(format('fn main() {\nlet a = 1 +\n2;\nif (a)\nreturn 1;\nreturn 0;\n}\n'), 'fn main() {\n\tlet a = 1 +\n\t\t2;\n\tif (a)\n\t\treturn 1;\n\treturn 0;\n}\n');
+assert.strictEqual(format('fn put_f(x) { asm { x @ #1220 } return 0; }\n'), 'fn put_f(x) { asm { x @ #1220 } return 0; }\n');
+assert.strictEqual(format('fn k() { asm {\n 0 , ?\n} return 1; }\n'), 'fn k() { asm {\n\t\t0 , ?\n\t} return 1; }\n');
+assert.strictEqual(format('/* a\n * b\n */\nint g = 1; // note\nint h;\n'), '/* a\n * b\n */\nint g = 1; // note\nint h;\n');
+assert.strictEqual(format('fn main() {\n\tif (x)\n\t{\n\t\ty();\n\t}\n\telse\n\t{\n\t\tz();\n\t}\n}'), 'fn main() {\n\tif (x)\n\t{\n\t\ty();\n\t}\n\telse\n\t{\n\t\tz();\n\t}\n}\n');
+assert.strictEqual(format('fn main() { return 0; }\n`data stays\n as it is\n'), 'fn main() { return 0; }\n`data stays\n as it is\n');
+// .zcformat next to a document: found from the document's directory upward; the editor's tab settings apply without one
+const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'zcfmt-'));
+fs.mkdirSync(path.join(dir, 'a', 'b'), { recursive: true });
+fs.writeFileSync(path.join(dir, '.zcformat'), '# comment\nUseTab: false\nIndentWidth: 2\nMaxEmptyLines: 0\nNormalizeSpacing: false\n');
+const frame = m => { const b = Buffer.from(JSON.stringify(m)); return Buffer.concat([Buffer.from('Content-Length: ' + b.length + '\r\n\r\n'), b]); };
+function lsp(uri, text, options) {
+ const input = Buffer.concat([
+ { jsonrpc: '2.0', id: 1, method: 'initialize', params: {} },
+ { jsonrpc: '2.0', method: 'textDocument/didOpen', params: { textDocument: { uri, version: 1, languageId: 'zc', text } } },
+ { jsonrpc: '2.0', id: 2, method: 'textDocument/formatting', params: { textDocument: { uri }, options } },
+ { jsonrpc: '2.0', method: 'exit' }].map(frame));
+ const r = cp.spawnSync(server, process.argv.slice(3), { input, cwd: root, maxBuffer: 1 << 26 });
+ let d = r.stdout, caps, res;
+ while (d.length) {
+ const e = d.indexOf('\r\n\r\n'), n = Number(/Content-Length:\s*(\d+)/i.exec(d.subarray(0, e).toString())[1]);
+ const m = JSON.parse(d.subarray(e + 4, e + 4 + n)); d = d.subarray(e + 4 + n);
+ if (m.id === 1) caps = m.result.capabilities;
+ if (m.id === 2) res = m.result;
+ }
+ return { caps, res };
+}
+const messy = 'fn main(){\nif(1){\nput_n( 1 );\n\n\nput_c(10);}\nreturn 0;}\n';
+let r = lsp('file://' + path.join(dir, 'a', 'b', 'x.zc'), messy, { tabSize: 8, insertSpaces: false });
+assert.ok(r.caps.documentFormattingProvider, 'formatting is advertised');
+assert.strictEqual(r.res.length, 1);
+assert.strictEqual(r.res[0].newText, 'fn main(){\n if(1){\n put_n( 1 );\n put_c(10);}\n return 0;}\n', '.zcformat from two directories up wins over the editor');
+assert.deepStrictEqual(r.res[0].range.start, { line: 0, character: 0 });
+fs.rmSync(path.join(dir, '.zcformat'));
+r = lsp('file://' + path.join(dir, 'a', 'y.zc'), messy, { tabSize: 3, insertSpaces: true });
+assert.strictEqual(r.res[0].newText, 'fn main() {\n if (1) {\n put_n(1);\n\n put_c(10); }\n return 0; }\n', 'the editor settings without a .zcformat');
+r = lsp('file://' + path.join(dir, 'a', 'y.zc'), 'fn main() {\n\treturn 0;\n}\n', { tabSize: 4, insertSpaces: false });
+assert.deepStrictEqual(r.res, [], 'a formatted document gets no edit');
+fs.rmSync(dir, { recursive: true });
+console.log('ok formatter: ' + checked + ' files keep their tokens and format stably; options and edits');