Skip to content

Commit 9436818

Browse files
committed
FYP-60: added simple test running AST command
FYP-60: add newline in utils.ts FYP-60: wait before checking HTML FYP-60: update the expect value FYP-60: added setup, reset settings to default FYP-60: console log html and hex for debugging FYP-60: update hex value being checked FYP-60: updated expected hex FYP-60: trim the htmls FYP-60: added some console logging debug FYP-60: minify html FYP-60: remove html check, causing issues on different platforms
1 parent 8b48526 commit 9436818

File tree

5 files changed

+44
-12
lines changed

5 files changed

+44
-12
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"devDependencies": {
3131
"@types/glob": "^7.1.3",
3232
"@types/mocha": "^8.2.1",
33-
"@types/node": "^14.14.32",
33+
"@types/node": "^14.14.33",
3434
"@types/prettier": "^2.2.2",
3535
"@types/vscode": "^1.54.0",
3636
"@typescript-eslint/eslint-plugin": "^4.16.1",

src/commands/displaySyntaxTree.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from 'vscode';
2-
import { getEditor } from '../utils';
2+
import { getActiveDocumentFilePath, getEditor } from '../utils';
33

4-
export async function displaySyntaxTree(): Promise<void> {
4+
export async function displaySyntaxTree(): Promise<vscode.WebviewPanel> {
55
const editor = getEditor();
66

77
const dotAst: string =
@@ -11,18 +11,12 @@ export async function displaySyntaxTree(): Promise<void> {
1111
)) ?? '';
1212

1313
if (dotAst.length === 0) {
14-
vscode.window.showInformationMessage('Error!');
15-
return;
14+
throw new Error('Unable to generate AST');
1615
}
1716

18-
// Get the last part of the filename (after the last slash), e.g. Refactor.java
19-
const filename = editor.document.fileName.substring(
20-
editor.document.fileName.lastIndexOf('/') + 1,
21-
);
22-
2317
const panel = vscode.window.createWebviewPanel(
2418
'syntaxTreeGraph',
25-
`${filename} - Simple Abstract Syntax Tree`,
19+
`${getActiveDocumentFilePath()} - Simple Abstract Syntax Tree`,
2620
vscode.ViewColumn.One,
2721
{
2822
enableScripts: true,
@@ -32,6 +26,8 @@ export async function displaySyntaxTree(): Promise<void> {
3226
// And set its HTML content
3327
const html = getWebviewContent(dotAst);
3428
panel.webview.html = html;
29+
30+
return panel;
3531
}
3632

3733
function getWebviewContent(treeData: string) {

src/test/suite/extension.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,27 @@ import * as vscode from 'vscode';
33
import { activate, getDocPath, sleep } from './helper';
44
import { Commands } from '../../commands';
55
import { TextDocument } from 'vscode';
6+
import { getActiveDocumentFilePath } from '../../utils';
67

78
const hex = require('string-hex');
89

910
suite('Extension Test Suite', () => {
11+
setup(async function () {
12+
// Reset all settings to default, so local settings don't affect the test results
13+
const configuration = vscode.workspace.getConfiguration('java');
14+
await configuration.update(
15+
'abstractSyntaxTree.showNodeType',
16+
false,
17+
vscode.ConfigurationTarget.Global,
18+
);
19+
20+
await configuration.update(
21+
'refactor.reduce.operators',
22+
['PLUS', 'MINUS', 'MULTIPLY', 'DIVIDE'],
23+
vscode.ConfigurationTarget.Global,
24+
);
25+
});
26+
1027
vscode.window.showInformationMessage('Start all tests.');
1128

1229
const FILE_PATH = getDocPath('App.java');
@@ -54,4 +71,14 @@ suite('Extension Test Suite', () => {
5471
'7075626c696320636c61737320417070207baa202020207075626c69632073746174696320766f6964206d61696e28537472696e675b5d206172677329207468726f777320457863657074696f6e207ba20202020202020204c6973743c537472696e673e206e616d6573203d204172726179732e61734c697374282244696f676f222c2022436f73746122293ba20202020202020206e616d65732e666f724561636828a202020202020202020202020737472696e67202d3e207ba2020202020202020202020202020202053797374656d2e6f75742e7072696e746c6e28737472696e67293ba2020202020202020202020207da2020202020202020293ba202020207da7d',
5572
);
5673
});
74+
75+
test('Running Display Abstract Syntax Tree Command', async () => {
76+
const tabTitle = `${getActiveDocumentFilePath()} - Simple Abstract Syntax Tree`;
77+
78+
const panel = (await vscode.commands.executeCommand(
79+
Commands.DISPLAY_AST,
80+
)) as vscode.WebviewPanel;
81+
82+
assert.strictEqual(panel.title, tabTitle);
83+
});
5784
});

src/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@ export function writeCode(code: string): void {
3434

3535
vscode.workspace.applyEdit(edit);
3636
}
37+
38+
export function getActiveDocumentFilePath(): string {
39+
return getEditor().document.fileName;
40+
}

yarn.lock

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,16 @@
8787
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-8.2.1.tgz#f3f3ae4590c5386fc7c543aae9b78d4cf30ffee9"
8888
integrity sha512-NysN+bNqj6E0Hv4CTGWSlPzMW6vTKjDpOteycDkV4IWBsO+PU48JonrPzV9ODjiI2XrjmA05KInLgF5ivZ/YGQ==
8989

90-
"@types/node@*", "@types/node@^14.14.32":
90+
"@types/node@*":
9191
version "14.14.32"
9292
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.32.tgz#90c5c4a8d72bbbfe53033f122341343249183448"
9393
integrity sha512-/Ctrftx/zp4m8JOujM5ZhwzlWLx22nbQJiVqz8/zE15gOeEW+uly3FSX4fGFpcfEvFzXcMCJwq9lGVWgyARXhg==
9494

95+
"@types/node@^14.14.33":
96+
version "14.14.33"
97+
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.33.tgz#9e4f8c64345522e4e8ce77b334a8aaa64e2b6c78"
98+
integrity sha512-oJqcTrgPUF29oUP8AsUqbXGJNuPutsetaa9kTQAQce5Lx5dTYWV02ScBiT/k1BX/Z7pKeqedmvp39Wu4zR7N7g==
99+
95100
"@types/prettier@^2.2.2":
96101
version "2.2.2"
97102
resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.2.2.tgz#e2280c89ddcbeef340099d6968d8c86ba155fdf6"

0 commit comments

Comments
 (0)