From 13b25a8d48d142b60d5c351aad4ad1bf80104320 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 30 Sep 2023 08:29:13 -0300 Subject: util, test: WIP decorate error with index symbol --- src/util/sugar.js | 1 + .../common-utilities/withPropertiesFromObject.js | 43 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/util/sugar.js b/src/util/sugar.js index 29fcf84..0522b59 100644 --- a/src/util/sugar.js +++ b/src/util/sugar.js @@ -624,6 +624,7 @@ export function decorateErrorWithIndex(fn) { return fn(x, index, array); } catch (error) { error.message = `(${colors.yellow(`#${index + 1}`)}) ${error.message}`; + error[Symbol.for('hsmusic.sugar.index')] = 1; throw error; } }; diff --git a/test/unit/data/composite/common-utilities/withPropertiesFromObject.js b/test/unit/data/composite/common-utilities/withPropertiesFromObject.js index 9bcb84c..6b4e10c 100644 --- a/test/unit/data/composite/common-utilities/withPropertiesFromObject.js +++ b/test/unit/data/composite/common-utilities/withPropertiesFromObject.js @@ -179,3 +179,46 @@ t.test(`withPropertiesFromObject: output shapes & values`, t => { outputDict); } }); + +t.test(`withPropertiesFromObject: validate static inputs`, t => { + t.plan(3); + + t.throws( + () => withPropertiesFromObject({}), + { + message: `Errors in input options passed to withPropertiesFromObject`, + errors: [ + {message: `Required these inputs: object, properties`}, + ], + }); + + t.throws( + () => withPropertiesFromObject({ + object: input.value('intriguing'), + properties: input.value('very'), + prefix: input.value({yes: 'yup'}), + }), + { + message: `Errors in input options passed to withPropertiesFromObject`, + errors: [ + {message: `object: Expected an object, got string`}, + {message: 'properties: Expected an array, got string'}, + {message: 'prefix: Expected a string, got object'}, + ], + }); + + t.throws( + () => withPropertiesFromObject({ + object: input.value([['abc', 1], ['def', 2], [123, 3]]), + properties: input.value(['abc', 'def', 123]), + }), + {message: `Errors in input options passed to withPropertiesFromObject`, errors: [ + {message: 'object: Expected an object, got array'}, + {message: 'properties: Errors validating array items', errors: [ + { + [Symbol.for('hsmusic.sugar.index')]: 2, + message: /Expected a string, got number/, + }, + ]}, + ]}); +}); -- cgit 1.3.0-6-gf8a5