forked from redhat-developer/yaml-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatter.test.ts
More file actions
133 lines (118 loc) · 3.73 KB
/
formatter.test.ts
File metadata and controls
133 lines (118 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { setupLanguageService, setupTextDocument } from './utils/testHelper';
import { ServiceSetup } from './utils/serviceSetup';
import * as assert from 'assert';
import { TextEdit } from 'vscode-languageserver-types';
import { SettingsState, TextDocumentTestManager } from '../src/yamlSettings';
import { LanguageHandlers } from '../src/languageserver/handlers/languageHandlers';
describe('Formatter Tests', () => {
let languageHandler: LanguageHandlers;
let yamlSettings: SettingsState;
before(() => {
const languageSettingsSetup = new ServiceSetup().withFormat();
const { languageHandler: langHandler, yamlSettings: settings } = setupLanguageService(languageSettingsSetup.languageSettings);
languageHandler = langHandler;
yamlSettings = settings;
});
// Tests for formatter
describe('Formatter', function () {
describe('Test that formatter works with custom tags', function () {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function parseSetup(content: string, options: any = {}): TextEdit[] {
const testTextDocument = setupTextDocument(content);
yamlSettings.documents = new TextDocumentTestManager();
(yamlSettings.documents as TextDocumentTestManager).set(testTextDocument);
yamlSettings.yamlFormatterSettings = options;
return languageHandler.formatterHandler({
options,
textDocument: testTextDocument,
});
}
it('Formatting works without custom tags', () => {
const content = 'cwd: test';
const edits = parseSetup(content);
assert.notEqual(edits.length, 0);
assert.equal(edits[0].newText, 'cwd: test\n');
});
it('Formatting works with custom tags', () => {
const content = 'cwd: !Test test';
const edits = parseSetup(content);
assert.notEqual(edits.length, 0);
assert.equal(edits[0].newText, 'cwd: !Test test\n');
});
it('Formatting wraps text', () => {
const content = `comments: >
test test test test test test test test test test test test`;
const edits = parseSetup(content, {
printWidth: 20,
proseWrap: 'always',
});
assert.equal(edits[0].newText, 'comments: >\n test test test test\n test test test test\n test test test test\n');
});
it('Formatting uses tabSize', () => {
const content = `map:
k1: v1
k2: v2
list:
- item1
- item2
`;
const edits = parseSetup(content, {
tabSize: 5,
});
const expected = `map:
k1: v1
k2: v2
list:
- item1
- item2
`;
assert.equal(edits[0].newText, expected);
});
it('Formatting uses tabWidth', () => {
const content = `map:
k1: v1
k2: v2
list:
- item1
- item2
`;
const edits = parseSetup(content, {
tabWidth: 5,
});
const expected = `map:
k1: v1
k2: v2
list:
- item1
- item2
`;
assert.equal(edits[0].newText, expected);
});
it('Formatting uses tabWidth over tabSize', () => {
const content = `map:
k1: v1
k2: v2
list:
- item1
- item2
`;
const edits = parseSetup(content, {
tabSize: 3,
tabWidth: 5,
});
const expected = `map:
k1: v1
k2: v2
list:
- item1
- item2
`;
assert.equal(edits[0].newText, expected);
});
});
});
});