Skip to content

Commit

Permalink
add tests as a method
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanong committed Dec 4, 2013
1 parent ec8d3bb commit 0357efd
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 21 deletions.
10 changes: 9 additions & 1 deletion README.md
Expand Up @@ -4,7 +4,7 @@ Concatenate a readable stream's data into a single array.

You may also be interested in:

- [cursor-methods](https://github.com/jonathanong/cursor-methods)
- [raw-body](https://github.com/stream-utils/raw-body) for strings

## API

Expand Down Expand Up @@ -42,6 +42,14 @@ stream.toArray = toArray
var arr = yield stream.toArray()
```

If you want to return a buffer, just use `Buffer.concat(arr)`

```js
var stream = new Stream.Readable()
var arr = yield toArray(stream)
var buffer = Buffer.concat(arr)
```

## License

The MIT License (MIT)
Expand Down
78 changes: 58 additions & 20 deletions test.js
Expand Up @@ -17,31 +17,69 @@ function emptyStream() {
}

describe('Stream To Array', function () {
it('should work', function (done) {
toArray(fs.createReadStream(file), function (err, arr) {
if (err)
return done(err)
describe('as a function', function () {
it('should work', function (done) {
toArray(fs.createReadStream(file), function (err, arr) {
if (err)
return done(err)

assert.ok(Array.isArray(arr))
assert.ok(arr.length)
assert.ok(Array.isArray(arr))
assert.ok(arr.length)

done()
done()
})
})

it('should work as a yieldable', function (done) {
co(function* () {
var arr = yield toArray(fs.createReadStream(file))
assert.ok(Array.isArray(arr))
assert.ok(arr.length)
})(done)
})
})

it('should work as a yieldable', function (done) {
co(function* () {
var arr = yield toArray(fs.createReadStream(file))
assert.ok(Array.isArray(arr))
assert.ok(arr.length)
})(done)
it('should work as a yieldable with zalgo', function (done) {
co(function* () {
var arr = yield toArray(emptyStream())
assert.ok(Array.isArray(arr))
assert.equal(arr.length, 0)
})(done)
})
})

it('should work as a yieldable with zalgo', function (done) {
co(function* () {
var arr = yield toArray(emptyStream())
assert.ok(Array.isArray(arr))
assert.equal(arr.length, 0)
})(done)
describe('as a method', function () {
it('should work', function (done) {
var stream = fs.createReadStream(file)
stream.toArray = toArray
stream.toArray(function (err, arr) {
if (err)
return done(err)

assert.ok(Array.isArray(arr))
assert.ok(arr.length)

done()
})
})

it('should work as a yieldable', function (done) {
co(function* () {
var stream = fs.createReadStream(file)
stream.toArray = toArray
var arr = yield stream.toArray()
assert.ok(Array.isArray(arr))
assert.ok(arr.length)
})(done)
})

it('should work as a yieldable with zalgo', function (done) {
co(function* () {
var stream = emptyStream()
stream.toArray = toArray
var arr = yield stream.toArray()
assert.ok(Array.isArray(arr))
assert.equal(arr.length, 0)
})(done)
})
})
})

0 comments on commit 0357efd

Please sign in to comment.