Skip to content

Instantly share code, notes, and snippets.

@axefrog
Last active February 29, 2016 14:05
Show Gist options
  • Select an option

  • Save axefrog/73d1d576c6b3c77a851a to your computer and use it in GitHub Desktop.

Select an option

Save axefrog/73d1d576c6b3c77a851a to your computer and use it in GitHub Desktop.
Utilities for easily running tests against streams of arrays of arrays of streams of streams of... you get the idea. (most.js + mocha + chai)
it('projectStaticsToArrayOfPairStreams() should return an array of streams of key/state objects', () => {
const statics = internals.projectStaticsToArrayOfPairStreams(partitioned.statics);
return deepCapture(statics)
.then(result => {
const expected = [
$([{ key: 'a', state: { x: 1, y: 2 } }, { key: 'a', state: { x: 3, y: 4 } }]),
$([{ key: 'b', state: { x: 5, y: 6 } }, { key: 'b', state: { x: 7, y: 8 } }]),
$([{ key: 'c', state: { } }])
];
assert.deepEqual(expected, result);
});
});
it('projectStreamsToArrayOfStreamsOfPairStreams() should return an array of nested streams of key/state pairs', () => {
const pair$$arr = internals.projectStreamsToArrayOfStreamsOfPairStreams(partitioned.streams);
return deepCapture(pair$$arr)
.then(result => {
const expected = [
$([
$([{ key: 'd', state: {x:10, y:11}}, { key: 'd', state: { x:12, y:13 }}]),
$([{ key: 'd', state: {x:14, y:15}}, { key: 'd', state: { x:16, y:17 }}])
]),
$([
$([{ key: 'e', state: { y: 18 } }])
])
];
assert.deepEqual(expected, result);
});
});
const run = fn => new Promise(resolve => resolve(fn()));
const $ = arr => ((arr.isStream = true), arr);
function deepCapture(source) {
return run(() => {
if(source instanceof Array) return Promise.all(source.map(deepCapture));
if(source instanceof most.Stream) return source
.reduce((acc, x) => (acc.push(Promise.resolve(deepCapture(x))), acc), [])
.then(arr => Promise.all(arr).then($));
return source;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment