]> git.r.bdr.sh - rbdr/cologne/blob - test/utilities.js
Add sonar-cloud configuration
[rbdr/cologne] / test / utilities.js
1 'use strict';
2
3 const Tap = require('tap');
4
5 const Utilities = require('../lib/utilities');
6
7 // Prepare the test
8
9 Tap.plan(17);
10
11 /**
12 * TEST: ::now()
13 */
14 const preciseTime = Utilities.now();
15
16 // I don't think this test makes sense lol
17 Tap.equal(typeof preciseTime, 'bigint',
18 '::now() should give a precise bigint timestamp');
19
20 /**
21 * TEST: ::stringify()
22 */
23
24 const regularObject = {
25 a: 1,
26 b: {
27 c: 'true',
28 d: false
29 }
30 };
31
32 const circularObject = {
33 a: 1,
34 b: {
35 c: 'true',
36 d: false
37 }
38 };
39 circularObject.b.circular = circularObject;
40
41 const bigint = 666n;
42
43 const regularStringify = JSON.stringify(regularObject);
44 const cologneStringify = Utilities.stringify(regularObject);
45 const circularStringify = Utilities.stringify(circularObject);
46 const bigintStringify = Utilities.stringify(bigint);
47
48 Tap.equal(regularStringify, cologneStringify,
49 '::stringify() should behave like JSON.stringify for non-circular objects');
50 Tap.equal(typeof JSON.parse(circularStringify).b.circular, 'string',
51 '::stringify() should replace circular references with a string');
52 Tap.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
63 Tap.equal(Utilities.getAnsiCode('reset'), '[0m',
64 '::getAnsiCode is sending the correct reset code');
65 Tap.equal(Utilities.getAnsiCode('someRandomString'), Utilities.getAnsiCode('reset'),
66 '::getAnsiCode() should give us a reset code if something weird is sent');
67
68 const 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'];
73 const resultingCodes = supportedCodes.map((code) => Utilities.getAnsiCode(code));
74 const isThereReset = resultingCodes.some((code) => code === '[0m');
75
76 Tap.equal((new Set(resultingCodes)).size, supportedCodes.length,
77 '::getAnsiCode() should not have duplicated non-reset codes');
78 Tap.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
89 Tap.equal(Utilities.getLevelAnsi(0), 'red',
90 '::gettLevelAnsi is red for emerg');
91 Tap.equal(Utilities.getLevelAnsi(1), 'red',
92 '::gettLevelAnsi is red for alert');
93 Tap.equal(Utilities.getLevelAnsi(2), 'red',
94 '::gettLevelAnsi is red for crit');
95 Tap.equal(Utilities.getLevelAnsi(3), 'red',
96 '::gettLevelAnsi is red for error');
97 Tap.equal(Utilities.getLevelAnsi(4), 'yellow',
98 '::gettLevelAnsi is yellow for warn');
99 Tap.equal(Utilities.getLevelAnsi(5), 'blue',
100 '::gettLevelAnsi is blue for notice');
101 Tap.equal(Utilities.getLevelAnsi(6), 'blue',
102 '::gettLevelAnsi is blue for info');
103 Tap.equal(Utilities.getLevelAnsi(7), 'green',
104 '::gettLevelAnsi is green for debug');
105 Tap.equal(Utilities.getLevelAnsi(8), 'default',
106 '::gettLevelAnsi is default for other values');