Files: 019de9e4266b7574ce6cf22a56c1cb051638fe1a / test / take.js
2683 bytesRaw
1 | var pull = require('../') |
2 | var test = require('tape') |
3 | |
4 | test('through - onEnd', function (t) { |
5 | t.plan(2) |
6 | var values = [1,2,3,4,5,6,7,8,9,10] |
7 | |
8 | //read values, and then just stop! |
9 | //this is a subtle edge case for take! |
10 | |
11 | //I did have a thing that used this edge case, |
12 | //but it broke take, actually. so removing it. |
13 | //TODO: fix that thing - was a test for some level-db stream thing.... |
14 | |
15 | // pull.Source(function () { |
16 | // return function (end, cb) { |
17 | // if(end) cb(end) |
18 | // else if(values.length) |
19 | // cb(null, values.shift()) |
20 | // else console.log('drop') |
21 | // } |
22 | // })() |
23 | |
24 | pull( |
25 | pull.values(values), |
26 | pull.take(10), |
27 | pull.through(null, function (err) { |
28 | console.log('end') |
29 | t.ok(true) |
30 | process.nextTick(function () { |
31 | t.end() |
32 | }) |
33 | }), |
34 | pull.collect(function (err, ary) { |
35 | console.log(ary) |
36 | t.ok(true) |
37 | }) |
38 | ) |
39 | }) |
40 | |
41 | |
42 | test('take - exclude last (default)', function (t) { |
43 | pull( |
44 | pull.values([1,2,3,4,5,6,7,8,9,10]), |
45 | pull.take(function(n) {return n<5}), |
46 | pull.collect(function (err, four) { |
47 | t.deepEqual(four, [1,2,3,4]) |
48 | t.end() |
49 | }) |
50 | ) |
51 | }) |
52 | test('take - include last', function (t) { |
53 | pull( |
54 | pull.values([1,2,3,4,5,6,7,8,9,10]), |
55 | pull.take(function(n) {return n<5}, {last: true}), |
56 | pull.collect(function (err, five) { |
57 | t.deepEqual(five, [1,2,3,4,5]) |
58 | t.end() |
59 | }) |
60 | ) |
61 | }) |
62 | |
63 | test('take 5 causes 5 reads upstream', function (t) { |
64 | var reads = 0 |
65 | pull( |
66 | pull.values([1,2,3,4,5,6,7,8,9,10]), |
67 | function (read) { |
68 | return function (end, cb) { |
69 | if (end !== true) reads++ |
70 | console.log(reads, end) |
71 | read(end, cb) |
72 | } |
73 | }, |
74 | pull.take(5), |
75 | pull.collect(function (err, five) { |
76 | t.deepEqual(five, [1,2,3,4,5]) |
77 | process.nextTick(function() { |
78 | t.equal(reads, 5) |
79 | t.end() |
80 | }) |
81 | }) |
82 | ) |
83 | }) |
84 | |
85 | test("take doesn't abort until the last read", function (t) { |
86 | |
87 | var aborted = false |
88 | |
89 | var ary = [1,2,3,4,5], i = 0 |
90 | |
91 | var read = pull( |
92 | function (abort, cb) { |
93 | if(abort) cb(aborted = true) |
94 | else if(i > ary.length) cb(true) |
95 | else cb(null, ary[i++]) |
96 | }, |
97 | pull.take(function (d) { |
98 | return d < 3 |
99 | }, {last: true}) |
100 | ) |
101 | |
102 | read(null, function (_, d) { |
103 | t.notOk(aborted, "hasn't aborted yet") |
104 | read(null, function (_, d) { |
105 | t.notOk(aborted, "hasn't aborted yet") |
106 | read(null, function (_, d) { |
107 | t.notOk(aborted, "hasn't aborted yet") |
108 | read(null, function (end, d) { |
109 | t.ok(end, 'stream ended') |
110 | t.equal(d, undefined, 'data undefined') |
111 | t.ok(aborted, "has aborted by now") |
112 | t.end() |
113 | }) |
114 | }) |
115 | }) |
116 | }) |
117 | |
118 | }) |
119 | |
120 | |
121 |
Built with git-ssb-web