Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Passing objects #18

Merged
merged 6 commits into from
Aug 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .vs/slnx.sqlite
Binary file not shown.
49 changes: 37 additions & 12 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface Printer {
warn: typeof console.warn;
log: typeof console.log;
debug: typeof console.debug;
dir: typeof console.dir;
}

/**
Expand All @@ -45,6 +46,9 @@ export class Logger {
// An array of prefixes that go at the beginning of each message
private prefixesAndStyles: Array<[string, string]> = [];

// An array of any message that is not a string
private nonStringsToLog: Array<[any]> = [];

// An array of message & their associated styles
private msgsAndStyles: Array<[string, string]> = [];

Expand Down Expand Up @@ -112,30 +116,43 @@ export class Logger {
}

/** Stage a string and accumulated styles for later console functions */
txt(str: string) {
this.msgsAndStyles.push([str, this.stylesInProgress.join('')]);
txt(...args: any[]) {
// EXAMPLE: ['my list is', [12, 22, 14], '<< it is great'
let fullString = '';
let fullStringStyles = this.stylesInProgress.join(''); // for example 'color: red; background-color: yellow;'
this.stylesInProgress = [];
for (let arg of args) {
if (typeof arg === 'string') {
fullString += arg;
} else {
this.nonStringsToLog.push(arg);
}
// in the case where there are some string arguments
}
if (fullString) {
this.msgsAndStyles.push([fullString, fullStringStyles]);
}
return this;
}

// Log an error message
error(str?: string) {
if (typeof str !== 'undefined') this.txt(str);
error(...args: any[]) {
this.txt(...args);
return this.printMessage(Level.error);
}
// Log a warning
warn(str?: string) {
if (typeof str !== 'undefined') this.txt(str);
warn(...args: any[]) {
this.txt(...args);
return this.printMessage(Level.warn);
}
// Print some general information
log(str?: string) {
if (typeof str !== 'undefined') this.txt(str);
log(...args: any[]) {
this.txt(...args);
return this.printMessage(Level.log);
}
// Print something for debugging purposes only
debug(str?: string) {
if (typeof str !== 'undefined') this.txt(str);
debug(...args: any[]) {
this.txt(...args);
return this.printMessage(Level.debug);
}

Expand Down Expand Up @@ -185,8 +202,9 @@ export class Logger {
/** Flush all prefix and styles into msgsAndStyles
* Note: there may not be styles associated with a message or prefix!
*/

// prefix styles
for (let [msg, style] of this.prefixesAndStyles) {
// prefix styles
if (style) {
allMsgs += `%c[${msg}]`;
allStyles.push(style); // Only add style to allStyles if present
Expand All @@ -199,8 +217,8 @@ export class Logger {
allMsgs += '%c '; // space between prefixes and rest of logged message
allStyles.push(WHITE_SPACE_STYLE);
}
// message styles
for (let [msg, style] of this.msgsAndStyles) {
// message styles
if (style) {
allMsgs += `%c${msg}`;
allStyles.push(style); // only add style to allStyles if present
Expand All @@ -211,6 +229,13 @@ export class Logger {
logFunction(allMsgs, ...allStyles);
this.msgsAndStyles = [];
}
// in printMessage, we need to deal with that array (i.e., printer.dir([1, 2, 3]) )
// and set the array of non-strings back to empty
// log.debug([1, 2, 3]);
for (let nonString of this.nonStringsToLog) {
this.printer.dir(nonString);
}
this.nonStringsToLog = [];
}
}

Expand Down
75 changes: 75 additions & 0 deletions test/logging-objects-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import Logger, { Level } from 'bite-log';
import { logCountAssert, makeTestPrinter } from './test-helpers';

QUnit.module('Objects, arrays, functions, etc... should be loggable');

QUnit.test('I can log an object after my string message', assert => {
const printer = makeTestPrinter();
const logger = new Logger(Level.debug, printer); // only warns and error

logger.log('Here are some numbers. They are increasing', [1, 2, 3, 4]);
logCountAssert(
{ message: 'after a debug', assert, printer },
{ l: 1 } // one log message has been printed
);

assert.deepEqual(
printer.messages,
{
log: [['Here are some numbers. They are increasing']],
dir: [[1, 2, 3, 4]],
error: [],
warn: [],
debug: []
},
'The thing passed to console.log was a string followed by my array of numbers'
);
});

QUnit.test('Formatting is applied to the string', assert => {
const printer = makeTestPrinter();
const logger = new Logger(Level.debug, printer); // only warns and error

logger.red.log('Here are some numbers. They are increasing', [1, 2, 3, 4]);
logCountAssert(
{ message: 'after a debug', assert, printer },
{ l: 1 } // one log message has been printed
);

assert.deepEqual(
printer.messages,
{
log: [['%cHere are some numbers. They are increasing', 'color: red;']],
dir: [[1, 2, 3, 4]],
error: [],
warn: [],
debug: []
},
'The thing passed to console.log was a string followed by my array of numbers'
);
});

QUnit.test(
'Formatting is applied to all strings passed to the logger',
assert => {
const printer = makeTestPrinter();
const logger = new Logger(Level.debug, printer); // only warns and error

logger.red.log(
'Here are some numbers ',
[1, 2, 3, 4],
'They are increasing'
);
logCountAssert(
{ message: 'after a debug', assert, printer },
{ l: 1 } // one log message has been printed
);

// logger.red.txt('Here are some numbers. They are increasing')
assert.deepEqual(
printer.messages.log[0],
['%cHere are some numbers They are increasing', 'color: red;'],
'The thing passed to console.log was a string followed by my array of numbers'
);
}
);
28 changes: 27 additions & 1 deletion test/test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ export function makeTestPrinter(): Printer & { messages: any } {
debug: [] as any[][],
warn: [] as any[][],
error: [] as any[][]
} as {
log: any[][];
debug: any[][];
warn: any[][];
error: any[][];
dir?: any[][];
},
log(_msg: string) {
printer.messages.log.push([...arguments]);
Expand All @@ -19,6 +25,13 @@ export function makeTestPrinter(): Printer & { messages: any } {
},
error(_msg: string) {
printer.messages.error.push([...arguments]);
},
dir(_msg: string) {
if (typeof printer.messages.dir === 'undefined') {
// if it's missing
printer.messages.dir = []; // define the array so we can use it on the next line
}
printer.messages.dir.push(...arguments);
}
};
return printer;
Expand All @@ -30,7 +43,13 @@ export function logCountAssert(
assert,
printer
}: { message: string; assert: Assert; printer: Printer },
{ e, w, l, d }: { e?: number; w?: number; l?: number; d?: number }
{
e,
w,
l,
d,
dir
Copy link
Contributor

@mike-north mike-north Aug 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're receiving dir as an argument here, but never make an assertion of how many times logger.dir() has been invoked. Just follow the same stuff you've set up for the other message counts below

}: { e?: number; w?: number; l?: number; d?: number; dir?: number }
) {
if (typeof e !== 'undefined') {
assert.equal(
Expand Down Expand Up @@ -60,4 +79,11 @@ export function logCountAssert(
`${message}: ${l} log(s) were logged`
);
}
if (typeof dir !== 'undefined') {
assert.equal(
(printer as any).messages.dir.length,
dir,
`${message}: ${dir} dir(s) were logged`
);
}
}