Skip to content

Commit

Permalink
Extends unit tests to cover 100% of statements.
Browse files Browse the repository at this point in the history
  • Loading branch information
kasargeant committed Jun 17, 2017
1 parent 462ae5c commit 8e1d226
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 63 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ Tinter has been benchmarked on Node versions upwards of v6.10. Running on a rec

For stacked composition (e.g. tinter.red.blueBg.underline("hi there!"); ):-

chalk - stacked color/styles x 470,192 ops/sec ±1.00% (89 runs sampled)
colors - stacked color/styles x 733,671 ops/sec ±1.04% (89 runs sampled)
chalk - stacked color/styles x 470,192 ops/sec ±1.00% (89 runs sampled)
colors - stacked color/styles x 733,671 ops/sec ±1.04% (89 runs sampled)
tinter - stacked color/styles x 8,176,748 ops/sec ±1.20% (83 runs sampled)

Fastest is: 'tinter - stacked color/styles'.
Expand Down
49 changes: 26 additions & 23 deletions src/js/Tinter.js
Original file line number Diff line number Diff line change
Expand Up @@ -649,37 +649,40 @@ Tinter.defaultBg = function(text) {return `\x1b[49m${text}`;};
Tinter.plain = Tinter.reset;
Tinter._setEnv = _setEnv;

// Adds a conservative 8 col, 8 style 'chained' syntax. No more though... otherwise both mem and perf impaired.
for(let s = 0; s < styles.length - 1; s++) {
let style = styles[s];
if(s === 0) {style = "plain";}
for(let fgIdx = 0; fgIdx < coreColors.length; fgIdx++) {
let color = coreColors[fgIdx];
let fg = (color === "default") ? 39 : 90 + fgIdx;
for(let bgIdx = 0; bgIdx < coreColors.length; bgIdx++) {
let colorBg = coreColors[bgIdx] + "Bg";
let bg = (colorBg === "defaultBg") ? 49 : 100 + bgIdx;
if(Tinter[color][colorBg] === undefined) {
//console.log(`-> Tinter[${color}][${colorBg}]`);
Tinter[color][colorBg] = function(text) {
function _setupTinterStacks() {
// Adds a conservative 8 col, 8 style 'chained' syntax. No more though... otherwise both mem and perf impaired.
for(let s = 0; s < styles.length - 1; s++) {
let style = styles[s];
if(s === 0) {style = "plain";}
for(let fgIdx = 0; fgIdx < coreColors.length; fgIdx++) {
let color = coreColors[fgIdx];
let fg = (color === "default") ? 39 : 90 + fgIdx;
for(let bgIdx = 0; bgIdx < coreColors.length; bgIdx++) {
let colorBg = coreColors[bgIdx] + "Bg";
let bg = (colorBg === "defaultBg") ? 49 : 100 + bgIdx;
if(Tinter[color][colorBg] === undefined) {
//console.log(`-> Tinter[${color}][${colorBg}]`);
Tinter[color][colorBg] = function(text) {
if(text === undefined) {
return `\x1b[${fg}m\x1b[${bg}m`;
}
return `\x1b[${fg}m\x1b[${bg}m${text}\x1b[0m`;
};
}
//console.log(`-> Tinter[${color}][${colorBg}][${style}]`);
Tinter[color][colorBg][style] = function(text) {
if(text === undefined) {
return `\x1b[${fg}m\x1b[${bg}m`;
return `\x1b[${s}m\x1b[${fg}m\x1b[${bg}m`;
}
return `\x1b[${fg}m\x1b[${bg}m${text}\x1b[0m`;
return `\x1b[${s}m\x1b[${fg}m\x1b[${bg}m${text}\x1b[0m`;
};
}
//console.log(`-> Tinter[${color}][${colorBg}][${style}]`);
Tinter[color][colorBg][style] = function(text) {
if(text === undefined) {
return `\x1b[${s}m\x1b[${fg}m\x1b[${bg}m`;
}
return `\x1b[${s}m\x1b[${fg}m\x1b[${bg}m${text}\x1b[0m`;
};
}
}
}

_setupTinterStacks();
/* jshint ignore:end */

// Exports
module.exports = Tinter;

43 changes: 31 additions & 12 deletions test/js/Tinter16.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,30 +146,49 @@ describe("Class: Tinter (Node/16-color ANSI mode)", function() {

});

describe("Colorization functions (composite)", function() {
describe("Colorization functions (naming)", function() {

it("should be able mark a string with overlapping characteristics", function() {
expect(Tinter.style(DUMMY_STRING, "yellow", "blue", "italic")).toBe(`\x1b[3m\x1b[1m\x1b[104m\x1b[1m\x1b[93m${DUMMY_STRING}\x1b[0m`);
it("should correctly support ANSI named colors", function() {
expect(Tinter.Black(DUMMY_STRING)).toBe(`\x1b[1m\x1b[30m${DUMMY_STRING}\x1b[0m`);
});

// NOTE: 8-col restriction for chaining.
it("should be able mark a string with chained characteristics", function() {
expect(Tinter.red.blueBg.inverse(DUMMY_STRING)).toBe(`\x1b[7m\x1b[91m\x1b[104m${DUMMY_STRING}\x1b[0m`);
it("should correctly support CSS4 named colors", function() {
expect(Tinter.rebeccapurple(DUMMY_STRING)).toBe(`\x1b[1m\x1b[34m${DUMMY_STRING}\x1b[0m`);
});
});

it("should degrade a truecolor to 16-color appropriately.", function() {
expect(Tinter.rgb(DUMMY_STRING, [255,255,127], [192, 0, 55], "underline")).toBe(`\x1b[4m\x1b[1m\x1b[101m\x1b[1m\x1b[93m${DUMMY_STRING}\x1b[0m`);
describe("Colorization functions (composition by stacking)", function() {

it("should be able mark a string with 2 stacked characteristics", function() {
expect(Tinter.red.blueBg(DUMMY_STRING)).toBe(`\x1b[91m\x1b[104m${DUMMY_STRING}\x1b[0m`);
});

it("should correctly support ANSI named colors", function() {
expect(Tinter.Black(DUMMY_STRING)).toBe(`\x1b[1m\x1b[30m${DUMMY_STRING}\x1b[0m`);
it("should be able mark a string with ALL stacked characteristics", function() {
expect(Tinter.red.blueBg.inverse(DUMMY_STRING)).toBe(`\x1b[7m\x1b[91m\x1b[104m${DUMMY_STRING}\x1b[0m`);
});
});

it("should correctly support CSS4 named colors", function() {
expect(Tinter.rebeccapurple(DUMMY_STRING)).toBe(`\x1b[1m\x1b[34m${DUMMY_STRING}\x1b[0m`);
describe("Colorization functions (composition by streaming)", function() {

it("should be able mark a string with 2 stacked characteristics", function() {
expect(Tinter.red.blueBg()).toBe(`\x1b[91m\x1b[104m`);
});

it("should be able mark a string with ALL stacked characteristics", function() {
expect(Tinter.red.blueBg.inverse()).toBe(`\x1b[7m\x1b[91m\x1b[104m`);
});
});

describe("Colorization functions (composition by direct calls)", function() {

it("should be able mark a string with overlapping characteristics", function() {
expect(Tinter.style(DUMMY_STRING, "yellow", "blue", "italic")).toBe(`\x1b[3m\x1b[1m\x1b[104m\x1b[1m\x1b[93m${DUMMY_STRING}\x1b[0m`);
});

it("should degrade a truecolor to 16-color appropriately.", function() {
expect(Tinter.rgb(DUMMY_STRING, [255,255,127], [192, 0, 55], "underline")).toBe(`\x1b[4m\x1b[1m\x1b[101m\x1b[1m\x1b[93m${DUMMY_STRING}\x1b[0m`);
});
});

describe("Color degrading functions", function() {

Expand Down
45 changes: 31 additions & 14 deletions test/js/Tinter16M.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ describe("Class: Tinter (Node/16M+ truecolor [using CSS Named colors])", functio
});

it("should be able mark a string as white", function() {
// console.log("IDX: " + Tinter.webColors.indexOf("white"));
expect(Tinter.white(DUMMY_STRING)).toBe(`\x1b[1m\x1b[38;2;255;255;255m${DUMMY_STRING}\x1b[0m`);
});

Expand Down Expand Up @@ -138,7 +137,6 @@ describe("Class: Tinter (Node/16M+ truecolor [using CSS Named colors])", functio
});

it("should be able mark a string with a white background", function() {
// console.log("IDX: " + Tinter.webColors.indexOf("whiteBg"));
expect(Tinter.whiteBg(DUMMY_STRING)).toBe(`\x1b[1m\x1b[48;2;255;255;255m${DUMMY_STRING}\x1b[0m`);
});

Expand All @@ -148,30 +146,49 @@ describe("Class: Tinter (Node/16M+ truecolor [using CSS Named colors])", functio

});

describe("Colorization functions (composite)", function() {
describe("Colorization functions (naming)", function() {

it("should be able mark a string with overlapping characteristics", function() {
expect(Tinter.style(DUMMY_STRING, "yellow", "blue", "italic")).toBe(`\x1b[3m\x1b[1m\x1b[48;2;0;0;255m\x1b[1m\x1b[38;2;255;255;0m${DUMMY_STRING}\x1b[0m`);
it("should correctly support ANSI named colors", function() {
expect(Tinter.Black(DUMMY_STRING)).toBe(`\x1b[1m\x1b[38;2;0;0;0m${DUMMY_STRING}\x1b[0m`);
});

// NOTE: 8-col restriction for chaining.
it("should be able mark a string with chained characteristics", function() {
expect(Tinter.red.blueBg.inverse(DUMMY_STRING)).toBe(`\x1b[7m\x1b[91m\x1b[104m${DUMMY_STRING}\x1b[0m`);
it("should correctly support CSS4 named colors", function() {
expect(Tinter.rebeccapurple(DUMMY_STRING)).toBe(`\x1b[1m\x1b[38;2;102;51;153m${DUMMY_STRING}\x1b[0m`);
});
});

it("should NOT NEED TO degrade a truecolor - but use RGB values directly.", function() {
expect(Tinter.rgb(DUMMY_STRING, [255,255,127], [192, 0, 55], "underline")).toBe(`\x1b[4m\x1b[1m\x1b[48;2;192;0;55m\x1b[1m\x1b[38;2;255;255;127m${DUMMY_STRING}\x1b[0m`);
describe("Colorization functions (composition by stacking)", function() {

it("should be able mark a string with 2 stacked characteristics", function() {
expect(Tinter.red.blueBg(DUMMY_STRING)).toBe(`\x1b[91m\x1b[104m${DUMMY_STRING}\x1b[0m`);
});

it("should correctly support ANSI named colors", function() {
expect(Tinter.Black(DUMMY_STRING)).toBe(`\x1b[1m\x1b[38;2;0;0;0m${DUMMY_STRING}\x1b[0m`);
it("should be able mark a string with ALL stacked characteristics", function() {
expect(Tinter.red.blueBg.inverse(DUMMY_STRING)).toBe(`\x1b[7m\x1b[91m\x1b[104m${DUMMY_STRING}\x1b[0m`);
});
});

it("should correctly support CSS4 named colors", function() {
expect(Tinter.rebeccapurple(DUMMY_STRING)).toBe(`\x1b[1m\x1b[38;2;102;51;153m${DUMMY_STRING}\x1b[0m`);
describe("Colorization functions (composition by streaming)", function() {

it("should be able mark a string with 2 stacked characteristics", function() {
expect(Tinter.red.blueBg()).toBe(`\x1b[91m\x1b[104m`);
});

it("should be able mark a string with ALL stacked characteristics", function() {
expect(Tinter.red.blueBg.inverse()).toBe(`\x1b[7m\x1b[91m\x1b[104m`);
});
});

describe("Colorization functions (composition by direct calls)", function() {

it("should be able mark a string with overlapping characteristics", function() {
expect(Tinter.style(DUMMY_STRING, "yellow", "blue", "italic")).toBe(`\x1b[3m\x1b[1m\x1b[48;2;0;0;255m\x1b[1m\x1b[38;2;255;255;0m${DUMMY_STRING}\x1b[0m`);
});

it("should NOT NEED TO degrade a truecolor - but use RGB values directly.", function() {
expect(Tinter.rgb(DUMMY_STRING, [255,255,127], [192, 0, 55], "underline")).toBe(`\x1b[4m\x1b[1m\x1b[48;2;192;0;55m\x1b[1m\x1b[38;2;255;255;127m${DUMMY_STRING}\x1b[0m`);
});
});

describe("Color degrading functions", function() {

Expand Down
43 changes: 31 additions & 12 deletions test/js/Tinter256.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,30 +148,49 @@ describe("Class: Tinter (Node/256-color [using CSS Named colors])", function() {

});

describe("Colorization functions (composite)", function() {
describe("Colorization functions (naming)", function() {

it("should be able mark a string with overlapping characteristics", function() {
expect(Tinter.style(DUMMY_STRING, "yellow", "blue", "italic")).toBe(`\x1b[3m\x1b[1m\x1b[48;5;21m\x1b[1m\x1b[38;5;226m${DUMMY_STRING}\x1b[0m`);
it("should correctly support ANSI named colors", function() {
expect(Tinter.Black(DUMMY_STRING)).toBe(`\x1b[1m\x1b[38;5;16m${DUMMY_STRING}\x1b[0m`);
});

// NOTE: 8-col restriction for chaining.
it("should be able mark a string with chained characteristics", function() {
expect(Tinter.red.blueBg.inverse(DUMMY_STRING)).toBe(`\x1b[7m\x1b[91m\x1b[104m${DUMMY_STRING}\x1b[0m`);
it("should correctly support CSS4 named colors", function() {
expect(Tinter.rebeccapurple(DUMMY_STRING)).toBe(`\x1b[1m\x1b[38;5;97m${DUMMY_STRING}\x1b[0m`);
});
});

it("should degrade a truecolor to 256-color appropriately.", function() {
expect(Tinter.rgb(DUMMY_STRING, [255,255,127], [192, 0, 55], "underline")).toBe(`\x1b[4m\x1b[1m\x1b[48;5;196m\x1b[1m\x1b[38;5;226m${DUMMY_STRING}\x1b[0m`);
describe("Colorization functions (composition by stacking)", function() {

it("should be able mark a string with 2 stacked characteristics", function() {
expect(Tinter.red.blueBg(DUMMY_STRING)).toBe(`\x1b[91m\x1b[104m${DUMMY_STRING}\x1b[0m`);
});

it("should correctly support ANSI named colors", function() {
expect(Tinter.Black(DUMMY_STRING)).toBe(`\x1b[1m\x1b[38;5;16m${DUMMY_STRING}\x1b[0m`);
it("should be able mark a string with ALL stacked characteristics", function() {
expect(Tinter.red.blueBg.inverse(DUMMY_STRING)).toBe(`\x1b[7m\x1b[91m\x1b[104m${DUMMY_STRING}\x1b[0m`);
});
});

it("should correctly support CSS4 named colors", function() {
expect(Tinter.rebeccapurple(DUMMY_STRING)).toBe(`\x1b[1m\x1b[38;5;97m${DUMMY_STRING}\x1b[0m`);
describe("Colorization functions (composition by streaming)", function() {

it("should be able mark a string with 2 stacked characteristics", function() {
expect(Tinter.red.blueBg()).toBe(`\x1b[91m\x1b[104m`);
});

it("should be able mark a string with ALL stacked characteristics", function() {
expect(Tinter.red.blueBg.inverse()).toBe(`\x1b[7m\x1b[91m\x1b[104m`);
});
});

describe("Colorization functions (composition by direct calls)", function() {

it("should be able mark a string with overlapping characteristics", function() {
expect(Tinter.style(DUMMY_STRING, "yellow", "blue", "italic")).toBe(`\x1b[3m\x1b[1m\x1b[48;5;21m\x1b[1m\x1b[38;5;226m${DUMMY_STRING}\x1b[0m`);
});

it("should degrade a truecolor to 256-color appropriately.", function() {
expect(Tinter.rgb(DUMMY_STRING, [255,255,127], [192, 0, 55], "underline")).toBe(`\x1b[4m\x1b[1m\x1b[48;5;196m\x1b[1m\x1b[38;5;226m${DUMMY_STRING}\x1b[0m`);
});
});

describe("Color degrading functions", function() {

Expand Down
12 changes: 12 additions & 0 deletions test/js/TinterErrors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ describe("Class: Tinter (Node/unknown environ)", function() {
});
});

describe("_nearest16() errors", function() {

// Bad types
it("should throw an error with a style of the wrong type", function() {
// expect(function() {Tinter._styleTruecolor("anything", [255,255,127], [192, 0, 55], "REALLYWRONG")}).toThrowError(new Error(`Unrecognised text style: 'REALLYWRONG'.`));
expect(function() {
// Tinter._nearest16([255, 255, 127]);
Tinter._nearest16(255, 255, 127);
}).toThrow();
});
});

describe("_styleTruecolor() errors", function() {

// Bad types
Expand Down

0 comments on commit 8e1d226

Please sign in to comment.