1 |
|
2 | var ps = require('../');
|
3 | var B = require('bluebird');
|
4 | var fs = require('fs');
|
5 | var split = require('split');
|
6 | var path = require('path');
|
7 |
|
8 | var t = require('blue-tape');
|
9 |
|
10 |
|
11 | function lines() {
|
12 | return raw().pipe(split())
|
13 | }
|
14 | function raw() {
|
15 | return fs.createReadStream(path.join(__dirname, 'test.txt'), 'utf8');
|
16 | }
|
17 |
|
18 | function delayer() {
|
19 | return ps.through(function(line) {
|
20 | return this.push(B.delay(1).then(function() {
|
21 | return line ? parseFloat(line) : null;
|
22 | }));
|
23 | });
|
24 | }
|
25 |
|
26 |
|
27 | t.test('ps.wait', function(t) {
|
28 | var last = 0;
|
29 | return ps.wait(lines().pipe(ps.map(function(el) {
|
30 | return B.delay(1).then(function() {
|
31 | if (el) last = el;
|
32 | return el;
|
33 | });
|
34 | }))).then(function() {
|
35 | t.equal(last, "9", 'should wait for the last element')
|
36 | });
|
37 | });
|
38 |
|
39 | t.test('map-wait', function(t) {
|
40 | var last = 0;
|
41 | return lines().pipe(delayer())
|
42 | .map(function(el) {
|
43 | return B.delay(1).then(function() {
|
44 | return (last = el);
|
45 | })
|
46 | }).wait().then(function() {
|
47 | t.equal(last, 9, 'should wait for the last element')
|
48 | });
|
49 | });
|
50 |
|
51 |
|
52 | t.test('combined', function(t) {
|
53 | return lines().pipe(delayer())
|
54 | .map(function(el) {
|
55 | return el * 2;
|
56 | })
|
57 | .filter(function(el) {
|
58 | return el > 4
|
59 | })
|
60 | .reduce(function(acc, el) {
|
61 | return acc + el;
|
62 | })
|
63 | .then(function(sum) {
|
64 | t.equal(sum, 84, 'should map-reduce to correct sum');
|
65 | });
|
66 |
|
67 | });
|
68 |
|
69 | t.test('collect', function(t) {
|
70 | return ps.collect(raw()).then(function(data) {
|
71 | t.equal(data.length, 18, 'test.txt should be 18 bytes long');
|
72 | });
|
73 | });
|