« get me outta code hell

flash data - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/test
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2022-01-30 21:33:34 -0400
committer(quasar) nebula <qznebula@protonmail.com>2022-01-30 21:33:34 -0400
commiteb4d99ef08e811b617ae88849f1d80827a9c74b0 (patch)
tree571da8539fa856f3d4b55d0d1e9892a9a60ec730 /test
parentdfd84ead5bd5dfbcd7c25d08b816b28b4620ee5a (diff)
flash data
Diffstat (limited to 'test')
-rw-r--r--test/data-validators.js48
1 files changed, 48 insertions, 0 deletions
diff --git a/test/data-validators.js b/test/data-validators.js
index 867068c7..feec127a 100644
--- a/test/data-validators.js
+++ b/test/data-validators.js
@@ -17,8 +17,12 @@ import {
     isDimensions,
     isDirectory,
     isDuration,
+    isFileExtension,
     validateReference,
     validateReferenceList,
+
+    // Compositional utilities
+    oneOf,
 } from '../src/thing/validators.js';
 
 function test(msg, fn) {
@@ -157,6 +161,16 @@ test('isDuration', t => {
     t.throws(() => isDuration('10:25'), TypeError);
 });
 
+test('isFileExtension', t => {
+    t.plan(6);
+    t.ok(isFileExtension('png'));
+    t.ok(isFileExtension('jpg'));
+    t.ok(isFileExtension('sub_loc'));
+    t.throws(() => isFileExtension(''), TypeError);
+    t.throws(() => isFileExtension('.jpg'), TypeError);
+    t.throws(() => isFileExtension('just an image bro!!!!'), TypeError);
+});
+
 test.skip('isName', t => {
     // TODO
 });
@@ -216,3 +230,37 @@ test('validateReferenceList', t => {
     t.true(caughtError.errors[0] instanceof TypeError);
     t.true(caughtError.errors[1] instanceof TypeError);
 });
+
+test('oneOf', t => {
+    t.plan(11);
+
+    const isStringOrNumber = oneOf(isString, isNumber);
+
+    t.ok(isStringOrNumber('hello world'));
+    t.ok(isStringOrNumber(42));
+    t.throws(() => isStringOrNumber(false));
+
+    const mockError = new Error();
+    const neverSucceeds = () => {
+        throw mockError;
+    };
+
+    const isStringOrGetRekt = oneOf(isString, neverSucceeds);
+
+    t.ok(isStringOrGetRekt('phew!'));
+
+    let caughtError = null;
+    try {
+        isStringOrGetRekt(0xdeadbeef);
+    } catch (err) {
+        caughtError = err;
+    }
+
+    t.isNot(caughtError, null);
+    t.true(caughtError instanceof AggregateError);
+    t.is(caughtError.errors.length, 2);
+    t.true(caughtError.errors[0] instanceof TypeError);
+    t.is(caughtError.errors[0].check, isString);
+    t.is(caughtError.errors[1], mockError);
+    t.is(caughtError.errors[1].check, neverSucceeds);
+});