From 7eace4d4799516f3f563cb3da5066c2eca6f700f Mon Sep 17 00:00:00 2001 From: Daniel Imms <2193314+Tyriar@users.noreply.github.com> Date: Fri, 1 Apr 2022 07:05:05 -0700 Subject: [PATCH] Add tests for ColorZoneStore --- .../Decorations/ColorZoneStore.test.ts | 88 +++++++++++++++++++ src/browser/Decorations/ColorZoneStore.ts | 8 +- 2 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 src/browser/Decorations/ColorZoneStore.test.ts diff --git a/src/browser/Decorations/ColorZoneStore.test.ts b/src/browser/Decorations/ColorZoneStore.test.ts new file mode 100644 index 00000000..73e3402f --- /dev/null +++ b/src/browser/Decorations/ColorZoneStore.test.ts @@ -0,0 +1,88 @@ +/** + * Copyright (c) 2017 The xterm.js authors. All rights reserved. + * @license MIT + */ + +import { assert } from 'chai'; +import { ColorZoneStore } from 'browser/Decorations/ColorZoneStore'; + +const optionsRedFull = { + overviewRulerOptions: { + color: 'red', + position: 'full' as 'full' + } +}; + +describe('ColorZoneStore', () => { + let store: ColorZoneStore; + + beforeEach(() => { + store = new ColorZoneStore(); + store.setPadding({ + full: 1, + left: 1, + center: 1, + right: 1 + }); + }); + + it('should merge adjacent zones', () => { + store.addDecoration({ + marker: { line: 0 }, + options: optionsRedFull + }); + store.addDecoration({ + marker: { line: 1 }, + options: optionsRedFull + }); + assert.deepStrictEqual(store.zones, [ + { + color: 'red', + position: 'full', + startBufferLine: 0, + endBufferLine: 1 + } + ]); + }); + + it('should not merge non-adjacent zones', () => { + store.addDecoration({ + marker: { line: 0 }, + options: optionsRedFull + }); + store.addDecoration({ + marker: { line: 2 }, + options: optionsRedFull + }); + assert.deepStrictEqual(store.zones, [ + { + color: 'red', + position: 'full', + startBufferLine: 0, + endBufferLine: 0 + }, + { + color: 'red', + position: 'full', + startBufferLine: 2, + endBufferLine: 2 + } + ]); + }); + + it('should reuse zone objects', () => { + const obj = { + marker: { line: 0 }, + options: optionsRedFull + }; + store.addDecoration(obj); + const zone = store.zones[0]; + store.clear(); + store.addDecoration({ + marker: { line: 1 }, + options: optionsRedFull + }); + // The object reference should be the same + assert.equal(zone, store.zones[0]); + }); +}); diff --git a/src/browser/Decorations/ColorZoneStore.ts b/src/browser/Decorations/ColorZoneStore.ts index bf68dc14..d066bedb 100644 --- a/src/browser/Decorations/ColorZoneStore.ts +++ b/src/browser/Decorations/ColorZoneStore.ts @@ -24,6 +24,11 @@ export interface IColorZone { endBufferLine: number; } +interface IMinimalDecorationForColorZone { + marker: Pick; + options: Pick; +} + export class ColorZoneStore implements IColorZoneStore { private _zones: IColorZone[] = []; @@ -51,7 +56,7 @@ export class ColorZoneStore implements IColorZoneStore { this._zonePoolIndex = 0; } - public addDecoration(decoration: IInternalDecoration): void { + public addDecoration(decoration: IMinimalDecorationForColorZone): void { if (!decoration.options.overviewRulerOptions) { return; } @@ -62,7 +67,6 @@ export class ColorZoneStore implements IColorZoneStore { return; } if (this._lineAdjacentToZone(z, decoration.marker.line, decoration.options.overviewRulerOptions.position)) { - console.log('add line to zone'); this._addLineToZone(z, decoration.marker.line); return; }