From 8bcae16b391762f6b533654ec06c3bf0c8770d35 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 23 Sep 2023 20:24:08 -0300 Subject: data, test: WIP tests for compositeFrom --- test/unit/data/composite/compositeFrom.js | 237 ++++++++++++++++++++++++++++++ 1 file changed, 237 insertions(+) create mode 100644 test/unit/data/composite/compositeFrom.js (limited to 'test/unit/data/composite') diff --git a/test/unit/data/composite/compositeFrom.js b/test/unit/data/composite/compositeFrom.js new file mode 100644 index 00000000..faeef59a --- /dev/null +++ b/test/unit/data/composite/compositeFrom.js @@ -0,0 +1,237 @@ +import t from 'tap'; + +import {isString} from '#validators'; + +import { + compositeFrom, + continuationSymbol, + debugComposite, + input, +} from '#composite'; + +t.test(`compositeFrom: basic behavior`, t => { + t.plan(2); + + const composite = compositeFrom({ + annotation: `myComposite`, + compose: false, + + steps: [ + { + dependencies: ['foo'], + compute: (continuation, {foo}) => + continuation({'#bar': foo * 2}), + }, + + { + dependencies: ['#bar', 'baz', 'suffix'], + compute: ({'#bar': bar, baz, suffix}) => + baz.repeat(bar) + suffix, + }, + ], + }); + + t.match(composite, { + annotation: `myComposite`, + + flags: {expose: true, compose: false, update: false}, + + expose: { + dependencies: ['foo', 'baz', 'suffix'], + compute: Function, + transform: null, + }, + + update: null, + }); + + t.equal( + composite.expose.compute({ + foo: 3, + baz: 'ba', + suffix: 'BOOM', + }), + 'babababababaBOOM'); +}); + +t.test(`compositeFrom: input-shaped step dependencies`, t => { + t.plan(2); + + const composite = compositeFrom({ + compose: false, + steps: [ + { + dependencies: [ + input.myself(), + input.updateValue(), + ], + + transform: (updateValue1, { + [input.myself()]: me, + [input.updateValue()]: updateValue2, + }) => ({me, updateValue1, updateValue2}), + }, + ], + }); + + t.match(composite, { + expose: { + dependencies: ['this'], + transform: Function, + compute: null, + }, + }); + + const myself = {foo: 'bar'}; + + t.same( + composite.expose.transform('banana', { + this: myself, + pomelo: 'delicious', + }), + { + me: myself, + updateValue1: 'banana', + updateValue2: 'banana', + }); +}); + +t.test(`compositeFrom: dependencies from inputs`, t => { + t.plan(3); + + const composite = compositeFrom({ + annotation: `myComposite`, + + compose: true, + + inputs: { + foo: input('bar'), + pomelo: input.value('delicious'), + humorous: input.dependency('#mammal'), + data: input.dependency('albumData'), + ref: input.updateValue(), + }, + + steps: [ + { + dependencies: [ + input('foo'), + input('pomelo'), + input('humorous'), + input('data'), + input('ref'), + ], + + compute: (continuation, { + [input('foo')]: foo, + [input('pomelo')]: pomelo, + [input('humorous')]: humorous, + [input('data')]: data, + [input('ref')]: ref, + }) => continuation.exit({foo, pomelo, humorous, data, ref}), + }, + ], + }); + + t.match(composite, { + expose: { + dependencies: [ + input('bar'), + '#mammal', + 'albumData', + ], + + transform: Function, + compute: null, + }, + }); + + const exitData = {}; + const continuation = { + exit(value) { + Object.assign(exitData, value); + return continuationSymbol; + }, + }; + + t.equal( + composite.expose.transform('album:bepis', continuation, { + [input('bar')]: 'squid time', + '#mammal': 'fox', + 'albumData': ['album1', 'album2'], + }), + continuationSymbol); + + t.same(exitData, { + foo: 'squid time', + pomelo: 'delicious', + humorous: 'fox', + data: ['album1', 'album2'], + ref: 'album:bepis', + }); +}); + +t.test(`compositeFrom: update from various sources`, t => { + t.plan(2); + + const match = { + flags: {update: true, expose: true, compose: false}, + + update: { + validate: isString, + default: 'foo', + }, + + expose: { + transform: Function, + compute: null, + }, + }; + + t.test(`compositeFrom: update from composition description`, t => { + t.plan(2); + + const composite = compositeFrom({ + compose: false, + + update: { + validate: isString, + default: 'foo', + }, + + steps: [ + {transform: (value, continuation) => continuation(value.repeat(2))}, + {transform: (value) => `Xx_${value}_xX`}, + ], + }); + + t.match(composite, match); + t.equal(composite.expose.transform('foo'), `Xx_foofoo_xX`); + }); + + t.test(`compositeFrom: update from step dependencies`, t => { + t.plan(2); + + const composite = compositeFrom({ + compose: false, + + steps: [ + { + dependencies: [ + input.updateValue({ + validate: isString, + default: 'foo', + }), + ], + + compute: ({ + [input.updateValue()]: value, + }) => `Xx_${value.repeat(2)}_xX`, + }, + ], + }); + + t.match(composite, match); + t.equal(debugComposite(() => composite.expose.transform('foo')), 'Xx_foofoo_xX'); + }); +}); -- cgit 1.3.0-6-gf8a5 From f3d98f5ea63db7f7b2155e7efb0812f025c5bcf3 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 23 Sep 2023 20:35:57 -0300 Subject: data, test: collate update description from composition inputs --- test/unit/data/composite/compositeFrom.js | 52 +++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) (limited to 'test/unit/data/composite') diff --git a/test/unit/data/composite/compositeFrom.js b/test/unit/data/composite/compositeFrom.js index faeef59a..6ae1e7bc 100644 --- a/test/unit/data/composite/compositeFrom.js +++ b/test/unit/data/composite/compositeFrom.js @@ -172,7 +172,7 @@ t.test(`compositeFrom: dependencies from inputs`, t => { }); t.test(`compositeFrom: update from various sources`, t => { - t.plan(2); + t.plan(3); const match = { flags: {update: true, expose: true, compose: false}, @@ -232,6 +232,54 @@ t.test(`compositeFrom: update from various sources`, t => { }); t.match(composite, match); - t.equal(debugComposite(() => composite.expose.transform('foo')), 'Xx_foofoo_xX'); + t.equal(composite.expose.transform('foo'), 'Xx_foofoo_xX'); + }); + + t.test(`compositeFrom: update from inputs`, t => { + t.plan(3); + + const composite = compositeFrom({ + inputs: { + myInput: input.updateValue({ + validate: isString, + default: 'foo', + }), + }, + + steps: [ + { + dependencies: [input('myInput')], + compute: (continuation, { + [input('myInput')]: value, + }) => continuation({ + '#value': `Xx_${value.repeat(2)}_xX`, + }), + }, + + { + dependencies: ['#value'], + transform: (_value, continuation, {'#value': value}) => + continuation(value), + }, + ], + }); + + let continuationValue = null; + const continuation = value => { + continuationValue = value; + return continuationSymbol; + }; + + t.match(composite, { + ...match, + + flags: {update: true, expose: true, compose: true}, + }); + + t.equal( + composite.expose.transform('foo', continuation), + continuationSymbol); + + t.equal(continuationValue, 'Xx_foofoo_xX'); }); }); -- cgit 1.3.0-6-gf8a5 From 942b05a2beed7d28e93ae256de0f58be5b7e385a Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 23 Sep 2023 22:15:49 -0300 Subject: data, test: exposeDependency (unit) --- test/unit/data/composite/compositeFrom.js | 8 +-- test/unit/data/composite/exposeDependency.js | 80 ++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 test/unit/data/composite/exposeDependency.js (limited to 'test/unit/data/composite') diff --git a/test/unit/data/composite/compositeFrom.js b/test/unit/data/composite/compositeFrom.js index 6ae1e7bc..06a66e61 100644 --- a/test/unit/data/composite/compositeFrom.js +++ b/test/unit/data/composite/compositeFrom.js @@ -1,14 +1,8 @@ import t from 'tap'; +import {compositeFrom, continuationSymbol, input} from '#composite'; import {isString} from '#validators'; -import { - compositeFrom, - continuationSymbol, - debugComposite, - input, -} from '#composite'; - t.test(`compositeFrom: basic behavior`, t => { t.plan(2); diff --git a/test/unit/data/composite/exposeDependency.js b/test/unit/data/composite/exposeDependency.js new file mode 100644 index 00000000..7487e44c --- /dev/null +++ b/test/unit/data/composite/exposeDependency.js @@ -0,0 +1,80 @@ +import t from 'tap'; + +import { + compositeFrom, + continuationSymbol, + exposeDependency, + input, +} from '#composite'; + +t.test(`exposeDependency: basic behavior`, t => { + t.plan(4); + + const composite1 = compositeFrom({ + compose: false, + + steps: [ + exposeDependency({dependency: 'foo'}), + ], + }); + + t.match(composite1, { + expose: { + dependencies: ['foo'], + }, + }); + + t.equal(composite1.expose.compute({foo: 'bar'}), 'bar'); + + const composite2 = compositeFrom({ + compose: false, + + steps: [ + { + dependencies: ['foo'], + compute: (continuation, {foo}) => + continuation({'#bar': foo.toUpperCase()}), + }, + + exposeDependency({dependency: '#bar'}), + ], + }); + + t.match(composite2, { + expose: { + dependencies: ['foo'], + }, + }); + + t.equal(composite2.expose.compute({foo: 'bar'}), 'BAR'); +}); + +t.test(`exposeDependency: validate inputs`, t => { + t.plan(2); + + let caughtError; + + try { + caughtError = null; + exposeDependency({}); + } catch (error) { + caughtError = error; + } + + t.match(caughtError, { + errors: [/Required these inputs: dependency/], + }); + + try { + caughtError = null; + exposeDependency({ + dependency: input.value('some static value'), + }); + } catch (error) { + caughtError = error; + } + + t.match(caughtError, { + errors: [/Expected static dependencies: dependency/], + }); +}); -- cgit 1.3.0-6-gf8a5 From 219596b6d52443d1090c94e50244cf79d548a167 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Mon, 25 Sep 2023 08:48:19 -0300 Subject: data, test: exposeConstant, withResultOfAvailabilityCheck --- test/unit/data/composite/exposeConstant.js | 60 ++++++++ .../composite/withResultOfAvailabilityCheck.js | 159 +++++++++++++++++++++ 2 files changed, 219 insertions(+) create mode 100644 test/unit/data/composite/exposeConstant.js create mode 100644 test/unit/data/composite/withResultOfAvailabilityCheck.js (limited to 'test/unit/data/composite') diff --git a/test/unit/data/composite/exposeConstant.js b/test/unit/data/composite/exposeConstant.js new file mode 100644 index 00000000..ce3f5e3d --- /dev/null +++ b/test/unit/data/composite/exposeConstant.js @@ -0,0 +1,60 @@ +import t from 'tap'; + +import { + compositeFrom, + continuationSymbol, + exposeConstant, + input, +} from '#composite'; + +t.test(`exposeConstant: basic behavior`, t => { + t.plan(2); + + const composite1 = compositeFrom({ + compose: false, + + steps: [ + exposeConstant({ + value: input.value('foo'), + }), + ], + }); + + t.match(composite1, { + expose: { + dependencies: [], + }, + }); + + t.equal(composite1.expose.compute(), 'foo'); +}); + +t.test(`exposeConstant: validate inputs`, t => { + t.plan(2); + + let caughtError; + + try { + caughtError = null; + exposeConstant({}); + } catch (error) { + caughtError = error; + } + + t.match(caughtError, { + errors: [/Required these inputs: value/], + }); + + try { + caughtError = null; + exposeConstant({ + value: 'some dependency', + }); + } catch (error) { + caughtError = error; + } + + t.match(caughtError, { + errors: [/Expected static values: value/], + }); +}); diff --git a/test/unit/data/composite/withResultOfAvailabilityCheck.js b/test/unit/data/composite/withResultOfAvailabilityCheck.js new file mode 100644 index 00000000..f5229fb3 --- /dev/null +++ b/test/unit/data/composite/withResultOfAvailabilityCheck.js @@ -0,0 +1,159 @@ +import t from 'tap'; + +import { + compositeFrom, + continuationSymbol, + withResultOfAvailabilityCheck, + input, +} from '#composite'; + +const composite = compositeFrom({ + compose: false, + + steps: [ + withResultOfAvailabilityCheck({ + from: 'from', + mode: 'mode', + }).outputs({ + ['#availability']: '#result', + }), + + { + dependencies: ['#result'], + compute: ({'#result': result}) => result, + }, + ], +}); + +t.test(`withResultOfAvailabilityCheck: basic behavior`, t => { + t.plan(1); + + t.match(composite, { + expose: { + dependencies: ['from', 'mode'], + }, + }); +}); + +const quickCompare = (t, expect, {from, mode}) => + t.equal(composite.expose.compute({from, mode}), expect); + +const quickThrows = (t, {from, mode}) => + t.throws(() => composite.expose.compute({from, mode})); + +t.test(`withResultOfAvailabilityCheck: mode = null`, t => { + t.plan(10); + + quickCompare(t, true, {mode: 'null', from: 'truthy string'}); + quickCompare(t, true, {mode: 'null', from: 123}); + quickCompare(t, true, {mode: 'null', from: true}); + + quickCompare(t, true, {mode: 'null', from: ''}); + quickCompare(t, true, {mode: 'null', from: 0}); + quickCompare(t, true, {mode: 'null', from: false}); + + quickCompare(t, true, {mode: 'null', from: [1, 2, 3]}); + quickCompare(t, true, {mode: 'null', from: []}); + + quickCompare(t, false, {mode: 'null', from: null}); + quickCompare(t, false, {mode: 'null', from: undefined}); +}); + +t.test(`withResultOfAvailabilityCheck: mode = empty`, t => { + t.plan(10); + + quickThrows(t, {mode: 'empty', from: 'truthy string'}); + quickThrows(t, {mode: 'empty', from: 123}); + quickThrows(t, {mode: 'empty', from: true}); + + quickThrows(t, {mode: 'empty', from: ''}); + quickThrows(t, {mode: 'empty', from: 0}); + quickThrows(t, {mode: 'empty', from: false}); + + quickCompare(t, true, {mode: 'empty', from: [1, 2, 3]}); + quickCompare(t, false, {mode: 'empty', from: []}); + + quickCompare(t, false, {mode: 'empty', from: null}); + quickCompare(t, false, {mode: 'empty', from: undefined}); +}); + +t.test(`withResultOfAvailabilityCheck: mode = falsy`, t => { + t.plan(10); + + quickCompare(t, true, {mode: 'falsy', from: 'truthy string'}); + quickCompare(t, true, {mode: 'falsy', from: 123}); + quickCompare(t, true, {mode: 'falsy', from: true}); + + quickCompare(t, false, {mode: 'falsy', from: ''}); + quickCompare(t, false, {mode: 'falsy', from: 0}); + quickCompare(t, false, {mode: 'falsy', from: false}); + + quickCompare(t, true, {mode: 'falsy', from: [1, 2, 3]}); + quickCompare(t, false, {mode: 'falsy', from: []}); + + quickCompare(t, false, {mode: 'falsy', from: null}); + quickCompare(t, false, {mode: 'falsy', from: undefined}); +}); + +t.test(`withResultOfAvailabilityCheck: default mode`, t => { + t.plan(1); + + const template = withResultOfAvailabilityCheck({ + from: 'foo', + }); + + t.match(template.toDescription(), { + inputs: { + from: input.dependency('foo'), + mode: input.value('null'), + }, + }); +}); + +t.test(`withResultOfAvailabilityCheck: validate inputs`, t => { + t.plan(4); + + let caughtError; + + try { + caughtError = null; + withResultOfAvailabilityCheck({}); + } catch (error) { + caughtError = error; + } + + t.match(caughtError, { + errors: [/Required these inputs: from/], + }); + + t.doesNotThrow(() => + withResultOfAvailabilityCheck({ + from: 'dependency1', + mode: 'dependency2', + })); + + t.doesNotThrow(() => + withResultOfAvailabilityCheck({ + from: input.value('static'), + mode: input.value('null'), + })); + + try { + caughtError = null; + withResultOfAvailabilityCheck({ + from: 'foo', + mode: input.value('invalid'), + }); + } catch (error) { + caughtError = error; + } + + t.match(caughtError, { + errors: [ + { + message: /mode: Validation failed for static value/, + cause: /Expected one of null empty falsy, got invalid/, + }, + ], + }); +}); -- cgit 1.3.0-6-gf8a5 From b5cfc2a793f22da60606a4dd7387fcf3d3163843 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Mon, 25 Sep 2023 14:23:23 -0300 Subject: data: misc. improvements for input validation & infrastructure --- test/unit/data/composite/compositeFrom.js | 78 ++++++++++++++++++++-- .../composite/withResultOfAvailabilityCheck.js | 2 +- 2 files changed, 73 insertions(+), 7 deletions(-) (limited to 'test/unit/data/composite') diff --git a/test/unit/data/composite/compositeFrom.js b/test/unit/data/composite/compositeFrom.js index 06a66e61..00296675 100644 --- a/test/unit/data/composite/compositeFrom.js +++ b/test/unit/data/composite/compositeFrom.js @@ -98,12 +98,20 @@ t.test(`compositeFrom: dependencies from inputs`, t => { compose: true, - inputs: { - foo: input('bar'), - pomelo: input.value('delicious'), + inputMapping: { + foo: input('bar'), + pomelo: input.value('delicious'), humorous: input.dependency('#mammal'), - data: input.dependency('albumData'), - ref: input.updateValue(), + data: input.dependency('albumData'), + ref: input.updateValue(), + }, + + inputDescriptions: { + foo: input(), + pomelo: input(), + humorous: input(), + data: input(), + ref: input(), }, steps: [ @@ -233,13 +241,17 @@ t.test(`compositeFrom: update from various sources`, t => { t.plan(3); const composite = compositeFrom({ - inputs: { + inputMapping: { myInput: input.updateValue({ validate: isString, default: 'foo', }), }, + inputDescriptions: { + myInput: input(), + }, + steps: [ { dependencies: [input('myInput')], @@ -277,3 +289,57 @@ t.test(`compositeFrom: update from various sources`, t => { t.equal(continuationValue, 'Xx_foofoo_xX'); }); }); + +t.test(`compositeFrom: dynamic input validation from type`, t => { + t.plan(2); + + const composite = compositeFrom({ + inputMapping: { + string: input('string'), + number: input('number'), + boolean: input('boolean'), + function: input('function'), + object: input('object'), + array: input('array'), + }, + + inputDescriptions: { + string: input({null: true, type: 'string'}), + number: input({null: true, type: 'number'}), + boolean: input({null: true, type: 'boolean'}), + function: input({null: true, type: 'function'}), + object: input({null: true, type: 'object'}), + array: input({null: true, type: 'array'}), + }, + + outputs: {'#result': '#result'}, + + steps: [ + {compute: continuation => continuation({'#result': 'OK'})}, + ], + }); + + const notCalledSymbol = Symbol('continuation not called'); + + let continuationValue; + const continuation = value => { + continuationValue = value; + return continuationSymbol; + }; + + let thrownError; + + try { + continuationValue = notCalledSymbol; + thrownError = null; + composite.expose.compute(continuation, { + [input('string')]: 123, + }); + } catch (error) { + thrownError = error; + } + + t.equal(continuationValue, notCalledSymbol); + t.match(thrownError, { + }); +}); diff --git a/test/unit/data/composite/withResultOfAvailabilityCheck.js b/test/unit/data/composite/withResultOfAvailabilityCheck.js index f5229fb3..cac98d3c 100644 --- a/test/unit/data/composite/withResultOfAvailabilityCheck.js +++ b/test/unit/data/composite/withResultOfAvailabilityCheck.js @@ -103,7 +103,7 @@ t.test(`withResultOfAvailabilityCheck: default mode`, t => { }); t.match(template.toDescription(), { - inputs: { + inputMapping: { from: input.dependency('foo'), mode: input.value('null'), }, -- cgit 1.3.0-6-gf8a5 From ef290302472bd66ff9823aad1a4e029a4b4e2eba Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 28 Sep 2023 14:13:09 -0300 Subject: test: templateCompositeFrom (WIP), various composite test updates --- test/unit/data/composite/exposeConstant.js | 38 ++-- test/unit/data/composite/exposeDependency.js | 38 ++-- test/unit/data/composite/templateCompositeFrom.js | 218 +++++++++++++++++++++ .../composite/withResultOfAvailabilityCheck.js | 81 +++++++- 4 files changed, 323 insertions(+), 52 deletions(-) create mode 100644 test/unit/data/composite/templateCompositeFrom.js (limited to 'test/unit/data/composite') diff --git a/test/unit/data/composite/exposeConstant.js b/test/unit/data/composite/exposeConstant.js index ce3f5e3d..829dc706 100644 --- a/test/unit/data/composite/exposeConstant.js +++ b/test/unit/data/composite/exposeConstant.js @@ -32,29 +32,21 @@ t.test(`exposeConstant: basic behavior`, t => { t.test(`exposeConstant: validate inputs`, t => { t.plan(2); - let caughtError; - - try { - caughtError = null; - exposeConstant({}); - } catch (error) { - caughtError = error; - } - - t.match(caughtError, { - errors: [/Required these inputs: value/], - }); - - try { - caughtError = null; - exposeConstant({ - value: 'some dependency', + t.throws( + () => exposeConstant({}), + { + message: `Errors in input options passed to exposeConstant`, + errors: [ + {message: `Required these inputs: value`}, + ], }); - } catch (error) { - caughtError = error; - } - t.match(caughtError, { - errors: [/Expected static values: value/], - }); + t.throws( + () => exposeConstant({value: 'some dependency'}), + { + message: `Errors in input options passed to exposeConstant`, + errors: [ + {message: `value: Expected input.value() call, got dependency name`}, + ], + }); }); diff --git a/test/unit/data/composite/exposeDependency.js b/test/unit/data/composite/exposeDependency.js index 7487e44c..78801343 100644 --- a/test/unit/data/composite/exposeDependency.js +++ b/test/unit/data/composite/exposeDependency.js @@ -52,29 +52,23 @@ t.test(`exposeDependency: basic behavior`, t => { t.test(`exposeDependency: validate inputs`, t => { t.plan(2); - let caughtError; - - try { - caughtError = null; - exposeDependency({}); - } catch (error) { - caughtError = error; - } - - t.match(caughtError, { - errors: [/Required these inputs: dependency/], - }); + t.throws( + () => exposeDependency({}), + { + message: `Errors in input options passed to exposeDependency`, + errors: [ + {message: `Required these inputs: dependency`}, + ], + }); - try { - caughtError = null; - exposeDependency({ + t.throws( + () => exposeDependency({ dependency: input.value('some static value'), + }), + { + message: `Errors in input options passed to exposeDependency`, + errors: [ + {message: `dependency: Expected dependency name, got input.value() call`}, + ], }); - } catch (error) { - caughtError = error; - } - - t.match(caughtError, { - errors: [/Expected static dependencies: dependency/], - }); }); diff --git a/test/unit/data/composite/templateCompositeFrom.js b/test/unit/data/composite/templateCompositeFrom.js new file mode 100644 index 00000000..e96b782e --- /dev/null +++ b/test/unit/data/composite/templateCompositeFrom.js @@ -0,0 +1,218 @@ +import t from 'tap'; + +import {isString} from '#validators'; + +import { + compositeFrom, + continuationSymbol, + input, + templateCompositeFrom, +} from '#composite'; + +t.test(`templateCompositeFrom: basic behavior`, t => { + t.plan(1); + + const myCoolUtility = templateCompositeFrom({ + annotation: `myCoolUtility`, + + inputs: { + foo: input(), + }, + + outputs: ['#bar'], + + steps: () => [ + { + dependencies: [input('foo')], + compute: (continuation, { + [input('foo')]: foo, + }) => continuation({ + ['#bar']: (typeof foo).toUpperCase() + }), + }, + ], + }); + + const instantiatedTemplate = myCoolUtility({ + foo: 'color', + }); + + t.match(instantiatedTemplate.toDescription(), { + annotation: `myCoolUtility`, + + inputMapping: { + foo: input.dependency('color'), + }, + + inputDescriptions: { + foo: input(), + }, + + outputs: { + '#bar': '#bar', + }, + + steps: Function, + }); +}); + +t.test(`templateCompositeFrom: validate static input values`, t => { + t.plan(3); + + const stub = { + annotation: 'stubComposite', + outputs: ['#result'], + steps: () => [{compute: continuation => continuation({'#result': 'OK'})}], + }; + + const quickThrows = (t, composite, inputOptions, ...errorMessages) => + t.throws( + () => composite(inputOptions), + { + message: `Errors in input options passed to stubComposite`, + errors: errorMessages.map(message => ({message})), + }); + + t.test(`templateCompositeFrom: validate input token shapes`, t => { + t.plan(15); + + const template1 = templateCompositeFrom({ + ...stub, inputs: { + foo: input(), + }, + }); + + t.doesNotThrow( + () => template1({foo: 'dependency'})); + + t.doesNotThrow( + () => template1({foo: input.dependency('dependency')})); + + t.doesNotThrow( + () => template1({foo: input.value('static value')})); + + t.doesNotThrow( + () => template1({foo: input('outerInput')})); + + t.doesNotThrow( + () => template1({foo: input.updateValue()})); + + t.doesNotThrow( + () => template1({foo: input.myself()})); + + quickThrows(t, template1, + {foo: input.staticValue()}, + `foo: Expected dependency name or value-providing input() call, got input.staticValue`); + + quickThrows(t, template1, + {foo: input.staticDependency()}, + `foo: Expected dependency name or value-providing input() call, got input.staticDependency`); + + const template2 = templateCompositeFrom({ + ...stub, inputs: { + bar: input.staticDependency(), + }, + }); + + t.doesNotThrow( + () => template2({bar: 'dependency'})); + + t.doesNotThrow( + () => template2({bar: input.dependency('dependency')})); + + quickThrows(t, template2, + {bar: input.value(123)}, + `bar: Expected dependency name, got input.value`); + + quickThrows(t, template2, + {bar: input('outOfPlace')}, + `bar: Expected dependency name, got input`); + + const template3 = templateCompositeFrom({ + ...stub, inputs: { + baz: input.staticValue(), + }, + }); + + t.doesNotThrow( + () => template3({baz: input.value(1025)})); + + quickThrows(t, template3, + {baz: 'dependency'}, + `baz: Expected input.value() call, got dependency name`); + + quickThrows(t, template3, + {baz: input('outOfPlace')}, + `baz: Expected input.value() call, got input() call`); + }); + + t.test(`templateCompositeFrom: validate missing / misplaced inputs`, t => { + t.plan(1); + + const template = templateCompositeFrom({ + ...stub, inputs: { + foo: input(), + bar: input(), + }, + }); + + t.throws( + () => template({ + baz: 'aeiou', + raz: input.value(123), + }), + { + message: `Errors in input options passed to stubComposite`, + errors: [ + {message: `Unexpected input names: baz, raz`}, + {message: `Required these inputs: foo, bar`}, + ], + }); + }); + + t.test(`templateCompositeFrom: validate acceptsNull / defaultValue: null`, t => { + t.plan(3); + + const template1 = templateCompositeFrom({ + ...stub, inputs: { + foo: input(), + }, + }); + + t.throws( + () => template1({}), + { + message: `Errors in input options passed to stubComposite`, + errors: [ + {message: `Required these inputs: foo`}, + ], + }, + `throws if input missing and not marked specially`); + + const template2 = templateCompositeFrom({ + ...stub, inputs: { + bar: input({acceptsNull: true}), + }, + }); + + t.throws( + () => template2({}), + { + message: `Errors in input options passed to stubComposite`, + errors: [ + {message: `Required these inputs: bar`}, + ], + }, + `throws if input missing even if marked {acceptsNull}`); + + const template3 = templateCompositeFrom({ + ...stub, inputs: { + baz: input({defaultValue: null}), + }, + }); + + t.doesNotThrow( + () => template3({}), + `does not throw if input missing if marked {defaultValue: null}`); + }); +}); diff --git a/test/unit/data/composite/withResultOfAvailabilityCheck.js b/test/unit/data/composite/withResultOfAvailabilityCheck.js index cac98d3c..01220a3a 100644 --- a/test/unit/data/composite/withResultOfAvailabilityCheck.js +++ b/test/unit/data/composite/withResultOfAvailabilityCheck.js @@ -110,8 +110,8 @@ t.test(`withResultOfAvailabilityCheck: default mode`, t => { }); }); -t.test(`withResultOfAvailabilityCheck: validate inputs`, t => { - t.plan(4); +t.test(`withResultOfAvailabilityCheck: validate static inputs`, t => { + t.plan(5); let caughtError; @@ -134,7 +134,7 @@ t.test(`withResultOfAvailabilityCheck: validate inputs`, t => { t.doesNotThrow(() => withResultOfAvailabilityCheck({ - from: input.value('static'), + from: input.value('some static value'), mode: input.value('null'), })); @@ -149,11 +149,78 @@ t.test(`withResultOfAvailabilityCheck: validate inputs`, t => { } t.match(caughtError, { + message: /Errors in input options passed to withResultOfAvailabilityCheck/, errors: [ - { - message: /mode: Validation failed for static value/, - cause: /Expected one of null empty falsy, got invalid/, - }, + /mode: Expected one of null empty falsy, got invalid/, ], }); + + try { + caughtError = null; + withResultOfAvailabilityCheck({ + from: input.value(null), + mode: input.value(null), + }); + } catch (error) { + caughtError = error; + } + + t.match(caughtError, { + message: /Errors in input options passed to withResultOfAvailabilityCheck/, + errors: [ + /mode: Expected value, got null/, + ], + }); +}); + +t.test(`withResultOfAvailabilityCheck: validate dynamic inputs`, t => { + t.plan(2); + + let caughtError; + + try { + caughtError = null; + composite.expose.compute({ + from: 'apple', + mode: 'banana', + }); + } catch (error) { + caughtError = error; + } + + t.match(caughtError, { + message: /Error computing composition/, + cause: { + message: /Error computing composition withResultOfAvailabilityCheck/, + cause: { + message: /Errors in input values provided to withResultOfAvailabilityCheck/, + errors: [ + /mode: Expected one of null empty falsy, got banana/, + ], + }, + }, + }); + + try { + caughtError = null; + composite.expose.compute({ + from: null, + mode: null, + }); + } catch (error) { + caughtError = error; + } + + t.match(caughtError, { + message: /Error computing composition/, + cause: { + message: /Error computing composition withResultOfAvailabilityCheck/, + cause: { + message: /Errors in input values provided to withResultOfAvailabilityCheck/, + errors: [ + /mode: Expected value, got null/, + ], + }, + }, + }); }); -- cgit 1.3.0-6-gf8a5 From ac37f9db30d997d64de069a7b3b53c3474bdc413 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 28 Sep 2023 18:29:33 -0300 Subject: test: reorganize data tests a lil --- .../composite/common-utilities/exposeConstant.js | 52 ++++ .../composite/common-utilities/exposeDependency.js | 74 +++++ .../withResultOfAvailabilityCheck.js | 226 ++++++++++++++ test/unit/data/composite/compositeFrom.js | 345 --------------------- test/unit/data/composite/exposeConstant.js | 52 ---- test/unit/data/composite/exposeDependency.js | 74 ----- test/unit/data/composite/templateCompositeFrom.js | 218 ------------- .../composite/withResultOfAvailabilityCheck.js | 226 -------------- 8 files changed, 352 insertions(+), 915 deletions(-) create mode 100644 test/unit/data/composite/common-utilities/exposeConstant.js create mode 100644 test/unit/data/composite/common-utilities/exposeDependency.js create mode 100644 test/unit/data/composite/common-utilities/withResultOfAvailabilityCheck.js delete mode 100644 test/unit/data/composite/compositeFrom.js delete mode 100644 test/unit/data/composite/exposeConstant.js delete mode 100644 test/unit/data/composite/exposeDependency.js delete mode 100644 test/unit/data/composite/templateCompositeFrom.js delete mode 100644 test/unit/data/composite/withResultOfAvailabilityCheck.js (limited to 'test/unit/data/composite') diff --git a/test/unit/data/composite/common-utilities/exposeConstant.js b/test/unit/data/composite/common-utilities/exposeConstant.js new file mode 100644 index 00000000..829dc706 --- /dev/null +++ b/test/unit/data/composite/common-utilities/exposeConstant.js @@ -0,0 +1,52 @@ +import t from 'tap'; + +import { + compositeFrom, + continuationSymbol, + exposeConstant, + input, +} from '#composite'; + +t.test(`exposeConstant: basic behavior`, t => { + t.plan(2); + + const composite1 = compositeFrom({ + compose: false, + + steps: [ + exposeConstant({ + value: input.value('foo'), + }), + ], + }); + + t.match(composite1, { + expose: { + dependencies: [], + }, + }); + + t.equal(composite1.expose.compute(), 'foo'); +}); + +t.test(`exposeConstant: validate inputs`, t => { + t.plan(2); + + t.throws( + () => exposeConstant({}), + { + message: `Errors in input options passed to exposeConstant`, + errors: [ + {message: `Required these inputs: value`}, + ], + }); + + t.throws( + () => exposeConstant({value: 'some dependency'}), + { + message: `Errors in input options passed to exposeConstant`, + errors: [ + {message: `value: Expected input.value() call, got dependency name`}, + ], + }); +}); diff --git a/test/unit/data/composite/common-utilities/exposeDependency.js b/test/unit/data/composite/common-utilities/exposeDependency.js new file mode 100644 index 00000000..78801343 --- /dev/null +++ b/test/unit/data/composite/common-utilities/exposeDependency.js @@ -0,0 +1,74 @@ +import t from 'tap'; + +import { + compositeFrom, + continuationSymbol, + exposeDependency, + input, +} from '#composite'; + +t.test(`exposeDependency: basic behavior`, t => { + t.plan(4); + + const composite1 = compositeFrom({ + compose: false, + + steps: [ + exposeDependency({dependency: 'foo'}), + ], + }); + + t.match(composite1, { + expose: { + dependencies: ['foo'], + }, + }); + + t.equal(composite1.expose.compute({foo: 'bar'}), 'bar'); + + const composite2 = compositeFrom({ + compose: false, + + steps: [ + { + dependencies: ['foo'], + compute: (continuation, {foo}) => + continuation({'#bar': foo.toUpperCase()}), + }, + + exposeDependency({dependency: '#bar'}), + ], + }); + + t.match(composite2, { + expose: { + dependencies: ['foo'], + }, + }); + + t.equal(composite2.expose.compute({foo: 'bar'}), 'BAR'); +}); + +t.test(`exposeDependency: validate inputs`, t => { + t.plan(2); + + t.throws( + () => exposeDependency({}), + { + message: `Errors in input options passed to exposeDependency`, + errors: [ + {message: `Required these inputs: dependency`}, + ], + }); + + t.throws( + () => exposeDependency({ + dependency: input.value('some static value'), + }), + { + message: `Errors in input options passed to exposeDependency`, + errors: [ + {message: `dependency: Expected dependency name, got input.value() call`}, + ], + }); +}); diff --git a/test/unit/data/composite/common-utilities/withResultOfAvailabilityCheck.js b/test/unit/data/composite/common-utilities/withResultOfAvailabilityCheck.js new file mode 100644 index 00000000..01220a3a --- /dev/null +++ b/test/unit/data/composite/common-utilities/withResultOfAvailabilityCheck.js @@ -0,0 +1,226 @@ +import t from 'tap'; + +import { + compositeFrom, + continuationSymbol, + withResultOfAvailabilityCheck, + input, +} from '#composite'; + +const composite = compositeFrom({ + compose: false, + + steps: [ + withResultOfAvailabilityCheck({ + from: 'from', + mode: 'mode', + }).outputs({ + ['#availability']: '#result', + }), + + { + dependencies: ['#result'], + compute: ({'#result': result}) => result, + }, + ], +}); + +t.test(`withResultOfAvailabilityCheck: basic behavior`, t => { + t.plan(1); + + t.match(composite, { + expose: { + dependencies: ['from', 'mode'], + }, + }); +}); + +const quickCompare = (t, expect, {from, mode}) => + t.equal(composite.expose.compute({from, mode}), expect); + +const quickThrows = (t, {from, mode}) => + t.throws(() => composite.expose.compute({from, mode})); + +t.test(`withResultOfAvailabilityCheck: mode = null`, t => { + t.plan(10); + + quickCompare(t, true, {mode: 'null', from: 'truthy string'}); + quickCompare(t, true, {mode: 'null', from: 123}); + quickCompare(t, true, {mode: 'null', from: true}); + + quickCompare(t, true, {mode: 'null', from: ''}); + quickCompare(t, true, {mode: 'null', from: 0}); + quickCompare(t, true, {mode: 'null', from: false}); + + quickCompare(t, true, {mode: 'null', from: [1, 2, 3]}); + quickCompare(t, true, {mode: 'null', from: []}); + + quickCompare(t, false, {mode: 'null', from: null}); + quickCompare(t, false, {mode: 'null', from: undefined}); +}); + +t.test(`withResultOfAvailabilityCheck: mode = empty`, t => { + t.plan(10); + + quickThrows(t, {mode: 'empty', from: 'truthy string'}); + quickThrows(t, {mode: 'empty', from: 123}); + quickThrows(t, {mode: 'empty', from: true}); + + quickThrows(t, {mode: 'empty', from: ''}); + quickThrows(t, {mode: 'empty', from: 0}); + quickThrows(t, {mode: 'empty', from: false}); + + quickCompare(t, true, {mode: 'empty', from: [1, 2, 3]}); + quickCompare(t, false, {mode: 'empty', from: []}); + + quickCompare(t, false, {mode: 'empty', from: null}); + quickCompare(t, false, {mode: 'empty', from: undefined}); +}); + +t.test(`withResultOfAvailabilityCheck: mode = falsy`, t => { + t.plan(10); + + quickCompare(t, true, {mode: 'falsy', from: 'truthy string'}); + quickCompare(t, true, {mode: 'falsy', from: 123}); + quickCompare(t, true, {mode: 'falsy', from: true}); + + quickCompare(t, false, {mode: 'falsy', from: ''}); + quickCompare(t, false, {mode: 'falsy', from: 0}); + quickCompare(t, false, {mode: 'falsy', from: false}); + + quickCompare(t, true, {mode: 'falsy', from: [1, 2, 3]}); + quickCompare(t, false, {mode: 'falsy', from: []}); + + quickCompare(t, false, {mode: 'falsy', from: null}); + quickCompare(t, false, {mode: 'falsy', from: undefined}); +}); + +t.test(`withResultOfAvailabilityCheck: default mode`, t => { + t.plan(1); + + const template = withResultOfAvailabilityCheck({ + from: 'foo', + }); + + t.match(template.toDescription(), { + inputMapping: { + from: input.dependency('foo'), + mode: input.value('null'), + }, + }); +}); + +t.test(`withResultOfAvailabilityCheck: validate static inputs`, t => { + t.plan(5); + + let caughtError; + + try { + caughtError = null; + withResultOfAvailabilityCheck({}); + } catch (error) { + caughtError = error; + } + + t.match(caughtError, { + errors: [/Required these inputs: from/], + }); + + t.doesNotThrow(() => + withResultOfAvailabilityCheck({ + from: 'dependency1', + mode: 'dependency2', + })); + + t.doesNotThrow(() => + withResultOfAvailabilityCheck({ + from: input.value('some static value'), + mode: input.value('null'), + })); + + try { + caughtError = null; + withResultOfAvailabilityCheck({ + from: 'foo', + mode: input.value('invalid'), + }); + } catch (error) { + caughtError = error; + } + + t.match(caughtError, { + message: /Errors in input options passed to withResultOfAvailabilityCheck/, + errors: [ + /mode: Expected one of null empty falsy, got invalid/, + ], + }); + + try { + caughtError = null; + withResultOfAvailabilityCheck({ + from: input.value(null), + mode: input.value(null), + }); + } catch (error) { + caughtError = error; + } + + t.match(caughtError, { + message: /Errors in input options passed to withResultOfAvailabilityCheck/, + errors: [ + /mode: Expected value, got null/, + ], + }); +}); + +t.test(`withResultOfAvailabilityCheck: validate dynamic inputs`, t => { + t.plan(2); + + let caughtError; + + try { + caughtError = null; + composite.expose.compute({ + from: 'apple', + mode: 'banana', + }); + } catch (error) { + caughtError = error; + } + + t.match(caughtError, { + message: /Error computing composition/, + cause: { + message: /Error computing composition withResultOfAvailabilityCheck/, + cause: { + message: /Errors in input values provided to withResultOfAvailabilityCheck/, + errors: [ + /mode: Expected one of null empty falsy, got banana/, + ], + }, + }, + }); + + try { + caughtError = null; + composite.expose.compute({ + from: null, + mode: null, + }); + } catch (error) { + caughtError = error; + } + + t.match(caughtError, { + message: /Error computing composition/, + cause: { + message: /Error computing composition withResultOfAvailabilityCheck/, + cause: { + message: /Errors in input values provided to withResultOfAvailabilityCheck/, + errors: [ + /mode: Expected value, got null/, + ], + }, + }, + }); +}); diff --git a/test/unit/data/composite/compositeFrom.js b/test/unit/data/composite/compositeFrom.js deleted file mode 100644 index 00296675..00000000 --- a/test/unit/data/composite/compositeFrom.js +++ /dev/null @@ -1,345 +0,0 @@ -import t from 'tap'; - -import {compositeFrom, continuationSymbol, input} from '#composite'; -import {isString} from '#validators'; - -t.test(`compositeFrom: basic behavior`, t => { - t.plan(2); - - const composite = compositeFrom({ - annotation: `myComposite`, - compose: false, - - steps: [ - { - dependencies: ['foo'], - compute: (continuation, {foo}) => - continuation({'#bar': foo * 2}), - }, - - { - dependencies: ['#bar', 'baz', 'suffix'], - compute: ({'#bar': bar, baz, suffix}) => - baz.repeat(bar) + suffix, - }, - ], - }); - - t.match(composite, { - annotation: `myComposite`, - - flags: {expose: true, compose: false, update: false}, - - expose: { - dependencies: ['foo', 'baz', 'suffix'], - compute: Function, - transform: null, - }, - - update: null, - }); - - t.equal( - composite.expose.compute({ - foo: 3, - baz: 'ba', - suffix: 'BOOM', - }), - 'babababababaBOOM'); -}); - -t.test(`compositeFrom: input-shaped step dependencies`, t => { - t.plan(2); - - const composite = compositeFrom({ - compose: false, - steps: [ - { - dependencies: [ - input.myself(), - input.updateValue(), - ], - - transform: (updateValue1, { - [input.myself()]: me, - [input.updateValue()]: updateValue2, - }) => ({me, updateValue1, updateValue2}), - }, - ], - }); - - t.match(composite, { - expose: { - dependencies: ['this'], - transform: Function, - compute: null, - }, - }); - - const myself = {foo: 'bar'}; - - t.same( - composite.expose.transform('banana', { - this: myself, - pomelo: 'delicious', - }), - { - me: myself, - updateValue1: 'banana', - updateValue2: 'banana', - }); -}); - -t.test(`compositeFrom: dependencies from inputs`, t => { - t.plan(3); - - const composite = compositeFrom({ - annotation: `myComposite`, - - compose: true, - - inputMapping: { - foo: input('bar'), - pomelo: input.value('delicious'), - humorous: input.dependency('#mammal'), - data: input.dependency('albumData'), - ref: input.updateValue(), - }, - - inputDescriptions: { - foo: input(), - pomelo: input(), - humorous: input(), - data: input(), - ref: input(), - }, - - steps: [ - { - dependencies: [ - input('foo'), - input('pomelo'), - input('humorous'), - input('data'), - input('ref'), - ], - - compute: (continuation, { - [input('foo')]: foo, - [input('pomelo')]: pomelo, - [input('humorous')]: humorous, - [input('data')]: data, - [input('ref')]: ref, - }) => continuation.exit({foo, pomelo, humorous, data, ref}), - }, - ], - }); - - t.match(composite, { - expose: { - dependencies: [ - input('bar'), - '#mammal', - 'albumData', - ], - - transform: Function, - compute: null, - }, - }); - - const exitData = {}; - const continuation = { - exit(value) { - Object.assign(exitData, value); - return continuationSymbol; - }, - }; - - t.equal( - composite.expose.transform('album:bepis', continuation, { - [input('bar')]: 'squid time', - '#mammal': 'fox', - 'albumData': ['album1', 'album2'], - }), - continuationSymbol); - - t.same(exitData, { - foo: 'squid time', - pomelo: 'delicious', - humorous: 'fox', - data: ['album1', 'album2'], - ref: 'album:bepis', - }); -}); - -t.test(`compositeFrom: update from various sources`, t => { - t.plan(3); - - const match = { - flags: {update: true, expose: true, compose: false}, - - update: { - validate: isString, - default: 'foo', - }, - - expose: { - transform: Function, - compute: null, - }, - }; - - t.test(`compositeFrom: update from composition description`, t => { - t.plan(2); - - const composite = compositeFrom({ - compose: false, - - update: { - validate: isString, - default: 'foo', - }, - - steps: [ - {transform: (value, continuation) => continuation(value.repeat(2))}, - {transform: (value) => `Xx_${value}_xX`}, - ], - }); - - t.match(composite, match); - t.equal(composite.expose.transform('foo'), `Xx_foofoo_xX`); - }); - - t.test(`compositeFrom: update from step dependencies`, t => { - t.plan(2); - - const composite = compositeFrom({ - compose: false, - - steps: [ - { - dependencies: [ - input.updateValue({ - validate: isString, - default: 'foo', - }), - ], - - compute: ({ - [input.updateValue()]: value, - }) => `Xx_${value.repeat(2)}_xX`, - }, - ], - }); - - t.match(composite, match); - t.equal(composite.expose.transform('foo'), 'Xx_foofoo_xX'); - }); - - t.test(`compositeFrom: update from inputs`, t => { - t.plan(3); - - const composite = compositeFrom({ - inputMapping: { - myInput: input.updateValue({ - validate: isString, - default: 'foo', - }), - }, - - inputDescriptions: { - myInput: input(), - }, - - steps: [ - { - dependencies: [input('myInput')], - compute: (continuation, { - [input('myInput')]: value, - }) => continuation({ - '#value': `Xx_${value.repeat(2)}_xX`, - }), - }, - - { - dependencies: ['#value'], - transform: (_value, continuation, {'#value': value}) => - continuation(value), - }, - ], - }); - - let continuationValue = null; - const continuation = value => { - continuationValue = value; - return continuationSymbol; - }; - - t.match(composite, { - ...match, - - flags: {update: true, expose: true, compose: true}, - }); - - t.equal( - composite.expose.transform('foo', continuation), - continuationSymbol); - - t.equal(continuationValue, 'Xx_foofoo_xX'); - }); -}); - -t.test(`compositeFrom: dynamic input validation from type`, t => { - t.plan(2); - - const composite = compositeFrom({ - inputMapping: { - string: input('string'), - number: input('number'), - boolean: input('boolean'), - function: input('function'), - object: input('object'), - array: input('array'), - }, - - inputDescriptions: { - string: input({null: true, type: 'string'}), - number: input({null: true, type: 'number'}), - boolean: input({null: true, type: 'boolean'}), - function: input({null: true, type: 'function'}), - object: input({null: true, type: 'object'}), - array: input({null: true, type: 'array'}), - }, - - outputs: {'#result': '#result'}, - - steps: [ - {compute: continuation => continuation({'#result': 'OK'})}, - ], - }); - - const notCalledSymbol = Symbol('continuation not called'); - - let continuationValue; - const continuation = value => { - continuationValue = value; - return continuationSymbol; - }; - - let thrownError; - - try { - continuationValue = notCalledSymbol; - thrownError = null; - composite.expose.compute(continuation, { - [input('string')]: 123, - }); - } catch (error) { - thrownError = error; - } - - t.equal(continuationValue, notCalledSymbol); - t.match(thrownError, { - }); -}); diff --git a/test/unit/data/composite/exposeConstant.js b/test/unit/data/composite/exposeConstant.js deleted file mode 100644 index 829dc706..00000000 --- a/test/unit/data/composite/exposeConstant.js +++ /dev/null @@ -1,52 +0,0 @@ -import t from 'tap'; - -import { - compositeFrom, - continuationSymbol, - exposeConstant, - input, -} from '#composite'; - -t.test(`exposeConstant: basic behavior`, t => { - t.plan(2); - - const composite1 = compositeFrom({ - compose: false, - - steps: [ - exposeConstant({ - value: input.value('foo'), - }), - ], - }); - - t.match(composite1, { - expose: { - dependencies: [], - }, - }); - - t.equal(composite1.expose.compute(), 'foo'); -}); - -t.test(`exposeConstant: validate inputs`, t => { - t.plan(2); - - t.throws( - () => exposeConstant({}), - { - message: `Errors in input options passed to exposeConstant`, - errors: [ - {message: `Required these inputs: value`}, - ], - }); - - t.throws( - () => exposeConstant({value: 'some dependency'}), - { - message: `Errors in input options passed to exposeConstant`, - errors: [ - {message: `value: Expected input.value() call, got dependency name`}, - ], - }); -}); diff --git a/test/unit/data/composite/exposeDependency.js b/test/unit/data/composite/exposeDependency.js deleted file mode 100644 index 78801343..00000000 --- a/test/unit/data/composite/exposeDependency.js +++ /dev/null @@ -1,74 +0,0 @@ -import t from 'tap'; - -import { - compositeFrom, - continuationSymbol, - exposeDependency, - input, -} from '#composite'; - -t.test(`exposeDependency: basic behavior`, t => { - t.plan(4); - - const composite1 = compositeFrom({ - compose: false, - - steps: [ - exposeDependency({dependency: 'foo'}), - ], - }); - - t.match(composite1, { - expose: { - dependencies: ['foo'], - }, - }); - - t.equal(composite1.expose.compute({foo: 'bar'}), 'bar'); - - const composite2 = compositeFrom({ - compose: false, - - steps: [ - { - dependencies: ['foo'], - compute: (continuation, {foo}) => - continuation({'#bar': foo.toUpperCase()}), - }, - - exposeDependency({dependency: '#bar'}), - ], - }); - - t.match(composite2, { - expose: { - dependencies: ['foo'], - }, - }); - - t.equal(composite2.expose.compute({foo: 'bar'}), 'BAR'); -}); - -t.test(`exposeDependency: validate inputs`, t => { - t.plan(2); - - t.throws( - () => exposeDependency({}), - { - message: `Errors in input options passed to exposeDependency`, - errors: [ - {message: `Required these inputs: dependency`}, - ], - }); - - t.throws( - () => exposeDependency({ - dependency: input.value('some static value'), - }), - { - message: `Errors in input options passed to exposeDependency`, - errors: [ - {message: `dependency: Expected dependency name, got input.value() call`}, - ], - }); -}); diff --git a/test/unit/data/composite/templateCompositeFrom.js b/test/unit/data/composite/templateCompositeFrom.js deleted file mode 100644 index e96b782e..00000000 --- a/test/unit/data/composite/templateCompositeFrom.js +++ /dev/null @@ -1,218 +0,0 @@ -import t from 'tap'; - -import {isString} from '#validators'; - -import { - compositeFrom, - continuationSymbol, - input, - templateCompositeFrom, -} from '#composite'; - -t.test(`templateCompositeFrom: basic behavior`, t => { - t.plan(1); - - const myCoolUtility = templateCompositeFrom({ - annotation: `myCoolUtility`, - - inputs: { - foo: input(), - }, - - outputs: ['#bar'], - - steps: () => [ - { - dependencies: [input('foo')], - compute: (continuation, { - [input('foo')]: foo, - }) => continuation({ - ['#bar']: (typeof foo).toUpperCase() - }), - }, - ], - }); - - const instantiatedTemplate = myCoolUtility({ - foo: 'color', - }); - - t.match(instantiatedTemplate.toDescription(), { - annotation: `myCoolUtility`, - - inputMapping: { - foo: input.dependency('color'), - }, - - inputDescriptions: { - foo: input(), - }, - - outputs: { - '#bar': '#bar', - }, - - steps: Function, - }); -}); - -t.test(`templateCompositeFrom: validate static input values`, t => { - t.plan(3); - - const stub = { - annotation: 'stubComposite', - outputs: ['#result'], - steps: () => [{compute: continuation => continuation({'#result': 'OK'})}], - }; - - const quickThrows = (t, composite, inputOptions, ...errorMessages) => - t.throws( - () => composite(inputOptions), - { - message: `Errors in input options passed to stubComposite`, - errors: errorMessages.map(message => ({message})), - }); - - t.test(`templateCompositeFrom: validate input token shapes`, t => { - t.plan(15); - - const template1 = templateCompositeFrom({ - ...stub, inputs: { - foo: input(), - }, - }); - - t.doesNotThrow( - () => template1({foo: 'dependency'})); - - t.doesNotThrow( - () => template1({foo: input.dependency('dependency')})); - - t.doesNotThrow( - () => template1({foo: input.value('static value')})); - - t.doesNotThrow( - () => template1({foo: input('outerInput')})); - - t.doesNotThrow( - () => template1({foo: input.updateValue()})); - - t.doesNotThrow( - () => template1({foo: input.myself()})); - - quickThrows(t, template1, - {foo: input.staticValue()}, - `foo: Expected dependency name or value-providing input() call, got input.staticValue`); - - quickThrows(t, template1, - {foo: input.staticDependency()}, - `foo: Expected dependency name or value-providing input() call, got input.staticDependency`); - - const template2 = templateCompositeFrom({ - ...stub, inputs: { - bar: input.staticDependency(), - }, - }); - - t.doesNotThrow( - () => template2({bar: 'dependency'})); - - t.doesNotThrow( - () => template2({bar: input.dependency('dependency')})); - - quickThrows(t, template2, - {bar: input.value(123)}, - `bar: Expected dependency name, got input.value`); - - quickThrows(t, template2, - {bar: input('outOfPlace')}, - `bar: Expected dependency name, got input`); - - const template3 = templateCompositeFrom({ - ...stub, inputs: { - baz: input.staticValue(), - }, - }); - - t.doesNotThrow( - () => template3({baz: input.value(1025)})); - - quickThrows(t, template3, - {baz: 'dependency'}, - `baz: Expected input.value() call, got dependency name`); - - quickThrows(t, template3, - {baz: input('outOfPlace')}, - `baz: Expected input.value() call, got input() call`); - }); - - t.test(`templateCompositeFrom: validate missing / misplaced inputs`, t => { - t.plan(1); - - const template = templateCompositeFrom({ - ...stub, inputs: { - foo: input(), - bar: input(), - }, - }); - - t.throws( - () => template({ - baz: 'aeiou', - raz: input.value(123), - }), - { - message: `Errors in input options passed to stubComposite`, - errors: [ - {message: `Unexpected input names: baz, raz`}, - {message: `Required these inputs: foo, bar`}, - ], - }); - }); - - t.test(`templateCompositeFrom: validate acceptsNull / defaultValue: null`, t => { - t.plan(3); - - const template1 = templateCompositeFrom({ - ...stub, inputs: { - foo: input(), - }, - }); - - t.throws( - () => template1({}), - { - message: `Errors in input options passed to stubComposite`, - errors: [ - {message: `Required these inputs: foo`}, - ], - }, - `throws if input missing and not marked specially`); - - const template2 = templateCompositeFrom({ - ...stub, inputs: { - bar: input({acceptsNull: true}), - }, - }); - - t.throws( - () => template2({}), - { - message: `Errors in input options passed to stubComposite`, - errors: [ - {message: `Required these inputs: bar`}, - ], - }, - `throws if input missing even if marked {acceptsNull}`); - - const template3 = templateCompositeFrom({ - ...stub, inputs: { - baz: input({defaultValue: null}), - }, - }); - - t.doesNotThrow( - () => template3({}), - `does not throw if input missing if marked {defaultValue: null}`); - }); -}); diff --git a/test/unit/data/composite/withResultOfAvailabilityCheck.js b/test/unit/data/composite/withResultOfAvailabilityCheck.js deleted file mode 100644 index 01220a3a..00000000 --- a/test/unit/data/composite/withResultOfAvailabilityCheck.js +++ /dev/null @@ -1,226 +0,0 @@ -import t from 'tap'; - -import { - compositeFrom, - continuationSymbol, - withResultOfAvailabilityCheck, - input, -} from '#composite'; - -const composite = compositeFrom({ - compose: false, - - steps: [ - withResultOfAvailabilityCheck({ - from: 'from', - mode: 'mode', - }).outputs({ - ['#availability']: '#result', - }), - - { - dependencies: ['#result'], - compute: ({'#result': result}) => result, - }, - ], -}); - -t.test(`withResultOfAvailabilityCheck: basic behavior`, t => { - t.plan(1); - - t.match(composite, { - expose: { - dependencies: ['from', 'mode'], - }, - }); -}); - -const quickCompare = (t, expect, {from, mode}) => - t.equal(composite.expose.compute({from, mode}), expect); - -const quickThrows = (t, {from, mode}) => - t.throws(() => composite.expose.compute({from, mode})); - -t.test(`withResultOfAvailabilityCheck: mode = null`, t => { - t.plan(10); - - quickCompare(t, true, {mode: 'null', from: 'truthy string'}); - quickCompare(t, true, {mode: 'null', from: 123}); - quickCompare(t, true, {mode: 'null', from: true}); - - quickCompare(t, true, {mode: 'null', from: ''}); - quickCompare(t, true, {mode: 'null', from: 0}); - quickCompare(t, true, {mode: 'null', from: false}); - - quickCompare(t, true, {mode: 'null', from: [1, 2, 3]}); - quickCompare(t, true, {mode: 'null', from: []}); - - quickCompare(t, false, {mode: 'null', from: null}); - quickCompare(t, false, {mode: 'null', from: undefined}); -}); - -t.test(`withResultOfAvailabilityCheck: mode = empty`, t => { - t.plan(10); - - quickThrows(t, {mode: 'empty', from: 'truthy string'}); - quickThrows(t, {mode: 'empty', from: 123}); - quickThrows(t, {mode: 'empty', from: true}); - - quickThrows(t, {mode: 'empty', from: ''}); - quickThrows(t, {mode: 'empty', from: 0}); - quickThrows(t, {mode: 'empty', from: false}); - - quickCompare(t, true, {mode: 'empty', from: [1, 2, 3]}); - quickCompare(t, false, {mode: 'empty', from: []}); - - quickCompare(t, false, {mode: 'empty', from: null}); - quickCompare(t, false, {mode: 'empty', from: undefined}); -}); - -t.test(`withResultOfAvailabilityCheck: mode = falsy`, t => { - t.plan(10); - - quickCompare(t, true, {mode: 'falsy', from: 'truthy string'}); - quickCompare(t, true, {mode: 'falsy', from: 123}); - quickCompare(t, true, {mode: 'falsy', from: true}); - - quickCompare(t, false, {mode: 'falsy', from: ''}); - quickCompare(t, false, {mode: 'falsy', from: 0}); - quickCompare(t, false, {mode: 'falsy', from: false}); - - quickCompare(t, true, {mode: 'falsy', from: [1, 2, 3]}); - quickCompare(t, false, {mode: 'falsy', from: []}); - - quickCompare(t, false, {mode: 'falsy', from: null}); - quickCompare(t, false, {mode: 'falsy', from: undefined}); -}); - -t.test(`withResultOfAvailabilityCheck: default mode`, t => { - t.plan(1); - - const template = withResultOfAvailabilityCheck({ - from: 'foo', - }); - - t.match(template.toDescription(), { - inputMapping: { - from: input.dependency('foo'), - mode: input.value('null'), - }, - }); -}); - -t.test(`withResultOfAvailabilityCheck: validate static inputs`, t => { - t.plan(5); - - let caughtError; - - try { - caughtError = null; - withResultOfAvailabilityCheck({}); - } catch (error) { - caughtError = error; - } - - t.match(caughtError, { - errors: [/Required these inputs: from/], - }); - - t.doesNotThrow(() => - withResultOfAvailabilityCheck({ - from: 'dependency1', - mode: 'dependency2', - })); - - t.doesNotThrow(() => - withResultOfAvailabilityCheck({ - from: input.value('some static value'), - mode: input.value('null'), - })); - - try { - caughtError = null; - withResultOfAvailabilityCheck({ - from: 'foo', - mode: input.value('invalid'), - }); - } catch (error) { - caughtError = error; - } - - t.match(caughtError, { - message: /Errors in input options passed to withResultOfAvailabilityCheck/, - errors: [ - /mode: Expected one of null empty falsy, got invalid/, - ], - }); - - try { - caughtError = null; - withResultOfAvailabilityCheck({ - from: input.value(null), - mode: input.value(null), - }); - } catch (error) { - caughtError = error; - } - - t.match(caughtError, { - message: /Errors in input options passed to withResultOfAvailabilityCheck/, - errors: [ - /mode: Expected value, got null/, - ], - }); -}); - -t.test(`withResultOfAvailabilityCheck: validate dynamic inputs`, t => { - t.plan(2); - - let caughtError; - - try { - caughtError = null; - composite.expose.compute({ - from: 'apple', - mode: 'banana', - }); - } catch (error) { - caughtError = error; - } - - t.match(caughtError, { - message: /Error computing composition/, - cause: { - message: /Error computing composition withResultOfAvailabilityCheck/, - cause: { - message: /Errors in input values provided to withResultOfAvailabilityCheck/, - errors: [ - /mode: Expected one of null empty falsy, got banana/, - ], - }, - }, - }); - - try { - caughtError = null; - composite.expose.compute({ - from: null, - mode: null, - }); - } catch (error) { - caughtError = error; - } - - t.match(caughtError, { - message: /Error computing composition/, - cause: { - message: /Error computing composition withResultOfAvailabilityCheck/, - cause: { - message: /Errors in input values provided to withResultOfAvailabilityCheck/, - errors: [ - /mode: Expected value, got null/, - ], - }, - }, - }); -}); -- cgit 1.3.0-6-gf8a5 From b606bb424026597560144750d8dcf7b3e2601755 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 28 Sep 2023 19:53:18 -0300 Subject: test: withPropertyFromObject (WIP) --- .../common-utilities/withPropertyFromObject.js | 124 +++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 test/unit/data/composite/common-utilities/withPropertyFromObject.js (limited to 'test/unit/data/composite') diff --git a/test/unit/data/composite/common-utilities/withPropertyFromObject.js b/test/unit/data/composite/common-utilities/withPropertyFromObject.js new file mode 100644 index 00000000..c0e3dccf --- /dev/null +++ b/test/unit/data/composite/common-utilities/withPropertyFromObject.js @@ -0,0 +1,124 @@ +import t from 'tap'; + +import { + compositeFrom, + exposeDependency, + input, + withPropertyFromObject, +} from '#composite'; + +t.test(`withPropertyFromObject: basic behavior`, t => { + t.plan(4); + + const composite = compositeFrom({ + compose: false, + + steps: [ + withPropertyFromObject({ + object: 'object', + property: 'property', + }), + + exposeDependency({dependency: '#value'}), + ], + }); + + t.match(composite, { + expose: { + dependencies: ['object', 'property'], + }, + }); + + t.equal(composite.expose.compute({ + object: {foo: 'bar', bim: 'BOOM'}, + property: 'bim', + }), 'BOOM'); + + t.equal(composite.expose.compute({ + object: {value1: 'uwah'}, + property: 'value2', + }), null); + + t.equal(composite.expose.compute({ + object: null, + property: 'oml where did me object go', + }), null); +}); + +t.test(`withPropertyFromObject: output shapes & values`, t => { + t.plan(3 * 3 * 2); + + const dependencies = { + ['object_dependency']: + {foo: 'apple', bar: 'banana', baz: 'orange'}, + [input('object_neither')]: + {foo: 'koala', bar: 'okapi', baz: 'mongoose'}, + ['property_dependency']: + 'foo', + [input('property_neither')]: + 'baz', + }; + + const map = [ + ['object_dependency', [ + ['property_dependency', { + '#value': 'apple', + }], + [input.value('bar'), { + '#object_dependency.bar': 'banana', + }], + [input('property_neither'), { + '#value': 'orange', + }]]], + + [input.value({foo: 'ouh', bar: 'rah', baz: 'nyu'}), [ + ['property_dependency', { + '#value': 'ouh', + }], + [input.value('bar'), { + '#value': 'rah', + }], + [input('property_neither'), { + '#value': 'nyu', + }]]], + + [input('object_neither'), [ + ['property_dependency', { + '#value': 'koala', + }], + [input.value('bar'), { + '#value': 'okapi', + }], + [input('property_neither'), { + '#value': 'mongoose', + }]]], + ]; + + for (const [objectInput, submap] of map) { + for (const [propertyInput, dict] of submap) { + const step = withPropertyFromObject({ + object: objectInput, + property: propertyInput, + }); + + t.same( + Object.keys(step.toDescription().outputs), + Object.keys(dict)); + + const composite = compositeFrom({ + compose: false, + + steps: [ + step, + + { + dependencies: Object.keys(dict), + compute: dependencies => dependencies, + }, + ], + }); + + t.same(composite.expose.compute(dependencies), dict); + } + } +}); -- cgit 1.3.0-6-gf8a5 From bc0f3fe427626bb3253c790898770f16701a512a Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 29 Sep 2023 10:02:37 -0300 Subject: test: tidy output-checking syntax --- .../common-utilities/withPropertyFromObject.js | 41 +++++++++++----------- 1 file changed, 21 insertions(+), 20 deletions(-) (limited to 'test/unit/data/composite') diff --git a/test/unit/data/composite/common-utilities/withPropertyFromObject.js b/test/unit/data/composite/common-utilities/withPropertyFromObject.js index c0e3dccf..11487226 100644 --- a/test/unit/data/composite/common-utilities/withPropertyFromObject.js +++ b/test/unit/data/composite/common-utilities/withPropertyFromObject.js @@ -46,7 +46,7 @@ t.test(`withPropertyFromObject: basic behavior`, t => { }); t.test(`withPropertyFromObject: output shapes & values`, t => { - t.plan(3 * 3 * 2); + t.plan(2 * 3 ** 2); const dependencies = { ['object_dependency']: @@ -59,7 +59,7 @@ t.test(`withPropertyFromObject: output shapes & values`, t => { 'baz', }; - const map = [ + const mapLevel1 = [ ['object_dependency', [ ['property_dependency', { '#value': 'apple', @@ -94,31 +94,32 @@ t.test(`withPropertyFromObject: output shapes & values`, t => { }]]], ]; - for (const [objectInput, submap] of map) { - for (const [propertyInput, dict] of submap) { + for (const [objectInput, mapLevel2] of mapLevel1) { + for (const [propertyInput, outputDict] of mapLevel2) { const step = withPropertyFromObject({ object: objectInput, property: propertyInput, }); - t.same( - Object.keys(step.toDescription().outputs), - Object.keys(dict)); - - const composite = compositeFrom({ - compose: false, + quickCheckOutputs(step, outputDict); + } + } - steps: [ - step, + function quickCheckOutputs(step, outputDict) { + t.same( + Object.keys(step.toDescription().outputs), + Object.keys(outputDict)); - { - dependencies: Object.keys(dict), - compute: dependencies => dependencies, - }, - ], - }); + const composite = compositeFrom({ + compose: false, + steps: [step, { + dependencies: Object.keys(outputDict), + compute: dependencies => dependencies, + }], + }); - t.same(composite.expose.compute(dependencies), dict); - } + t.same( + composite.expose.compute(dependencies), + outputDict); } }); -- cgit 1.3.0-6-gf8a5 From 90492f9e58251f63a1175d0e52d76f00fa78e3bf Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 29 Sep 2023 10:29:04 -0300 Subject: test: withPropertiesFromObject (WIP) --- .../common-utilities/withPropertiesFromObject.js | 181 +++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 test/unit/data/composite/common-utilities/withPropertiesFromObject.js (limited to 'test/unit/data/composite') diff --git a/test/unit/data/composite/common-utilities/withPropertiesFromObject.js b/test/unit/data/composite/common-utilities/withPropertiesFromObject.js new file mode 100644 index 00000000..9bcb84c3 --- /dev/null +++ b/test/unit/data/composite/common-utilities/withPropertiesFromObject.js @@ -0,0 +1,181 @@ +import t from 'tap'; + +import { + compositeFrom, + exposeDependency, + input, + withPropertiesFromObject, +} from '#composite'; + +t.test(`withPropertiesFromObject: basic behavior`, t => { + t.plan(4); + + const composite = compositeFrom({ + compose: false, + + steps: [ + withPropertiesFromObject({ + object: 'object', + properties: 'properties', + }), + + exposeDependency({dependency: '#object'}), + ], + }); + + t.match(composite, { + expose: { + dependencies: ['object', 'properties'], + }, + }); + + t.same( + composite.expose.compute({ + object: {foo: 'bar', bim: 'BOOM', bam: 'baz'}, + properties: ['foo', 'bim'], + }), + {foo: 'bar', bim: 'BOOM'}); + + t.same( + composite.expose.compute({ + object: {value1: 'uwah', value2: 'arah'}, + properties: ['value1', 'value3'], + }), + {value1: 'uwah', value3: null}); + + t.same( + composite.expose.compute({ + object: null, + properties: ['ohMe', 'ohMy', 'ohDear'], + }), + {ohMe: null, ohMy: null, ohDear: null}); +}); + +t.test(`withPropertiesFromObject: output shapes & values`, t => { + t.plan(2 * 2 * 3 ** 2); + + const dependencies = { + ['object_dependency']: + {foo: 'apple', bar: 'banana', baz: 'orange'}, + [input('object_neither')]: + {foo: 'koala', bar: 'okapi', baz: 'mongoose'}, + ['properties_dependency']: + ['foo', 'bar', 'missing1'], + [input('properties_neither')]: + ['foo', 'baz', 'missing3'], + }; + + const mapLevel1 = [ + [input.value('prefix_value'), [ + ['object_dependency', [ + ['properties_dependency', { + '#object': {foo: 'apple', bar: 'banana', missing1: null}, + }], + [input.value(['bar', 'baz', 'missing2']), { + '#prefix_value.bar': 'banana', + '#prefix_value.baz': 'orange', + '#prefix_value.missing2': null, + }], + [input('properties_neither'), { + '#object': {foo: 'apple', baz: 'orange', missing3: null}, + }]]], + + [input.value({foo: 'ouh', bar: 'rah', baz: 'nyu'}), [ + ['properties_dependency', { + '#object': {foo: 'ouh', bar: 'rah', missing1: null}, + }], + [input.value(['bar', 'baz', 'missing2']), { + '#prefix_value.bar': 'rah', + '#prefix_value.baz': 'nyu', + '#prefix_value.missing2': null, + }], + [input('properties_neither'), { + '#object': {foo: 'ouh', baz: 'nyu', missing3: null}, + }]]], + + [input('object_neither'), [ + ['properties_dependency', { + '#object': {foo: 'koala', bar: 'okapi', missing1: null}, + }], + [input.value(['bar', 'baz', 'missing2']), { + '#prefix_value.bar': 'okapi', + '#prefix_value.baz': 'mongoose', + '#prefix_value.missing2': null, + }], + [input('properties_neither'), { + '#object': {foo: 'koala', baz: 'mongoose', missing3: null}, + }]]]]], + + [input.value(null), [ + ['object_dependency', [ + ['properties_dependency', { + '#object': {foo: 'apple', bar: 'banana', missing1: null}, + }], + [input.value(['bar', 'baz', 'missing2']), { + '#object_dependency.bar': 'banana', + '#object_dependency.baz': 'orange', + '#object_dependency.missing2': null, + }], + [input('properties_neither'), { + '#object': {foo: 'apple', baz: 'orange', missing3: null}, + }]]], + + [input.value({foo: 'ouh', bar: 'rah', baz: 'nyu'}), [ + ['properties_dependency', { + '#object': {foo: 'ouh', bar: 'rah', missing1: null}, + }], + [input.value(['bar', 'baz', 'missing2']), { + '#object.bar': 'rah', + '#object.baz': 'nyu', + '#object.missing2': null, + }], + [input('properties_neither'), { + '#object': {foo: 'ouh', baz: 'nyu', missing3: null}, + }]]], + + [input('object_neither'), [ + ['properties_dependency', { + '#object': {foo: 'koala', bar: 'okapi', missing1: null}, + }], + [input.value(['bar', 'baz', 'missing2']), { + '#object.bar': 'okapi', + '#object.baz': 'mongoose', + '#object.missing2': null, + }], + [input('properties_neither'), { + '#object': {foo: 'koala', baz: 'mongoose', missing3: null}, + }]]]]], + ]; + + for (const [prefixInput, mapLevel2] of mapLevel1) { + for (const [objectInput, mapLevel3] of mapLevel2) { + for (const [propertiesInput, outputDict] of mapLevel3) { + const step = withPropertiesFromObject({ + prefix: prefixInput, + object: objectInput, + properties: propertiesInput, + }); + + quickCheckOutputs(step, outputDict); + } + } + } + + function quickCheckOutputs(step, outputDict) { + t.same( + Object.keys(step.toDescription().outputs), + Object.keys(outputDict)); + + const composite = compositeFrom({ + compose: false, + steps: [step, { + dependencies: Object.keys(outputDict), + compute: dependencies => dependencies, + }], + }); + + t.same( + composite.expose.compute(dependencies), + outputDict); + } +}); -- cgit 1.3.0-6-gf8a5 From e4dc2be4c12a5578bfb5d5945a592907aed1cb4f Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 29 Sep 2023 10:36:59 -0300 Subject: data, test: type validation message adjustments --- .../data/composite/common-utilities/withResultOfAvailabilityCheck.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test/unit/data/composite') diff --git a/test/unit/data/composite/common-utilities/withResultOfAvailabilityCheck.js b/test/unit/data/composite/common-utilities/withResultOfAvailabilityCheck.js index 01220a3a..c6fa7b21 100644 --- a/test/unit/data/composite/common-utilities/withResultOfAvailabilityCheck.js +++ b/test/unit/data/composite/common-utilities/withResultOfAvailabilityCheck.js @@ -168,7 +168,7 @@ t.test(`withResultOfAvailabilityCheck: validate static inputs`, t => { t.match(caughtError, { message: /Errors in input options passed to withResultOfAvailabilityCheck/, errors: [ - /mode: Expected value, got null/, + /mode: Expected a value, got null/, ], }); }); @@ -218,7 +218,7 @@ t.test(`withResultOfAvailabilityCheck: validate dynamic inputs`, t => { cause: { message: /Errors in input values provided to withResultOfAvailabilityCheck/, errors: [ - /mode: Expected value, got null/, + /mode: Expected a value, got null/, ], }, }, -- cgit 1.3.0-6-gf8a5 From 38e8ed330aa238dc258b7749ce704eb98f5ac670 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 29 Sep 2023 10:40:46 -0300 Subject: test: tidy error matching --- .../withResultOfAvailabilityCheck.js | 124 ++++++++------------- 1 file changed, 49 insertions(+), 75 deletions(-) (limited to 'test/unit/data/composite') diff --git a/test/unit/data/composite/common-utilities/withResultOfAvailabilityCheck.js b/test/unit/data/composite/common-utilities/withResultOfAvailabilityCheck.js index c6fa7b21..dacf60f0 100644 --- a/test/unit/data/composite/common-utilities/withResultOfAvailabilityCheck.js +++ b/test/unit/data/composite/common-utilities/withResultOfAvailabilityCheck.js @@ -113,18 +113,14 @@ t.test(`withResultOfAvailabilityCheck: default mode`, t => { t.test(`withResultOfAvailabilityCheck: validate static inputs`, t => { t.plan(5); - let caughtError; - - try { - caughtError = null; - withResultOfAvailabilityCheck({}); - } catch (error) { - caughtError = error; - } - - t.match(caughtError, { - errors: [/Required these inputs: from/], - }); + t.throws( + () => withResultOfAvailabilityCheck({}), + { + message: `Errors in input options passed to withResultOfAvailabilityCheck`, + errors: [ + {message: `Required these inputs: from`} + ], + }); t.doesNotThrow(() => withResultOfAvailabilityCheck({ @@ -138,89 +134,67 @@ t.test(`withResultOfAvailabilityCheck: validate static inputs`, t => { mode: input.value('null'), })); - try { - caughtError = null; - withResultOfAvailabilityCheck({ + t.throws( + () => withResultOfAvailabilityCheck({ from: 'foo', mode: input.value('invalid'), + }), + { + message: `Errors in input options passed to withResultOfAvailabilityCheck`, + errors: [ + {message: `mode: Expected one of null empty falsy, got invalid`}, + ], }); - } catch (error) { - caughtError = error; - } - - t.match(caughtError, { - message: /Errors in input options passed to withResultOfAvailabilityCheck/, - errors: [ - /mode: Expected one of null empty falsy, got invalid/, - ], - }); - try { - caughtError = null; + t.throws(() => withResultOfAvailabilityCheck({ from: input.value(null), mode: input.value(null), + }), + { + message: `Errors in input options passed to withResultOfAvailabilityCheck`, + errors: [ + {message: `mode: Expected a value, got null`}, + ], }); - } catch (error) { - caughtError = error; - } - - t.match(caughtError, { - message: /Errors in input options passed to withResultOfAvailabilityCheck/, - errors: [ - /mode: Expected a value, got null/, - ], - }); }); t.test(`withResultOfAvailabilityCheck: validate dynamic inputs`, t => { t.plan(2); - let caughtError; - - try { - caughtError = null; - composite.expose.compute({ + t.throws( + () => composite.expose.compute({ from: 'apple', mode: 'banana', - }); - } catch (error) { - caughtError = error; - } - - t.match(caughtError, { - message: /Error computing composition/, - cause: { - message: /Error computing composition withResultOfAvailabilityCheck/, + }), + { + message: `Error computing composition`, cause: { - message: /Errors in input values provided to withResultOfAvailabilityCheck/, - errors: [ - /mode: Expected one of null empty falsy, got banana/, - ], + message: `Error computing composition withResultOfAvailabilityCheck`, + cause: { + message: `Errors in input values provided to withResultOfAvailabilityCheck`, + errors: [ + {message: `mode: Expected one of null empty falsy, got banana`}, + ], + }, }, - }, - }); + }); - try { - caughtError = null; - composite.expose.compute({ + t.throws( + () => composite.expose.compute({ from: null, mode: null, - }); - } catch (error) { - caughtError = error; - } - - t.match(caughtError, { - message: /Error computing composition/, - cause: { - message: /Error computing composition withResultOfAvailabilityCheck/, + }), + { + message: `Error computing composition`, cause: { - message: /Errors in input values provided to withResultOfAvailabilityCheck/, - errors: [ - /mode: Expected a value, got null/, - ], + message: `Error computing composition withResultOfAvailabilityCheck`, + cause: { + message: `Errors in input values provided to withResultOfAvailabilityCheck`, + errors: [ + {message: `mode: Expected a value, got null`}, + ], + }, }, - }, - }); + }); }); -- cgit 1.3.0-6-gf8a5 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 --- .../common-utilities/withPropertiesFromObject.js | 43 ++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'test/unit/data/composite') diff --git a/test/unit/data/composite/common-utilities/withPropertiesFromObject.js b/test/unit/data/composite/common-utilities/withPropertiesFromObject.js index 9bcb84c3..6b4e10c4 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 From 6eaa070e5c036ba8cd45f79c16dc2732b40ea480 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 30 Sep 2023 09:14:29 -0300 Subject: data, util: hsmusic.sugar.index -> hsmusic.decorate.indexInSourceArray --- test/unit/data/composite/common-utilities/withPropertiesFromObject.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/unit/data/composite') diff --git a/test/unit/data/composite/common-utilities/withPropertiesFromObject.js b/test/unit/data/composite/common-utilities/withPropertiesFromObject.js index 6b4e10c4..b1b8be7a 100644 --- a/test/unit/data/composite/common-utilities/withPropertiesFromObject.js +++ b/test/unit/data/composite/common-utilities/withPropertiesFromObject.js @@ -216,7 +216,7 @@ t.test(`withPropertiesFromObject: validate static inputs`, t => { {message: 'object: Expected an object, got array'}, {message: 'properties: Errors validating array items', errors: [ { - [Symbol.for('hsmusic.sugar.index')]: 2, + [Symbol.for('hsmusic.decorate.indexInSourceArray')]: 2, message: /Expected a string, got number/, }, ]}, -- cgit 1.3.0-6-gf8a5 From 902ebbafb7cfb56f7a878d33cf94e5708861ff4a Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 30 Sep 2023 19:22:52 -0300 Subject: test: various error whitespace cleanup --- .../composite/common-utilities/exposeConstant.js | 18 +++---- .../composite/common-utilities/exposeDependency.js | 18 +++---- .../common-utilities/withPropertiesFromObject.js | 26 ++++----- .../withResultOfAvailabilityCheck.js | 63 +++++++--------------- 4 files changed, 42 insertions(+), 83 deletions(-) (limited to 'test/unit/data/composite') diff --git a/test/unit/data/composite/common-utilities/exposeConstant.js b/test/unit/data/composite/common-utilities/exposeConstant.js index 829dc706..bfed0951 100644 --- a/test/unit/data/composite/common-utilities/exposeConstant.js +++ b/test/unit/data/composite/common-utilities/exposeConstant.js @@ -34,19 +34,13 @@ t.test(`exposeConstant: validate inputs`, t => { t.throws( () => exposeConstant({}), - { - message: `Errors in input options passed to exposeConstant`, - errors: [ - {message: `Required these inputs: value`}, - ], - }); + {message: `Errors in input options passed to exposeConstant`, errors: [ + {message: `Required these inputs: value`}, + ]}); t.throws( () => exposeConstant({value: 'some dependency'}), - { - message: `Errors in input options passed to exposeConstant`, - errors: [ - {message: `value: Expected input.value() call, got dependency name`}, - ], - }); + {message: `Errors in input options passed to exposeConstant`, errors: [ + {message: `value: Expected input.value() call, got dependency name`}, + ]}); }); diff --git a/test/unit/data/composite/common-utilities/exposeDependency.js b/test/unit/data/composite/common-utilities/exposeDependency.js index 78801343..4f07cc16 100644 --- a/test/unit/data/composite/common-utilities/exposeDependency.js +++ b/test/unit/data/composite/common-utilities/exposeDependency.js @@ -54,21 +54,15 @@ t.test(`exposeDependency: validate inputs`, t => { t.throws( () => exposeDependency({}), - { - message: `Errors in input options passed to exposeDependency`, - errors: [ - {message: `Required these inputs: dependency`}, - ], - }); + {message: `Errors in input options passed to exposeDependency`, errors: [ + {message: `Required these inputs: dependency`}, + ]}); t.throws( () => exposeDependency({ dependency: input.value('some static value'), }), - { - message: `Errors in input options passed to exposeDependency`, - errors: [ - {message: `dependency: Expected dependency name, got input.value() call`}, - ], - }); + {message: `Errors in input options passed to exposeDependency`, errors: [ + {message: `dependency: Expected dependency name, got input.value() call`}, + ]}); }); diff --git a/test/unit/data/composite/common-utilities/withPropertiesFromObject.js b/test/unit/data/composite/common-utilities/withPropertiesFromObject.js index b1b8be7a..835767cb 100644 --- a/test/unit/data/composite/common-utilities/withPropertiesFromObject.js +++ b/test/unit/data/composite/common-utilities/withPropertiesFromObject.js @@ -185,12 +185,9 @@ t.test(`withPropertiesFromObject: validate static inputs`, t => { t.throws( () => withPropertiesFromObject({}), - { - message: `Errors in input options passed to withPropertiesFromObject`, - errors: [ - {message: `Required these inputs: object, properties`}, - ], - }); + {message: `Errors in input options passed to withPropertiesFromObject`, errors: [ + {message: `Required these inputs: object, properties`}, + ]}); t.throws( () => withPropertiesFromObject({ @@ -198,14 +195,11 @@ t.test(`withPropertiesFromObject: validate static inputs`, t => { 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'}, - ], - }); + {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({ @@ -213,8 +207,8 @@ t.test(`withPropertiesFromObject: validate static inputs`, t => { 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: [ + {message: `object: Expected an object, got array`}, + {message: `properties: Errors validating array items`, errors: [ { [Symbol.for('hsmusic.decorate.indexInSourceArray')]: 2, message: /Expected a string, got number/, diff --git a/test/unit/data/composite/common-utilities/withResultOfAvailabilityCheck.js b/test/unit/data/composite/common-utilities/withResultOfAvailabilityCheck.js index dacf60f0..50c127d3 100644 --- a/test/unit/data/composite/common-utilities/withResultOfAvailabilityCheck.js +++ b/test/unit/data/composite/common-utilities/withResultOfAvailabilityCheck.js @@ -3,8 +3,8 @@ import t from 'tap'; import { compositeFrom, continuationSymbol, - withResultOfAvailabilityCheck, input, + withResultOfAvailabilityCheck, } from '#composite'; const composite = compositeFrom({ @@ -115,12 +115,9 @@ t.test(`withResultOfAvailabilityCheck: validate static inputs`, t => { t.throws( () => withResultOfAvailabilityCheck({}), - { - message: `Errors in input options passed to withResultOfAvailabilityCheck`, - errors: [ - {message: `Required these inputs: from`} - ], - }); + {message: `Errors in input options passed to withResultOfAvailabilityCheck`, errors: [ + {message: `Required these inputs: from`}, + ]}); t.doesNotThrow(() => withResultOfAvailabilityCheck({ @@ -139,24 +136,18 @@ t.test(`withResultOfAvailabilityCheck: validate static inputs`, t => { from: 'foo', mode: input.value('invalid'), }), - { - message: `Errors in input options passed to withResultOfAvailabilityCheck`, - errors: [ - {message: `mode: Expected one of null empty falsy, got invalid`}, - ], - }); + {message: `Errors in input options passed to withResultOfAvailabilityCheck`, errors: [ + {message: `mode: Expected one of null empty falsy, got invalid`}, + ]}); t.throws(() => withResultOfAvailabilityCheck({ from: input.value(null), mode: input.value(null), }), - { - message: `Errors in input options passed to withResultOfAvailabilityCheck`, - errors: [ - {message: `mode: Expected a value, got null`}, - ], - }); + {message: `Errors in input options passed to withResultOfAvailabilityCheck`, errors: [ + {message: `mode: Expected a value, got null`}, + ]}); }); t.test(`withResultOfAvailabilityCheck: validate dynamic inputs`, t => { @@ -167,34 +158,20 @@ t.test(`withResultOfAvailabilityCheck: validate dynamic inputs`, t => { from: 'apple', mode: 'banana', }), - { - message: `Error computing composition`, - cause: { - message: `Error computing composition withResultOfAvailabilityCheck`, - cause: { - message: `Errors in input values provided to withResultOfAvailabilityCheck`, - errors: [ - {message: `mode: Expected one of null empty falsy, got banana`}, - ], - }, - }, - }); + {message: `Error computing composition`, cause: + {message: `Error computing composition withResultOfAvailabilityCheck`, cause: + {message: `Errors in input values provided to withResultOfAvailabilityCheck`, errors: [ + {message: `mode: Expected one of null empty falsy, got banana`}, + ]}}}); t.throws( () => composite.expose.compute({ from: null, mode: null, }), - { - message: `Error computing composition`, - cause: { - message: `Error computing composition withResultOfAvailabilityCheck`, - cause: { - message: `Errors in input values provided to withResultOfAvailabilityCheck`, - errors: [ - {message: `mode: Expected a value, got null`}, - ], - }, - }, - }); + {message: `Error computing composition`, cause: + {message: `Error computing composition withResultOfAvailabilityCheck`, cause: + {message: `Errors in input values provided to withResultOfAvailabilityCheck`, errors: [ + {message: `mode: Expected a value, got null`}, + ]}}}); }); -- cgit 1.3.0-6-gf8a5 From b09540f108de841a067f8f6a8e62c82335221ee9 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 30 Sep 2023 19:23:38 -0300 Subject: test: withPropertiesFromObject: dynamic input validation --- .../common-utilities/withPropertiesFromObject.js | 57 +++++++++++++++++----- 1 file changed, 45 insertions(+), 12 deletions(-) (limited to 'test/unit/data/composite') diff --git a/test/unit/data/composite/common-utilities/withPropertiesFromObject.js b/test/unit/data/composite/common-utilities/withPropertiesFromObject.js index 835767cb..3431382e 100644 --- a/test/unit/data/composite/common-utilities/withPropertiesFromObject.js +++ b/test/unit/data/composite/common-utilities/withPropertiesFromObject.js @@ -7,21 +7,21 @@ import { withPropertiesFromObject, } from '#composite'; -t.test(`withPropertiesFromObject: basic behavior`, t => { - t.plan(4); +const composite = compositeFrom({ + compose: false, - const composite = compositeFrom({ - compose: false, + steps: [ + withPropertiesFromObject({ + object: 'object', + properties: 'properties', + }), - steps: [ - withPropertiesFromObject({ - object: 'object', - properties: 'properties', - }), + exposeDependency({dependency: '#object'}), + ], +}); - exposeDependency({dependency: '#object'}), - ], - }); +t.test(`withPropertiesFromObject: basic behavior`, t => { + t.plan(4); t.match(composite, { expose: { @@ -216,3 +216,36 @@ t.test(`withPropertiesFromObject: validate static inputs`, t => { ]}, ]}); }); + +t.test(`withPropertiesFromObject: validate dynamic inputs`, t => { + t.plan(2); + + t.throws( + () => composite.expose.compute({ + object: 'intriguing', + properties: 'onceMore', + }), + {message: `Error computing composition`, cause: + {message: `Error computing composition withPropertiesFromObject`, cause: + {message: `Errors in input values provided to withPropertiesFromObject`, errors: [ + {message: `object: Expected an object, got string`}, + {message: `properties: Expected an array, got string`}, + ]}}}); + + t.throws( + () => composite.expose.compute({ + object: [['abc', 1], ['def', 2], [123, 3]], + properties: ['abc', 'def', 123], + }), + {message: `Error computing composition`, cause: + {message: `Error computing composition withPropertiesFromObject`, cause: + {message: `Errors in input values provided to withPropertiesFromObject`, errors: [ + {message: `object: Expected an object, got array`}, + {message: `properties: Errors validating array items`, errors: [ + { + [Symbol.for('hsmusic.decorate.indexInSourceArray')]: 2, + message: /Expected a string, got number/, + }, + ]}, + ]}}}); +}); -- cgit 1.3.0-6-gf8a5 From ab7591e45e7e31b4e2c0e2f81e224672145993fa Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sun, 1 Oct 2023 17:01:21 -0300 Subject: data, test: refactor utilities into own file Primarily for more precies test coverage mapping, but also to make navigation a bit easier and consolidate complex functions with lots of imports out of the same space as other, more simple or otherwise specialized files. --- .../composite/common-utilities/exposeConstant.js | 46 ---- .../composite/common-utilities/exposeDependency.js | 68 ------ .../common-utilities/withPropertiesFromObject.js | 251 --------------------- .../common-utilities/withPropertyFromObject.js | 125 ---------- .../withResultOfAvailabilityCheck.js | 177 --------------- .../data/composite/control-flow/exposeConstant.js | 42 ++++ .../composite/control-flow/exposeDependency.js | 64 ++++++ .../control-flow/withResultOfAvailabilityCheck.js | 173 ++++++++++++++ .../composite/data/withPropertiesFromObject.js | 248 ++++++++++++++++++++ .../data/composite/data/withPropertyFromObject.js | 122 ++++++++++ 10 files changed, 649 insertions(+), 667 deletions(-) delete mode 100644 test/unit/data/composite/common-utilities/exposeConstant.js delete mode 100644 test/unit/data/composite/common-utilities/exposeDependency.js delete mode 100644 test/unit/data/composite/common-utilities/withPropertiesFromObject.js delete mode 100644 test/unit/data/composite/common-utilities/withPropertyFromObject.js delete mode 100644 test/unit/data/composite/common-utilities/withResultOfAvailabilityCheck.js create mode 100644 test/unit/data/composite/control-flow/exposeConstant.js create mode 100644 test/unit/data/composite/control-flow/exposeDependency.js create mode 100644 test/unit/data/composite/control-flow/withResultOfAvailabilityCheck.js create mode 100644 test/unit/data/composite/data/withPropertiesFromObject.js create mode 100644 test/unit/data/composite/data/withPropertyFromObject.js (limited to 'test/unit/data/composite') diff --git a/test/unit/data/composite/common-utilities/exposeConstant.js b/test/unit/data/composite/common-utilities/exposeConstant.js deleted file mode 100644 index bfed0951..00000000 --- a/test/unit/data/composite/common-utilities/exposeConstant.js +++ /dev/null @@ -1,46 +0,0 @@ -import t from 'tap'; - -import { - compositeFrom, - continuationSymbol, - exposeConstant, - input, -} from '#composite'; - -t.test(`exposeConstant: basic behavior`, t => { - t.plan(2); - - const composite1 = compositeFrom({ - compose: false, - - steps: [ - exposeConstant({ - value: input.value('foo'), - }), - ], - }); - - t.match(composite1, { - expose: { - dependencies: [], - }, - }); - - t.equal(composite1.expose.compute(), 'foo'); -}); - -t.test(`exposeConstant: validate inputs`, t => { - t.plan(2); - - t.throws( - () => exposeConstant({}), - {message: `Errors in input options passed to exposeConstant`, errors: [ - {message: `Required these inputs: value`}, - ]}); - - t.throws( - () => exposeConstant({value: 'some dependency'}), - {message: `Errors in input options passed to exposeConstant`, errors: [ - {message: `value: Expected input.value() call, got dependency name`}, - ]}); -}); diff --git a/test/unit/data/composite/common-utilities/exposeDependency.js b/test/unit/data/composite/common-utilities/exposeDependency.js deleted file mode 100644 index 4f07cc16..00000000 --- a/test/unit/data/composite/common-utilities/exposeDependency.js +++ /dev/null @@ -1,68 +0,0 @@ -import t from 'tap'; - -import { - compositeFrom, - continuationSymbol, - exposeDependency, - input, -} from '#composite'; - -t.test(`exposeDependency: basic behavior`, t => { - t.plan(4); - - const composite1 = compositeFrom({ - compose: false, - - steps: [ - exposeDependency({dependency: 'foo'}), - ], - }); - - t.match(composite1, { - expose: { - dependencies: ['foo'], - }, - }); - - t.equal(composite1.expose.compute({foo: 'bar'}), 'bar'); - - const composite2 = compositeFrom({ - compose: false, - - steps: [ - { - dependencies: ['foo'], - compute: (continuation, {foo}) => - continuation({'#bar': foo.toUpperCase()}), - }, - - exposeDependency({dependency: '#bar'}), - ], - }); - - t.match(composite2, { - expose: { - dependencies: ['foo'], - }, - }); - - t.equal(composite2.expose.compute({foo: 'bar'}), 'BAR'); -}); - -t.test(`exposeDependency: validate inputs`, t => { - t.plan(2); - - t.throws( - () => exposeDependency({}), - {message: `Errors in input options passed to exposeDependency`, errors: [ - {message: `Required these inputs: dependency`}, - ]}); - - t.throws( - () => exposeDependency({ - dependency: input.value('some static value'), - }), - {message: `Errors in input options passed to exposeDependency`, errors: [ - {message: `dependency: Expected dependency name, got input.value() call`}, - ]}); -}); diff --git a/test/unit/data/composite/common-utilities/withPropertiesFromObject.js b/test/unit/data/composite/common-utilities/withPropertiesFromObject.js deleted file mode 100644 index 3431382e..00000000 --- a/test/unit/data/composite/common-utilities/withPropertiesFromObject.js +++ /dev/null @@ -1,251 +0,0 @@ -import t from 'tap'; - -import { - compositeFrom, - exposeDependency, - input, - withPropertiesFromObject, -} from '#composite'; - -const composite = compositeFrom({ - compose: false, - - steps: [ - withPropertiesFromObject({ - object: 'object', - properties: 'properties', - }), - - exposeDependency({dependency: '#object'}), - ], -}); - -t.test(`withPropertiesFromObject: basic behavior`, t => { - t.plan(4); - - t.match(composite, { - expose: { - dependencies: ['object', 'properties'], - }, - }); - - t.same( - composite.expose.compute({ - object: {foo: 'bar', bim: 'BOOM', bam: 'baz'}, - properties: ['foo', 'bim'], - }), - {foo: 'bar', bim: 'BOOM'}); - - t.same( - composite.expose.compute({ - object: {value1: 'uwah', value2: 'arah'}, - properties: ['value1', 'value3'], - }), - {value1: 'uwah', value3: null}); - - t.same( - composite.expose.compute({ - object: null, - properties: ['ohMe', 'ohMy', 'ohDear'], - }), - {ohMe: null, ohMy: null, ohDear: null}); -}); - -t.test(`withPropertiesFromObject: output shapes & values`, t => { - t.plan(2 * 2 * 3 ** 2); - - const dependencies = { - ['object_dependency']: - {foo: 'apple', bar: 'banana', baz: 'orange'}, - [input('object_neither')]: - {foo: 'koala', bar: 'okapi', baz: 'mongoose'}, - ['properties_dependency']: - ['foo', 'bar', 'missing1'], - [input('properties_neither')]: - ['foo', 'baz', 'missing3'], - }; - - const mapLevel1 = [ - [input.value('prefix_value'), [ - ['object_dependency', [ - ['properties_dependency', { - '#object': {foo: 'apple', bar: 'banana', missing1: null}, - }], - [input.value(['bar', 'baz', 'missing2']), { - '#prefix_value.bar': 'banana', - '#prefix_value.baz': 'orange', - '#prefix_value.missing2': null, - }], - [input('properties_neither'), { - '#object': {foo: 'apple', baz: 'orange', missing3: null}, - }]]], - - [input.value({foo: 'ouh', bar: 'rah', baz: 'nyu'}), [ - ['properties_dependency', { - '#object': {foo: 'ouh', bar: 'rah', missing1: null}, - }], - [input.value(['bar', 'baz', 'missing2']), { - '#prefix_value.bar': 'rah', - '#prefix_value.baz': 'nyu', - '#prefix_value.missing2': null, - }], - [input('properties_neither'), { - '#object': {foo: 'ouh', baz: 'nyu', missing3: null}, - }]]], - - [input('object_neither'), [ - ['properties_dependency', { - '#object': {foo: 'koala', bar: 'okapi', missing1: null}, - }], - [input.value(['bar', 'baz', 'missing2']), { - '#prefix_value.bar': 'okapi', - '#prefix_value.baz': 'mongoose', - '#prefix_value.missing2': null, - }], - [input('properties_neither'), { - '#object': {foo: 'koala', baz: 'mongoose', missing3: null}, - }]]]]], - - [input.value(null), [ - ['object_dependency', [ - ['properties_dependency', { - '#object': {foo: 'apple', bar: 'banana', missing1: null}, - }], - [input.value(['bar', 'baz', 'missing2']), { - '#object_dependency.bar': 'banana', - '#object_dependency.baz': 'orange', - '#object_dependency.missing2': null, - }], - [input('properties_neither'), { - '#object': {foo: 'apple', baz: 'orange', missing3: null}, - }]]], - - [input.value({foo: 'ouh', bar: 'rah', baz: 'nyu'}), [ - ['properties_dependency', { - '#object': {foo: 'ouh', bar: 'rah', missing1: null}, - }], - [input.value(['bar', 'baz', 'missing2']), { - '#object.bar': 'rah', - '#object.baz': 'nyu', - '#object.missing2': null, - }], - [input('properties_neither'), { - '#object': {foo: 'ouh', baz: 'nyu', missing3: null}, - }]]], - - [input('object_neither'), [ - ['properties_dependency', { - '#object': {foo: 'koala', bar: 'okapi', missing1: null}, - }], - [input.value(['bar', 'baz', 'missing2']), { - '#object.bar': 'okapi', - '#object.baz': 'mongoose', - '#object.missing2': null, - }], - [input('properties_neither'), { - '#object': {foo: 'koala', baz: 'mongoose', missing3: null}, - }]]]]], - ]; - - for (const [prefixInput, mapLevel2] of mapLevel1) { - for (const [objectInput, mapLevel3] of mapLevel2) { - for (const [propertiesInput, outputDict] of mapLevel3) { - const step = withPropertiesFromObject({ - prefix: prefixInput, - object: objectInput, - properties: propertiesInput, - }); - - quickCheckOutputs(step, outputDict); - } - } - } - - function quickCheckOutputs(step, outputDict) { - t.same( - Object.keys(step.toDescription().outputs), - Object.keys(outputDict)); - - const composite = compositeFrom({ - compose: false, - steps: [step, { - dependencies: Object.keys(outputDict), - compute: dependencies => dependencies, - }], - }); - - t.same( - composite.expose.compute(dependencies), - 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.decorate.indexInSourceArray')]: 2, - message: /Expected a string, got number/, - }, - ]}, - ]}); -}); - -t.test(`withPropertiesFromObject: validate dynamic inputs`, t => { - t.plan(2); - - t.throws( - () => composite.expose.compute({ - object: 'intriguing', - properties: 'onceMore', - }), - {message: `Error computing composition`, cause: - {message: `Error computing composition withPropertiesFromObject`, cause: - {message: `Errors in input values provided to withPropertiesFromObject`, errors: [ - {message: `object: Expected an object, got string`}, - {message: `properties: Expected an array, got string`}, - ]}}}); - - t.throws( - () => composite.expose.compute({ - object: [['abc', 1], ['def', 2], [123, 3]], - properties: ['abc', 'def', 123], - }), - {message: `Error computing composition`, cause: - {message: `Error computing composition withPropertiesFromObject`, cause: - {message: `Errors in input values provided to withPropertiesFromObject`, errors: [ - {message: `object: Expected an object, got array`}, - {message: `properties: Errors validating array items`, errors: [ - { - [Symbol.for('hsmusic.decorate.indexInSourceArray')]: 2, - message: /Expected a string, got number/, - }, - ]}, - ]}}}); -}); diff --git a/test/unit/data/composite/common-utilities/withPropertyFromObject.js b/test/unit/data/composite/common-utilities/withPropertyFromObject.js deleted file mode 100644 index 11487226..00000000 --- a/test/unit/data/composite/common-utilities/withPropertyFromObject.js +++ /dev/null @@ -1,125 +0,0 @@ -import t from 'tap'; - -import { - compositeFrom, - exposeDependency, - input, - withPropertyFromObject, -} from '#composite'; - -t.test(`withPropertyFromObject: basic behavior`, t => { - t.plan(4); - - const composite = compositeFrom({ - compose: false, - - steps: [ - withPropertyFromObject({ - object: 'object', - property: 'property', - }), - - exposeDependency({dependency: '#value'}), - ], - }); - - t.match(composite, { - expose: { - dependencies: ['object', 'property'], - }, - }); - - t.equal(composite.expose.compute({ - object: {foo: 'bar', bim: 'BOOM'}, - property: 'bim', - }), 'BOOM'); - - t.equal(composite.expose.compute({ - object: {value1: 'uwah'}, - property: 'value2', - }), null); - - t.equal(composite.expose.compute({ - object: null, - property: 'oml where did me object go', - }), null); -}); - -t.test(`withPropertyFromObject: output shapes & values`, t => { - t.plan(2 * 3 ** 2); - - const dependencies = { - ['object_dependency']: - {foo: 'apple', bar: 'banana', baz: 'orange'}, - [input('object_neither')]: - {foo: 'koala', bar: 'okapi', baz: 'mongoose'}, - ['property_dependency']: - 'foo', - [input('property_neither')]: - 'baz', - }; - - const mapLevel1 = [ - ['object_dependency', [ - ['property_dependency', { - '#value': 'apple', - }], - [input.value('bar'), { - '#object_dependency.bar': 'banana', - }], - [input('property_neither'), { - '#value': 'orange', - }]]], - - [input.value({foo: 'ouh', bar: 'rah', baz: 'nyu'}), [ - ['property_dependency', { - '#value': 'ouh', - }], - [input.value('bar'), { - '#value': 'rah', - }], - [input('property_neither'), { - '#value': 'nyu', - }]]], - - [input('object_neither'), [ - ['property_dependency', { - '#value': 'koala', - }], - [input.value('bar'), { - '#value': 'okapi', - }], - [input('property_neither'), { - '#value': 'mongoose', - }]]], - ]; - - for (const [objectInput, mapLevel2] of mapLevel1) { - for (const [propertyInput, outputDict] of mapLevel2) { - const step = withPropertyFromObject({ - object: objectInput, - property: propertyInput, - }); - - quickCheckOutputs(step, outputDict); - } - } - - function quickCheckOutputs(step, outputDict) { - t.same( - Object.keys(step.toDescription().outputs), - Object.keys(outputDict)); - - const composite = compositeFrom({ - compose: false, - steps: [step, { - dependencies: Object.keys(outputDict), - compute: dependencies => dependencies, - }], - }); - - t.same( - composite.expose.compute(dependencies), - outputDict); - } -}); diff --git a/test/unit/data/composite/common-utilities/withResultOfAvailabilityCheck.js b/test/unit/data/composite/common-utilities/withResultOfAvailabilityCheck.js deleted file mode 100644 index 50c127d3..00000000 --- a/test/unit/data/composite/common-utilities/withResultOfAvailabilityCheck.js +++ /dev/null @@ -1,177 +0,0 @@ -import t from 'tap'; - -import { - compositeFrom, - continuationSymbol, - input, - withResultOfAvailabilityCheck, -} from '#composite'; - -const composite = compositeFrom({ - compose: false, - - steps: [ - withResultOfAvailabilityCheck({ - from: 'from', - mode: 'mode', - }).outputs({ - ['#availability']: '#result', - }), - - { - dependencies: ['#result'], - compute: ({'#result': result}) => result, - }, - ], -}); - -t.test(`withResultOfAvailabilityCheck: basic behavior`, t => { - t.plan(1); - - t.match(composite, { - expose: { - dependencies: ['from', 'mode'], - }, - }); -}); - -const quickCompare = (t, expect, {from, mode}) => - t.equal(composite.expose.compute({from, mode}), expect); - -const quickThrows = (t, {from, mode}) => - t.throws(() => composite.expose.compute({from, mode})); - -t.test(`withResultOfAvailabilityCheck: mode = null`, t => { - t.plan(10); - - quickCompare(t, true, {mode: 'null', from: 'truthy string'}); - quickCompare(t, true, {mode: 'null', from: 123}); - quickCompare(t, true, {mode: 'null', from: true}); - - quickCompare(t, true, {mode: 'null', from: ''}); - quickCompare(t, true, {mode: 'null', from: 0}); - quickCompare(t, true, {mode: 'null', from: false}); - - quickCompare(t, true, {mode: 'null', from: [1, 2, 3]}); - quickCompare(t, true, {mode: 'null', from: []}); - - quickCompare(t, false, {mode: 'null', from: null}); - quickCompare(t, false, {mode: 'null', from: undefined}); -}); - -t.test(`withResultOfAvailabilityCheck: mode = empty`, t => { - t.plan(10); - - quickThrows(t, {mode: 'empty', from: 'truthy string'}); - quickThrows(t, {mode: 'empty', from: 123}); - quickThrows(t, {mode: 'empty', from: true}); - - quickThrows(t, {mode: 'empty', from: ''}); - quickThrows(t, {mode: 'empty', from: 0}); - quickThrows(t, {mode: 'empty', from: false}); - - quickCompare(t, true, {mode: 'empty', from: [1, 2, 3]}); - quickCompare(t, false, {mode: 'empty', from: []}); - - quickCompare(t, false, {mode: 'empty', from: null}); - quickCompare(t, false, {mode: 'empty', from: undefined}); -}); - -t.test(`withResultOfAvailabilityCheck: mode = falsy`, t => { - t.plan(10); - - quickCompare(t, true, {mode: 'falsy', from: 'truthy string'}); - quickCompare(t, true, {mode: 'falsy', from: 123}); - quickCompare(t, true, {mode: 'falsy', from: true}); - - quickCompare(t, false, {mode: 'falsy', from: ''}); - quickCompare(t, false, {mode: 'falsy', from: 0}); - quickCompare(t, false, {mode: 'falsy', from: false}); - - quickCompare(t, true, {mode: 'falsy', from: [1, 2, 3]}); - quickCompare(t, false, {mode: 'falsy', from: []}); - - quickCompare(t, false, {mode: 'falsy', from: null}); - quickCompare(t, false, {mode: 'falsy', from: undefined}); -}); - -t.test(`withResultOfAvailabilityCheck: default mode`, t => { - t.plan(1); - - const template = withResultOfAvailabilityCheck({ - from: 'foo', - }); - - t.match(template.toDescription(), { - inputMapping: { - from: input.dependency('foo'), - mode: input.value('null'), - }, - }); -}); - -t.test(`withResultOfAvailabilityCheck: validate static inputs`, t => { - t.plan(5); - - t.throws( - () => withResultOfAvailabilityCheck({}), - {message: `Errors in input options passed to withResultOfAvailabilityCheck`, errors: [ - {message: `Required these inputs: from`}, - ]}); - - t.doesNotThrow(() => - withResultOfAvailabilityCheck({ - from: 'dependency1', - mode: 'dependency2', - })); - - t.doesNotThrow(() => - withResultOfAvailabilityCheck({ - from: input.value('some static value'), - mode: input.value('null'), - })); - - t.throws( - () => withResultOfAvailabilityCheck({ - from: 'foo', - mode: input.value('invalid'), - }), - {message: `Errors in input options passed to withResultOfAvailabilityCheck`, errors: [ - {message: `mode: Expected one of null empty falsy, got invalid`}, - ]}); - - t.throws(() => - withResultOfAvailabilityCheck({ - from: input.value(null), - mode: input.value(null), - }), - {message: `Errors in input options passed to withResultOfAvailabilityCheck`, errors: [ - {message: `mode: Expected a value, got null`}, - ]}); -}); - -t.test(`withResultOfAvailabilityCheck: validate dynamic inputs`, t => { - t.plan(2); - - t.throws( - () => composite.expose.compute({ - from: 'apple', - mode: 'banana', - }), - {message: `Error computing composition`, cause: - {message: `Error computing composition withResultOfAvailabilityCheck`, cause: - {message: `Errors in input values provided to withResultOfAvailabilityCheck`, errors: [ - {message: `mode: Expected one of null empty falsy, got banana`}, - ]}}}); - - t.throws( - () => composite.expose.compute({ - from: null, - mode: null, - }), - {message: `Error computing composition`, cause: - {message: `Error computing composition withResultOfAvailabilityCheck`, cause: - {message: `Errors in input values provided to withResultOfAvailabilityCheck`, errors: [ - {message: `mode: Expected a value, got null`}, - ]}}}); -}); diff --git a/test/unit/data/composite/control-flow/exposeConstant.js b/test/unit/data/composite/control-flow/exposeConstant.js new file mode 100644 index 00000000..0c75894b --- /dev/null +++ b/test/unit/data/composite/control-flow/exposeConstant.js @@ -0,0 +1,42 @@ +import t from 'tap'; + +import {compositeFrom, continuationSymbol, input} from '#composite'; +import {exposeConstant} from '#composite/control-flow'; + +t.test(`exposeConstant: basic behavior`, t => { + t.plan(2); + + const composite1 = compositeFrom({ + compose: false, + + steps: [ + exposeConstant({ + value: input.value('foo'), + }), + ], + }); + + t.match(composite1, { + expose: { + dependencies: [], + }, + }); + + t.equal(composite1.expose.compute(), 'foo'); +}); + +t.test(`exposeConstant: validate inputs`, t => { + t.plan(2); + + t.throws( + () => exposeConstant({}), + {message: `Errors in input options passed to exposeConstant`, errors: [ + {message: `Required these inputs: value`}, + ]}); + + t.throws( + () => exposeConstant({value: 'some dependency'}), + {message: `Errors in input options passed to exposeConstant`, errors: [ + {message: `value: Expected input.value() call, got dependency name`}, + ]}); +}); diff --git a/test/unit/data/composite/control-flow/exposeDependency.js b/test/unit/data/composite/control-flow/exposeDependency.js new file mode 100644 index 00000000..8f6bfd01 --- /dev/null +++ b/test/unit/data/composite/control-flow/exposeDependency.js @@ -0,0 +1,64 @@ +import t from 'tap'; + +import {compositeFrom, continuationSymbol, input} from '#composite'; +import {exposeDependency} from '#composite/control-flow'; + +t.test(`exposeDependency: basic behavior`, t => { + t.plan(4); + + const composite1 = compositeFrom({ + compose: false, + + steps: [ + exposeDependency({dependency: 'foo'}), + ], + }); + + t.match(composite1, { + expose: { + dependencies: ['foo'], + }, + }); + + t.equal(composite1.expose.compute({foo: 'bar'}), 'bar'); + + const composite2 = compositeFrom({ + compose: false, + + steps: [ + { + dependencies: ['foo'], + compute: (continuation, {foo}) => + continuation({'#bar': foo.toUpperCase()}), + }, + + exposeDependency({dependency: '#bar'}), + ], + }); + + t.match(composite2, { + expose: { + dependencies: ['foo'], + }, + }); + + t.equal(composite2.expose.compute({foo: 'bar'}), 'BAR'); +}); + +t.test(`exposeDependency: validate inputs`, t => { + t.plan(2); + + t.throws( + () => exposeDependency({}), + {message: `Errors in input options passed to exposeDependency`, errors: [ + {message: `Required these inputs: dependency`}, + ]}); + + t.throws( + () => exposeDependency({ + dependency: input.value('some static value'), + }), + {message: `Errors in input options passed to exposeDependency`, errors: [ + {message: `dependency: Expected dependency name, got input.value() call`}, + ]}); +}); diff --git a/test/unit/data/composite/control-flow/withResultOfAvailabilityCheck.js b/test/unit/data/composite/control-flow/withResultOfAvailabilityCheck.js new file mode 100644 index 00000000..4c4be04a --- /dev/null +++ b/test/unit/data/composite/control-flow/withResultOfAvailabilityCheck.js @@ -0,0 +1,173 @@ +import t from 'tap'; + +import {compositeFrom, continuationSymbol, input} from '#composite'; +import {withResultOfAvailabilityCheck} from '#composite/control-flow'; + +const composite = compositeFrom({ + compose: false, + + steps: [ + withResultOfAvailabilityCheck({ + from: 'from', + mode: 'mode', + }).outputs({ + ['#availability']: '#result', + }), + + { + dependencies: ['#result'], + compute: ({'#result': result}) => result, + }, + ], +}); + +t.test(`withResultOfAvailabilityCheck: basic behavior`, t => { + t.plan(1); + + t.match(composite, { + expose: { + dependencies: ['from', 'mode'], + }, + }); +}); + +const quickCompare = (t, expect, {from, mode}) => + t.equal(composite.expose.compute({from, mode}), expect); + +const quickThrows = (t, {from, mode}) => + t.throws(() => composite.expose.compute({from, mode})); + +t.test(`withResultOfAvailabilityCheck: mode = null`, t => { + t.plan(10); + + quickCompare(t, true, {mode: 'null', from: 'truthy string'}); + quickCompare(t, true, {mode: 'null', from: 123}); + quickCompare(t, true, {mode: 'null', from: true}); + + quickCompare(t, true, {mode: 'null', from: ''}); + quickCompare(t, true, {mode: 'null', from: 0}); + quickCompare(t, true, {mode: 'null', from: false}); + + quickCompare(t, true, {mode: 'null', from: [1, 2, 3]}); + quickCompare(t, true, {mode: 'null', from: []}); + + quickCompare(t, false, {mode: 'null', from: null}); + quickCompare(t, false, {mode: 'null', from: undefined}); +}); + +t.test(`withResultOfAvailabilityCheck: mode = empty`, t => { + t.plan(10); + + quickThrows(t, {mode: 'empty', from: 'truthy string'}); + quickThrows(t, {mode: 'empty', from: 123}); + quickThrows(t, {mode: 'empty', from: true}); + + quickThrows(t, {mode: 'empty', from: ''}); + quickThrows(t, {mode: 'empty', from: 0}); + quickThrows(t, {mode: 'empty', from: false}); + + quickCompare(t, true, {mode: 'empty', from: [1, 2, 3]}); + quickCompare(t, false, {mode: 'empty', from: []}); + + quickCompare(t, false, {mode: 'empty', from: null}); + quickCompare(t, false, {mode: 'empty', from: undefined}); +}); + +t.test(`withResultOfAvailabilityCheck: mode = falsy`, t => { + t.plan(10); + + quickCompare(t, true, {mode: 'falsy', from: 'truthy string'}); + quickCompare(t, true, {mode: 'falsy', from: 123}); + quickCompare(t, true, {mode: 'falsy', from: true}); + + quickCompare(t, false, {mode: 'falsy', from: ''}); + quickCompare(t, false, {mode: 'falsy', from: 0}); + quickCompare(t, false, {mode: 'falsy', from: false}); + + quickCompare(t, true, {mode: 'falsy', from: [1, 2, 3]}); + quickCompare(t, false, {mode: 'falsy', from: []}); + + quickCompare(t, false, {mode: 'falsy', from: null}); + quickCompare(t, false, {mode: 'falsy', from: undefined}); +}); + +t.test(`withResultOfAvailabilityCheck: default mode`, t => { + t.plan(1); + + const template = withResultOfAvailabilityCheck({ + from: 'foo', + }); + + t.match(template.toDescription(), { + inputMapping: { + from: input.dependency('foo'), + mode: input.value('null'), + }, + }); +}); + +t.test(`withResultOfAvailabilityCheck: validate static inputs`, t => { + t.plan(5); + + t.throws( + () => withResultOfAvailabilityCheck({}), + {message: `Errors in input options passed to withResultOfAvailabilityCheck`, errors: [ + {message: `Required these inputs: from`}, + ]}); + + t.doesNotThrow(() => + withResultOfAvailabilityCheck({ + from: 'dependency1', + mode: 'dependency2', + })); + + t.doesNotThrow(() => + withResultOfAvailabilityCheck({ + from: input.value('some static value'), + mode: input.value('null'), + })); + + t.throws( + () => withResultOfAvailabilityCheck({ + from: 'foo', + mode: input.value('invalid'), + }), + {message: `Errors in input options passed to withResultOfAvailabilityCheck`, errors: [ + {message: `mode: Expected one of null empty falsy, got invalid`}, + ]}); + + t.throws(() => + withResultOfAvailabilityCheck({ + from: input.value(null), + mode: input.value(null), + }), + {message: `Errors in input options passed to withResultOfAvailabilityCheck`, errors: [ + {message: `mode: Expected a value, got null`}, + ]}); +}); + +t.test(`withResultOfAvailabilityCheck: validate dynamic inputs`, t => { + t.plan(2); + + t.throws( + () => composite.expose.compute({ + from: 'apple', + mode: 'banana', + }), + {message: `Error computing composition`, cause: + {message: `Error computing composition withResultOfAvailabilityCheck`, cause: + {message: `Errors in input values provided to withResultOfAvailabilityCheck`, errors: [ + {message: `mode: Expected one of null empty falsy, got banana`}, + ]}}}); + + t.throws( + () => composite.expose.compute({ + from: null, + mode: null, + }), + {message: `Error computing composition`, cause: + {message: `Error computing composition withResultOfAvailabilityCheck`, cause: + {message: `Errors in input values provided to withResultOfAvailabilityCheck`, errors: [ + {message: `mode: Expected a value, got null`}, + ]}}}); +}); diff --git a/test/unit/data/composite/data/withPropertiesFromObject.js b/test/unit/data/composite/data/withPropertiesFromObject.js new file mode 100644 index 00000000..ead1b9b2 --- /dev/null +++ b/test/unit/data/composite/data/withPropertiesFromObject.js @@ -0,0 +1,248 @@ +import t from 'tap'; + +import {compositeFrom, input} from '#composite'; +import {exposeDependency} from '#composite/control-flow'; +import {withPropertiesFromObject} from '#composite/data'; + +const composite = compositeFrom({ + compose: false, + + steps: [ + withPropertiesFromObject({ + object: 'object', + properties: 'properties', + }), + + exposeDependency({dependency: '#object'}), + ], +}); + +t.test(`withPropertiesFromObject: basic behavior`, t => { + t.plan(4); + + t.match(composite, { + expose: { + dependencies: ['object', 'properties'], + }, + }); + + t.same( + composite.expose.compute({ + object: {foo: 'bar', bim: 'BOOM', bam: 'baz'}, + properties: ['foo', 'bim'], + }), + {foo: 'bar', bim: 'BOOM'}); + + t.same( + composite.expose.compute({ + object: {value1: 'uwah', value2: 'arah'}, + properties: ['value1', 'value3'], + }), + {value1: 'uwah', value3: null}); + + t.same( + composite.expose.compute({ + object: null, + properties: ['ohMe', 'ohMy', 'ohDear'], + }), + {ohMe: null, ohMy: null, ohDear: null}); +}); + +t.test(`withPropertiesFromObject: output shapes & values`, t => { + t.plan(2 * 2 * 3 ** 2); + + const dependencies = { + ['object_dependency']: + {foo: 'apple', bar: 'banana', baz: 'orange'}, + [input('object_neither')]: + {foo: 'koala', bar: 'okapi', baz: 'mongoose'}, + ['properties_dependency']: + ['foo', 'bar', 'missing1'], + [input('properties_neither')]: + ['foo', 'baz', 'missing3'], + }; + + const mapLevel1 = [ + [input.value('prefix_value'), [ + ['object_dependency', [ + ['properties_dependency', { + '#object': {foo: 'apple', bar: 'banana', missing1: null}, + }], + [input.value(['bar', 'baz', 'missing2']), { + '#prefix_value.bar': 'banana', + '#prefix_value.baz': 'orange', + '#prefix_value.missing2': null, + }], + [input('properties_neither'), { + '#object': {foo: 'apple', baz: 'orange', missing3: null}, + }]]], + + [input.value({foo: 'ouh', bar: 'rah', baz: 'nyu'}), [ + ['properties_dependency', { + '#object': {foo: 'ouh', bar: 'rah', missing1: null}, + }], + [input.value(['bar', 'baz', 'missing2']), { + '#prefix_value.bar': 'rah', + '#prefix_value.baz': 'nyu', + '#prefix_value.missing2': null, + }], + [input('properties_neither'), { + '#object': {foo: 'ouh', baz: 'nyu', missing3: null}, + }]]], + + [input('object_neither'), [ + ['properties_dependency', { + '#object': {foo: 'koala', bar: 'okapi', missing1: null}, + }], + [input.value(['bar', 'baz', 'missing2']), { + '#prefix_value.bar': 'okapi', + '#prefix_value.baz': 'mongoose', + '#prefix_value.missing2': null, + }], + [input('properties_neither'), { + '#object': {foo: 'koala', baz: 'mongoose', missing3: null}, + }]]]]], + + [input.value(null), [ + ['object_dependency', [ + ['properties_dependency', { + '#object': {foo: 'apple', bar: 'banana', missing1: null}, + }], + [input.value(['bar', 'baz', 'missing2']), { + '#object_dependency.bar': 'banana', + '#object_dependency.baz': 'orange', + '#object_dependency.missing2': null, + }], + [input('properties_neither'), { + '#object': {foo: 'apple', baz: 'orange', missing3: null}, + }]]], + + [input.value({foo: 'ouh', bar: 'rah', baz: 'nyu'}), [ + ['properties_dependency', { + '#object': {foo: 'ouh', bar: 'rah', missing1: null}, + }], + [input.value(['bar', 'baz', 'missing2']), { + '#object.bar': 'rah', + '#object.baz': 'nyu', + '#object.missing2': null, + }], + [input('properties_neither'), { + '#object': {foo: 'ouh', baz: 'nyu', missing3: null}, + }]]], + + [input('object_neither'), [ + ['properties_dependency', { + '#object': {foo: 'koala', bar: 'okapi', missing1: null}, + }], + [input.value(['bar', 'baz', 'missing2']), { + '#object.bar': 'okapi', + '#object.baz': 'mongoose', + '#object.missing2': null, + }], + [input('properties_neither'), { + '#object': {foo: 'koala', baz: 'mongoose', missing3: null}, + }]]]]], + ]; + + for (const [prefixInput, mapLevel2] of mapLevel1) { + for (const [objectInput, mapLevel3] of mapLevel2) { + for (const [propertiesInput, outputDict] of mapLevel3) { + const step = withPropertiesFromObject({ + prefix: prefixInput, + object: objectInput, + properties: propertiesInput, + }); + + quickCheckOutputs(step, outputDict); + } + } + } + + function quickCheckOutputs(step, outputDict) { + t.same( + Object.keys(step.toDescription().outputs), + Object.keys(outputDict)); + + const composite = compositeFrom({ + compose: false, + steps: [step, { + dependencies: Object.keys(outputDict), + compute: dependencies => dependencies, + }], + }); + + t.same( + composite.expose.compute(dependencies), + 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.decorate.indexInSourceArray')]: 2, + message: /Expected a string, got number/, + }, + ]}, + ]}); +}); + +t.test(`withPropertiesFromObject: validate dynamic inputs`, t => { + t.plan(2); + + t.throws( + () => composite.expose.compute({ + object: 'intriguing', + properties: 'onceMore', + }), + {message: `Error computing composition`, cause: + {message: `Error computing composition withPropertiesFromObject`, cause: + {message: `Errors in input values provided to withPropertiesFromObject`, errors: [ + {message: `object: Expected an object, got string`}, + {message: `properties: Expected an array, got string`}, + ]}}}); + + t.throws( + () => composite.expose.compute({ + object: [['abc', 1], ['def', 2], [123, 3]], + properties: ['abc', 'def', 123], + }), + {message: `Error computing composition`, cause: + {message: `Error computing composition withPropertiesFromObject`, cause: + {message: `Errors in input values provided to withPropertiesFromObject`, errors: [ + {message: `object: Expected an object, got array`}, + {message: `properties: Errors validating array items`, errors: [ + { + [Symbol.for('hsmusic.decorate.indexInSourceArray')]: 2, + message: /Expected a string, got number/, + }, + ]}, + ]}}}); +}); diff --git a/test/unit/data/composite/data/withPropertyFromObject.js b/test/unit/data/composite/data/withPropertyFromObject.js new file mode 100644 index 00000000..6a772c36 --- /dev/null +++ b/test/unit/data/composite/data/withPropertyFromObject.js @@ -0,0 +1,122 @@ +import t from 'tap'; + +import {compositeFrom, input} from '#composite'; +import {exposeDependency} from '#composite/control-flow'; +import {withPropertyFromObject} from '#composite/data'; + +t.test(`withPropertyFromObject: basic behavior`, t => { + t.plan(4); + + const composite = compositeFrom({ + compose: false, + + steps: [ + withPropertyFromObject({ + object: 'object', + property: 'property', + }), + + exposeDependency({dependency: '#value'}), + ], + }); + + t.match(composite, { + expose: { + dependencies: ['object', 'property'], + }, + }); + + t.equal(composite.expose.compute({ + object: {foo: 'bar', bim: 'BOOM'}, + property: 'bim', + }), 'BOOM'); + + t.equal(composite.expose.compute({ + object: {value1: 'uwah'}, + property: 'value2', + }), null); + + t.equal(composite.expose.compute({ + object: null, + property: 'oml where did me object go', + }), null); +}); + +t.test(`withPropertyFromObject: output shapes & values`, t => { + t.plan(2 * 3 ** 2); + + const dependencies = { + ['object_dependency']: + {foo: 'apple', bar: 'banana', baz: 'orange'}, + [input('object_neither')]: + {foo: 'koala', bar: 'okapi', baz: 'mongoose'}, + ['property_dependency']: + 'foo', + [input('property_neither')]: + 'baz', + }; + + const mapLevel1 = [ + ['object_dependency', [ + ['property_dependency', { + '#value': 'apple', + }], + [input.value('bar'), { + '#object_dependency.bar': 'banana', + }], + [input('property_neither'), { + '#value': 'orange', + }]]], + + [input.value({foo: 'ouh', bar: 'rah', baz: 'nyu'}), [ + ['property_dependency', { + '#value': 'ouh', + }], + [input.value('bar'), { + '#value': 'rah', + }], + [input('property_neither'), { + '#value': 'nyu', + }]]], + + [input('object_neither'), [ + ['property_dependency', { + '#value': 'koala', + }], + [input.value('bar'), { + '#value': 'okapi', + }], + [input('property_neither'), { + '#value': 'mongoose', + }]]], + ]; + + for (const [objectInput, mapLevel2] of mapLevel1) { + for (const [propertyInput, outputDict] of mapLevel2) { + const step = withPropertyFromObject({ + object: objectInput, + property: propertyInput, + }); + + quickCheckOutputs(step, outputDict); + } + } + + function quickCheckOutputs(step, outputDict) { + t.same( + Object.keys(step.toDescription().outputs), + Object.keys(outputDict)); + + const composite = compositeFrom({ + compose: false, + steps: [step, { + dependencies: Object.keys(outputDict), + compute: dependencies => dependencies, + }], + }); + + t.same( + composite.expose.compute(dependencies), + outputDict); + } +}); -- cgit 1.3.0-6-gf8a5 From bdad6e4abd6c456a65e30b93247964fdd9a1f48c Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 26 Oct 2023 17:47:54 -0300 Subject: test: Track.withAlbum (unit) --- test/unit/data/composite/things/track/withAlbum.js | 144 +++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 test/unit/data/composite/things/track/withAlbum.js (limited to 'test/unit/data/composite') diff --git a/test/unit/data/composite/things/track/withAlbum.js b/test/unit/data/composite/things/track/withAlbum.js new file mode 100644 index 00000000..30f8cc5d --- /dev/null +++ b/test/unit/data/composite/things/track/withAlbum.js @@ -0,0 +1,144 @@ +import t from 'tap'; + +import {compositeFrom, input} from '#composite'; +import {exposeConstant, exposeDependency} from '#composite/control-flow'; +import {withAlbum} from '#composite/things/track'; + +t.test(`withAlbum: basic behavior`, t => { + t.plan(3); + + const composite = compositeFrom({ + compose: false, + steps: [ + withAlbum(), + exposeDependency({dependency: '#album'}), + ], + }); + + t.match(composite, { + expose: { + dependencies: ['albumData', 'this'], + }, + }); + + const fakeTrack1 = {directory: 'foo'}; + const fakeTrack2 = {directory: 'bar'}; + const fakeAlbum = {directory: 'baz', tracks: [fakeTrack1]}; + + t.equal( + composite.expose.compute({ + albumData: [fakeAlbum], + this: fakeTrack1, + }), + fakeAlbum); + + t.equal( + composite.expose.compute({ + albumData: [fakeAlbum], + this: fakeTrack2, + }), + null); +}); + +t.test(`withAlbum: early exit conditions (notFoundMode: null)`, t => { + t.plan(4); + + const composite = compositeFrom({ + compose: false, + steps: [ + withAlbum(), + exposeConstant({ + value: input.value('bimbam'), + }), + ], + }); + + const fakeTrack1 = {directory: 'foo'}; + const fakeTrack2 = {directory: 'bar'}; + const fakeAlbum = {directory: 'baz', tracks: [fakeTrack1]}; + + t.equal( + composite.expose.compute({ + albumData: [fakeAlbum], + this: fakeTrack1, + }), + 'bimbam', + `does not early exit if albumData is present and contains the track`); + + t.equal( + composite.expose.compute({ + albumData: [fakeAlbum], + this: fakeTrack2, + }), + 'bimbam', + `does not early exit if albumData is present and does not contain the track`); + + t.equal( + composite.expose.compute({ + albumData: [], + this: fakeTrack1, + }), + 'bimbam', + `does not early exit if albumData is empty array`); + + t.equal( + composite.expose.compute({ + albumData: null, + this: fakeTrack1, + }), + null, + `early exits if albumData is null`); +}); + +t.test(`withAlbum: early exit conditions (notFoundMode: exit)`, t => { + t.plan(4); + + const composite = compositeFrom({ + compose: false, + steps: [ + withAlbum({ + notFoundMode: input.value('exit'), + }), + + exposeConstant({ + value: input.value('bimbam'), + }), + ], + }); + + const fakeTrack1 = {directory: 'foo'}; + const fakeTrack2 = {directory: 'bar'}; + const fakeAlbum = {directory: 'baz', tracks: [fakeTrack1]}; + + t.equal( + composite.expose.compute({ + albumData: [fakeAlbum], + this: fakeTrack1, + }), + 'bimbam', + `does not early exit if albumData is present and contains the track`); + + t.equal( + composite.expose.compute({ + albumData: [fakeAlbum], + this: fakeTrack2, + }), + null, + `early exits if albumData is present and does not contain the track`); + + t.equal( + composite.expose.compute({ + albumData: [], + this: fakeTrack1, + }), + null, + `early exits if albumData is empty array`); + + t.equal( + composite.expose.compute({ + albumData: null, + this: fakeTrack1, + }), + null, + `early exits if albumData is null`); +}); -- cgit 1.3.0-6-gf8a5 From 5f740046562c85ab4e00063037ddf3af5a545279 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 26 Oct 2023 19:05:39 -0300 Subject: data, test: withResultOfAvailabilityCheck: index mode --- .../control-flow/withResultOfAvailabilityCheck.js | 32 ++++++++++++++++++---- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'test/unit/data/composite') diff --git a/test/unit/data/composite/control-flow/withResultOfAvailabilityCheck.js b/test/unit/data/composite/control-flow/withResultOfAvailabilityCheck.js index 4c4be04a..2bcabb4f 100644 --- a/test/unit/data/composite/control-flow/withResultOfAvailabilityCheck.js +++ b/test/unit/data/composite/control-flow/withResultOfAvailabilityCheck.js @@ -38,7 +38,7 @@ const quickThrows = (t, {from, mode}) => t.throws(() => composite.expose.compute({from, mode})); t.test(`withResultOfAvailabilityCheck: mode = null`, t => { - t.plan(10); + t.plan(11); quickCompare(t, true, {mode: 'null', from: 'truthy string'}); quickCompare(t, true, {mode: 'null', from: 123}); @@ -46,6 +46,7 @@ t.test(`withResultOfAvailabilityCheck: mode = null`, t => { quickCompare(t, true, {mode: 'null', from: ''}); quickCompare(t, true, {mode: 'null', from: 0}); + quickCompare(t, true, {mode: 'null', from: -1}); quickCompare(t, true, {mode: 'null', from: false}); quickCompare(t, true, {mode: 'null', from: [1, 2, 3]}); @@ -56,7 +57,7 @@ t.test(`withResultOfAvailabilityCheck: mode = null`, t => { }); t.test(`withResultOfAvailabilityCheck: mode = empty`, t => { - t.plan(10); + t.plan(11); quickThrows(t, {mode: 'empty', from: 'truthy string'}); quickThrows(t, {mode: 'empty', from: 123}); @@ -64,6 +65,7 @@ t.test(`withResultOfAvailabilityCheck: mode = empty`, t => { quickThrows(t, {mode: 'empty', from: ''}); quickThrows(t, {mode: 'empty', from: 0}); + quickThrows(t, {mode: 'empty', from: -1}); quickThrows(t, {mode: 'empty', from: false}); quickCompare(t, true, {mode: 'empty', from: [1, 2, 3]}); @@ -74,7 +76,7 @@ t.test(`withResultOfAvailabilityCheck: mode = empty`, t => { }); t.test(`withResultOfAvailabilityCheck: mode = falsy`, t => { - t.plan(10); + t.plan(11); quickCompare(t, true, {mode: 'falsy', from: 'truthy string'}); quickCompare(t, true, {mode: 'falsy', from: 123}); @@ -82,6 +84,7 @@ t.test(`withResultOfAvailabilityCheck: mode = falsy`, t => { quickCompare(t, false, {mode: 'falsy', from: ''}); quickCompare(t, false, {mode: 'falsy', from: 0}); + quickCompare(t, true, {mode: 'falsy', from: -1}); quickCompare(t, false, {mode: 'falsy', from: false}); quickCompare(t, true, {mode: 'falsy', from: [1, 2, 3]}); @@ -91,6 +94,25 @@ t.test(`withResultOfAvailabilityCheck: mode = falsy`, t => { quickCompare(t, false, {mode: 'falsy', from: undefined}); }); +t.test(`withResultOfAvailabilityCheck: mode = index`, t => { + t.plan(11); + + quickCompare(t, false, {mode: 'index', from: 'truthy string'}); + quickCompare(t, true, {mode: 'index', from: 123}); + quickCompare(t, false, {mode: 'index', from: true}); + + quickCompare(t, false, {mode: 'index', from: ''}); + quickCompare(t, true, {mode: 'index', from: 0}); + quickCompare(t, false, {mode: 'index', from: -1}); + quickCompare(t, false, {mode: 'index', from: false}); + + quickCompare(t, false, {mode: 'index', from: [1, 2, 3]}); + quickCompare(t, false, {mode: 'index', from: []}); + + quickCompare(t, false, {mode: 'index', from: null}); + quickCompare(t, false, {mode: 'index', from: undefined}); +}); + t.test(`withResultOfAvailabilityCheck: default mode`, t => { t.plan(1); @@ -133,7 +155,7 @@ t.test(`withResultOfAvailabilityCheck: validate static inputs`, t => { mode: input.value('invalid'), }), {message: `Errors in input options passed to withResultOfAvailabilityCheck`, errors: [ - {message: `mode: Expected one of null empty falsy, got invalid`}, + {message: `mode: Expected one of null empty falsy index, got invalid`}, ]}); t.throws(() => @@ -157,7 +179,7 @@ t.test(`withResultOfAvailabilityCheck: validate dynamic inputs`, t => { {message: `Error computing composition`, cause: {message: `Error computing composition withResultOfAvailabilityCheck`, cause: {message: `Errors in input values provided to withResultOfAvailabilityCheck`, errors: [ - {message: `mode: Expected one of null empty falsy, got banana`}, + {message: `mode: Expected one of null empty falsy index, got banana`}, ]}}}); t.throws( -- cgit 1.3.0-6-gf8a5