Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 7, 2016
1 parent 2dc9259 commit e89bea4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"node": ">=0.12.0"
},
"scripts": {
"test": "xo && ava test.js && echo unicorns | node test-real.js"
"test": "xo && ava test.js test-buffer.js && echo unicorns | node test-real.js"
},
"files": [
"index.js"
Expand All @@ -29,7 +29,7 @@
"read"
],
"devDependencies": {
"ava": "^0.2.0",
"ava": "*",
"buffer-equals": "^1.0.3",
"xo": "*"
}
Expand Down
21 changes: 21 additions & 0 deletions test-buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import test from 'ava';
import bufferEquals from 'buffer-equals';
import fn from './';

test.serial('get stdin', async t => {
t.plan(2);
process.stdin.isTTY = false;

const promise = fn.buffer();
process.stdin.push(new Buffer('uni'));
process.stdin.push(new Buffer('corns'));
process.stdin.emit('end');
const data = await promise;
t.true(bufferEquals(data, new Buffer('unicorns')));
t.is(data.toString(), 'unicorns');
});

test.serial('get empty buffer when no stdin', async t => {
process.stdin.isTTY = true;
t.true(bufferEquals(await fn.buffer(), new Buffer('')));
});
34 changes: 6 additions & 28 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,17 @@
import test from 'ava';
import bufferEquals from 'buffer-equals';
import fn from './';

test.serial('get stdin', async t => {
t.plan(1);
process.stdin.isTTY = false;

setImmediate(() => {
process.stdin.push('unicorns');
process.stdin.emit('end');
});

t.is((await fn()).trim(), 'unicorns');
const promise = fn();
process.stdin.push('uni');
process.stdin.push('corns');
process.stdin.emit('end');
t.is((await promise).trim(), 'unicorns');
});

test.serial('get empty string when no stdin', async t => {
process.stdin.isTTY = true;
t.is(await fn(), '');
});

test.serial('get stdin as a buffer', t => {
process.stdin.isTTY = false;

const promise = fn.buffer(data => {
t.true(bufferEquals(data, new Buffer('unicorns')));
t.is(data.toString().trim(), 'unicorns');
});

process.stdin.push(new Buffer('unicorns'));
process.stdin.emit('end');

return promise;
});

test.serial('get empty buffer when no stdin', async t => {
process.stdin.isTTY = true;

t.true(bufferEquals(await fn.buffer(), new Buffer('')));
});

1 comment on commit e89bea4

@moos
Copy link

@moos moos commented on e89bea4 Jan 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're welcome! ;)

Please sign in to comment.