Skip to content

Commit

Permalink
feat: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Poon committed Feb 6, 2019
1 parent adff634 commit c863403
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 10 deletions.
4 changes: 2 additions & 2 deletions extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ async function loadConfiguration() {
const logger = Logger.get('Configuration');

const validatorResults = await configuration.load();
logger.debug(`${validatorResults.numErrors()} errors found with vim configuration`);
logger.debug(`${validatorResults.numErrors} errors found with vim configuration`);

if (validatorResults.numErrors() > 0) {
if (validatorResults.numErrors > 0) {
for (let validatorResult of validatorResults.get()) {
switch (validatorResult.level) {
case 'error':
Expand Down
2 changes: 1 addition & 1 deletion src/configuration/configurationValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ConfigurationValidator {

for (const validator of this._validators) {
let validatorResults = await validator.validate(config);
if (validatorResults.hasError()) {
if (validatorResults.hasError) {
// errors found in configuration, disable feature
validator.disable(config);
}
Expand Down
7 changes: 7 additions & 0 deletions src/configuration/iconfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,13 @@ export interface IConfiguration {
visualModeKeyBindings: IKeyRemapping[];
visualModeKeyBindingsNonRecursive: IKeyRemapping[];

insertModeKeyBindingsMap: Map<string, IKeyRemapping>;
insertModeKeyBindingsNonRecursiveMap: Map<string, IKeyRemapping>;
normalModeKeyBindingsMap: Map<string, IKeyRemapping>;
normalModeKeyBindingsNonRecursiveMap: Map<string, IKeyRemapping>;
visualModeKeyBindingsMap: Map<string, IKeyRemapping>;
visualModeKeyBindingsNonRecursiveMap: Map<string, IKeyRemapping>;

/**
* Comma-separated list of motion keys that should wrap to next/previous line.
*/
Expand Down
6 changes: 3 additions & 3 deletions src/configuration/iconfigurationValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ export class ValidatorResults {
return this.errors;
}

public numErrors(): number {
public get numErrors(): number {
return this.errors.filter(e => e.level === 'error').length;
}

public hasError(): boolean {
return this.numErrors() > 0;
public get hasError(): boolean {
return this.numErrors > 0;
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/configuration/validators/remappingValidator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as vscode from 'vscode';
import { IConfiguration, IKeyRemapping } from '../iconfiguration';
import { Notation } from '../notation';
import { configuration } from '../configuration';
import { IConfigurationValidator, ValidatorResults } from '../iconfigurationValidator';

export class RemappingValidator implements IConfigurationValidator {
Expand All @@ -27,7 +26,7 @@ export class RemappingValidator implements IConfigurationValidator {
// validate
let remappingError = await this.isRemappingValid(remapping);
result.concat(remappingError);
if (remappingError.hasError()) {
if (remappingError.hasError) {
// errors with remapping, skip
keybindings.splice(i, 1);
continue;
Expand All @@ -50,7 +49,7 @@ export class RemappingValidator implements IConfigurationValidator {
const beforeKeys = remapping.before.join('');
if (modeKeyBindingsMap.has(beforeKeys)) {
result.append({
level: 'error',
level: 'warning',
message: `${remapping.before}. Duplicate remapped key for ${beforeKeys}.`,
});
continue;
Expand All @@ -60,7 +59,7 @@ export class RemappingValidator implements IConfigurationValidator {
modeKeyBindingsMap.set(beforeKeys, remapping);
}

configuration[modeKeyBindingsKey + 'Map'] = modeKeyBindingsMap;
config[modeKeyBindingsKey + 'Map'] = modeKeyBindingsMap;
}

return result;
Expand Down
36 changes: 36 additions & 0 deletions test/configuration/validators/neovimValidator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as assert from 'assert';
import { Configuration } from '../../testConfiguration';
import { NeovimValidator } from '../../../src/configuration/validators/neovimValidator';

suite('Neovim Validator', () => {
test('neovim enabled without path', async () => {
// setup
let configuration = new Configuration();
configuration.enableNeovim = true;
configuration.neovimPath = '';

// test
const validator = new NeovimValidator();
let actual = await validator.validate(configuration);
validator.disable(configuration);

// assert
assert.equal(actual.numErrors, 1);
assert.equal(actual.hasError, true);
assert.equal(configuration.enableNeovim, false);
});

test('neovim disabled', async () => {
// setup
let configuration = new Configuration();
configuration.enableNeovim = false;
configuration.neovimPath = '';

// test
const validator = new NeovimValidator();
let actual = await validator.validate(configuration);

// assert
assert.equal(actual.numErrors, 0);
});
});
92 changes: 92 additions & 0 deletions test/configuration/validators/remappingValidator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import * as assert from 'assert';
import { Configuration } from '../../testConfiguration';
import { RemappingValidator } from '../../../src/configuration/validators/remappingValidator';

suite('Remapping Validator', () => {
test('no remappings', async () => {
// setup
const configuration = new Configuration();
configuration.insertModeKeyBindings = [];
configuration.insertModeKeyBindingsNonRecursive = [];
configuration.normalModeKeyBindings = [];
configuration.normalModeKeyBindingsNonRecursive = [];
configuration.visualModeKeyBindings = [];
configuration.visualModeKeyBindingsNonRecursive = [];

// test
const validator = new RemappingValidator();
let actual = await validator.validate(configuration);

// assert
assert.equal(actual.numErrors, 0);
assert.equal(actual.hasError, false);

assert.equal(configuration.insertModeKeyBindingsMap.size, 0);
assert.equal(configuration.insertModeKeyBindingsNonRecursiveMap.size, 0);
assert.equal(configuration.normalModeKeyBindingsMap.size, 0);
assert.equal(configuration.normalModeKeyBindingsNonRecursiveMap.size, 0);
assert.equal(configuration.visualModeKeyBindingsMap.size, 0);
assert.equal(configuration.visualModeKeyBindingsNonRecursiveMap.size, 0);
});

test('jj->esc', async () => {
// setup
let configuration = new Configuration();
configuration.insertModeKeyBindings = [
{
before: ['j', 'j'],
after: ['<Esc>'],
}];
configuration.insertModeKeyBindingsNonRecursive = [];
configuration.normalModeKeyBindings = [];
configuration.normalModeKeyBindingsNonRecursive = [];
configuration.visualModeKeyBindings = [];
configuration.visualModeKeyBindingsNonRecursive = [];

// test
const validator = new RemappingValidator();
let actual = await validator.validate(configuration);

// assert
assert.equal(actual.numErrors, 0);
assert.equal(actual.hasError, false);

assert.equal(configuration.insertModeKeyBindingsMap.size, 1);
assert.equal(configuration.insertModeKeyBindingsNonRecursiveMap.size, 0);
assert.equal(configuration.normalModeKeyBindingsMap.size, 0);
assert.equal(configuration.normalModeKeyBindingsNonRecursiveMap.size, 0);
assert.equal(configuration.visualModeKeyBindingsMap.size, 0);
assert.equal(configuration.visualModeKeyBindingsNonRecursiveMap.size, 0);

assert.equal(configuration.insertModeKeyBindingsMap.get("jj"), configuration.insertModeKeyBindings[0]);
});

test('remapping missing after and command', async () => {
// setup
let configuration = new Configuration();
configuration.insertModeKeyBindings = [
{
before: ['j', 'j']
}];
configuration.insertModeKeyBindingsNonRecursive = [];
configuration.normalModeKeyBindings = [];
configuration.normalModeKeyBindingsNonRecursive = [];
configuration.visualModeKeyBindings = [];
configuration.visualModeKeyBindingsNonRecursive = [];

// test
const validator = new RemappingValidator();
let actual = await validator.validate(configuration);

// assert
assert.equal(actual.numErrors, 1);
assert.equal(actual.hasError, true);

assert.equal(configuration.insertModeKeyBindingsMap.size, 0);
assert.equal(configuration.insertModeKeyBindingsNonRecursiveMap.size, 0);
assert.equal(configuration.normalModeKeyBindingsMap.size, 0);
assert.equal(configuration.normalModeKeyBindingsNonRecursiveMap.size, 0);
assert.equal(configuration.visualModeKeyBindingsMap.size, 0);
assert.equal(configuration.visualModeKeyBindingsNonRecursiveMap.size, 0);
});
});
6 changes: 6 additions & 0 deletions test/testConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ export class Configuration implements IConfiguration {
normalModeKeyBindingsNonRecursive: IKeyRemapping[] = [];
visualModeKeyBindings: IKeyRemapping[] = [];
visualModeKeyBindingsNonRecursive: IKeyRemapping[] = [];
insertModeKeyBindingsMap: Map<string, IKeyRemapping>;
insertModeKeyBindingsNonRecursiveMap: Map<string, IKeyRemapping>;
normalModeKeyBindingsMap: Map<string, IKeyRemapping>;
normalModeKeyBindingsNonRecursiveMap: Map<string, IKeyRemapping>;
visualModeKeyBindingsMap: Map<string, IKeyRemapping>;
visualModeKeyBindingsNonRecursiveMap: Map<string, IKeyRemapping>;
whichwrap = '';
wrapKeys = {};
report = 2;
Expand Down

0 comments on commit c863403

Please sign in to comment.