Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/test/common/iterator.test.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { Iterable } from '../../common/iterator.js';
8
import { ensureNoDisposablesAreLeakedInTestSuite } from './utils.js';
9
10
suite('Iterable', function () {
11
12
ensureNoDisposablesAreLeakedInTestSuite();
13
14
const customIterable = new class {
15
16
*[Symbol.iterator]() {
17
yield 'one';
18
yield 'two';
19
yield 'three';
20
}
21
};
22
23
test('first', function () {
24
25
assert.strictEqual(Iterable.first([]), undefined);
26
assert.strictEqual(Iterable.first([1]), 1);
27
assert.strictEqual(Iterable.first(customIterable), 'one');
28
assert.strictEqual(Iterable.first(customIterable), 'one'); // fresh
29
});
30
31
test('wrap', function () {
32
assert.deepStrictEqual([...Iterable.wrap(1)], [1]);
33
assert.deepStrictEqual([...Iterable.wrap([1, 2, 3])], [1, 2, 3]);
34
});
35
36
test('every', function () {
37
// Empty iterable should return true (vacuous truth)
38
assert.strictEqual(Iterable.every([], () => false), true);
39
40
// All elements match predicate
41
assert.strictEqual(Iterable.every([2, 4, 6, 8], x => x % 2 === 0), true);
42
assert.strictEqual(Iterable.every([1, 2, 3], x => x > 0), true);
43
44
// Not all elements match predicate
45
assert.strictEqual(Iterable.every([1, 2, 3, 4], x => x % 2 === 0), false);
46
assert.strictEqual(Iterable.every([1, 2, 3], x => x > 2), false);
47
48
// Single element - matches
49
assert.strictEqual(Iterable.every([5], x => x === 5), true);
50
51
// Single element - doesn't match
52
assert.strictEqual(Iterable.every([5], x => x === 6), false);
53
54
// Test index parameter in predicate
55
const numbers = [10, 11, 12, 13];
56
assert.strictEqual(Iterable.every(numbers, (x, i) => x === 10 + i), true);
57
assert.strictEqual(Iterable.every(numbers, (x, i) => i < 2), false);
58
59
// Test early termination - predicate should not be called for all elements
60
let callCount = 0;
61
const result = Iterable.every([1, 2, 3, 4, 5], x => {
62
callCount++;
63
return x < 3;
64
});
65
assert.strictEqual(result, false);
66
assert.strictEqual(callCount, 3); // Should stop at the third element
67
68
// Test with truthy/falsy values
69
assert.strictEqual(Iterable.every([1, 2, 3], x => x), true);
70
assert.strictEqual(Iterable.every([1, 0, 3], x => x), false);
71
assert.strictEqual(Iterable.every(['a', 'b', 'c'], x => x), true);
72
assert.strictEqual(Iterable.every(['a', '', 'c'], x => x), false);
73
});
74
});
75
76