From c44524b55363345133fd377a38a181674e591ad6 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Sat, 8 Jun 2019 17:06:43 -0700 Subject: [PATCH] Use MockBufferService in tests --- src/Buffer.test.ts | 23 ++++++++++------------- src/Buffer.ts | 14 +++++++------- src/BufferSet.test.ts | 12 +++++------- src/BufferSet.ts | 12 ++++++------ src/SelectionManager.test.ts | 9 +++++---- src/SelectionModel.test.ts | 9 +++++---- src/Terminal.ts | 2 +- src/TestUtils.test.ts | 14 +++++++++++++- 8 files changed, 52 insertions(+), 43 deletions(-) diff --git a/src/Buffer.test.ts b/src/Buffer.test.ts index d0d098054..be9afe93d 100644 --- a/src/Buffer.test.ts +++ b/src/Buffer.test.ts @@ -4,10 +4,9 @@ */ import { assert, expect } from 'chai'; -import { ITerminal } from './Types'; import { Buffer } from './Buffer'; import { CircularList } from 'common/CircularList'; -import { MockTerminal, TestTerminal, MockOptionsService } from './TestUtils.test'; +import { TestTerminal, MockOptionsService, MockBufferService } from './TestUtils.test'; import { BufferLine, CellData, DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine'; const INIT_COLS = 80; @@ -15,25 +14,23 @@ const INIT_ROWS = 24; const INIT_SCROLLBACK = 1000; describe('Buffer', () => { - let terminal: ITerminal; let optionsService: MockOptionsService; + let bufferService: MockBufferService; let buffer: Buffer; beforeEach(() => { - terminal = new MockTerminal(); - (terminal as any).cols = INIT_COLS; - (terminal as any).rows = INIT_ROWS; optionsService = new MockOptionsService({ scrollback: INIT_SCROLLBACK }); - buffer = new Buffer(terminal, true, optionsService); + bufferService = new MockBufferService(INIT_COLS, INIT_ROWS); + buffer = new Buffer(true, optionsService, bufferService); }); describe('constructor', () => { it('should create a CircularList with max length equal to rows + scrollback, for its lines', () => { assert.instanceOf(buffer.lines, CircularList); - assert.equal(buffer.lines.maxLength, terminal.rows + INIT_SCROLLBACK); + assert.equal(buffer.lines.maxLength, bufferService.rows + INIT_SCROLLBACK); }); it('should set the Buffer\'s scrollBottom value equal to the terminal\'s rows -1', () => { - assert.equal(buffer.scrollBottom, terminal.rows - 1); + assert.equal(buffer.scrollBottom, bufferService.rows - 1); }); }); @@ -152,7 +149,7 @@ describe('Buffer', () => { describe('no scrollback', () => { it('should trim from the top of the buffer when the cursor reaches the bottom', () => { - buffer = new Buffer(terminal, true, new MockOptionsService({ scrollback: 0 })); + buffer = new Buffer(true, new MockOptionsService({ scrollback: 0 }), bufferService); assert.equal(buffer.lines.maxLength, INIT_ROWS); buffer.y = INIT_ROWS - 1; buffer.fillViewportRows(); @@ -1055,7 +1052,7 @@ describe('Buffer', () => { describe('buffer marked to have no scrollback', () => { it('should always have a scrollback of 0', () => { // Test size on initialization - buffer = new Buffer(terminal, false, new MockOptionsService({ scrollback: 1000 })); + buffer = new Buffer(false, new MockOptionsService({ scrollback: 1000 }), bufferService); buffer.fillViewportRows(); assert.equal(buffer.lines.maxLength, INIT_ROWS); // Test size on buffer increase @@ -1069,7 +1066,7 @@ describe('Buffer', () => { describe('addMarker', () => { it('should adjust a marker line when the buffer is trimmed', () => { - buffer = new Buffer(terminal, true, new MockOptionsService({ scrollback: 0 })); + buffer = new Buffer(true, new MockOptionsService({ scrollback: 0 }), bufferService); buffer.fillViewportRows(); const marker = buffer.addMarker(buffer.lines.length - 1); assert.equal(marker.line, buffer.lines.length - 1); @@ -1077,7 +1074,7 @@ describe('Buffer', () => { assert.equal(marker.line, buffer.lines.length - 2); }); it('should dispose of a marker if it is trimmed off the buffer', () => { - buffer = new Buffer(terminal, true, new MockOptionsService({ scrollback: 0 })); + buffer = new Buffer(true, new MockOptionsService({ scrollback: 0 }), bufferService); buffer.fillViewportRows(); assert.equal(buffer.markers.length, 0); const marker = buffer.addMarker(0); diff --git a/src/Buffer.ts b/src/Buffer.ts index dee112d9e..c359ffff7 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -4,12 +4,12 @@ */ import { CircularList, IInsertEvent } from 'common/CircularList'; -import { ITerminal, IBuffer, BufferIndex, IBufferStringIterator, IBufferStringIteratorResult } from './Types'; +import { IBuffer, BufferIndex, IBufferStringIterator, IBufferStringIteratorResult } from './Types'; import { IBufferLine, ICellData, IAttributeData } from 'common/Types'; import { BufferLine, CellData, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE, WHITESPACE_CELL_CHAR, WHITESPACE_CELL_WIDTH, WHITESPACE_CELL_CODE, CHAR_DATA_WIDTH_INDEX, CHAR_DATA_CHAR_INDEX, DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine'; import { reflowLargerApplyNewLayout, reflowLargerCreateNewLayout, reflowLargerGetLinesToRemove, reflowSmallerGetNewLineLengths, getWrappedLineTrimmedLength } from 'common/buffer/BufferReflow'; import { Marker } from 'common/buffer/Marker'; -import { IOptionsService } from 'common/services/Services'; +import { IOptionsService, IBufferService } from 'common/services/Services'; export const MAX_BUFFER_SIZE = 4294967295; // 2^32 - 1 @@ -39,12 +39,12 @@ export class Buffer implements IBuffer { private _rows: number; constructor( - private _terminal: ITerminal, private _hasScrollback: boolean, - private _optionsService: IOptionsService + private _optionsService: IOptionsService, + private _bufferService: IBufferService ) { - this._cols = this._terminal.cols; - this._rows = this._terminal.rows; + this._cols = this._bufferService.cols; + this._rows = this._bufferService.rows; this.clear(); } @@ -71,7 +71,7 @@ export class Buffer implements IBuffer { } public getBlankLine(attr: IAttributeData, isWrapped?: boolean): IBufferLine { - return new BufferLine(this._terminal.cols, this.getNullCell(attr), isWrapped); + return new BufferLine(this._bufferService.cols, this.getNullCell(attr), isWrapped); } public get hasScrollback(): boolean { diff --git a/src/BufferSet.test.ts b/src/BufferSet.test.ts index fb70e9543..e8e978e50 100644 --- a/src/BufferSet.test.ts +++ b/src/BufferSet.test.ts @@ -4,20 +4,18 @@ */ import { assert } from 'chai'; -import { ITerminal } from './Types'; import { BufferSet } from './BufferSet'; import { Buffer } from './Buffer'; -import { MockTerminal, MockOptionsService } from './TestUtils.test'; +import { MockOptionsService, MockBufferService } from './TestUtils.test'; describe('BufferSet', () => { - let terminal: ITerminal; let bufferSet: BufferSet; beforeEach(() => { - terminal = new MockTerminal(); - (terminal as any).cols = 80; - (terminal as any).rows = 24; - bufferSet = new BufferSet(terminal, new MockOptionsService({ scrollback: 1000 })); + bufferSet = new BufferSet( + new MockOptionsService({ scrollback: 1000 }), + new MockBufferService(80, 24) + ); }); describe('constructor', () => { diff --git a/src/BufferSet.ts b/src/BufferSet.ts index f5df2073e..4b61c39ad 100644 --- a/src/BufferSet.ts +++ b/src/BufferSet.ts @@ -3,11 +3,11 @@ * @license MIT */ -import { ITerminal, IBufferSet, IBuffer } from './Types'; +import { IBufferSet, IBuffer } from './Types'; import { IAttributeData } from 'common/Types'; import { Buffer } from './Buffer'; import { EventEmitter2, IEvent } from 'common/EventEmitter2'; -import { IOptionsService } from 'common/services/Services'; +import { IOptionsService, IBufferService } from 'common/services/Services'; /** * The BufferSet represents the set of two buffers used by xterm terminals (normal and alt) and @@ -27,15 +27,15 @@ export class BufferSet implements IBufferSet { * @param _terminal - The terminal the BufferSet will belong to */ constructor( - private _terminal: ITerminal, - readonly optionsService: IOptionsService + readonly optionsService: IOptionsService, + readonly bufferService: IBufferService ) { - this._normal = new Buffer(this._terminal, true, optionsService); + this._normal = new Buffer(true, optionsService, bufferService); this._normal.fillViewportRows(); // The alt buffer should never have scrollback. // See http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-The-Alternate-Screen-Buffer - this._alt = new Buffer(this._terminal, false, optionsService); + this._alt = new Buffer(false, optionsService, bufferService); this._activeBuffer = this._normal; this.setupTabStops(); diff --git a/src/SelectionManager.test.ts b/src/SelectionManager.test.ts index 970a8e9bf..f2f5cb9aa 100644 --- a/src/SelectionManager.test.ts +++ b/src/SelectionManager.test.ts @@ -9,7 +9,7 @@ import { SelectionModel } from './SelectionModel'; import { BufferSet } from './BufferSet'; import { ITerminal, IBuffer } from './Types'; import { IBufferLine } from 'common/Types'; -import { MockTerminal, MockCharSizeService, MockOptionsService } from './TestUtils.test'; +import { MockTerminal, MockCharSizeService, MockOptionsService, MockBufferService } from './TestUtils.test'; import { BufferLine, CellData } from 'common/buffer/BufferLine'; class TestMockTerminal extends MockTerminal { @@ -44,9 +44,10 @@ describe('SelectionManager', () => { beforeEach(() => { terminal = new TestMockTerminal(); - (terminal as any).cols = 80; - (terminal as any).rows = 2; - terminal.buffers = new BufferSet(terminal, new MockOptionsService({ scrollback: 100 })); + terminal.buffers = new BufferSet( + new MockOptionsService({ scrollback: 100 }), + new MockBufferService(80, 2) + ); terminal.buffer = terminal.buffers.active; buffer = terminal.buffer; selectionManager = new TestSelectionManager(terminal); diff --git a/src/SelectionModel.test.ts b/src/SelectionModel.test.ts index 05444aac6..1b011e25d 100644 --- a/src/SelectionModel.test.ts +++ b/src/SelectionModel.test.ts @@ -7,7 +7,7 @@ import { assert } from 'chai'; import { ITerminal } from './Types'; import { SelectionModel } from './SelectionModel'; import { BufferSet } from './BufferSet'; -import { MockTerminal, MockOptionsService } from './TestUtils.test'; +import { MockTerminal, MockOptionsService, MockBufferService } from './TestUtils.test'; class TestSelectionModel extends SelectionModel { constructor( @@ -23,9 +23,10 @@ describe('SelectionManager', () => { beforeEach(() => { terminal = new MockTerminal(); - (terminal as any).cols = 80; - (terminal as any).rows = 2; - terminal.buffers = new BufferSet(terminal, new MockOptionsService({ scrollback: 10 })); + terminal.buffers = new BufferSet( + new MockOptionsService({ scrollback: 10 }), + new MockBufferService(80, 2) + ); terminal.buffer = terminal.buffers.active; model = new TestSelectionModel(terminal); diff --git a/src/Terminal.ts b/src/Terminal.ts index 402a4a2bc..38efd3b4a 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -313,7 +313,7 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II this.soundManager = this.soundManager || new SoundManager(this); // Create the terminal's buffers and set the current buffer - this.buffers = new BufferSet(this, this.optionsService); + this.buffers = new BufferSet(this.optionsService, this._bufferService); if (this.selectionManager) { this.selectionManager.clearSelection(); this.selectionManager.initBuffersListeners(); diff --git a/src/TestUtils.test.ts b/src/TestUtils.test.ts index 5ac908e4f..0ffdcf92f 100644 --- a/src/TestUtils.test.ts +++ b/src/TestUtils.test.ts @@ -12,7 +12,7 @@ import { IDisposable, IMarker, IEvent, ISelectionPosition } from 'xterm'; import { Terminal } from './Terminal'; import { AttributeData } from 'common/buffer/BufferLine'; import { IColorManager, IColorSet } from 'browser/Types'; -import { IOptionsService, IPartialTerminalOptions, ITerminalOptions } from 'common/services/Services'; +import { IOptionsService, IPartialTerminalOptions, ITerminalOptions, IBufferService } from 'common/services/Services'; import { ICharSizeService } from 'browser/services/Services'; import { DEFAULT_OPTIONS } from 'common/services/OptionsService'; import { clone } from 'common/Clone'; @@ -451,3 +451,15 @@ export class MockOptionsService implements IOptionsService { throw new Error('Method not implemented.'); } } + +export class MockBufferService implements IBufferService { + constructor( + public cols: number, + public rows: number + ) {} + resize(cols: number, rows: number): void { + this.cols = cols; + this.rows = rows; + } + +}