simple-ascii-chart
Version:
Simple ascii chart generator
208 lines (207 loc) • 10.7 kB
JavaScript
;
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
var overrides_1 = require("../overrides");
var constants_1 = require("../../constants");
describe('Graph Utility Functions', function () {
var graph = [];
var defaultGraph = [];
var backgroundSymbol = constants_1.EMPTY;
var plotWidth = 10;
var yShift = 2;
beforeEach(function () {
defaultGraph = Array(8).fill(__spreadArray([], __read(Array(plotWidth).fill(backgroundSymbol)), false)); // Adjusted size to 8 rows
graph = Array(8).fill(__spreadArray([], __read(Array(plotWidth).fill(backgroundSymbol)), false)); // Adjusted size to 8 rows
});
describe('setTitle', function () {
it('should set the title correctly', function () {
var title = 'TestTitle';
(0, overrides_1.setTitle)({ title: title, graph: graph, backgroundSymbol: backgroundSymbol, plotWidth: plotWidth, yShift: yShift });
expect(graph[0].join('')).toContain(title);
});
});
describe('addXLable', function () {
it('should add the xLabel correctly', function () {
var xLabel = 'XLabel';
(0, overrides_1.addXLable)({ xLabel: xLabel, graph: graph, backgroundSymbol: backgroundSymbol, plotWidth: plotWidth, yShift: yShift });
expect(graph[graph.length - 1].join('')).toContain(xLabel);
});
});
describe('addYLabel', function () {
it('should add the yLabel correctly', function () {
var yLabel = 'YLabel';
(0, overrides_1.addYLabel)({ yLabel: yLabel, graph: graph, backgroundSymbol: backgroundSymbol });
expect(graph[0].reverse().join('')).toContain(yLabel);
});
});
describe('addLegend', function () {
it('should add the legend correctly', function () {
var legend = { position: 'top', series: ['A', 'B'] };
(0, overrides_1.addLegend)({ legend: legend, graph: graph, backgroundSymbol: backgroundSymbol, input: [[]] });
expect(graph[0].join('')).toContain('A');
expect(graph[1].join('')).toContain('B');
});
});
describe('addBorder', function () {
it('should add the border correctly', function () {
var borderSymbol = '#';
(0, overrides_1.addBorder)({ graph: graph, borderSymbol: borderSymbol });
expect(graph[0][0]).toBe(borderSymbol);
expect(graph[0][graph[0].length - 1]).toBe(borderSymbol);
expect(graph[graph.length - 1][0]).toBe(borderSymbol);
expect(graph[graph.length - 1][graph[0].length - 1]).toBe(borderSymbol);
});
});
describe('addBackgroundSymbol', function () {
it('should replace empty symbols with background symbols', function () {
var emptySymbol = ' ';
graph[1][1] = emptySymbol;
(0, overrides_1.addBackgroundSymbol)({ graph: graph, backgroundSymbol: backgroundSymbol, emptySymbol: emptySymbol });
expect(graph[1][1]).toBe(backgroundSymbol);
});
});
describe('addThresholds', function () {
it('should add thresholds correctly', function () {
var thresholds = [{ x: 2, color: 'ansiRed' }];
var axis = { x: 0, y: 0 };
var plotHeight = 10;
var expansionX = [0, plotWidth];
var expansionY = [0, plotHeight];
(0, overrides_1.addThresholds)({ graph: graph, thresholds: thresholds, axis: axis, plotWidth: plotWidth, plotHeight: plotHeight, expansionX: expansionX, expansionY: expansionY });
expect(graph[2][3]).toContain(constants_1.CHART.ns);
});
it('should add thresholds correctly - y', function () {
var thresholds = [{ y: 5, color: 'ansiRed' }];
var axis = { x: 0, y: 0 };
var plotHeight = 10;
var expansionX = [0, plotWidth];
var expansionY = [0, plotHeight];
(0, overrides_1.addThresholds)({ graph: graph, thresholds: thresholds, axis: axis, plotWidth: plotWidth, plotHeight: plotHeight, expansionX: expansionX, expansionY: expansionY });
expect(graph[2][3]).toContain(constants_1.CHART.we);
});
it('should add color', function () {
var thresholds = [{ x: 1, y: 2, color: 'ansiBlue' }];
var axis = { x: 0, y: 0 };
var plotHeight = 10;
var expansionX = [0, plotWidth];
var expansionY = [0, plotHeight];
(0, overrides_1.addThresholds)({ graph: graph, thresholds: thresholds, axis: axis, plotWidth: plotWidth, plotHeight: plotHeight, expansionX: expansionX, expansionY: expansionY });
expect(graph[2].join('')).toContain('\u001b[34m');
});
it('should add two thresholds ', function () {
var thresholds = [
{ x: 2, color: 'ansiBlue' },
{ x: 3, color: 'ansiRed' },
];
var axis = { x: 0, y: 0 };
var plotHeight = 10;
var expansionX = [0, plotWidth];
var expansionY = [0, plotHeight];
(0, overrides_1.addThresholds)({ graph: graph, thresholds: thresholds, axis: axis, plotWidth: plotWidth, plotHeight: plotHeight, expansionX: expansionX, expansionY: expansionY });
expect(graph[0][3]).toContain('\u001b[34m');
expect(graph[0][4]).toContain('\u001b[31m');
});
it('should not add color if not set', function () {
var thresholds = [{ x: 1, y: 2 }];
var axis = { x: 0, y: 0 };
var plotHeight = 10;
var expansionX = [0, plotWidth];
var expansionY = [0, plotHeight];
(0, overrides_1.addThresholds)({ graph: graph, thresholds: thresholds, axis: axis, plotWidth: plotWidth, plotHeight: plotHeight, expansionX: expansionX, expansionY: expansionY });
expect(graph[2].join('')).not.toContain('\u001b[0m');
});
it('should not add color if not set', function () {
var thresholds = [{ x: undefined }];
var axis = { x: 0, y: 0 };
var plotHeight = 10;
var expansionX = [0, plotWidth];
var expansionY = [0, plotHeight];
(0, overrides_1.addThresholds)({ graph: graph, thresholds: thresholds, axis: axis, plotWidth: plotWidth, plotHeight: plotHeight, expansionX: expansionX, expansionY: expansionY });
expect(graph).toEqual(defaultGraph);
});
it('should not add thresholds if its outside - x', function () {
var thresholds = [{ x: 100, color: 'ansiRed' }];
var axis = { x: 0, y: 0 };
var plotHeight = 10;
var expansionX = [0, plotWidth];
var expansionY = [0, plotHeight];
(0, overrides_1.addThresholds)({ graph: graph, thresholds: thresholds, axis: axis, plotWidth: plotWidth, plotHeight: plotHeight, expansionX: expansionX, expansionY: expansionY });
expect(graph.map(function (line) { return line.join(''); }).join('')).not.toContain(constants_1.CHART.ns);
});
it('should not add thresholds if its outside - y', function () {
var thresholds = [{ y: 100, color: 'ansiRed' }];
var axis = { x: 0, y: 0 };
var plotHeight = 10;
var expansionX = [0, plotWidth];
var expansionY = [0, plotHeight];
(0, overrides_1.addThresholds)({ graph: graph, thresholds: thresholds, axis: axis, plotWidth: plotWidth, plotHeight: plotHeight, expansionX: expansionX, expansionY: expansionY });
expect(graph.map(function (line) { return line.join(''); }).join('')).not.toContain(constants_1.CHART.we);
});
});
describe('setFillArea', function () {
it('should set fill area correctly', function () {
var chartSymbols = { nse: constants_1.CHART.nse, wsn: constants_1.CHART.wsn, we: constants_1.CHART.we, area: 'A' };
graph[1][1] = constants_1.CHART.nse;
(0, overrides_1.setFillArea)({ graph: graph, chartSymbols: chartSymbols });
expect(graph[0][1]).toBe('A');
});
it('should use fill area correctly', function () {
var chartSymbols = { nse: constants_1.CHART.nse, wsn: constants_1.CHART.wsn, we: constants_1.CHART.we };
graph[1][1] = constants_1.CHART.nse;
(0, overrides_1.setFillArea)({ graph: graph, chartSymbols: chartSymbols });
expect(graph[0][1]).toBe(constants_1.CHART.area);
});
});
describe('removeEmptyLines', function () {
it('should remove empty lines correctly', function () {
var currentGraph = [
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i'],
__spreadArray([], __read(Array(plotWidth).fill(backgroundSymbol)), false),
];
(0, overrides_1.removeEmptyLines)({ graph: currentGraph, backgroundSymbol: backgroundSymbol });
expect(currentGraph.length).toBe(3);
expect(currentGraph).toEqual([
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i'],
]);
});
});
describe('getTransformLabel', function () {
it('should return a formatter function', function () {
var formatter = (0, overrides_1.getTransformLabel)({});
expect(formatter).toBeInstanceOf(Function);
});
it('should format labels correctly with a custom formatter', function () {
var customFormatter = function (value) { return "Custom: ".concat(value); };
var formatter = (0, overrides_1.getTransformLabel)({ formatter: customFormatter });
expect(formatter(1, {})).toBe('Custom: 1');
});
});
});