Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/parser/test/node/getStructure.cpp.spec.ts
13405 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { outdent } from 'outdent';
7
import { afterAll, describe, expect, test } from 'vitest';
8
import { _dispose } from '../../node/parserImpl';
9
import { WASMLanguage } from '../../node/treeSitterLanguages';
10
import { fromFixture, snapshotPathInFixture, srcWithAnnotatedStructure } from './getStructure.util';
11
12
describe('getStructure - cpp', () => {
13
afterAll(() => _dispose());
14
15
function cppStruct(source: string) {
16
return srcWithAnnotatedStructure(WASMLanguage.Cpp, source);
17
}
18
19
test('source with different syntax constructs', async () => {
20
21
const filename = 'test.cpp';
22
23
const source = await fromFixture(filename);
24
25
await expect(await cppStruct(source)).toMatchFileSnapshot(snapshotPathInFixture(filename));
26
});
27
28
test('do not throw invalid range error', async () => {
29
30
const filename = 'problem1.cpp';
31
32
const source = await fromFixture(filename);
33
34
await expect(await cppStruct(source)).toMatchFileSnapshot(snapshotPathInFixture(filename));
35
});
36
37
test('do not throw invalid range error - 2', async () => {
38
const source = outdent`
39
void main() {
40
// Trigger servo movement based on the difference
41
if (remappedDifference > 0) {
42
Serial.println("Turning clockwise...");
43
controlServo(SERVO_CW); // Spin the servo clockwise
44
} else if (remappedDifference < 0) {
45
Serial.println("Turning counterclockwise...");
46
controlServo(SERVO_CCW); // Spin the servo counterclockwise
47
} /* else {
48
Serial.println("Stopping servo...");
49
controlServo(SERVO_STOP); // Stop the servo if the difference is zero
50
} */
51
}
52
`;
53
54
expect(await cppStruct(source)).toMatchInlineSnapshot(`
55
"<FUNCTION_DEFINITION>void main() {
56
<COMMENT> // Trigger servo movement based on the difference
57
</COMMENT><IF_STATEMENT> if (remappedDifference > 0) {
58
<EXPRESSION_STATEMENT> Serial.println("Turning clockwise...");
59
</EXPRESSION_STATEMENT><EXPRESSION_STATEMENT-1> controlServo(SERVO_CW); // Spin the servo clockwise
60
</EXPRESSION_STATEMENT-1> } else<IF_STATEMENT-1> if (remappedDifference < 0) {
61
<EXPRESSION_STATEMENT-2> Serial.println("Turning counterclockwise...");
62
</EXPRESSION_STATEMENT-2><EXPRESSION_STATEMENT-3> controlServo(SERVO_CCW); // Spin the servo counterclockwise
63
</EXPRESSION_STATEMENT-3> }</IF_STATEMENT-1> /* else {
64
Serial.println("Stopping servo...");
65
controlServo(SERVO_STOP); // Stop the servo if the difference is zero
66
} */
67
</IF_STATEMENT>}</FUNCTION_DEFINITION>"
68
`);
69
});
70
71
test('trailing semicolon after class declaration', async () => {
72
const source = `class A {};`;
73
74
expect(await cppStruct(source)).toMatchInlineSnapshot(`"<CLASS_SPECIFIER>class A {};</CLASS_SPECIFIER>"`);
75
});
76
});
77
78