-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.spec.js
235 lines (178 loc) · 7.63 KB
/
test.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
'use strict';
/*global describe, it*/
var semlog = require('../');
var log = semlog.log;
var expect = require('chai').expect;
describe('semlog logger', function() {
it('logs a string message to the console', function() {
console.log('');
console.log('-------------------------------------------------------------');
console.log(' Testing Log Messages');
console.log('-------------------------------------------------------------');
log('[i] info log entry ');
log('[W] warning log entry');
log('[E] error log entry');
log('[S] success log entry');
log('[D] debug log entry');
log('[+] added log entry');
log('[-] removed log entry');
log('[C] changed log entry');
log('[TODO] todo log entry');
log('unknown todo log entry');
console.dir(semlog.getLogHistory());
console.log('');
console.log('-------------------------------------------------------------');
console.log(' Testing Numbers and Datatypes');
console.log('-------------------------------------------------------------');
log(3);
log([1, 2, 3]);
log(true);
log(false);
log(null);
log(Infinity);
console.log('');
console.log('-------------------------------------------------------------');
console.log(' Testing Log Objects and Errors');
console.log('-------------------------------------------------------------');
// Create a new object, that prototypical inherits from the Error constructor.
function MyError(message) {
this.name = 'MyError';
this.message = message || 'Default Message';
}
MyError.prototype = Object.create(Error.prototype);
MyError.prototype.constructor = MyError;
log({title: 'Object log entry', number: 10});
log(new Error('error log entry'));
log(new TypeError('error log entry'));
log(new MyError('error log entry'));
console.log('-------------------------------------------------------------');
console.log('');
});
it('prints objects as colorized YAML', function() {
semlog.updateConfig({printYaml: true});
log({
text: 'text',
number: 42,
array: [1, '2'],
object: {
key: 'value'
}
});
semlog.updateConfig({printYaml: false});
});
it('prints errors', function() {
semlog.error(new Error('Test Error'));
});
it('handles various invalid log objects', function() {
log(undefined);
log(null);
log(Error);
log(Infinity);
});
it('returns the log history as an array', function() {
var logArchive = semlog.getLogHistory();
expect(logArchive).to.be.instanceof(Array);
expect(logArchive.length).to.be.least(1);
});
it('clears global log object', function() {
semlog.clearLogHistory();
var logArchive = semlog.getLogHistory();
expect(logArchive).to.be.instanceof(Array);
expect(logArchive.length).to.equal(0);
});
it('logs silently', function() {
semlog.log(' [i] info log entry ', true);
semlog.log(' [W] warning log entry', true);
semlog.log(' [E] error log entry', true);
var logArchive = semlog.getLogHistory();
expect(logArchive).to.be.instanceof(Array);
expect(logArchive.length).to.equal(3);
});
it('gets config', function() {
var config = semlog.getConfig();
expect(config).to.be.instanceof(Object);
expect(Object.keys(config).length).to.be.least(3);
});
it('updates the config', function() {
var config = semlog.getConfig();
var newConfig = semlog.updateConfig({printTime: false});
expect(newConfig).to.be.instanceof(Object);
expect(newConfig.printTime).to.equal(false);
expect(newConfig.historySize).to.equal(config.historySize);
});
it('keeps the log object at a specific size', function() {
var historySize = 7;
var newConfig = semlog.updateConfig({historySize: historySize, logDateTime: true});
for (var i = 0; i < 32; i++) {
log('[i] Index added: ' + i, true);
}
var logHistory = semlog.getLogHistory();
expect(newConfig.historySize).to.equal(logHistory.length);
expect(logHistory.length).to.equal(historySize);
});
it('returns the log archive as non circular array', function() {
log(semlog.getLogHistory());
var logHistory = semlog.getLogHistory();
expect(JSON.stringify(logHistory)).to.be.a('string');
});
it('returns log statistics', function() {
log(semlog.getStatistics());
var statistics = semlog.getStatistics();
expect(statistics.total).to.be.a('number');
});
});
describe('semlog utilities', function() {
it('pad numbers', function() {
expect(semlog.pad(7, 1)).to.equal('7');
expect(semlog.pad(7, 2)).to.equal('07');
expect(semlog.pad(7, 3)).to.equal('007');
});
it('pretty prints numbers', function() {
expect(semlog.prettyNumber(1)).to.equal('1');
expect(semlog.prettyNumber(100000)).to.equal('100.000');
expect(semlog.prettyNumber(-100000)).to.equal('-100.000');
});
it('pretty prints bytes', function() {
expect(semlog.prettyBytes(1024)).to.be.a('string');
expect(semlog.prettyBytes(1024)).to.equal('1.0 KiB');
expect(semlog.prettyBytes(1024, true)).to.equal('1.0 kB');
});
it('strips trailing slashes from URLs', function() {
expect(semlog.stripTrailingSlash('http://fannon.de/')).to.equal('http://fannon.de');
});
it('strips leading and ending slashes from URLs / URL paths', function() {
expect(semlog.cleanUrl('http://fannon.de/')).to.equal('http://fannon.de');
expect(semlog.cleanUrl('http://fannon.de ')).to.equal('http://fannon.de');
expect(semlog.cleanUrl('/test/')).to.equal('test');
expect(semlog.cleanUrl('/test/ ')).to.equal('test');
expect(semlog.cleanUrl(' test/ ')).to.equal('test');
});
it('creates date arrays', function() {
expect(semlog.getDateArray().length).to.equal(7);
expect(semlog.getDateArray()[0]).to.equal(new Date().getFullYear());
});
it('return human readable date-times', function() {
expect(semlog.humanDate().length).to.equal(19);
expect(semlog.humanDate(new Date('October 13, 2014 11:13:00'))).to.equal('2014-10-13 11:13:00');
});
it('return machine optimized date-times', function() {
expect(semlog.roboDate().length).to.equal(19);
expect(semlog.roboDate(new Date('October 13, 2014 11:13:00'))).to.equal('2014-10-13_11-13-00');
});
it('return human readable time', function() {
expect(semlog.humanTime().length).to.equal(8);
expect(semlog.humanTime(new Date('October 13, 2014 11:13:00'))).to.equal('11:13:00');
});
it('return machine optimized time', function() {
expect(semlog.roboTime().length).to.equal(8);
expect(semlog.roboTime(new Date('October 13, 2014 11:13:00'))).to.equal('11-13-00');
});
it('calculates the bytesize of strings', function() {
expect(semlog.byteSize('internationalization')).to.be.a('number');
expect(semlog.byteSize('internationalization')).to.equal(20);
});
it('calculates the bytesize of objects (parsed to JSON)', function() {
expect(semlog.byteSize({title: 'internationalization'})).to.be.a('number');
expect(semlog.byteSize({title: 'internationalization'})).to.equal(32);
});
});