Skip to content

Commit

Permalink
Add warning tester
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyaTura authored and Shinigami92 committed Apr 7, 2021
1 parent a66432d commit 56544e3
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
27 changes: 27 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export interface ILogger {
error: typeof console.error;
}

/**
*
*/
export type LoggerListener = (level: LogLevel, message?: unknown, ...optionalParams: any[]) => void;

/** The logger class. */
export class Logger implements ILogger {
private static readonly LOG_LEVELS: ['debug', 'log', 'info', 'warn', 'error'] = [
Expand All @@ -27,6 +32,8 @@ export class Logger implements ILogger {
'error'
];

private readonly listeners: LoggerListener[] = [];

/**
* Constructs a new logger.
*
Expand Down Expand Up @@ -103,12 +110,32 @@ export class Logger implements ILogger {
this.message(LogLevel.ERROR, message, ...optionalParams);
}

/**
* Adds a log listener.
*
* @param callback The listener callback.
*/
public addListener(callback: LoggerListener): void {
this.listeners.push(callback);
}

/**
* Removes a log listener.
*
* @param callback The listener callback.
*/
public removeListener(callback: LoggerListener): void {
const index: number = this.listeners.indexOf(callback);
this.listeners.splice(index, 1);
}

private message(level: LogLevel, message?: any, ...optionalParams: any[]): void {
if (this.level !== LogLevel.OFF && this.level <= level) {
const logLevel: 'debug' | 'log' | 'info' | 'warn' | 'error' | undefined =
Logger.LOG_LEVELS[level as number];
if (logLevel) {
this.logger[logLevel](message, ...optionalParams);
this.listeners.forEach((cb) => cb(level, message, ...optionalParams));
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,14 @@ import {
} from './utils/common';
import { isVueEventBinding, isVueExpression, isVueVForWithOf, isVueVOnExpression } from './utils/vue';

/**
* Printer logger instance.
*/
const logger: Logger = createLogger(console);
/**
*
*/
export const loggerInstance: Logger = logger;
if (process.env.NODE_ENV === 'test') {
logger.setLogLevel(LogLevel.DEBUG);
}
Expand Down Expand Up @@ -436,21 +443,21 @@ export class PugPrinter {
`code: \`${code.trim()}\``
);
} else if (error.includes("Unexpected token '('")) {
if (this.framework !== 'vue') {
if (this.framework === 'angular') {
logger.warn(
'[PugPrinter:formatText]: Found unexpected token `(`.',
`code: \`${code.trim()}\``
);
}
} else if (error.includes('Missing expected `)`')) {
if (this.framework !== 'vue') {
if (this.framework === 'angular') {
logger.warn(
'[PugPrinter:formatText]: Missing expected `)`.',
`code: \`${code.trim()}\``
);
}
} else if (error.includes('Missing expected `:`')) {
if (this.framework !== 'vue') {
if (this.framework === 'angular') {
logger.warn(
'[PugPrinter:formatText]: Missing expected `:`.',
`code: \`${code.trim()}\``
Expand Down
1 change: 1 addition & 0 deletions tests/formatText/warnings/unexpected-end-of-expression.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
p {{ foo( }}
46 changes: 46 additions & 0 deletions tests/formatText/warnings/warnings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { format } from 'prettier';
import { plugin } from '../../../src/index';
import { LoggerListener } from '../../../src/logger';
import { loggerInstance } from '../../../src/printer';

const loggerListener: LoggerListener = jest.fn();

const getFormatWarnings = (): string[] =>
loggerListener.mock.calls
.filter((call) => call[0] === 3)
.filter((call) => call[1].toString().startsWith('[PugPrinter:formatText]: '))
.map((call) => call[1].toString().substring(25));

const getCode = (filename: string): string => readFileSync(resolve(__dirname, filename), 'utf8');

beforeAll(() => {
loggerInstance.addListener(loggerListener);
});
beforeEach(() => {
// Clear all instances and calls to constructor and all methods:
loggerListener.mockClear();
});
afterAll(() => {
loggerInstance.removeListener(loggerListener);
});

describe('Frameworks', () => {
describe('Angular', () => {
test('foo-bar', () => {
// this test is not completed yet
const code: string = getCode('unexpected-end-of-expression.pug');
format(code, {
parser: 'pug',
plugins: [plugin],

// @ts-expect-error
pugFramework: 'angular'
});
const thrownWarnings: string[] = getFormatWarnings();

expect(thrownWarnings).toContain('');
});
});
});

0 comments on commit 56544e3

Please sign in to comment.