Files
shaka-player/test/util/iterables_unit.js
T
Jacob Trimble 47533d1173 Add an enumerable() method for loops.
This is a helper to aid in iterating over items.  This returns a list
of objects that contain:
- "item": The current value.
- "prev": The previous value in the list.
- "next": The next value in the list.
- "i": The zero-based index in the list.

Issue #1518

Change-Id: Id18ab977e3ae45dfbfd2b4137a1bffb6e53c6bce
2019-06-27 16:31:42 +00:00

150 lines
4.2 KiB
JavaScript

/**
* @license
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
describe('Iterables', () => {
const Iterables = shaka.util.Iterables;
describe('map', () => {
const map = Iterables.map;
it('works with no items', () => {
const input = new Set([]);
const output = Array.from(map(input, (x) => -x));
expect(output).toEqual([]);
});
it('works with items', () => {
const input = new Set([1, 2, 3]);
const output = Array.from(map(input, (x) => -x));
expect(output).toEqual([-1, -2, -3]);
});
});
describe('every', () => {
const every = Iterables.every;
it('works with no items', () => {
const input = new Set([]);
expect(every(input, (x) => x >= 0)).toBeTruthy();
});
it('works with items', () => {
const input = new Set([0, 1, 2, 3]);
expect(every(input, (x) => x >= 0)).toBeTruthy();
expect(every(input, (x) => x > 0)).toBeFalsy();
});
});
describe('some', () => {
const some = Iterables.some;
it('works with no items', () => {
const input = new Set([]);
expect(some(input, (x) => x >= 2)).toBeFalsy();
});
it('works with items', () => {
const input = new Set([0, 1, 2, 3]);
expect(some(input, (x) => x > 2)).toBeTruthy();
expect(some(input, (x) => x < 0)).toBeFalsy();
});
});
describe('filter', () => {
const filter = Iterables.filter;
it('works with no items', () => {
const input = new Set([]);
expect(filter(input, (x) => x >= 2)).toEqual([]);
});
it('works with items', () => {
const input = new Set([0, 1, 2, 3]);
// Everything
expect(filter(input, (x) => x < 7)).toEqual([0, 1, 2, 3]);
// Some things
expect(filter(input, (x) => x < 2)).toEqual([0, 1]);
// Nothing
expect(filter(input, (x) => x < 0)).toEqual([]);
});
});
describe('enumerate', () => {
function enumerate(it) {
return Array.from(Iterables.enumerate(it));
}
it('works with no items', () => {
expect(enumerate([])).toEqual([]);
});
it('works with one item', () => {
expect(enumerate([999]))
.toEqual([{i: 0, item: 999, prev: undefined, next: undefined}]);
});
it('works with special values', () => {
expect(enumerate([[]]))
.toEqual([{i: 0, item: [], prev: undefined, next: undefined}]);
expect(enumerate([0]))
.toEqual([{i: 0, item: 0, prev: undefined, next: undefined}]);
expect(enumerate([null]))
.toEqual([{i: 0, item: null, prev: undefined, next: undefined}]);
expect(enumerate([undefined]))
.toEqual([{i: 0, item: undefined, prev: undefined, next: undefined}]);
});
it('works with two items', () => {
expect(enumerate([888, 999]))
.toEqual([
{i: 0, item: 888, prev: undefined, next: 999},
{i: 1, item: 999, prev: 888, next: undefined},
]);
});
it('works with three items', () => {
expect(enumerate([777, 888, 999]))
.toEqual([
{i: 0, item: 777, prev: undefined, next: 888},
{i: 1, item: 888, prev: 777, next: 999},
{i: 2, item: 999, prev: 888, next: undefined},
]);
});
it('keeps references', () => {
const expected = [
{a: 'x'},
{b: 'y'},
{c: 'z'},
];
const actual = enumerate(expected);
expect(actual[0].item).toBe(expected[0]);
expect(actual[0].next).toBe(expected[1]);
expect(actual[1].prev).toBe(expected[0]);
expect(actual[1].item).toBe(expected[1]);
expect(actual[1].next).toBe(expected[2]);
expect(actual[2].prev).toBe(expected[1]);
expect(actual[2].item).toBe(expected[2]);
});
});
});