]> git.r.bdr.sh - rbdr/cologne/blob - test/cologne/log_utilities.js
e37268399eb78e2f08dbc7fe37235357f02b939a
[rbdr/cologne] / test / cologne / log_utilities.js
1 'use strict';
2
3 let tap = require('tap');
4
5 let LogUtilities = require('../../lib/cologne/log_utilities');
6
7 // Prepare the test
8 let t1, t2, preciseTime, regularObject, circularObject,
9 regularStringify, cologneStringify, circularStringify;
10
11 tap.plan(7);
12
13 regularObject = {
14 a: 1,
15 b: {
16 c: 'true',
17 d: false
18 }
19 };
20
21 circularObject = {
22 a: 1,
23 b: {
24 c: 'true',
25 d: false
26 }
27 };
28 circularObject.b.circular = circularObject;
29
30 /**
31 * TEST: ::now()
32 */
33 t1 = Date.now();
34 preciseTime = LogUtilities.now();
35 t2 = Date.now();
36
37 // This test is sloppy :(
38 tap.ok(Math.abs(t1 - preciseTime) < 1,
39 '::now() should give a precise timestamp (before)');
40 tap.ok(Math.abs(t2 - preciseTime) < 1,
41 '::now() should give a precise timestamp (after)');
42
43 /**
44 * TEST: ::stringify()
45 */
46
47 regularStringify = JSON.stringify(regularObject);
48 cologneStringify = LogUtilities.stringify(regularObject);
49 circularStringify = LogUtilities.stringify(circularObject);
50
51 tap.equal(regularStringify, cologneStringify,
52 '::stringify() should behave like JSON.stringify for non-circular objects');
53 tap.equal(typeof JSON.parse(circularStringify).b.circular, 'string',
54 '::stringify() should replace circular references with a string');
55
56 /**
57 * TEST: ::getAnsiCode()
58 */
59
60 // NOTE: This isn't even trying to be a complete test... Just testing
61 // that we get other than reset if valid, and the same as reset for all
62 // invalid ones. knowing that reset is [0m
63
64 tap.equal(LogUtilities.getAnsiCode('reset'), '[0m',
65 '::getAnsiCode is sending the correct reset code');
66 tap.equal(LogUtilities.getAnsiCode('someRandomString'), LogUtilities.getAnsiCode('reset'),
67 '::getAnsiCode() should give us a reset code if something weird is sent');
68 tap.notEqual(LogUtilities.getAnsiCode('red'), LogUtilities.getAnsiCode('reset'),
69 '::getAnsiCode() should give us a non-reset code if it\'s something real');