forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.helper.test.ts
More file actions
114 lines (97 loc) · 4.75 KB
/
format.helper.test.ts
File metadata and controls
114 lines (97 loc) · 4.75 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
import * as assert from 'assert';
import * as TypeMoq from 'typemoq';
import { IConfigurationService, IFormattingSettings, Product } from '../../client/common/types';
import * as EnumEx from '../../client/common/utils/enum';
import { FormatterHelper } from '../../client/formatters/helper';
import { FormatterId } from '../../client/formatters/types';
import { getExtensionSettings } from '../common';
import { initialize } from '../initialize';
import { UnitTestIocContainer } from '../testing/serviceRegistry';
// tslint:disable-next-line:max-func-body-length
suite('Formatting - Helper', () => {
let ioc: UnitTestIocContainer;
let formatHelper: FormatterHelper;
suiteSetup(initialize);
setup(() => {
ioc = new UnitTestIocContainer();
const config = TypeMoq.Mock.ofType<IConfigurationService>();
config.setup(x => x.getSettings(TypeMoq.It.isAny())).returns(() => getExtensionSettings(undefined));
ioc.serviceManager.addSingletonInstance<IConfigurationService>(IConfigurationService, config.object);
formatHelper = new FormatterHelper(ioc.serviceManager);
});
test('Ensure product is set in Execution Info', async () => {
[Product.autopep8, Product.black, Product.yapf].forEach(formatter => {
const info = formatHelper.getExecutionInfo(formatter, []);
assert.equal(info.product, formatter, `Incorrect products for ${formatHelper.translateToId(formatter)}`);
});
});
test('Ensure executable is set in Execution Info', async () => {
const settings = getExtensionSettings(undefined);
[Product.autopep8, Product.black, Product.yapf].forEach(formatter => {
const info = formatHelper.getExecutionInfo(formatter, []);
const names = formatHelper.getSettingsPropertyNames(formatter);
const execPath = settings.formatting[names.pathName] as string;
assert.equal(
info.execPath,
execPath,
`Incorrect executable paths for product ${formatHelper.translateToId(formatter)}`
);
});
});
test('Ensure arguments are set in Execution Info', async () => {
const settings = getExtensionSettings(undefined);
const customArgs = ['1', '2', '3'];
[Product.autopep8, Product.black, Product.yapf].forEach(formatter => {
const names = formatHelper.getSettingsPropertyNames(formatter);
const args: string[] = Array.isArray(settings.formatting[names.argsName])
? (settings.formatting[names.argsName] as string[])
: [];
const expectedArgs = args.concat(customArgs).join(',');
assert.equal(
expectedArgs.endsWith(customArgs.join(',')),
true,
`Incorrect custom arguments for product ${formatHelper.translateToId(formatter)}`
);
});
});
test('Ensure correct setting names are returned', async () => {
[Product.autopep8, Product.black, Product.yapf].forEach(formatter => {
const translatedId = formatHelper.translateToId(formatter)!;
const settings = {
argsName: `${translatedId}Args` as keyof IFormattingSettings,
pathName: `${translatedId}Path` as keyof IFormattingSettings
};
assert.deepEqual(
formatHelper.getSettingsPropertyNames(formatter),
settings,
`Incorrect settings for product ${formatHelper.translateToId(formatter)}`
);
});
});
test('Ensure translation of ids works', async () => {
const formatterMapping = new Map<Product, FormatterId>();
formatterMapping.set(Product.autopep8, 'autopep8');
formatterMapping.set(Product.black, 'black');
formatterMapping.set(Product.yapf, 'yapf');
[Product.autopep8, Product.black, Product.yapf].forEach(formatter => {
const translatedId = formatHelper.translateToId(formatter);
assert.equal(
translatedId,
formatterMapping.get(formatter)!,
`Incorrect translation for product ${formatHelper.translateToId(formatter)}`
);
});
});
EnumEx.getValues<Product>(Product).forEach(product => {
const formatterMapping = new Map<Product, FormatterId>();
formatterMapping.set(Product.autopep8, 'autopep8');
formatterMapping.set(Product.black, 'black');
formatterMapping.set(Product.yapf, 'yapf');
if (formatterMapping.has(product)) {
return;
}
test(`Ensure translation of ids throws exceptions for unknown formatters (${product})`, async () => {
assert.throws(() => formatHelper.translateToId(product));
});
});
});