Skip to content

Commit

Permalink
feat: add automated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
1nVitr0 committed Mar 28, 2021
1 parent fbd2105 commit 6f33690
Show file tree
Hide file tree
Showing 20 changed files with 491 additions and 59 deletions.
10 changes: 5 additions & 5 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
"presentation": {
"reveal": "never"
},
"group": {
"kind": "build",
"isDefault": true
}
"group": "build"
},
{
"type": "npm",
Expand All @@ -24,7 +21,10 @@
"presentation": {
"reveal": "never"
},
"group": "build"
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"format",
"block",
"order",
"arrange"
"block sort"
],
"activationEvents": [
"onCommand:blocksort.sortBlocksAsc",
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { commands, ExtensionContext } from 'vscode';
import { ExtensionContext } from 'vscode';
import contributeCommands from './contribute/commands';

export function activate(context: ExtensionContext) {
Expand Down
3 changes: 1 addition & 2 deletions src/providers/BlockSortProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Range, Selection, TextDocument, TextEditorEdit, window, workspace } from 'vscode';
import * as code from 'vscode';
import { Range, Selection, TextDocument, workspace } from 'vscode';

type FoldingMarker = '()' | '[]' | '{}';
type SortingStrategy = 'asc' | 'desc';
Expand Down
10 changes: 10 additions & 0 deletions src/test/fixtures/expand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Range } from 'vscode';
import { ExpandTest } from './types';

export const expandTests: ExpandTest[] = [
{
file: 'expand.ts.fixture',
ranges: [new Range(3, 9, 3, 9)],
targetRanges: [new Range(3, 0, 16, 20)],
},
];
5 changes: 5 additions & 0 deletions src/test/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { join } from 'path';

export const fixtureDir = join(__dirname, '../../../test/fixtures');
export * from './expand';
export * from './sort';
21 changes: 21 additions & 0 deletions src/test/fixtures/sort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { join } from 'path';
import { Range } from 'vscode';
import { ExpandTest, SortTest } from './types';

export const sortTests: SortTest[] = [
{
file: 'block.ts.fixture',
compareFile: 'block.ts.expect',
ranges: [new Range(3, 0, 11, 22), new Range(17, 0, 31, 8), new Range(37, 0, 45, 8)],
},
{
file: 'toplevel.ts.fixture',
compareFile: 'toplevel.ts.expect',
ranges: [new Range(0, 0, 59, 0)],
},
{
file: 'comments.ts.fixture',
compareFile: 'comments.ts.expect',
ranges: [new Range(1, 0, 27, 3)],
},
];
14 changes: 14 additions & 0 deletions src/test/fixtures/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Range } from 'vscode';

interface BaseTest {
file: string;
ranges: Range[];
}

export interface SortTest extends BaseTest {
compareFile: string;
}

export interface ExpandTest extends BaseTest {
targetRanges: Range[];
}
26 changes: 13 additions & 13 deletions src/test/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import * as path from 'path';
import { runTests } from 'vscode-test';

async function main() {
try {
// The folder containing the Extension Manifest package.json
// Passed to `--extensionDevelopmentPath`
const extensionDevelopmentPath = path.resolve(__dirname, '../../');
try {
// The folder containing the Extension Manifest package.json
// Passed to `--extensionDevelopmentPath`
const extensionDevelopmentPath = path.resolve(__dirname, '../../');

// The path to test runner
// Passed to --extensionTestsPath
const extensionTestsPath = path.resolve(__dirname, './suite/index');
// The path to test runner
// Passed to --extensionTestsPath
const extensionTestsPath = path.resolve(__dirname, './suite/index');

// Download VS Code, unzip it and run the integration test
await runTests({ extensionDevelopmentPath, extensionTestsPath });
} catch (err) {
console.error('Failed to run tests');
process.exit(1);
}
// Download VS Code, unzip it and run the integration test
await runTests({ extensionDevelopmentPath, extensionTestsPath });
} catch (err) {
console.error('Failed to run tests');
process.exit(1);
}
}

main();
46 changes: 46 additions & 0 deletions src/test/suite/BlockSortProvider.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as assert from 'assert';
import { join } from 'path';
import { Range, TextDocument, window, workspace, Selection } from 'vscode';
import BlockSortProvider from '../../providers/BlockSortProvider';
import { expandTests, fixtureDir, sortTests } from '../fixtures';

suite('Unit Suite for BlockSortProvider', async () => {
let document: TextDocument;
let compareDocument: TextDocument;
let blockSortProvider: BlockSortProvider;

window.showInformationMessage('Start tests for BlockSortProvider.');

expandTests.map(({ file, ranges, targetRanges }) => {
ranges
.map((range, i) => ({ position: range, target: targetRanges[i] }))
.forEach(({ position, target }) => {
test('Expands selection', async () => {
document = await workspace.openTextDocument(join(fixtureDir, file));
blockSortProvider = new BlockSortProvider(document);
const selection = new Selection(position.start, position.end);
const expanded = blockSortProvider.expandSelection(selection);

assert(expanded.isEqual(target), 'range did not expand correctly');
});
});
});

sortTests.map(({ file, compareFile, ranges }) => {
ranges.forEach((range, i) => {
const descriptor = file.match(/^(.*)\.(.*)\.fixture/);
const [_, type, lang] = descriptor || ['generic', 'generic'];
test(`Sort Blocks (${type}, lang ${lang}) #${i}`, async () => {
compareDocument = await workspace.openTextDocument(join(fixtureDir, compareFile));
document = await workspace.openTextDocument(join(fixtureDir, file));
blockSortProvider = new BlockSortProvider(document);

const blocks = blockSortProvider.getBlocks(range);
const sorted = blockSortProvider.sortBlocks(blocks).join('\n');
const compareSorted = compareDocument.getText(range);

assert.strictEqual(sorted, compareSorted, 'sorted ranges are not equal');
});
});
});
});
12 changes: 6 additions & 6 deletions src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import * as assert from 'assert';
// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
import * as vscode from 'vscode';
// import * as myExtension from '../../extension';
import * as blocksort from '../../extension';

suite('Extension Test Suite', () => {
vscode.window.showInformationMessage('Start all tests.');
vscode.window.showInformationMessage('Start all tests.');

test('Sample test', () => {
assert.strictEqual(-1, [1, 2, 3].indexOf(5));
assert.strictEqual(-1, [1, 2, 3].indexOf(0));
});
test('Sample test', () => {
assert.strictEqual(-1, [1, 2, 3].indexOf(5));
assert.strictEqual(-1, [1, 2, 3].indexOf(0));
});
});
51 changes: 23 additions & 28 deletions src/test/suite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,31 @@ import * as Mocha from 'mocha';
import * as glob from 'glob';

export function run(): Promise<void> {
// Create the mocha test
const mocha = new Mocha({
ui: 'tdd',
color: true
});
// Create the mocha test
const mocha = new Mocha({
ui: 'tdd',
color: true,
});

const testsRoot = path.resolve(__dirname, '..');
const testsRoot = path.resolve(__dirname, '..');

return new Promise((c, e) => {
glob('**/**.test.js', { cwd: testsRoot }, (err, files) => {
if (err) {
return e(err);
}
return new Promise((c, e) => {
glob('**/**.test.js', { cwd: testsRoot }, (err, files) => {
if (err) return e(err);

// Add files to the test suite
files.forEach(f => mocha.addFile(path.resolve(testsRoot, f)));
// Add files to the test suite
files.forEach((f) => mocha.addFile(path.resolve(testsRoot, f)));

try {
// Run the mocha test
mocha.run(failures => {
if (failures > 0) {
e(new Error(`${failures} tests failed.`));
} else {
c();
}
});
} catch (err) {
console.error(err);
e(err);
}
});
});
try {
// Run the mocha test
mocha.run((failures) => {
if (failures > 0) e(new Error(`${failures} tests failed.`));
else c();
});
} catch (err) {
console.error(err);
e(err);
}
});
});
}
83 changes: 83 additions & 0 deletions test/fixtures/block.ts.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
class Test {
testSwitch(input: string) {
switch (input) {
case 'a':
return 'first';
case 'b':
const result = 'second';
return result;
case 'c':
return 'third';
default:
return 'last';
}
}

testObject() {
return {
a: {
2: 2,
0: 0,
1: 1
},
b: {
2: 2,
0: 0,
1: 1
},
c: {
2: 2,
0: 0,
1: 1
}
};
}

testReverse() {
return {
a: {
0: 0
},
b: {
0: 0
},
c: {
0: 0
}
};
}

testNatural() {
return {
3: {
0: 0
},
5: {
0: 0
},
16: {
0: 0
},
300: {
0: 0
}
};
}

testNaturalExtra() {
return {
0o3: {
0: 0
},
0b101: {
0: 0
},
0xF: {
0: 0
},
300: {
0: 0
}
};
}
}
Loading

0 comments on commit 6f33690

Please sign in to comment.