Skip to content

Commit

Permalink
Add tests about delegation to the locked stream
Browse files Browse the repository at this point in the history
  • Loading branch information
domenic committed Dec 12, 2014
1 parent 98112b3 commit 0699244
Showing 1 changed file with 64 additions and 1 deletion.
65 changes: 64 additions & 1 deletion reference-implementation/test/exclusive-stream-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,70 @@ test('Using the reader directly on a mundane stream', t => {
});
});

test('Trying to use a released reader', t => {
test('Readers delegate to underlying stream implementations', t => {
t.plan(3 * 3 + 2 * 4);

var rs = new ReadableStream();
var reader = rs.getReader();

testGetter('ready');
testGetter('state');
testGetter('closed');
testMethod('read');
testMethod('cancel');

// Generates 4 assertions
function testGetter(propertyName) {
Object.defineProperty(rs, propertyName, {
get() {
t.pass('overriden ' + propertyName + ' called');
t.equal(this, rs, propertyName + ' called with the correct this value');
return propertyName + ' return value';
}
});
t.equal(reader[propertyName], propertyName + ' return value',
`reader's ${propertyName} returns the return value of the stream's ${propertyName}`);
}

// Generates 5 assertions
function testMethod(methodName) {
var testArgs = ['arg1', 'arg2', 'arg3'];
rs[methodName] = function (...args) {
t.pass('overridden ' + methodName + ' called');
t.deepEqual(args, testArgs, methodName + ' called with the correct arguments');
t.equal(this, rs, methodName + ' called with the correct this value');
return methodName + ' return value';
}
t.equal(reader[methodName](...testArgs), methodName + ' return value',
`reader's ${methodName} returns the return value of the stream's ${methodName}`);
}
});

test('Reading from a reader for an empty stream throws but doesn\'t break anything', t => {
var enqueue;
var rs = new ReadableStream({
start(e) {
enqueue = e;
}
});
var reader = rs.getReader();

t.equal(reader.isActive, true, 'reader is active to start with');
t.equal(reader.state, 'waiting', 'reader state is waiting to start with');
t.throws(() => reader.read(), /TypeError/, 'calling reader.read() throws a TypeError');
t.equal(reader.isActive, true, 'reader is still active');
t.equal(reader.state, 'waiting', 'reader state is still waiting');

enqueue('a');

reader.ready.then(() => {
t.equal(reader.state, 'readable', 'after enqueuing the reader state is readable');
t.equal(reader.read(), 'a', 'the enqueued chunk can be read back through the reader');
t.end();
});
});

test('Trying to use a released reader should fail', t => {
t.plan(6);

var rs = new ReadableStream({
Expand Down

0 comments on commit 0699244

Please sign in to comment.