Skip to content

Commit

Permalink
Option stream tests
Browse files Browse the repository at this point in the history
  • Loading branch information
akshita31 committed May 16, 2018
1 parent 9b389b9 commit 4e0e114
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import { ProjectStatusBarObserver } from './observers/ProjectStatusBarObserver';
import CSharpExtensionExports from './CSharpExtensionExports';
import { vscodeNetworkSettingsProvider, NetworkSettingsProvider } from './NetworkSettings';
import { ErrorMessageObserver } from './observers/ErrorMessageObserver';
import { createOptionStream } from './observables/OptionStream';
import OptionProvider from './observers/OptionProvider';
import { ShowOmniSharpConfigChangePrompt } from './observers/OptionChangeObserver';
import createOptionStream from './observables/CreateOptionStream';

export async function activate(context: vscode.ExtensionContext): Promise<CSharpExtensionExports> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'rxjs/add/operator/publishBehavior';
import { Observable } from "rxjs/Observable";
import { Observer } from "rxjs/Observer";

export function createOptionStream(vscode: vscode): Observable<Options> {
export default function createOptionStream(vscode: vscode): Observable<Options> {
return Observable.create((observer: Observer<Options>) => {
let disposable = vscode.workspace.onDidChangeConfiguration(e => {
//if the omnisharp or csharp configuration are affected only then read the options
Expand Down
42 changes: 22 additions & 20 deletions test/unitTests/OptionObserver/OptionChangeObserver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ suite("OmniSharpConfigChangeObserver", () => {
signalCommandDone = () => { resolve(); };
});
});

[
{ config: "omnisharp", section: "path", value: "somePath" },
{ config: "omnisharp", section: "waitForDebugger", value: true },
Expand Down Expand Up @@ -68,25 +68,27 @@ suite("OmniSharpConfigChangeObserver", () => {
expect(invokedCommand).to.be.equal("o.restart");
});
});
});

[
{ config: "csharp", section: 'disableCodeActions', value: true },
{ config: "csharp", section: 'testsCodeLens.enabled', value: false },
{ config: "omnisharp", section: 'referencesCodeLens.enabled', value: false },
{ config: "csharp", section: 'format.enable', value: false },
{ config: "omnisharp", section: 'useEditorFormattingSettings', value: false },
{ config: "omnisharp", section: 'maxProjectResults', value: 1000 },
{ config: "omnisharp", section: 'projectLoadTimeout', value: 1000 },
{ config: "omnisharp", section: 'autoStart', value: false },
{ config: "omnisharp", section: 'loggingLevel', value: 'verbose' }
].forEach(elem => {
test(`The information message is not shown when ${elem.config} ${elem.section} changes`, async () => {
expect(infoMessage).to.be.undefined;
expect(invokedCommand).to.be.undefined;
updateConfig(vscode, elem.config, elem.section, elem.value);
optionObservable.next(Options.Read(vscode));
expect(infoMessage).to.be.undefined;
});

suite('Information Message is not shown on change in',() => {
[
{ config: "csharp", section: 'disableCodeActions', value: true },
{ config: "csharp", section: 'testsCodeLens.enabled', value: false },
{ config: "omnisharp", section: 'referencesCodeLens.enabled', value: false },
{ config: "csharp", section: 'format.enable', value: false },
{ config: "omnisharp", section: 'useEditorFormattingSettings', value: false },
{ config: "omnisharp", section: 'maxProjectResults', value: 1000 },
{ config: "omnisharp", section: 'projectLoadTimeout', value: 1000 },
{ config: "omnisharp", section: 'autoStart', value: false },
{ config: "omnisharp", section: 'loggingLevel', value: 'verbose' }
].forEach(elem => {
test(`${elem.config} ${elem.section}`, async () => {
expect(infoMessage).to.be.undefined;
expect(invokedCommand).to.be.undefined;
updateConfig(vscode, elem.config, elem.section, elem.value);
optionObservable.next(Options.Read(vscode));
expect(infoMessage).to.be.undefined;
});
});
});

Expand Down
100 changes: 100 additions & 0 deletions test/unitTests/optionStream.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { should, expect } from 'chai';
import { ConfigurationChangeEvent, vscode } from "../../src/vscodeAdapter";
import { getVSCodeWithConfig, updateConfig } from "./testAssets/Fakes";
import Disposable from "../../src/Disposable";
import { Observable } from "rxjs/Observable";
import { Options } from "../../src/omnisharp/options";
import { GetConfigChangeEvent } from './testAssets/GetConfigChangeEvent';
import { Subscription } from 'rxjs/Subscription';
import createOptionStream from '../../src/observables/CreateOptionStream';

suite('OptionStream', () => {
suiteSetup(() => should());

let listenerFunction: Array<(e: ConfigurationChangeEvent) => any>;
let vscode: vscode;
let optionStream: Observable<Options>;
let disposeCalled: boolean;

setup(() => {
listenerFunction = new Array<(e: ConfigurationChangeEvent) => any>();
vscode = getVSCode(listenerFunction);
optionStream = createOptionStream(vscode);
disposeCalled = false;
});

suite('Returns the recent options to the subscriber', () => {
let subscription: Subscription;
let options: Options;

setup(() => {
subscription = optionStream.subscribe(newOptions => options = newOptions);
});

test('Returns the default options if there is no change', () => {
expect(options.path).to.be.null;
options.useGlobalMono.should.equal("auto");
options.waitForDebugger.should.equal(false);
options.loggingLevel.should.equal("information");
options.autoStart.should.equal(true);
options.projectLoadTimeout.should.equal(60);
options.maxProjectResults.should.equal(250);
options.useEditorFormattingSettings.should.equal(true);
options.useFormatting.should.equal(true);
options.showReferencesCodeLens.should.equal(true);
options.showTestsCodeLens.should.equal(true);
options.disableCodeActions.should.equal(false);
});

test('Gives the changed option when the omnisharp config changes', () => {
expect(options.path).to.be.null;
let changingConfig = "omnisharp";
updateConfig(vscode, changingConfig, 'path', "somePath");
listenerFunction.forEach(listener => listener(GetConfigChangeEvent(changingConfig)));
options.path.should.equal("somePath");
});

test('Gives the changed option when the csharp config changes', () => {
options.disableCodeActions.should.equal(false);
let changingConfig = "csharp";
updateConfig(vscode, changingConfig, 'disableCodeActions', true);
listenerFunction.forEach(listener => listener(GetConfigChangeEvent(changingConfig)));
options.disableCodeActions.should.equal(true);
});

teardown(() => {
options = undefined;
listenerFunction = undefined;
subscription.unsubscribe();
subscription = undefined;
});
});

test('Dispose is called when the last subscriber unsubscribes', () => {
disposeCalled.should.equal(false);
let subscription1 = optionStream.subscribe(_ => { });
let subscription2 = optionStream.subscribe(_ => { });
let subscription3 = optionStream.subscribe(_ => { });
subscription1.unsubscribe();
disposeCalled.should.equal(false);
subscription2.unsubscribe();
disposeCalled.should.equal(false);
subscription3.unsubscribe();
disposeCalled.should.equal(true);
});

function getVSCode(listenerFunction: Array<(e: ConfigurationChangeEvent) => any>): vscode {
let vscode = getVSCodeWithConfig();
vscode.workspace.onDidChangeConfiguration = (listener: (e: ConfigurationChangeEvent) => any, thisArgs?: any, disposables?: Disposable[]) => {
listenerFunction.push(listener);
return new Disposable(() => disposeCalled = true);
};

return vscode;
}
});

0 comments on commit 4e0e114

Please sign in to comment.