-
-
Notifications
You must be signed in to change notification settings - Fork 430
/
Copy pathclang-formatter.test.ts
162 lines (148 loc) · 3.92 KB
/
clang-formatter.test.ts
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import {
Disposable,
DisposableCollection,
} from '@theia/core/lib/common/disposable';
import { FileUri } from '@theia/core/lib/common/file-uri';
import { expect } from 'chai';
import { promises as fs } from 'node:fs';
import path from 'node:path';
import temp from 'temp';
import {
clangFormatFilename,
ClangFormatter,
} from '../../node/clang-formatter';
import { spawnCommand } from '../../node/exec-util';
import { createBaseContainer, startDaemon } from './node-test-bindings';
const unformattedContent = `void setup ( ) { pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite( LED_BUILTIN , HIGH );
delay( 1000 ) ;
digitalWrite( LED_BUILTIN , LOW);
delay ( 1000 ) ;
}
`;
const formattedContent = `void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
`;
type ClangStyleValue =
| string
| number
| boolean
| ClangStyleValue[]
| { [key: string]: ClangStyleValue };
type ClangConfiguration = Record<string, ClangStyleValue>;
export interface ClangStyle {
readonly key: string;
readonly value: ClangStyleValue;
}
const singleClangStyles: ClangStyle[] = [
{
key: 'SpacesBeforeTrailingComments',
value: 0,
},
{
key: 'SortIncludes',
value: 'Never',
},
{
key: 'AlignTrailingComments',
value: true,
},
{
key: 'IfMacros',
value: ['KJ_IF_MAYBE'],
},
{
key: 'SpacesInLineCommentPrefix',
value: {
Minimum: 0,
Maximum: -1,
},
},
];
async function expectNoChanges(
formatter: ClangFormatter,
styleArg: string
): Promise<void> {
const minimalContent = `
void setup() {}
void loop() {}
`.trim();
const execPath = formatter['execPath']();
const actual = await spawnCommand(
execPath,
['-style', styleArg],
console.error,
minimalContent
);
expect(actual).to.be.equal(minimalContent);
}
describe('clang-formatter', () => {
let tracked: typeof temp;
let formatter: ClangFormatter;
let toDispose: DisposableCollection;
before(async () => {
tracked = temp.track();
toDispose = new DisposableCollection(
Disposable.create(() => tracked.cleanupSync())
);
const container = await createBaseContainer({
additionalBindings: (bind) =>
bind(ClangFormatter).toSelf().inSingletonScope(),
});
await startDaemon(container, toDispose);
formatter = container.get<ClangFormatter>(ClangFormatter);
});
after(() => toDispose.dispose());
singleClangStyles
.map((style) => ({
...style,
styleArg: JSON.stringify({ [style.key]: style.value }),
}))
.map(({ value, styleArg }) =>
it(`should execute the formatter with a single ${
Array.isArray(value) ? 'array' : typeof value
} type style configuration value: ${styleArg}`, async () => {
await expectNoChanges(formatter, styleArg);
})
);
it('should execute the formatter with a multiple clang formatter styles', async () => {
const styleArg = JSON.stringify(
singleClangStyles.reduce((config, curr) => {
config[curr.key] = curr.value;
return config;
}, {} as ClangConfiguration)
);
await expectNoChanges(formatter, styleArg);
});
it('should format with the default styles', async () => {
const actual = await formatter.format({
content: unformattedContent,
formatterConfigFolderUris: [],
});
expect(actual).to.be.equal(formattedContent);
});
it('should format with custom formatter configuration file', async () => {
const tempPath = tracked.mkdirSync();
await fs.writeFile(
path.join(tempPath, clangFormatFilename),
'SpaceInEmptyParentheses: true',
{
encoding: 'utf8',
}
);
const actual = await formatter.format({
content: 'void foo() {}',
formatterConfigFolderUris: [FileUri.create(tempPath).toString()],
});
expect(actual).to.be.equal('void foo( ) {}');
});
});