Skip to content

Commit

Permalink
added more tests
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Wilkowski <[email protected]>
  • Loading branch information
dominikwilkowski committed Apr 22, 2018
1 parent 3772d33 commit 5bfc387
Show file tree
Hide file tree
Showing 9 changed files with 1,070 additions and 33 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ npm run test


## Release History
* 2.0.0 - Added tests, split into more pure functions
* 1.2.0 - Added `transparent` and `system` as default background and color option, added `backgroundColor` as alias for `background`, upgraded deps
* 1.1.3 - Fixed help text, removing old -t option
* 1.1.2 - Fixed issue with older commander version #3, updated docs
Expand Down
72 changes: 39 additions & 33 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,16 +447,19 @@ const CheckInput = (
/**
* Render our input with the console font
*
* @param {string} INPUT - The string you want to write out
* @param {object} OPTIONS - All user options
* @param {string} INPUT - The string you want to write out
* @param {object} OPTIONS - All user options
* @param {object} size - The size of the terminal as an object, default: Size
* @param {integer} size.width - The width of the terminal
* @param {integer} size.height - The height of the terminal
*
* @typedef {object} ReturnObject
* @property {array} output - An array of each line of the output
* @property {integer} lines - The count of line breaks
* @property {array} output - An array of each line of the output
* @property {integer} lines - The count of line breaks
*
* @return {ReturnObject} - An object with the output and the line breaks
* @return {ReturnObject} - An object with the output and the line breaks
*/
const RenderConsole = ( INPUT, FONTFACE, OPTIONS ) => {
const RenderConsole = ( INPUT, OPTIONS, size = Size ) => {
let output = [];
let i = 0;

Expand Down Expand Up @@ -484,24 +487,24 @@ const RenderConsole = ( INPUT, FONTFACE, OPTIONS ) => {
while( i < outputLines.length ) {
let line = outputLines[ i ];

if( line.length > Size.width ) {
outputLines[ i ] = line.slice( 0, Size.width ).trim();
outputLines.splice( i + 1, 0, line.slice( Size.width ).trim() );
if( line.length > size.width ) {
outputLines[ i ] = line.slice( 0, size.width ).trim();
outputLines.splice( i + 1, 0, line.slice( size.width ).trim() );
line = outputLines[ i ];
}

if( OPTIONS.colors[ 0 ] === "candy" ) {
output.push( line
.split('')
.map( character => Colorize( character, FONTFACE.colors, OPTIONS.colors ) )
.map( character => Colorize( character, 1, OPTIONS.colors ) )
.join('')
);
}
else {
output.push( line );
}

output = AlignText( output, line.length, FONTFACE.lines, OPTIONS.align );
output = AlignText( output, line.length, 1, OPTIONS.align, size );
output = AddLine( output, 0, [''], OPTIONS.lineHeight );

i++;
Expand All @@ -518,14 +521,14 @@ const RenderConsole = ( INPUT, FONTFACE, OPTIONS ) => {
* Filter only allowed character
*
* @param {string} INPUT - The input text to be filtered
* @param {array} CHARS - An array of all allowed characters
* @param {array} chars - An array of all allowed characters
*
* @return {string} - The filtered input text
*/
const CleanInput = ( INPUT, CHARS ) => {
const CleanInput = ( INPUT, chars = CHARS ) => {
const clean = INPUT
.split('')
.filter( char => CHARS.includes( char.toUpperCase() ) )
.filter( char => chars.includes( char.toUpperCase() ) )
.join('');

return clean;
Expand Down Expand Up @@ -564,18 +567,21 @@ const GetOptions = ({ font, align, colors, background, backgroundColor, letterSp
/**
* Main method to get the ANSI output for a string
*
* @param {string} input - The string you want to write out
* @param {object} SETTINGS - Settings object
* @param {string} input - The string you want to write out
* @param {object} SETTINGS - Settings object
* @param {object} size - The size of the terminal as an object, default: Size
* @param {integer} size.width - The width of the terminal
* @param {integer} size.height - The height of the terminal
*
* @typedef {object} ReturnObject
* @property {string} string - The pure string for output with all line breaks
* @property {array} array - Each line of output in an array
* @property {integer} lines - The number of lines
* @property {object} options - All options used
* @property {string} string - The pure string for output with all line breaks
* @property {array} array - Each line of output in an array
* @property {integer} lines - The number of lines
* @property {object} options - All options used
*
* @return {ReturnObject} - CLI output of INPUT to be consoled out
* @return {ReturnObject} - CLI output of INPUT to be consoled out
*/
const Render = ( input, SETTINGS = {} ) => {
const Render = ( input, SETTINGS = {}, size = Size ) => {
Debugging.report(`Running render`, 1);

const INPUT = CleanInput( input, CHARS );
Expand Down Expand Up @@ -610,7 +616,7 @@ const Render = ( input, SETTINGS = {} ) => {
lines: 1,
};

const consoleOutput = RenderConsole( INPUT, FONTFACE, OPTIONS );
const consoleOutput = RenderConsole( INPUT, OPTIONS, size );

output = consoleOutput.output;
lines = consoleOutput.lines;
Expand Down Expand Up @@ -669,18 +675,18 @@ const Render = ( input, SETTINGS = {} ) => {
}

// jump to next line after OPTIONS.maxLength characters or when line break is found or the console windows would have ran out of space
if( maxChars >= OPTIONS.maxLength && OPTIONS.maxLength != 0 || CHAR === `|` || lineLength > Size.width ) {
if( maxChars >= OPTIONS.maxLength && OPTIONS.maxLength != 0 || CHAR === `|` || lineLength > size.width ) {
lines ++;

Debugging.report(
`NEWLINE: maxChars: ${ maxChars }, ` +
`OPTIONS.maxLength: ${ OPTIONS.maxLength }, ` +
`CHAR: ${ CHAR }, ` +
`lineLength: ${ lineLength }, ` +
`Size.width: ${ Size.width } `, 2
`Size.width: ${ size.width } `, 2
);

output = AlignText( output, lastLineLength, FONTFACE.lines, OPTIONS.align ); // calculate alignment based on lineLength
output = AlignText( output, lastLineLength, FONTFACE.lines, OPTIONS.align, size ); // calculate alignment based on lineLength

lineLength = CharLength( FONTFACE.buffer, FONTFACE.lines, OPTIONS ); // new line: new line length
lineLength += CharLength( FONTFACE.letterspace, FONTFACE.lines, OPTIONS ) * OPTIONS.letterSpacing; // each new line starts with letter spacing
Expand All @@ -707,7 +713,7 @@ const Render = ( input, SETTINGS = {} ) => {
}
}

output = AlignText( output, lineLength, FONTFACE.lines, OPTIONS.align ); // alignment last line
output = AlignText( output, lineLength, FONTFACE.lines, OPTIONS.align, size ); // alignment last line
}

let write = output.join(`\n`);
Expand Down Expand Up @@ -765,8 +771,8 @@ const Debugging = {
* @param {string} text - The sting you want to log
* @param {integer} level - The debug level. Show equal and greater levels. Default: 99
*/
headline: ( text, level = 99 ) => {
if( DEBUG && level >= DEBUGLEVEL ) {
headline: ( text, level = 99, debug = DEBUG, debuglevel = DEBUGLEVEL ) => {
if( debug && level >= debuglevel ) {
console.log(
Chalk.bgWhite(`\n${ Chalk.bold(' \u2611 ') } ${ text }`)
);
Expand All @@ -779,8 +785,8 @@ const Debugging = {
* @param {string} text - The sting you want to log
* @param {integer} level - The debug level. Show equal and greater levels. Default: 99
*/
report: ( text, level = 99 ) => {
if( DEBUG && level >= DEBUGLEVEL ) {
report: ( text, level = 99, debug = DEBUG, debuglevel = DEBUGLEVEL ) => {
if( debug && level >= debuglevel ) {
console.log(
Chalk.bgWhite(`\n${ Chalk.bold.green(' \u2611 ') } ${ Chalk.black(`${ text } `) }`)
);
Expand All @@ -793,8 +799,8 @@ const Debugging = {
* @param {string} text - The sting you want to log
* @param {integer} level - The debug level. Show equal and greater levels. Default: 99
*/
error: ( text, level = 99 ) => {
if( DEBUG && level >= DEBUGLEVEL ) {
error: ( text, level = 99, debug = DEBUG, debuglevel = DEBUGLEVEL ) => {
if( debug && level >= debuglevel ) {
console.error(
Chalk.bgWhite(`\n${ Chalk.red(' \u2612 ') } ${ Chalk.black(`${ text } `) }`)
);
Expand Down
24 changes: 24 additions & 0 deletions test/cleaninput.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/***************************************************************************************************************************************************************
*
* CleanInput unit tests
*
**************************************************************************************************************************************************************/


const CFonts = require('../src/lib.js')
const CleanInput = CFonts.__test__.CleanInput;
const CHARS = CFonts.CHARS;


test(`CleanInput - Should white list characters`, () => {
expect( CleanInput( 'abcd', ['A','B','C'] ) ).toEqual( 'abc' );
expect( CleanInput( 'abdc', ['A','B','C'] ) ).toEqual( 'abc' );
expect( CleanInput( 'ab c', ['A','B','C'] ) ).toEqual( 'abc' );
expect( CleanInput( 'abc', ['A','B','C'] ) ).toEqual( 'abc' );
expect( CleanInput( 'abc•', ['A','B','C'] ) ).toEqual( 'abc' );
expect( CleanInput( ' abc', ['A','B','C'] ) ).toEqual( 'abc' );
});

test(`CleanInput - Should keep all letters that are allowed`, () => {
expect( CleanInput( CHARS.join(' ') ) ).toEqual( CHARS.join(' ') );
});
76 changes: 76 additions & 0 deletions test/debugging.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/***************************************************************************************************************************************************************
*
* Debugging unit tests
*
**************************************************************************************************************************************************************/


const CFonts = require('../src/lib.js')
const Debugging = CFonts.Debugging;


const StripColor = ( text ) => {
const pattern = [
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\\u0007)',
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))'
].join('|');
const ansi = new RegExp(pattern, 'g');

if( typeof text === 'string' ) {
return text.replace( ansi, '' );
}
else {
return text;
}
};


test(`Debugging - Show headline message when debug is enabled`, () => {
console.log = jest.fn();

Debugging.headline( 'text', 1, true, 1 );

expect( console.log.mock.calls[0][0] ).toBe( '\u001b[47m\u001b[49m\n\u001b[47m\u001b[1m ☑ \u001b[22m text\u001b[49m' );
});


test(`Debugging - Show report message when debug is enabled`, () => {
console.log = jest.fn();

Debugging.report( 'text', 1, true, 1 );

expect( console.log.mock.calls[0][0] )
.toBe( '\u001b[47m\u001b[49m\n\u001b[47m\u001b[1m\u001b[32m ☑ \u001b[39m\u001b[22m \u001b[30mtext \u001b[39m\u001b[49m' );
});


test(`Debugging - Show error message when debug is enabled`, () => {
console.error = jest.fn();

Debugging.error( 'text', 1, true, 1 );

expect( console.error.mock.calls[0][0] )
.toBe( '\u001b[47m\u001b[49m\n\u001b[47m\u001b[31m ☒ \u001b[39m \u001b[30mtext \u001b[39m\u001b[49m' );
});


test(`Debugging - Don’t show message when debug is disabled`, () => {
console.log = jest.fn();

Debugging.headline( 'text', 1, false, 1 );
Debugging.report( 'text', 1, false, 1 );
Debugging.error( 'text', 1, false, 1 );

expect( console.log.mock.calls.length ).toBe( 0 );
});


test(`Debugging - Don’t show message when debuglevel is too high`, () => {
console.log = jest.fn();

Debugging.headline( 'text', 1, true, 2 );
Debugging.report( 'text', 1, true, 2 );
Debugging.error( 'text', 1, true, 2 );

expect( console.log.mock.calls.length ).toBe( 0 );
});
Loading

0 comments on commit 5bfc387

Please sign in to comment.