|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as vscode from 'vscode'; |
| 3 | +import * as path from 'path'; |
| 4 | +import * as fs from 'fs'; |
| 5 | +import * as os from 'os'; |
| 6 | +import { extractModuleName } from '../../../src/formatters/tlaFormatter'; |
| 7 | +import { applyDocEdits, replaceDocContents } from '../document'; |
| 8 | + |
| 9 | +const UNFORMATTED = |
| 10 | + 'This is some text explaining the spec.\n' + |
| 11 | + '---------------------- MODULE HourClock ----------------------\n' + |
| 12 | + 'EXTENDS Naturals, TLC\n' + |
| 13 | + 'VARIABLE hr\n' + |
| 14 | + 'HCini == hr \\in (1 .. 12)\n' + |
| 15 | + 'HCnxt == hr\' = IF hr # 12 THEN hr + 1 ELSE 1\n' + |
| 16 | + 'HC == HCini /\\ [][HCnxt]_hr\n' + |
| 17 | + '--------------------------------------------------------------\n' + |
| 18 | + 'THEOREM HC => []HCini\n' + |
| 19 | + '==============================================================\n' + |
| 20 | + 'This is post text\n' + |
| 21 | + 'Has multiple lines in it.\n'; |
| 22 | + |
| 23 | +const EXPECTED = |
| 24 | + 'This is some text explaining the spec.\n' + |
| 25 | + '---------------------- MODULE HourClock ----------------------\n' + |
| 26 | + 'EXTENDS Naturals, TLC\n' + |
| 27 | + 'VARIABLE hr\n' + |
| 28 | + 'HCini == hr \\in ( 1 .. 12 )\n' + |
| 29 | + 'HCnxt == hr\' = IF hr # 12 THEN hr + 1 ELSE 1\n' + |
| 30 | + 'HC == HCini /\\ [] [HCnxt]_hr\n' + |
| 31 | + '--------------------------------------------------------------\n' + |
| 32 | + 'THEOREM HC => [] HCini\n' + |
| 33 | + '==============================================================\n' + |
| 34 | + 'This is post text\n' + |
| 35 | + 'Has multiple lines in it.\n'; |
| 36 | + |
| 37 | +async function formatDocument(doc: vscode.TextDocument): Promise<void> { |
| 38 | + const edits = await vscode.commands.executeCommand<vscode.TextEdit[]>( |
| 39 | + 'vscode.executeFormatDocumentProvider', |
| 40 | + doc.uri, |
| 41 | + { insertSpaces: true, tabSize: 4 } as vscode.FormattingOptions |
| 42 | + ); |
| 43 | + assert.ok(edits && edits.length > 0, `Expected formatting edits but got ${edits ? edits.length : 'null'}`); |
| 44 | + await applyDocEdits(doc.uri, edits); |
| 45 | +} |
| 46 | + |
| 47 | +suite('TLA+ Document Formatter Test Suite', () => { |
| 48 | + suite('extractModuleName', () => { |
| 49 | + test('Extracts module name from standard header', () => { |
| 50 | + const text = '---- MODULE MySpec ----\nVARIABLE x\n===='; |
| 51 | + assert.strictEqual(extractModuleName(text), 'MySpec'); |
| 52 | + }); |
| 53 | + |
| 54 | + test('Extracts module name with multiple dashes', () => { |
| 55 | + const text = '---------- MODULE TestModule ----------\n===='; |
| 56 | + assert.strictEqual(extractModuleName(text), 'TestModule'); |
| 57 | + }); |
| 58 | + |
| 59 | + test('Returns null for text without module declaration', () => { |
| 60 | + const text = 'VARIABLE x\nx == 1'; |
| 61 | + assert.strictEqual(extractModuleName(text), null); |
| 62 | + }); |
| 63 | + |
| 64 | + test('Extracts module name with underscores', () => { |
| 65 | + const text = '---- MODULE My_Spec_V2 ----\n===='; |
| 66 | + assert.strictEqual(extractModuleName(text), 'My_Spec_V2'); |
| 67 | + }); |
| 68 | + |
| 69 | + test('Extracts first module name when multiple present', () => { |
| 70 | + const text = '---- MODULE First ----\n---- MODULE Second ----\n===='; |
| 71 | + assert.strictEqual(extractModuleName(text), 'First'); |
| 72 | + }); |
| 73 | + |
| 74 | + test('Ignores MODULE keyword in comments', () => { |
| 75 | + const text = '\\* Notes: MODULE Fake\n---- MODULE RealSpec ----\n===='; |
| 76 | + assert.strictEqual(extractModuleName(text), 'RealSpec'); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + suite('Document formatting', () => { |
| 81 | + let tempDir: string; |
| 82 | + |
| 83 | + suiteSetup(async () => { |
| 84 | + // Trigger extension activation by opening a tlaplus document. |
| 85 | + const doc = await vscode.workspace.openTextDocument({ language: 'tlaplus' }); |
| 86 | + await vscode.window.showTextDocument(doc); |
| 87 | + await vscode.commands.executeCommand('workbench.action.closeActiveEditor'); |
| 88 | + }); |
| 89 | + |
| 90 | + setup(() => { |
| 91 | + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'tlaplus-fmt-test-')); |
| 92 | + }); |
| 93 | + |
| 94 | + teardown(async () => { |
| 95 | + await vscode.commands.executeCommand('workbench.action.closeActiveEditor'); |
| 96 | + if (fs.existsSync(tempDir)) { |
| 97 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + test('Formats a saved TLA+ file', async function() { |
| 102 | + const isWin = process.platform === 'win32'; |
| 103 | + this.timeout(isWin ? 60000 : 30000); |
| 104 | + |
| 105 | + const filePath = path.join(tempDir, 'HourClock.tla'); |
| 106 | + fs.writeFileSync(filePath, UNFORMATTED, 'utf-8'); |
| 107 | + |
| 108 | + const doc = await vscode.workspace.openTextDocument(filePath); |
| 109 | + if (doc.languageId !== 'tlaplus') { |
| 110 | + await vscode.languages.setTextDocumentLanguage(doc, 'tlaplus'); |
| 111 | + } |
| 112 | + await vscode.window.showTextDocument(doc); |
| 113 | + |
| 114 | + await formatDocument(doc); |
| 115 | + assert.strictEqual(doc.getText(), EXPECTED); |
| 116 | + }); |
| 117 | + |
| 118 | + test('Formats an unsaved TLA+ document', async function() { |
| 119 | + const isWin = process.platform === 'win32'; |
| 120 | + this.timeout(isWin ? 60000 : 30000); |
| 121 | + |
| 122 | + const doc = await vscode.workspace.openTextDocument({ language: 'tlaplus', content: UNFORMATTED }); |
| 123 | + await vscode.window.showTextDocument(doc); |
| 124 | + |
| 125 | + await formatDocument(doc); |
| 126 | + assert.strictEqual(doc.getText(), EXPECTED); |
| 127 | + }); |
| 128 | + |
| 129 | + test('Formats unsaved buffer modifications of a saved file', async function() { |
| 130 | + const isWin = process.platform === 'win32'; |
| 131 | + this.timeout(isWin ? 60000 : 30000); |
| 132 | + |
| 133 | + // Save a minimal placeholder to disk. |
| 134 | + const filePath = path.join(tempDir, 'HourClock.tla'); |
| 135 | + fs.writeFileSync(filePath, '---- MODULE HourClock ----\n====\n', 'utf-8'); |
| 136 | + |
| 137 | + const doc = await vscode.workspace.openTextDocument(filePath); |
| 138 | + if (doc.languageId !== 'tlaplus') { |
| 139 | + await vscode.languages.setTextDocumentLanguage(doc, 'tlaplus'); |
| 140 | + } |
| 141 | + await vscode.window.showTextDocument(doc); |
| 142 | + |
| 143 | + // Replace the buffer with unformatted content without saving. |
| 144 | + await replaceDocContents(doc, UNFORMATTED); |
| 145 | + assert.ok(doc.isDirty, 'Document should have unsaved changes'); |
| 146 | + |
| 147 | + await formatDocument(doc); |
| 148 | + assert.strictEqual(doc.getText(), EXPECTED); |
| 149 | + }); |
| 150 | + |
| 151 | + test('Does not format when formatter is disabled', async function() { |
| 152 | + const isWin = process.platform === 'win32'; |
| 153 | + this.timeout(isWin ? 60000 : 30000); |
| 154 | + |
| 155 | + const config = vscode.workspace.getConfiguration(); |
| 156 | + await config.update('tlaplus.formatter.enabled', false, vscode.ConfigurationTarget.Global); |
| 157 | + |
| 158 | + try { |
| 159 | + const doc = await vscode.workspace.openTextDocument({ language: 'tlaplus', content: UNFORMATTED }); |
| 160 | + await vscode.window.showTextDocument(doc); |
| 161 | + |
| 162 | + const edits = await vscode.commands.executeCommand<vscode.TextEdit[]>( |
| 163 | + 'vscode.executeFormatDocumentProvider', |
| 164 | + doc.uri, |
| 165 | + { insertSpaces: true, tabSize: 4 } as vscode.FormattingOptions |
| 166 | + ); |
| 167 | + |
| 168 | + const noEdits = !edits || edits.length === 0; |
| 169 | + assert.ok(noEdits, `Expected no edits when formatter is disabled but got ${edits?.length}`); |
| 170 | + assert.strictEqual(doc.getText(), UNFORMATTED, 'Document should remain unchanged'); |
| 171 | + } finally { |
| 172 | + await config.update('tlaplus.formatter.enabled', undefined, vscode.ConfigurationTarget.Global); |
| 173 | + } |
| 174 | + }); |
| 175 | + }); |
| 176 | +}); |
0 commit comments