Skip to content

Commit 0e7d935

Browse files
committed
Merge branch 'fix/custom-iterator'
Fixes #9 Merges #10
2 parents 727652a + 9e49218 commit 0e7d935

File tree

7 files changed

+51
-4
lines changed

7 files changed

+51
-4
lines changed

chai-iterator.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
}));
4545

4646
Assertion.addMethod('from', iterateMethod('to begin iteration with', function(exp) {
47-
return slice(this._obj, exp.length);
47+
return slice(this._obj[Symbol.iterator](), exp.length);
4848
}));
4949

5050
Assertion.addMethod('until', iterateMethod('to end iteration with', function(exp) {
@@ -259,7 +259,7 @@
259259
assert(utils.flag(this, 'iterate'), 'the iterate flag must be set');
260260
new Assertion(iterable).iterable;
261261

262-
var exp = slice(iterable);
262+
var exp = slice(iterable[Symbol.iterator]());
263263
var act = getActual.call(this, exp);
264264

265265
var deep = utils.flag(this, 'deep') ? ' deep' : '';
@@ -305,12 +305,11 @@
305305
});
306306
}
307307

308-
function slice(iterable, stop) {
308+
function slice(it, stop) {
309309
stop = stop == null ? Infinity : stop;
310310

311311
var result = [];
312312
var max = stop - 1;
313-
var it = iterable[Symbol.iterator]();
314313
var step = it.next();
315314

316315
for (var i = 0; i <= max && !step.done; i++) {

test/assert/iterate-from.coffee

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{assert} = require 'chai'
2+
customIterableFactory = require '../fixtures/custom'
23
err = require '../helpers/err'
34

45
describe 'assert: iteratesFrom(value, expected, [message])', ->
@@ -82,3 +83,10 @@ describe 'assert: doesNotDeepIterateFrom(value, expected, [message])', ->
8283

8384
it 'throws', ->
8485
err -> assert.doesNotDeepIterateFrom([{id: 1}, {id: 2}], [{id: 1}])
86+
87+
context 'iterator returned by @@iterator is not itself iterable', ->
88+
89+
it 'works correctly', ->
90+
iterable = customIterableFactory();
91+
err -> assert.iteratesFrom(iterable, [2, 3]);
92+
assert.iteratesFrom(iterable, [0, 1]);

test/assert/iterate-over.coffee

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{assert} = require 'chai'
2+
customIterableFactory = require '../fixtures/custom'
23
err = require '../helpers/err'
34

45
describe 'assert: iteratesOver(value, expected, [message])', ->
@@ -89,3 +90,10 @@ describe 'assert: doesNotDeepIterateOver(value, expected, [message])', ->
8990

9091
it 'throws', ->
9192
err -> assert.doesNotDeepIterateOver([{id: 1}, {id: 2}], [{id: 1}, {id: 2}])
93+
94+
context 'iterator returned by @@iterator is not itself iterable', ->
95+
96+
it 'works correctly', ->
97+
iterable = customIterableFactory();
98+
err -> assert.iteratesOver(iterable, [2, 3, 5]);
99+
assert.iteratesOver(iterable, [0, 1, 2]);

test/assert/iterate-until.coffee

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{assert} = require 'chai'
2+
customIterableFactory = require '../fixtures/custom'
23
err = require '../helpers/err'
34

45
describe 'assert: iteratesUntil(value, expected, [message])', ->
@@ -82,3 +83,10 @@ describe 'assert: doesNotDeepIterateUntil(value, expected, [message])', ->
8283

8384
it 'throws', ->
8485
err -> assert.doesNotDeepIterateUntil([{id: 1}, {id: 2}], [{id: 2}])
86+
87+
context 'iterator returned by @@iterator is not itself iterable', ->
88+
89+
it 'works correctly', ->
90+
iterable = customIterableFactory();
91+
err -> assert.iteratesUntil(iterable, [3, 5]);
92+
assert.iteratesUntil(iterable, [1, 2]);

test/expect-should/iterate-from.coffee

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
err = require '../helpers/err'
2+
customIterableFactory = require '../fixtures/custom'
23

34
describe 'expect/should: iterate.from(iterable)', ->
45

@@ -35,3 +36,10 @@ describe 'expect/should: iterate.from(iterable)', ->
3536
it 'throws whether negated or not', ->
3637
err -> [2, 3, 5].should.from [2, 3]
3738
err -> [2, 3, 5].should.not.from [3, 5]
39+
40+
context 'iterator returned by @@iterator is not itself iterable', ->
41+
42+
it 'works correctly', ->
43+
iterable = customIterableFactory();
44+
err -> iterable.should.iterate.from [2, 3]
45+
iterable.should.iterate.from [0, 1]

test/expect-should/iterate-over.coffee

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
err = require '../helpers/err'
2+
customIterableFactory = require '../fixtures/custom'
23

34
describe 'expect/should: iterate.over(iterable)', ->
45

@@ -37,3 +38,10 @@ describe 'expect/should: iterate.over(iterable)', ->
3738
it 'throws whether negated or not', ->
3839
err -> [2, 3, 5].should.over [2, 3, 5]
3940
err -> [2, 3, 5].should.not.over [2, 3, 6]
41+
42+
context 'iterator returned by @@iterator is not itself iterable', ->
43+
44+
it 'works correctly', ->
45+
iterable = customIterableFactory();
46+
err -> iterable.should.iterate.over [2, 3, 5]
47+
iterable.should.iterate.over [0, 1, 2]

test/expect-should/iterate-until.coffee

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
err = require '../helpers/err'
2+
customIterableFactory = require '../fixtures/custom'
23

34
describe 'expect/should: iterate.until(iterable)', ->
45

@@ -35,3 +36,10 @@ describe 'expect/should: iterate.until(iterable)', ->
3536
it 'throws whether negated or not', ->
3637
err -> [2, 3, 5].should.until [3, 5]
3738
err -> [2, 3, 5].should.not.until [2, 3]
39+
40+
context 'iterator returned by @@iterator is not itself iterable', ->
41+
42+
it 'works correctly', ->
43+
iterable = customIterableFactory();
44+
err -> iterable.should.iterate.until [3, 5]
45+
iterable.should.iterate.until [1, 2]

0 commit comments

Comments
 (0)