forked from tensorflow/tfjs-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjasmine_util.ts
110 lines (100 loc) · 3.12 KB
/
jasmine_util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* @license
* Copyright 2017 Google Inc. All Rights Reserved.
* 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.
* =============================================================================
*/
import {ENV, Environment, Features} from './environment';
import {MathBackendCPU} from './kernels/backend_cpu';
import {MathBackendWebGL} from './kernels/backend_webgl';
export function describeWithFlags(
name: string, constraints: Features, tests: () => void) {
const envFeatures = TEST_ENV_FEATURES.filter(f => {
return Object.keys(constraints).every(key => {
// tslint:disable-next-line:no-any
return (constraints as any)[key] === (f as any)[key];
});
});
envFeatures.forEach(features => {
const testName = name + ' ' + JSON.stringify(features);
executeTests(testName, tests, features);
});
}
let BEFORE_ALL = (features: Features) => {
ENV.registerBackend('test-webgl', () => new MathBackendWebGL());
ENV.registerBackend('test-cpu', () => new MathBackendCPU());
};
let AFTER_ALL = (features: Features) => {
ENV.removeBackend('test-webgl');
ENV.removeBackend('test-cpu');
};
let BEFORE_EACH = (features: Features) => {};
let AFTER_EACH = (features: Features) => {};
let TEST_ENV_FEATURES: Features[] = [
{
'BACKEND': 'test-webgl',
'WEBGL_FLOAT_TEXTURE_ENABLED': true,
'WEBGL_VERSION': 1
},
{
'BACKEND': 'test-webgl',
'WEBGL_FLOAT_TEXTURE_ENABLED': true,
'WEBGL_VERSION': 2
},
{'BACKEND': 'test-cpu'}
// TODO(nsthorat,smilkov): Enable when byte-backed textures are fixed.
// {
// 'BACKEND': 'webgl',
// 'WEBGL_FLOAT_TEXTURE_ENABLED': false,
// 'WEBGL_VERSION': 1
// }
];
export function setBeforeAll(f: (features: Features) => void) {
BEFORE_ALL = f;
}
export function setAfterAll(f: (features: Features) => void) {
AFTER_ALL = f;
}
export function setBeforeEach(f: (features: Features) => void) {
BEFORE_EACH = f;
}
export function setAfterEach(f: (features: Features) => void) {
AFTER_EACH = f;
}
export function setTestEnvFeatures(features: Features[]) {
TEST_ENV_FEATURES = features;
}
function executeTests(testName: string, tests: () => void, features: Features) {
describe(testName, () => {
beforeAll(() => {
ENV.setFeatures(features);
BEFORE_ALL(features);
});
beforeEach(() => {
BEFORE_EACH(features);
if (features && features.BACKEND != null) {
Environment.setBackend(features.BACKEND);
}
ENV.engine.startScope();
});
afterEach(() => {
ENV.engine.endScope(null);
AFTER_EACH(features);
});
afterAll(() => {
AFTER_ALL(features);
ENV.reset();
});
tests();
});
}