Skip to content

Commit

Permalink
test: improve base function tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TabSpace committed Jan 12, 2021
1 parent 0971245 commit b7f4e91
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 15 deletions.
22 changes: 12 additions & 10 deletions src/mods/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,25 @@ const defaultIcons = {
},
};

export declare type LogMethod = (...args: unknown[]) => void;

export default abstract class Logger {
public conf: LoggerOptions;
public meta: PlainObject;
public levels: PlainObject;
public colors: PlainObject;
public icons: IconOptions;
public transport: (msg: Message) => void;
public log: (...args: unknown[]) => void;
public info: (...args: unknown[]) => void;
public debug: (...args: unknown[]) => void;
public warn: (...args: unknown[]) => void;
public error: (...args: unknown[]) => void;
public success: (...args: unknown[]) => void;
public fail: (...args: unknown[]) => void;
public tip: (...args: unknown[]) => void;
public stress: (...args: unknown[]) => void;
[key: string]: ((...args: unknown[]) => void) | unknown;
public log: LogMethod;
public info: LogMethod;
public debug: LogMethod;
public warn: LogMethod;
public error: LogMethod;
public success: LogMethod;
public fail: LogMethod;
public tip: LogMethod;
public stress: LogMethod;
[key: string]: LogMethod | unknown;

public constructor(options?: LoggerOptions) {
this.conf = {};
Expand Down
2 changes: 1 addition & 1 deletion test/exec/client_base.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import $logger from '../../src/client';
import baseTest from '../lib/base';

baseTest($logger);
baseTest($logger, 'client');
2 changes: 1 addition & 1 deletion test/exec/server_base.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import $logger from '../../src/server';
import baseTest from '../lib/base';

baseTest($logger);
baseTest($logger, 'server');
94 changes: 92 additions & 2 deletions test/lib/base.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,135 @@
import $assert from 'power-assert';
import $lodash from 'lodash';
import $chalk from 'chalk';
import { Message } from '../../src/types';
import { Factory } from './types';

export default function baseTest(Logger: Factory) {
const logger = new Logger();
export default function baseTest(Logger: Factory, mode: string) {
let msg: Message = null;

const logger = new Logger({
transport: (message) => {
msg = message;
},
});

describe('logger methods', () => {
test('logger has fn log', () => {
$assert(typeof logger.log === 'function');
});

test('logger.log color is normal', () => {
logger.log('log');
if (mode === 'client') {
$assert.equal($lodash.get(msg, '__content[1]'), 'color: gray;');
$assert.equal($lodash.get(msg, '__content[2]'), 'color: ;');
} else {
$assert.equal($lodash.get(msg, '__content[1]'), '[.]');
}
});

test('logger has fn info', () => {
$assert(typeof logger.info === 'function');
});

test('logger.info color is #1e90ff', () => {
logger.info('info');
if (mode === 'client') {
$assert.equal($lodash.get(msg, '__content[2]'), 'color: #1e90ff;');
} else {
$assert.equal($lodash.get(msg, '__content[1]'), $chalk.hex('#1e90ff')('[*]'));
}
});

test('logger has fn debug', () => {
$assert(typeof logger.debug === 'function');
});

test('logger.debug color is magenta', () => {
logger.debug('debug');
if (mode === 'client') {
$assert.equal($lodash.get(msg, '__content[2]'), 'color: magenta;');
} else {
$assert.equal($lodash.get(msg, '__content[1]'), $chalk.magenta('[#]'));
}
});

test('logger has fn warn', () => {
$assert(typeof logger.warn === 'function');
});

test('logger.warn color is #ffd700', () => {
logger.warn('warn');
if (mode === 'client') {
$assert.equal($lodash.get(msg, '__content[2]'), 'color: #ffd700;');
} else {
$assert.equal($lodash.get(msg, '__content[1]'), $chalk.hex('#ffd700')('[!]'));
}
});

test('logger has fn error', () => {
$assert(typeof logger.error === 'function');
});

test('logger.error color is red', () => {
logger.error('error');
if (mode === 'client') {
$assert.equal($lodash.get(msg, '__content[2]'), 'color: red;');
} else {
$assert.equal($lodash.get(msg, '__content[1]'), $chalk.red('[x]'));
}
});

test('logger has fn success', () => {
$assert(typeof logger.success === 'function');
});

test('logger.success color is green', () => {
logger.success('success');
if (mode === 'client') {
$assert.equal($lodash.get(msg, '__content[2]'), 'color: green;');
} else {
$assert.equal($lodash.get(msg, '__content[1]'), $chalk.green('[✓]'));
}
});

test('logger has fn fail', () => {
$assert(typeof logger.fail === 'function');
});

test('logger.fail color is red', () => {
logger.fail('fail');
if (mode === 'client') {
$assert.equal($lodash.get(msg, '__content[2]'), 'color: red;');
} else {
$assert.equal($lodash.get(msg, '__content[1]'), $chalk.red('[☢]'));
}
});

test('logger has fn tip', () => {
$assert(typeof logger.tip === 'function');
});

test('logger.tip color is cyan', () => {
logger.tip('tip');
if (mode === 'client') {
$assert.equal($lodash.get(msg, '__content[2]'), 'color: cyan;');
} else {
$assert.equal($lodash.get(msg, '__content[1]'), $chalk.cyan('[✱]'));
}
});

test('logger has fn stress', () => {
$assert(typeof logger.stress === 'function');
});

test('logger.stress color is magenta', () => {
logger.stress('stress');
if (mode === 'client') {
$assert.equal($lodash.get(msg, '__content[2]'), 'color: magenta;');
} else {
$assert.equal($lodash.get(msg, '__content[1]'), $chalk.magenta('[⚑]'));
}
});
});
}
3 changes: 2 additions & 1 deletion test/lib/meta.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import $assert from 'power-assert';
import $lodash from 'lodash';
import $chalk from 'chalk';
import { Factory } from './types';
import { Message } from '../../src/types';

Expand Down Expand Up @@ -156,7 +157,7 @@ export default function metaTest(Logger: Factory, mode: string) {
});
if (mode === 'server') {
test('msg.tag5 color', () => {
$assert($lodash.get(msg, '__content[4]') === '\u001b[34m[tag5]\u001b[39m');
$assert($lodash.get(msg, '__content[4]') === $chalk.blue('[tag5]'));
});
} else {
test('msg.tag5 color', () => {
Expand Down

0 comments on commit b7f4e91

Please sign in to comment.