From b7f4e910471b6a969c3565cec79f296f9912c57a Mon Sep 17 00:00:00 2001 From: tabliang Date: Tue, 12 Jan 2021 12:38:45 +0800 Subject: [PATCH] test: improve base function tests --- src/mods/logger.ts | 22 ++++---- test/exec/client_base.test.ts | 2 +- test/exec/server_base.test.ts | 2 +- test/lib/base.ts | 94 ++++++++++++++++++++++++++++++++++- test/lib/meta.ts | 3 +- 5 files changed, 108 insertions(+), 15 deletions(-) diff --git a/src/mods/logger.ts b/src/mods/logger.ts index e2d629f..f835fe8 100644 --- a/src/mods/logger.ts +++ b/src/mods/logger.ts @@ -62,6 +62,8 @@ const defaultIcons = { }, }; +export declare type LogMethod = (...args: unknown[]) => void; + export default abstract class Logger { public conf: LoggerOptions; public meta: PlainObject; @@ -69,16 +71,16 @@ export default abstract class Logger { 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 = {}; diff --git a/test/exec/client_base.test.ts b/test/exec/client_base.test.ts index 555b94d..e22a68c 100644 --- a/test/exec/client_base.test.ts +++ b/test/exec/client_base.test.ts @@ -1,4 +1,4 @@ import $logger from '../../src/client'; import baseTest from '../lib/base'; -baseTest($logger); +baseTest($logger, 'client'); diff --git a/test/exec/server_base.test.ts b/test/exec/server_base.test.ts index 1bb3e4e..9f273c4 100644 --- a/test/exec/server_base.test.ts +++ b/test/exec/server_base.test.ts @@ -1,4 +1,4 @@ import $logger from '../../src/server'; import baseTest from '../lib/base'; -baseTest($logger); +baseTest($logger, 'server'); diff --git a/test/lib/base.ts b/test/lib/base.ts index 04911f5..e389310 100644 --- a/test/lib/base.ts +++ b/test/lib/base.ts @@ -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('[⚑]')); + } }); }); } diff --git a/test/lib/meta.ts b/test/lib/meta.ts index dde5d8d..0f82640 100644 --- a/test/lib/meta.ts +++ b/test/lib/meta.ts @@ -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'; @@ -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', () => {