From 19f152cee6bd48cccc89e6940adefa557edfdc2d Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Wed, 2 Nov 2016 10:43:53 +0100 Subject: [PATCH] add tests --- .../api/node/mainThreadSaveParticipant.ts | 6 +- .../api/mainThreadSaveParticipant.test.ts | 75 +++++++++++++++++++ 2 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 src/vs/workbench/test/node/api/mainThreadSaveParticipant.test.ts diff --git a/src/vs/workbench/api/node/mainThreadSaveParticipant.ts b/src/vs/workbench/api/node/mainThreadSaveParticipant.ts index 6d9988ef869..e07ed69d1da 100644 --- a/src/vs/workbench/api/node/mainThreadSaveParticipant.ts +++ b/src/vs/workbench/api/node/mainThreadSaveParticipant.ts @@ -24,7 +24,7 @@ import { TextFileEditorModel } from 'vs/workbench/services/textfile/common/textF import { ExtHostContext, ExtHostDocumentSaveParticipantShape } from './extHost.protocol'; import { EditOperation } from 'vs/editor/common/core/editOperation'; -interface INamedSaveParticpant extends ISaveParticipant { +export interface INamedSaveParticpant extends ISaveParticipant { readonly name: string; } @@ -93,7 +93,7 @@ function findEditor(model: IModel, codeEditorService: ICodeEditorService): IComm return null; } -class FinalNewLineParticipant implements INamedSaveParticpant { +export class FinalNewLineParticipant implements INamedSaveParticpant { readonly name = 'FinalNewLineParticipant'; @@ -115,7 +115,7 @@ class FinalNewLineParticipant implements INamedSaveParticpant { const lastLine = model.getLineContent(lineCount); const lastLineIsEmptyOrWhitespace = strings.lastNonWhitespaceIndex(lastLine) === -1; - if (lineCount <= 1 || lastLineIsEmptyOrWhitespace) { + if (!lineCount || lastLineIsEmptyOrWhitespace) { return; } diff --git a/src/vs/workbench/test/node/api/mainThreadSaveParticipant.test.ts b/src/vs/workbench/test/node/api/mainThreadSaveParticipant.test.ts new file mode 100644 index 00000000000..59092f586f8 --- /dev/null +++ b/src/vs/workbench/test/node/api/mainThreadSaveParticipant.test.ts @@ -0,0 +1,75 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +'use strict'; + +import * as assert from 'assert'; +import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; +import { FinalNewLineParticipant } from 'vs/workbench/api/node/mainThreadSaveParticipant'; +import { TestConfigurationService } from 'vs/platform/configuration/test/common/testConfigurationService'; +import { workbenchInstantiationService, TestTextFileService, toResource } from 'vs/test/utils/servicesTestUtils'; +import { IModelService } from 'vs/editor/common/services/modelService'; +import { TextFileEditorModel } from 'vs/workbench/services/textfile/common/textFileEditorModel'; +import { IEventService } from 'vs/platform/event/common/event'; +import { ITextFileService, SaveReason } from 'vs/workbench/services/textfile/common/textfiles'; +import { TextFileEditorModelManager } from 'vs/workbench/services/textfile/common/textFileEditorModelManager'; + +class ServiceAccessor { + constructor( @IEventService public eventService: IEventService, @ITextFileService public textFileService: TestTextFileService, @IModelService public modelService: IModelService) { + } +} + +suite('MainThreadSaveParticipant', function () { + + let instantiationService: IInstantiationService; + let accessor: ServiceAccessor; + + setup(() => { + instantiationService = workbenchInstantiationService(); + accessor = instantiationService.createInstance(ServiceAccessor); + }); + + teardown(() => { + (accessor.textFileService.models).clear(); + TextFileEditorModel.setSaveParticipant(null); // reset any set participant + }); + + test('insert final new line', function (done) { + const model: TextFileEditorModel = instantiationService.createInstance(TextFileEditorModel, toResource.call(this, '/path/final_new_line.txt'), 'utf8'); + + model.load().then(() => { + const configService = new TestConfigurationService(); + configService.setUserConfiguration('files', { 'insertFinalNewline': true }); + + const participant = new FinalNewLineParticipant(configService, undefined); + + // No new line for empty lines + let lineContent = ''; + model.textEditorModel.setValue(lineContent); + participant.participate(model, { reason: SaveReason.EXPLICIT }); + assert.equal(model.getValue(), lineContent); + + // No new line if last line already empty + lineContent = `Hello New Line${model.textEditorModel.getEOL()}`; + model.textEditorModel.setValue(lineContent); + participant.participate(model, { reason: SaveReason.EXPLICIT }); + assert.equal(model.getValue(), lineContent); + + // New empty line added (single line) + lineContent = 'Hello New Line'; + model.textEditorModel.setValue(lineContent); + participant.participate(model, { reason: SaveReason.EXPLICIT }); + assert.equal(model.getValue(), `${lineContent}${model.textEditorModel.getEOL()}`); + + // New empty line added (multi line) + lineContent = `Hello New Line${model.textEditorModel.getEOL()}Hello New Line${model.textEditorModel.getEOL()}Hello New Line`; + model.textEditorModel.setValue(lineContent); + participant.participate(model, { reason: SaveReason.EXPLICIT }); + assert.equal(model.getValue(), `${lineContent}${model.textEditorModel.getEOL()}`); + + done(); + }); + }); +}); \ No newline at end of file