mirror of
https://github.com/shaka-project/shaka-player.git
synced 2026-06-17 16:26:39 +03:00
11f3347a48
We used to alias static utility methods by assigning the method itself to a local variable. This is not allowed by the new Closure Compiler release that we are adopting, and for good reason. The old compiler did not understand the use of "this" in static methods, but the new one does. And it turns out that when we start using "this" in static methods (see #2532), aliasing the method itself can break everything. When you refer to "this" in a static method, it refers to the class. This is really useful in utility classes that have private static methods they use to perform common tasks. However, just as it ruins the value of "this" to alias an instance method, the same is true of an ES6 class's static method. The fix is to always alias the class instead of the method. The new compiler will simply not let us get away with the old way any more, so regressions after this are unlikely. Issue #2528 (compiler upgrade) Issue #2532 (static "this") Change-Id: Id800d466e639c7cbcf4aa6fbb05114c772a2229f
79 lines
2.1 KiB
JavaScript
79 lines
2.1 KiB
JavaScript
/** @license
|
|
* Copyright 2016 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
describe('ObjectUtils', () => {
|
|
const ObjectUtils = shaka.util.ObjectUtils;
|
|
|
|
describe('cloneObject', () => {
|
|
it('clones values and plain objects', () => {
|
|
expect(ObjectUtils.cloneObject(2)).toBe(2);
|
|
expect(ObjectUtils.cloneObject('foo')).toBe('foo');
|
|
expect(ObjectUtils.cloneObject(false)).toBe(false);
|
|
|
|
let o = {foo: 'bar', count: 123};
|
|
let copy = ObjectUtils.cloneObject(o);
|
|
expect(copy).not.toBe(o);
|
|
expect(copy).toEqual(o);
|
|
|
|
o = [1, 2, undefined, 4, 5];
|
|
copy = ObjectUtils.cloneObject(o);
|
|
expect(copy).not.toBe(o);
|
|
expect(copy).toEqual(o);
|
|
});
|
|
|
|
it('clones nested objects', () => {
|
|
const o = {
|
|
foo: 'bar',
|
|
lorem: 'ipsum',
|
|
dolor: {
|
|
sit: 123,
|
|
amet: ',',
|
|
consectetur: {},
|
|
adipisecing: [1, 2, 3],
|
|
elit: [
|
|
1, null,
|
|
{
|
|
sed: null,
|
|
do_: undefined,
|
|
eiusmod: [],
|
|
},
|
|
],
|
|
},
|
|
player: {
|
|
other: 123,
|
|
},
|
|
};
|
|
const copy = ObjectUtils.cloneObject(o);
|
|
expect(copy).not.toBe(o);
|
|
expect(copy).toEqual(o);
|
|
});
|
|
|
|
it('clones Arrays with non-default length', () => {
|
|
const a = [1, 2, 3];
|
|
a.length = 10;
|
|
const copy = ObjectUtils.cloneObject(a);
|
|
expect(copy).toEqual(a);
|
|
expect(copy.length).toBe(10);
|
|
});
|
|
|
|
it('ignores cyclic objects', () => {
|
|
const o = {foo: 'bar'};
|
|
o['baz'] = o;
|
|
expect(ObjectUtils.cloneObject(o)).toEqual({foo: 'bar', baz: null});
|
|
});
|
|
|
|
it('ignores non-simple Object objects', () => {
|
|
let o = {foo: 1, baz: /foo/g};
|
|
expect(ObjectUtils.cloneObject(o)).toEqual({foo: 1, baz: null});
|
|
|
|
o = {foo: 2, baz: new Date(123)};
|
|
expect(ObjectUtils.cloneObject(o)).toEqual({foo: 2, baz: null});
|
|
|
|
o = {foo: 3, baz: document.createElement('div')};
|
|
expect(ObjectUtils.cloneObject(o)).toEqual({foo: 3, baz: null});
|
|
});
|
|
});
|
|
});
|