]> git.r.bdr.sh - rbdr/cologne/blobdiff - test/utilities.js
Update the code.
[rbdr/cologne] / test / utilities.js
diff --git a/test/utilities.js b/test/utilities.js
new file mode 100644 (file)
index 0000000..2bff47f
--- /dev/null
@@ -0,0 +1,106 @@
+'use strict';
+
+const Tap = require('tap');
+
+const Utilities = require('../lib/utilities');
+
+// Prepare the test
+
+Tap.plan(17);
+
+/**
+ * TEST: ::now()
+ */
+const preciseTime = Utilities.now();
+
+// I don't think this test makes sense lol
+Tap.equal(typeof preciseTime, 'bigint',
+  '::now() should give a precise bigint timestamp');
+
+/**
+ * TEST: ::stringify()
+ */
+
+const regularObject = {
+  a: 1,
+  b: {
+    c: 'true',
+    d: false
+  }
+};
+
+const circularObject = {
+  a: 1,
+  b: {
+    c: 'true',
+    d: false
+  }
+};
+circularObject.b.circular = circularObject;
+
+const bigint = 666n;
+
+const regularStringify = JSON.stringify(regularObject);
+const cologneStringify = Utilities.stringify(regularObject);
+const circularStringify = Utilities.stringify(circularObject);
+const bigintStringify = Utilities.stringify(bigint);
+
+Tap.equal(regularStringify, cologneStringify,
+  '::stringify() should behave like JSON.stringify for non-circular objects');
+Tap.equal(typeof JSON.parse(circularStringify).b.circular, 'string',
+  '::stringify() should replace circular references with a string');
+Tap.equal(bigintStringify, '"666n"',
+  '::stringify() should convert bigint to string');
+
+/**
+ * TEST: ::getAnsiCode()
+ */
+
+// NOTE: This isn't even trying to be a complete test... Just testing
+// that we get distinct non-reset codes if valid, and the same as reset
+// for all invalid ones. knowing that reset is [0m
+
+Tap.equal(Utilities.getAnsiCode('reset'), '[0m',
+  '::getAnsiCode is sending the correct reset code');
+Tap.equal(Utilities.getAnsiCode('someRandomString'), Utilities.getAnsiCode('reset'),
+  '::getAnsiCode() should give us a reset code if something weird is sent');
+
+const supportedCodes = ['bold', 'italics', 'underline', 'inverse', 'strikethrough',
+  'bold_off', 'italics_off', 'underline_off', 'inverse_off', 'strikethrough_off',
+  'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'default',
+  'black_bg', 'red_bg', 'green_bg', 'yellow_bg', 'blue_bg', 'magenta_bg',
+  'cyan_bg', 'white_bg', 'default_bg'];
+const resultingCodes = supportedCodes.map((code) => Utilities.getAnsiCode(code));
+const isThereReset = resultingCodes.some((code) => code === '[0m');
+
+Tap.equal((new Set(resultingCodes)).size, supportedCodes.length,
+  '::getAnsiCode() should not have duplicated non-reset codes');
+Tap.false(isThereReset,
+  '::getAnsiCode() should not return a reset code in any other supported code');
+
+/**
+ * TEST: ::getLevelAnsi()
+ */
+
+// NOTE: This isn't even trying to be a complete test... Just testing
+// that we get other than reset if valid, and the same as reset for all
+// invalid ones. knowing that reset is [0m
+
+Tap.equal(Utilities.getLevelAnsi(0), 'red',
+  '::gettLevelAnsi is red for emerg');
+Tap.equal(Utilities.getLevelAnsi(1), 'red',
+  '::gettLevelAnsi is red for alert');
+Tap.equal(Utilities.getLevelAnsi(2), 'red',
+  '::gettLevelAnsi is red for crit');
+Tap.equal(Utilities.getLevelAnsi(3), 'red',
+  '::gettLevelAnsi is red for error');
+Tap.equal(Utilities.getLevelAnsi(4), 'yellow',
+  '::gettLevelAnsi is yellow for warn');
+Tap.equal(Utilities.getLevelAnsi(5), 'blue',
+  '::gettLevelAnsi is blue for notice');
+Tap.equal(Utilities.getLevelAnsi(6), 'blue',
+  '::gettLevelAnsi is blue for info');
+Tap.equal(Utilities.getLevelAnsi(7), 'green',
+  '::gettLevelAnsi is green for debug');
+Tap.equal(Utilities.getLevelAnsi(8), 'default',
+  '::gettLevelAnsi is default for other values');