Skip to content

Commit

Permalink
Add unit tests (#519)
Browse files Browse the repository at this point in the history
* Use `e2e` and `test` to distinguish test types

* Sort package.json

* Use ahk2 main branch
  • Loading branch information
mark-wiemer authored Sep 28, 2024
1 parent 6730bb4 commit e064be6
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .vscode-test.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// https://github.com/microsoft/vscode-test-cli
import { defineConfig } from '@vscode/test-cli';
export default defineConfig({
files: ['out/src/**/*.test.js'],
files: ['out/src/**/*.e2e.js'],
// https://mochajs.org/#command-line-usage
// https://github.com/mochajs/mocha/tree/main/example/config
mocha: {
Expand Down
2 changes: 1 addition & 1 deletion ahk2
Submodule ahk2 updated from 9d6e81 to 02004d
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@
"prepare": "git submodule update --init --recursive && cd ahk2 && npm install",
"sort-package-json": "sort-package-json --check",
"sort-package-json:fix": "sort-package-json",
"test": "npm run test-grammar && npm run test-e2e",
"pretest-e2e": "npm run compile-ts && npm run build",
"test": "npm run test-grammar && npm run test-unit && npm run test-e2e",
"pretest-e2e": "npm run compile-ts",
"test-e2e": "vscode-test",
"test-e2e:ci": "npm run test-e2e -- -i --fgrep @ignoreCI",
"pretest-grammar": "npm run compile-grammar",
"test-grammar": "vscode-tmgrammar-snap language/samples/*.{ahk1,ahk2}",
"pretest-unit": "npm run compile-ts",
"test-unit": "mocha",
"test:ci": "npm run test-grammar && npm run test-e2e:ci",
"validate": "npm run lint && npm run test && npm run package",
"validate:ci": "npm run lint:ci && npm run test:ci && npm run package",
Expand Down Expand Up @@ -1141,6 +1143,9 @@
}
]
},
"mocha": {
"spec": "out/**/*.test.js"
},
"dependencies": {
"@vscode/debugadapter": "^1.57.0",
"get-port": "^5.1.1",
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/parser/parser.test.ts → src/parser/parser.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getDocument } from '../test/utils';
import * as assert from 'assert';
import * as path from 'path';
import * as vscode from 'vscode';
import { Parser } from '../parser/parser';
import { Parser } from './parser';

suite('Parser', () => {
suite('detectVariableByLine', () => {
Expand Down
File renamed without changes.
File renamed without changes.
52 changes: 52 additions & 0 deletions src/providers/formattingProvider.utils.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as vscode from 'vscode';
import * as assert from 'assert';
import * as path from 'path';
import { documentToString } from './formattingProvider.utils';

suite('FormattingProvider utils', () => {
// External tests use real VS Code behavior to ensure extension works end-to-end
const externalDocumentToString = 'external documentToString';
suite(externalDocumentToString, () => {
// Currently in `out` folder, need to get back to main `src` folder
const filesParentPath = path.join(
__dirname, // ./out/src/providers
'..', // ./out/src
'..', // ./out
'..', // .
'src', // ./src
'providers', // ./src/providers
'samples', // ./src/providers/samples
);

const myTests = [
{ filename: '1-one-empty-line.txt', expected: '' },
{ filename: '2-two-empty-lines.txt', expected: '\n' },
{ filename: '3-three-empty-lines.txt', expected: '\n\n' },
{
filename: '4-multiline-ends-with-newline.txt',
expected: 'hello\nworld\nhow are you\n',
},
{
filename: '5-multiline-no-newline.txt',
expected: 'hello\nworld\nhow are you',
},
{
filename: '6-single-line-ends-with-newline.txt',
expected: 'hello\n',
},
{ filename: '7-single-line-no-newline.txt', expected: 'hello' },
];

myTests.forEach((myTest) =>
test(myTest.filename, async () => {
const vscodeDocument = await vscode.workspace.openTextDocument(
path.join(filesParentPath, myTest.filename),
);

const actual = documentToString(vscodeDocument);

assert.strictEqual(actual, myTest.expected);
}),
);
});
});
49 changes: 1 addition & 48 deletions src/providers/formattingProvider.utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as vscode from 'vscode';
import * as assert from 'assert';
import * as path from 'path';
import { suite, test } from 'mocha';
import {
alignLineAssignOperator,
alignSingleLineComments,
Expand Down Expand Up @@ -1054,50 +1053,4 @@ suite('FormattingProvider utils', () => {
}),
);
});

// External tests use real VS Code behavior to ensure extension works end-to-end
const externalDocumentToString = 'external documentToString';
suite(externalDocumentToString, () => {
// Currently in `out` folder, need to get back to main `src` folder
const filesParentPath = path.join(
__dirname, // ./out/src/providers
'..', // ./out/src
'..', // ./out
'..', // .
'src', // ./src
'providers', // ./src/providers
'samples', // ./src/providers/samples
);

const myTests = [
{ filename: '1-one-empty-line.txt', expected: '' },
{ filename: '2-two-empty-lines.txt', expected: '\n' },
{ filename: '3-three-empty-lines.txt', expected: '\n\n' },
{
filename: '4-multiline-ends-with-newline.txt',
expected: 'hello\nworld\nhow are you\n',
},
{
filename: '5-multiline-no-newline.txt',
expected: 'hello\nworld\nhow are you',
},
{
filename: '6-single-line-ends-with-newline.txt',
expected: 'hello\n',
},
{ filename: '7-single-line-no-newline.txt', expected: 'hello' },
];

myTests.forEach((myTest) =>
test(myTest.filename, async () => {
const vscodeDocument = await vscode.workspace.openTextDocument(
path.join(filesParentPath, myTest.filename),
);

const actual = documentToString(vscodeDocument);

assert.strictEqual(actual, myTest.expected);
}),
);
});
});
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit e064be6

Please sign in to comment.