1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
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}`);
});
});
|