Files
openstapps/test/Logger.spec.ts
Anselm Stordeur 911d71cf3b feat: add logger
2018-11-28 18:31:01 +01:00

228 lines
4.3 KiB
TypeScript

/*
* Copyright (C) 2018 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {expect} from 'chai';
import {slow, suite, test, timeout} from 'mocha-typescript';
import * as sinon from 'sinon';
import {Logger} from '../src/Logger';
const logger1 = new Logger();
@suite(timeout(2000), slow(1000))
export class LoggerSpec {
@test
error1(done: () => void) {
let
stub;
stub = sinon.stub(console, 'error');
logger1.error('Foobar');
stub.restore();
expect(stub.args[0][0]).contains('[ERROR] Foobar');
done();
}
@test
error2(done: () => void) {
const e = new Error();
const stub = sinon.stub(console, 'error');
logger1.error(e);
stub.restore();
expect(stub.args[0][0]).contains('Error').contains('at').contains(process.cwd());
done();
}
@test
info(done: () => void) {
let
stub;
stub = sinon.stub(console, 'info');
logger1.info('Foobar');
stub.restore();
expect(stub.args[0][0]).contains('[INFO] Foobar');
done();
}
@test
log(done: () => void) {
let
stub;
stub = sinon.stub(console, 'log');
logger1.log('Foobar');
stub.restore();
expect(stub.args[0][0]).contains('[LOG] Foobar');
done();
}
@test
logLevel1(done: () => void) {
let logStub;
let warnStub;
process.env.STAPPS_LOG_LEVEL = '8';
logStub = sinon.stub(console, 'log');
warnStub = sinon.stub(console, 'warn');
logger1.ok('foo');
logger1.warn('bar');
logStub.restore();
/* tslint:disable:no-unused-expression */
expect(logStub.called).to.be.false;
expect(warnStub.called).to.be.false;
/* tslint:enable */
delete process.env.STAPPS_LOG_LEVEL;
logger1.warn('bar');
/* tslint:disable:no-unused-expression */
expect(warnStub.called).to.be.true;
/* tslint:enable */
warnStub.restore();
done();
}
@test
logLevel2(done: () => void) {
let logStub;
let warnStub;
process.env.STAPPS_LOG_LEVEL = '12';
logStub = sinon.stub(console, 'log');
warnStub = sinon.stub(console, 'warn');
logger1.ok('foo');
logger1.warn('bar');
logStub.restore();
/* tslint:disable:no-unused-expression */
expect(logStub.called).to.be.false;
expect(warnStub.called).to.be.true;
/* tslint:enable */
delete process.env.STAPPS_LOG_LEVEL;
logger1.warn('bar');
/* tslint:disable:no-unused-expression */
expect(warnStub.called).to.be.true;
/* tslint:enable */
warnStub.restore();
done();
}
@test
logLevel3(done: () => void) {
let logStub;
let warnStub;
process.env.STAPPS_LOG_LEVEL = '20';
logStub = sinon.stub(console, 'log');
warnStub = sinon.stub(console, 'warn');
logger1.ok('foo');
logger1.warn('bar');
logStub.restore();
/* tslint:disable:no-unused-expression */
expect(logStub.called).to.be.true;
expect(warnStub.called).to.be.true;
/* tslint:enable */
delete process.env.STAPPS_LOG_LEVEL;
logger1.warn('bar');
/* tslint:disable:no-unused-expression */
expect(warnStub.called).to.be.true;
/* tslint:enable */
warnStub.restore();
done();
}
@test
ok(done: () => void) {
let
stub;
stub = sinon.stub(console, 'log');
logger1.ok('Foobar');
stub.restore();
expect(stub.args[0][0]).contains('[OK] Foobar');
done();
}
@test
stringifyArguments(done: () => void) {
let
stub;
stub = sinon.stub(console, 'log');
logger1.log('foo', 'bar');
stub.restore();
expect(stub.args[0][0]).contains('[LOG]').and.contains('foo').and.contains('bar');
done();
}
@test
warn(done: () => void) {
let
stub;
stub = sinon.stub(console, 'warn');
logger1.warn('Foobar');
stub.restore();
expect(stub.args[0][0]).contains('[WARN] Foobar');
done();
}
}