Skip to content

Commit c7f7215

Browse files
committed
Add unit tests (will conflict with microsoft#4241)
1 parent acbe130 commit c7f7215

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

src/client/extension.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ durations.codeLoadingTime = stopWatch.elapsedTime;
112112
const activationDeferred = createDeferred<void>();
113113
let activatedServiceContainer: ServiceContainer | undefined;
114114

115+
export const INCREASE_INDENT_REGEX = /^\s*(?:def|class|for|if|elif|else|while|try|with|finally|except|async)\b.*:\s*/;
116+
export const DECREASE_INDENT_REGEX = /^\s*(?:elif|else)\b.*:\s*/;
117+
115118
export async function activate(context: ExtensionContext): Promise<IExtensionApi> {
116119
try {
117120
return await activateUnsafe(context);
@@ -193,8 +196,8 @@ async function activateUnsafe(context: ExtensionContext): Promise<IExtensionApi>
193196
}
194197
],
195198
indentationRules: {
196-
increaseIndentPattern: /^\s*(?:def|class|for|if|elif|else|while|try|with|finally|except|async)\b.*:\s*/,
197-
decreaseIndentPattern: /^\s*(?:elif|else)\b.*:\s*/
199+
increaseIndentPattern: INCREASE_INDENT_REGEX,
200+
decreaseIndentPattern: DECREASE_INDENT_REGEX
198201
}
199202
});
200203

src/test/extension.unit.test.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@
66
// tslint:disable:no-any
77

88
import { expect } from 'chai';
9+
import * as sinon from 'sinon';
910
import { buildApi } from '../client/api';
1011
import { EXTENSION_ROOT_DIR } from '../client/common/constants';
1112

13+
// Stub sourceMapSupport.initialize before we import from extension.ts (we don't actually need it)
14+
// tslint:disable-next-line: no-require-imports no-var-requires
15+
const sourceMapSupport = require('../client/sourceMapSupport');
16+
sinon.stub(sourceMapSupport, 'initialize');
17+
import { DECREASE_INDENT_REGEX, INCREASE_INDENT_REGEX } from '../client/extension';
18+
1219
const expectedPath = `${EXTENSION_ROOT_DIR.fileToCommandArgument()}/pythonFiles/ptvsd_launcher.py`;
1320

1421
suite('Extension API Debugger', () => {
@@ -22,4 +29,84 @@ suite('Extension API Debugger', () => {
2229
const expectedArgs = [expectedPath, '--default', '--host', 'something', '--port', '1234', '--wait'];
2330
expect(args).to.be.deep.equal(expectedArgs);
2431
});
32+
[
33+
{
34+
keyword: 'def',
35+
line: 'def foo:',
36+
dedent: false
37+
},
38+
{
39+
keyword: 'class',
40+
line: 'class TestClass:',
41+
dedent: false
42+
},
43+
{
44+
keyword: 'for',
45+
line: 'for item in items:',
46+
dedent: false
47+
},
48+
{
49+
keyword: 'if',
50+
line: 'if foo is None:',
51+
dedent: false
52+
},
53+
{
54+
keyword: 'elif',
55+
line: 'elif x < 5:',
56+
dedent: true
57+
},
58+
{
59+
keyword: 'else',
60+
line: 'else:',
61+
dedent: true
62+
},
63+
{
64+
keyword: 'while',
65+
line: 'while \'::\' in macaddress:',
66+
dedent: false
67+
},
68+
{
69+
keyword: 'try',
70+
line: 'try:',
71+
dedent: false
72+
},
73+
{
74+
keyword: 'with',
75+
line: 'with self.test:',
76+
dedent: false
77+
},
78+
{
79+
keyword: 'finally',
80+
line: 'finally:',
81+
dedent: false
82+
},
83+
{
84+
keyword: 'except',
85+
line: 'except TestError:',
86+
dedent: false
87+
},
88+
{
89+
keyword: 'async',
90+
line: 'async def test(self):',
91+
dedent: false
92+
}
93+
].forEach(({keyword, line, dedent}) => {
94+
test(`Increase indent regex should pick up lines containing the ${keyword} keyword`, async () => {
95+
const result = INCREASE_INDENT_REGEX.test(line);
96+
expect(result).to.be.equal(true, `Increase indent regex should pick up lines containing the ${keyword} keyword`);
97+
});
98+
99+
test(`Decrease indent regex should ${dedent ? '' : 'not '}pick up lines containing the ${keyword} keyword`, async () => {
100+
const result = DECREASE_INDENT_REGEX.test(line);
101+
expect(result).to.be.equal(dedent, `Decrease indent regex should ${dedent ? '' : 'not '}pick up lines containing the ${keyword} keyword`);
102+
});
103+
});
104+
test('Increase indent regex should not pick up lines without keywords', async () => {
105+
const result = INCREASE_INDENT_REGEX.test('a = \'hello \\n \'');
106+
expect(result).to.be.equal(false, 'Increase indent regex should not pick up lines without keywords');
107+
});
108+
test('Decrease indent regex should not pick up lines without keywords', async () => {
109+
const result = DECREASE_INDENT_REGEX.test('a = \'hello \\n \'');
110+
expect(result).to.be.equal(false, 'Decrease indent regex should not pick up lines without keywords');
111+
});
25112
});

0 commit comments

Comments
 (0)