]> git.r.bdr.sh - rbdr/cologne/blame - test/utilities.js
Add a License
[rbdr/cologne] / test / utilities.js
CommitLineData
ae85c067
RBR
1'use strict';
2
3const Tap = require('tap');
4
5const Utilities = require('../lib/utilities');
6
7// Prepare the test
8
9Tap.plan(17);
10
11/**
12 * TEST: ::now()
13 */
14const preciseTime = Utilities.now();
15
16// I don't think this test makes sense lol
17Tap.equal(typeof preciseTime, 'bigint',
18 '::now() should give a precise bigint timestamp');
19
20/**
21 * TEST: ::stringify()
22 */
23
24const regularObject = {
25 a: 1,
26 b: {
27 c: 'true',
28 d: false
29 }
30};
31
32const circularObject = {
33 a: 1,
34 b: {
35 c: 'true',
36 d: false
37 }
38};
39circularObject.b.circular = circularObject;
40
41const bigint = 666n;
42
43const regularStringify = JSON.stringify(regularObject);
44const cologneStringify = Utilities.stringify(regularObject);
45const circularStringify = Utilities.stringify(circularObject);
46const bigintStringify = Utilities.stringify(bigint);
47
48Tap.equal(regularStringify, cologneStringify,
49 '::stringify() should behave like JSON.stringify for non-circular objects');
50Tap.equal(typeof JSON.parse(circularStringify).b.circular, 'string',
51 '::stringify() should replace circular references with a string');
52Tap.equal(bigintStringify, '"666n"',
53 '::stringify() should convert bigint to string');
54
55/**
56 * TEST: ::getAnsiCode()
57 */
58
59// NOTE: This isn't even trying to be a complete test... Just testing
60// that we get distinct non-reset codes if valid, and the same as reset
61// for all invalid ones. knowing that reset is [0m
62
63Tap.equal(Utilities.getAnsiCode('reset'), '[0m',
64 '::getAnsiCode is sending the correct reset code');
65Tap.equal(Utilities.getAnsiCode('someRandomString'), Utilities.getAnsiCode('reset'),
66 '::getAnsiCode() should give us a reset code if something weird is sent');
67
68const supportedCodes = ['bold', 'italics', 'underline', 'inverse', 'strikethrough',
69 'bold_off', 'italics_off', 'underline_off', 'inverse_off', 'strikethrough_off',
70 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'default',
71 'black_bg', 'red_bg', 'green_bg', 'yellow_bg', 'blue_bg', 'magenta_bg',
72 'cyan_bg', 'white_bg', 'default_bg'];
73const resultingCodes = supportedCodes.map((code) => Utilities.getAnsiCode(code));
74const isThereReset = resultingCodes.some((code) => code === '[0m');
75
76Tap.equal((new Set(resultingCodes)).size, supportedCodes.length,
77 '::getAnsiCode() should not have duplicated non-reset codes');
78Tap.false(isThereReset,
79 '::getAnsiCode() should not return a reset code in any other supported code');
80
81/**
82 * TEST: ::getLevelAnsi()
83 */
84
85// NOTE: This isn't even trying to be a complete test... Just testing
86// that we get other than reset if valid, and the same as reset for all
87// invalid ones. knowing that reset is [0m
88
89Tap.equal(Utilities.getLevelAnsi(0), 'red',
90 '::gettLevelAnsi is red for emerg');
91Tap.equal(Utilities.getLevelAnsi(1), 'red',
92 '::gettLevelAnsi is red for alert');
93Tap.equal(Utilities.getLevelAnsi(2), 'red',
94 '::gettLevelAnsi is red for crit');
95Tap.equal(Utilities.getLevelAnsi(3), 'red',
96 '::gettLevelAnsi is red for error');
97Tap.equal(Utilities.getLevelAnsi(4), 'yellow',
98 '::gettLevelAnsi is yellow for warn');
99Tap.equal(Utilities.getLevelAnsi(5), 'blue',
100 '::gettLevelAnsi is blue for notice');
101Tap.equal(Utilities.getLevelAnsi(6), 'blue',
102 '::gettLevelAnsi is blue for info');
103Tap.equal(Utilities.getLevelAnsi(7), 'green',
104 '::gettLevelAnsi is green for debug');
105Tap.equal(Utilities.getLevelAnsi(8), 'default',
106 '::gettLevelAnsi is default for other values');