Add tests for ColorZoneStore

This commit is contained in:
Daniel Imms
2022-04-01 07:05:05 -07:00
parent e98a20ba9e
commit 7eace4d479
2 changed files with 94 additions and 2 deletions
@@ -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]);
});
});
+6 -2
View File
@@ -24,6 +24,11 @@ export interface IColorZone {
endBufferLine: number;
}
interface IMinimalDecorationForColorZone {
marker: Pick<IInternalDecoration['marker'], 'line'>;
options: Pick<IInternalDecoration['options'], 'overviewRulerOptions'>;
}
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;
}