interplane.Get Omega
Read in your language

vscode-omega

aien-dev/vscode-omega

Brought by drakestapleton

branch main Branches Tags History

vscode-omega / test / omc.test.js

137 lines · 5048 bytes Raw Blame History
1'use strict';
2// Tests for omc.js and the snippets. The omc tests need a real omc in $OMC (CI births it from the
3// 408-byte seed of aien-dev/omega-mv at omega.ref); with OMEGA_REQUIRE_OMC=1 a missing $OMC fails.
4const test = require('node:test');
5const assert = require('node:assert/strict');
6const fs = require('fs');
7const path = require('path');
8const omc = require('../omc');
9
10const OMC = process.env.OMC || '';
11const haveOmc = OMC !== '' && omc.findOmc(OMC) !== null;
12if (!haveOmc && process.env.OMEGA_REQUIRE_OMC === '1') {
13 throw new Error(`OMEGA_REQUIRE_OMC=1 but $OMC (${JSON.stringify(OMC)}) is not an executable file`);
14}
15const needOmc = haveOmc ? {} : { skip: 'set $OMC to an omc binary' };
16
17test('parseDiagnostics: human line only', () => {
18 assert.deepEqual(omc.parseDiagnostics('omc: line 3: error 16\n'), [{ line: 3, code: 16 }]);
19});
20
21test('parseDiagnostics: om2 diag record replaces its human line', () => {
22 const err =
23 'omc: line 3: error 4\n' +
24 'omc-diag 1 code=4 sev=incomplete line=3 col=14 span=37-37 expect=value found=- obligation=- profile=OM-2\n';
25 assert.deepEqual(omc.parseDiagnostics(err), [
26 {
27 line: 3,
28 code: 4,
29 sev: 'incomplete',
30 col: 14,
31 span: [37, 37],
32 detail: 'expect=value found=- obligation=- profile=OM-2',
33 },
34 ]);
35});
36
37test('parseDiagnostics: unrelated stderr gives nothing', () => {
38 assert.deepEqual(omc.parseDiagnostics('Segmentation fault\nomc: line x: error 4\n'), []);
39});
40
41test('findOmc: explicit path that is not executable gives null', () => {
42 assert.equal(omc.findOmc(path.join(__dirname, 'no-such-omc')), null);
43 assert.equal(omc.findOmc('', ''), null);
44});
45
46test('check: an accepted unit has no diagnostics', needOmc, async () => {
47 const r = await omc.check(OMC, 'fn main to int\nret 0\nend\n');
48 assert.equal(r.ok, true);
49 assert.equal(r.exit, 0);
50 assert.deepEqual(r.diagnostics, []);
51});
52
53test('check: unclosed paren (omega-mv seed/omc/tests/paren/neg_unclosed.om) is error 16 at line 3', needOmc, async () => {
54 const r = await omc.check(OMC, 'fn main to int\nret (5\nend\n');
55 assert.equal(r.ok, false);
56 assert.equal(r.exit, 16);
57 assert.deepEqual(r.diagnostics, [{ line: 3, code: 16 }]);
58});
59
60test('check: om2 diag unit cut at end of input (seed/omc/om2/s1/diag-eof.om) gives a column', needOmc, async () => {
61 const r = await omc.check(OMC, 'om2 diag\nfn main to int\nret add int 1');
62 assert.equal(r.exit, 4);
63 assert.equal(r.diagnostics.length, 1);
64 assert.deepEqual([r.diagnostics[0].line, r.diagnostics[0].col, r.diagnostics[0].sev], [3, 14, 'incomplete']);
65});
66
67test('format: blanks between words become one space, notes and indentation kept', needOmc, async () => {
68 const r = await omc.format(OMC, 'note hi there\nfn main to int\n ret 0\nend\n');
69 assert.equal(r.ok, true);
70 assert.equal(r.text, 'note hi there\nfn main to int\n ret 0\nend\n');
71 const again = await omc.format(OMC, r.text);
72 assert.equal(again.text, r.text, 'fmt is idempotent');
73});
74
75test('format: a refused unit is not rewritten and its line is the buffer line', needOmc, async () => {
76 const r = await omc.format(OMC, 'fn main to int\nret (5\nend\n');
77 assert.equal(r.ok, false);
78 assert.equal(r.exit, 16);
79 assert.deepEqual(r.diagnostics, [{ line: 3, code: 16 }]);
80 assert.equal(r.text, undefined);
81});
82
83// ---- snippets: expand every placeholder to its default and compile the result ----
84const snippets = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'snippets', 'omega.json'), 'utf8'));
85
86function expand(name, fill = '') {
87 let s = snippets[name].body.join('\n');
88 s = s.replace(/\$0|\$\{0\}/g, '\u0000');
89 for (let prev = ''; prev !== s; ) {
90 prev = s;
91 s = s.replace(/\$\{\d+:([^${}]*)\}/g, '$1').replace(/\$\{\d+\|([^,|]*)[^|]*\|\}/g, '$1');
92 }
93 s = s.replace(/\$\d+/g, '');
94 return s.replace('\u0000', fill);
95}
96
97test('snippets: every snippet has a prefix, body and description', () => {
98 for (const [k, v] of Object.entries(snippets)) {
99 assert.ok(v.prefix && Array.isArray(v.body) && v.description, k);
100 }
101});
102
103test('snippets: OM-1 snippets expand to a unit omc accepts', needOmc, async () => {
104 const body = [
105 'a = int 1',
106 'b = int 0',
107 expand('if', 'a = 2'),
108 expand('if else', 'a = 3'),
109 expand('while bound', 'b = 0'),
110 expand('for', 'a = 4'),
111 expand('note', 'inside main'),
112 ].join('\n');
113 const unit = [
114 expand('struct'),
115 expand('text'),
116 expand('function', 'ret x'),
117 expand('main', body),
118 '',
119 ].join('\n');
120 const r = await omc.check(OMC, unit);
121 assert.deepEqual([r.ok, r.diagnostics], [true, []], unit);
122});
123
124test('snippets: OM-2 snippets expand to a unit omc accepts', needOmc, async () => {
125 const unit = [
126 expand('om2 profile'),
127 expand('layout'),
128 'fn f s edit uint 8 fresh to uint',
129 expand('for unroll', 'put s i add i 1'),
130 'ret get s 7',
131 'end',
132 expand('main'),
133 '',
134 ].join('\n');
135 const r = await omc.check(OMC, unit);
136 assert.deepEqual([r.ok, r.diagnostics], [true, []], unit);
137});