|
4 | 4 | *--------------------------------------------------------------------------------------------*/ |
5 | 5 | import * as assert from 'assert'; |
6 | 6 | import * as sinon from 'sinon'; |
7 | | -import { TextEdit } from 'vscode-languageserver-types'; |
| 7 | +import { FormattingOptions, TextEdit } from 'vscode-languageserver-types'; |
| 8 | +import { CustomFormatterOptions } from '../src'; |
8 | 9 | import { LanguageHandlers } from '../src/languageserver/handlers/languageHandlers'; |
9 | 10 | import { SettingsState, TextDocumentTestManager } from '../src/yamlSettings'; |
10 | 11 | import { ServiceSetup } from './utils/serviceSetup'; |
@@ -38,13 +39,16 @@ describe('Formatter Tests', () => { |
38 | 39 | describe('Formatter', function () { |
39 | 40 | describe('Test that formatter works with custom tags', function () { |
40 | 41 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
41 | | - function parseSetup(content: string, options: any = {}): Promise<TextEdit[]> { |
| 42 | + function parseSetup( |
| 43 | + content: string, |
| 44 | + options: Partial<FormattingOptions | CustomFormatterOptions> = {} |
| 45 | + ): Promise<TextEdit[]> { |
42 | 46 | const testTextDocument = setupTextDocument(content); |
43 | 47 | yamlSettings.documents = new TextDocumentTestManager(); |
44 | 48 | (yamlSettings.documents as TextDocumentTestManager).set(testTextDocument); |
45 | | - yamlSettings.yamlFormatterSettings = options; |
| 49 | + yamlSettings.yamlFormatterSettings = options as CustomFormatterOptions; |
46 | 50 | return languageHandler.formatterHandler({ |
47 | | - options, |
| 51 | + options: options as FormattingOptions, |
48 | 52 | textDocument: testTextDocument, |
49 | 53 | }); |
50 | 54 | } |
@@ -98,7 +102,7 @@ describe('Formatter Tests', () => { |
98 | 102 | } |
99 | 103 | `; |
100 | 104 | const edits = await parseSetup(content, { singleQuote: true }); |
101 | | - assert.equal(edits[0].newText, content); |
| 105 | + assert.equal(edits.length, 0); |
102 | 106 | }); |
103 | 107 |
|
104 | 108 | it('Formatting handles trailing commas (disabled)', async () => { |
@@ -194,6 +198,82 @@ list: |
194 | 198 | `; |
195 | 199 | assert.equal(edits[0].newText, expected); |
196 | 200 | }); |
| 201 | + |
| 202 | + it("Formatting doesn't replace escaped newlines with real ones", async () => { |
| 203 | + const content = `- name: Example task |
| 204 | + set_fact: |
| 205 | + my_var: "{{ content | regex_replace('\\\\\\\\n', '\\n') }}" |
| 206 | +`; |
| 207 | + |
| 208 | + const edits = await parseSetup(content, { |
| 209 | + tabSize: 1, |
| 210 | + tabWidth: 2, |
| 211 | + }); |
| 212 | + |
| 213 | + assert.equal(edits.length, 0, `Edits: ${JSON.stringify(edits)}`); |
| 214 | + }); |
| 215 | + it("Formatting doesn't strip trailing zeros of floats", async () => { |
| 216 | + const content = `value: 1.0e+4 |
| 217 | +`; |
| 218 | + |
| 219 | + const edits = await parseSetup(content, { |
| 220 | + tabSize: 1, |
| 221 | + tabWidth: 2, |
| 222 | + }); |
| 223 | + |
| 224 | + assert.equal(edits.length, 0, `Edits: ${JSON.stringify(edits)}`); |
| 225 | + }); |
| 226 | + it("Formatting doesn't convert long integers to floats", async () => { |
| 227 | + const content = `value: 12345678901234567890 |
| 228 | +`; |
| 229 | + |
| 230 | + const edits = await parseSetup(content, { |
| 231 | + tabSize: 1, |
| 232 | + tabWidth: 2, |
| 233 | + }); |
| 234 | + |
| 235 | + assert.equal(edits.length, 0, `Edits: ${JSON.stringify(edits)}`); |
| 236 | + }); |
| 237 | + it("Formatting respects 'no prose wrap' setting", async () => { |
| 238 | + const content = `value: aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa |
| 239 | +`; |
| 240 | + |
| 241 | + const edits = await parseSetup(content, { |
| 242 | + tabSize: 1, |
| 243 | + tabWidth: 2, |
| 244 | + proseWrap: 'never', |
| 245 | + }); |
| 246 | + |
| 247 | + assert.equal(edits.length, 0, `Edits: ${JSON.stringify(edits)}`); |
| 248 | + |
| 249 | + const edits2 = await parseSetup(content, { |
| 250 | + tabSize: 1, |
| 251 | + tabWidth: 2, |
| 252 | + proseWrap: 'preserve', |
| 253 | + }); |
| 254 | + |
| 255 | + assert.equal(edits2.length, 0, `Edits: ${JSON.stringify(edits)}`); |
| 256 | + }); |
| 257 | + it('Formatting keeps comments on the same line', async () => { |
| 258 | + const content = `0002-https-from-avdpool-to-server: # Allow HTTPS access from AVD Pool 2 to Qlik server |
| 259 | + name: 0002-https-from-avdpool-to-server |
| 260 | + source_addresses: |
| 261 | + - 1.2.3.4/32 |
| 262 | + protocols: |
| 263 | + - TCP |
| 264 | + destination_ports: |
| 265 | + - "443" |
| 266 | + destination_addresses: |
| 267 | + - 4.5.6.7/32 |
| 268 | +`; |
| 269 | + |
| 270 | + const edits = await parseSetup(content, { |
| 271 | + tabSize: 1, |
| 272 | + tabWidth: 2, |
| 273 | + }); |
| 274 | + |
| 275 | + assert.equal(edits.length, 0, `Edits: ${JSON.stringify(edits)}`); |
| 276 | + }); |
197 | 277 | }); |
198 | 278 | }); |
199 | 279 | }); |
0 commit comments