vscode-omega
aien-dev/vscode-omega
Brought by drakestapleton
vscode-omega / test / vscode / suite.js
| 1 | 'use strict'; |
| 2 | // Inside VS Code: the .om association, omc diagnostics through the omega.check command, and Format |
| 3 | // Document through om2 fmt. With a fake omc (negative control) the diagnostic check must fail. |
| 4 | const assert = require('assert'); |
| 5 | const fs = require('fs'); |
| 6 | const os = require('os'); |
| 7 | const path = require('path'); |
| 8 | const vscode = require('vscode'); |
| 9 | |
| 10 | async function waitFor(fn, ms = 15000) { |
| 11 | const end = Date.now() + ms; |
| 12 | for (;;) { |
| 13 | const v = fn(); |
| 14 | if (v) return v; |
| 15 | if (Date.now() > end) return v; |
| 16 | await new Promise((r) => setTimeout(r, 100)); |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | async function open(dir, name, text) { |
| 21 | const file = path.join(dir, name); |
| 22 | fs.writeFileSync(file, text); |
| 23 | const doc = await vscode.workspace.openTextDocument(vscode.Uri.file(file)); |
| 24 | await vscode.window.showTextDocument(doc); |
| 25 | return doc; |
| 26 | } |
| 27 | |
| 28 | exports.run = async function run() { |
| 29 | const ext = vscode.extensions.getExtension('aien-dev.omega'); |
| 30 | assert.ok(ext, 'extension aien-dev.omega is loaded'); |
| 31 | await vscode.workspace.getConfiguration('omega').update('omcPath', process.env.OMC, vscode.ConfigurationTarget.Global); |
| 32 | const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'omega-vscode-')); |
| 33 | |
| 34 | const bad = await open(dir, 'bad.om', 'fn main to int\nret (5\nend\n'); |
| 35 | assert.strictEqual(bad.languageId, 'omega', '.om opens as omega'); |
| 36 | await ext.activate(); |
| 37 | await vscode.commands.executeCommand('omega.check'); |
| 38 | const diags = await waitFor(() => { |
| 39 | const d = vscode.languages.getDiagnostics(bad.uri); |
| 40 | return d.length ? d : null; |
| 41 | }); |
| 42 | assert.ok(diags && diags.length === 1, `one diagnostic for bad.om, got ${JSON.stringify(diags)}`); |
| 43 | assert.strictEqual(diags[0].range.start.line, 2, 'omc reports line 3 (0-based 2)'); |
| 44 | assert.strictEqual(diags[0].message, 'omc error 16'); |
| 45 | assert.strictEqual(diags[0].source, 'omc'); |
| 46 | console.log('PASS diagnostic', diags[0].range.start.line + 1, diags[0].message); |
| 47 | |
| 48 | const good = await open(dir, 'good.om', 'fn main to int\nret 0\nend\n'); |
| 49 | await vscode.commands.executeCommand('omega.check'); |
| 50 | await new Promise((r) => setTimeout(r, 1500)); |
| 51 | assert.deepStrictEqual(vscode.languages.getDiagnostics(good.uri), [], 'no diagnostic for an accepted unit'); |
| 52 | console.log('PASS accepted unit has no diagnostic'); |
| 53 | |
| 54 | const messy = await open(dir, 'messy.om', 'note keep this\nfn main to int\n ret 0\nend\n'); |
| 55 | const edits = await vscode.commands.executeCommand('vscode.executeFormatDocumentProvider', messy.uri, { |
| 56 | tabSize: 4, |
| 57 | insertSpaces: true, |
| 58 | }); |
| 59 | // VS Code may shrink the provider's whole-document edit into smaller ones, so apply them and compare. |
| 60 | assert.ok(edits && edits.length > 0, `formatting edits, got ${JSON.stringify(edits)}`); |
| 61 | const we = new vscode.WorkspaceEdit(); |
| 62 | we.set(messy.uri, edits); |
| 63 | assert.ok(await vscode.workspace.applyEdit(we), 'formatting edits apply'); |
| 64 | assert.strictEqual(messy.getText(), 'note keep this\nfn main to int\n ret 0\nend\n'); |
| 65 | console.log('PASS format', edits.length, 'edit(s)'); |
| 66 | }; |