Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong return in ModeHandlerMap.get #4581

Merged
merged 2 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 1 addition & 12 deletions src/editorIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,7 @@ export class EditorIdentity {
return new EditorIdentity(textEditor?.document?.fileName ?? '');
}

/**
* For use in tests
*/
public static createRandomEditorIdentity(): EditorIdentity {
return new EditorIdentity(
Math.random()
.toString(36)
.substring(7)
);
}

private constructor(fileName: string) {
public constructor(fileName: string) {
this._fileName = fileName;
}

Expand Down
15 changes: 8 additions & 7 deletions src/mode/modeHandlerMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ class ModeHandlerMapImpl {

public async getOrCreate(editorId: EditorIdentity): Promise<[ModeHandler, boolean]> {
let isNew = false;
let modeHandler: ModeHandler | undefined;
for (const [key, value] of this.modeHandlerMap.entries()) {
if (key.isEqual(editorId)) {
modeHandler = value;
}
}
let modeHandler: ModeHandler | undefined = this.get(editorId);

if (!modeHandler) {
isNew = true;
modeHandler = await ModeHandler.Create();
Expand All @@ -24,7 +20,12 @@ class ModeHandlerMapImpl {
}

public get(editorId: EditorIdentity): ModeHandler | undefined {
return this.modeHandlerMap.get(editorId);
for (const [key, value] of this.modeHandlerMap.entries()) {
if (key.isEqual(editorId)) {
return value;
}
}
return undefined;
}

public getKeys(): EditorIdentity[] {
Expand Down
26 changes: 25 additions & 1 deletion test/mode/modeHandlerMap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ import * as assert from 'assert';

import { ModeHandlerMap } from '../../src/mode/modeHandlerMap';
import { EditorIdentity } from '../../src/editorIdentity';
import { testIt } from '../testSimplifier';
import { KeypressState } from '../../src/actions/base';

function createRandomEditorIdentity(): EditorIdentity {
return new EditorIdentity(
Math.random()
.toString(36)
.substring(7)
);
}

suite('Mode Handler Map', () => {
setup(() => {
Expand All @@ -13,7 +23,7 @@ suite('Mode Handler Map', () => {
});

test('getOrCreate', async () => {
const key = EditorIdentity.createRandomEditorIdentity();
const key = createRandomEditorIdentity();
let [modeHandler, isNew] = await ModeHandlerMap.getOrCreate(key);
assert.strictEqual(isNew, true);
assert.notEqual(modeHandler, undefined);
Expand All @@ -34,4 +44,18 @@ suite('Mode Handler Map', () => {
ModeHandlerMap.delete(key);
assert.strictEqual(ModeHandlerMap.getAll().length, 0);
});

test('get', async () => {
// same file name should return the same modehandler
const identity = createRandomEditorIdentity();

let [modeHandler, isNew] = await ModeHandlerMap.getOrCreate(identity);
assert.strictEqual(isNew, true);
assert.notEqual(modeHandler, undefined);

const prevModeHandler = modeHandler;
[modeHandler, isNew] = await ModeHandlerMap.getOrCreate(new EditorIdentity(identity.fileName));
assert.strictEqual(isNew, false);
assert.strictEqual(prevModeHandler, modeHandler);
});
});