|
| 1 | +import * as assert from "assert"; |
| 2 | +import * as vscode from "vscode"; |
| 3 | +import { ensureExtensionActivated } from "./testUtils.js"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Tests for untitled documents (unsaved files) |
| 7 | + */ |
| 8 | +describe("Test untitled documents", () => { |
| 9 | + before(async () => { |
| 10 | + await ensureExtensionActivated(); |
| 11 | + }); |
| 12 | + |
| 13 | + it("formats untitled JavaScript document", async () => { |
| 14 | + // Create an untitled document with ugly JavaScript |
| 15 | + const uglyCode = 'const x = 1 ; console.log( x ) ;'; |
| 16 | + const doc = await vscode.workspace.openTextDocument({ |
| 17 | + language: "javascript", |
| 18 | + content: uglyCode, |
| 19 | + }); |
| 20 | + |
| 21 | + const editor = await vscode.window.showTextDocument(doc); |
| 22 | + |
| 23 | + // Format the document |
| 24 | + await vscode.commands.executeCommand("editor.action.formatDocument"); |
| 25 | + |
| 26 | + // The document should be formatted |
| 27 | + const formattedText = editor.document.getText(); |
| 28 | + |
| 29 | + assert.notEqual( |
| 30 | + formattedText, |
| 31 | + uglyCode, |
| 32 | + "Untitled document should have been formatted", |
| 33 | + ); |
| 34 | + |
| 35 | + // Should not have excessive spacing |
| 36 | + assert.ok( |
| 37 | + !formattedText.includes(" "), |
| 38 | + "Formatted code should not have excessive spacing", |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + it("formats untitled TypeScript document", async () => { |
| 43 | + // Create an untitled document with ugly TypeScript |
| 44 | + const uglyCode = 'function test ( a : number ) : number { return a * 2 ; }'; |
| 45 | + const doc = await vscode.workspace.openTextDocument({ |
| 46 | + language: "typescript", |
| 47 | + content: uglyCode, |
| 48 | + }); |
| 49 | + |
| 50 | + const editor = await vscode.window.showTextDocument(doc); |
| 51 | + |
| 52 | + // Format the document |
| 53 | + await vscode.commands.executeCommand("editor.action.formatDocument"); |
| 54 | + |
| 55 | + // The document should be formatted |
| 56 | + const formattedText = editor.document.getText(); |
| 57 | + |
| 58 | + assert.notEqual( |
| 59 | + formattedText, |
| 60 | + uglyCode, |
| 61 | + "Untitled TypeScript document should have been formatted", |
| 62 | + ); |
| 63 | + |
| 64 | + // Should not have excessive spacing |
| 65 | + assert.ok( |
| 66 | + !formattedText.includes(" "), |
| 67 | + "Formatted code should not have excessive spacing", |
| 68 | + ); |
| 69 | + }); |
| 70 | +}); |
0 commit comments