forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix compatibility for plugin factories
In v3.0, we changed our plugin interfaces so that all are factories that return objects. We no longer call those with "new" as we did in v2.5. We provided backward compatibility and a deprecation warning to alert applications to update their usage of those interfaces. However, this compatibility shim was broken for ES6 classes, which behaved differently than ES5 constructor functions. This fixes the shim and adds regression tests. The tests were tricky because we use Babel to transpile our tests, but we needed raw, untranspiled code to define a class to test these features. So we use eval and some associated trickery to get exactly the different kinds of plugin registrations we intend to support. These tests are skipped on non-ES6 browsers. Fixes shaka-project#2958 Change-Id: Ife8b63c0d89f4ea0aff085d3a4c156c4eb657604
- Loading branch information
1 parent
40d4451
commit 9ece285
Showing
3 changed files
with
87 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/*! @license | ||
* Shaka Player | ||
* Copyright 2016 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
goog.require('shaka.util.Functional'); | ||
|
||
describe('Functional', () => { | ||
const Functional = shaka.util.Functional; | ||
|
||
function supportsEs6Classes() { | ||
// The callFactory tests should only be run on platforms that support ES6 | ||
// classes. We need to use classes directly to ensure that callFactory is | ||
// working correctly. | ||
try { | ||
eval('class Foo {}'); | ||
return true; | ||
} catch (e) { // eslint-disable-line no-restricted-syntax | ||
return false; | ||
} | ||
} | ||
|
||
filterDescribe('callFactory', supportsEs6Classes, () => { | ||
// All of the following factories/functions/classes create objects with a | ||
// field called "val" with a value of 1. This is a type def to satisfy the | ||
// compiler. | ||
/** @typedef {{val: number}} */ | ||
let DummyObjType; | ||
|
||
// Normally, our tests are transpiled by Babel to allow them to run on all | ||
// browsers. However, that would convert all of these into plain functions, | ||
// which would defeat the purpose. Therefore, we're using eval to make sure | ||
// these get defined in exactly this way. Furthermore, to make sure these | ||
// are returned to names that are in scope of this test suite in strict mode | ||
// (used by Babel), each eval must use an assignment syntax to a dummy | ||
// variable, then return it. | ||
const FactoryFunction = /** @type {function():DummyObjType} */(eval( | ||
'const f = function() { return { val: 1 }; }; f;')); | ||
const FactoryArrowFunction = /** @type {function():DummyObjType} */(eval( | ||
'const f = () => { return { val: 1 }; }; f;')); | ||
const Es5ConstructorFunction = /** @type {function():DummyObjType} */(eval( | ||
'const f = function() { this.val = 1; }; f;')); | ||
const Es6Class = /** @type {function():DummyObjType} */(eval( | ||
'const f = class { constructor() { this.val = 1; } }; f;')); | ||
|
||
it('supports true factory functions', () => { | ||
const obj = Functional.callFactory(FactoryFunction); | ||
expect(obj.val).toBe(1); | ||
}); | ||
|
||
it('supports true factory arrow functions', () => { | ||
const obj = Functional.callFactory(FactoryArrowFunction); | ||
expect(obj.val).toBe(1); | ||
}); | ||
|
||
it('supports ES5 constructor functions', () => { | ||
const obj = Functional.callFactory(Es5ConstructorFunction); | ||
expect(obj.val).toBe(1); | ||
}); | ||
|
||
// Regression test for https://github.com/google/shaka-player/issues/2958 | ||
it('supports ES6 classes', () => { | ||
const obj = Functional.callFactory(Es6Class); | ||
expect(obj.val).toBe(1); | ||
}); | ||
}); | ||
}); |