forked from tensorflow/tfjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment_test.ts
More file actions
156 lines (121 loc) · 4.92 KB
/
environment_test.ts
File metadata and controls
156 lines (121 loc) · 4.92 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* @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 * as environment from './environment';
import {Environment} from './environment';
describe('initializes flags from the url', () => {
// Silence console.warns for these tests.
beforeAll(() => spyOn(console, 'warn').and.returnValue(null));
it('no overrides one registered flag', () => {
spyOn(environment, 'getQueryParams').and.returnValue({});
const global = {location: {search: ''}};
const env = new Environment(global);
env.registerFlag('FLAG1', () => false);
expect(env.get('FLAG1')).toBe(false);
});
it('one unregistered flag', () => {
spyOn(environment, 'getQueryParams').and.returnValue({
'tfjsflags': 'FLAG1:true'
});
const global = {location: {search: ''}};
const env = new Environment(global);
expect(env.features).toEqual({});
});
it('one registered flag true', () => {
const global = {location: {search: '?tfjsflags=FLAG1:true'}};
const env = new Environment(global);
env.registerFlag('FLAG1', () => false);
expect(env.get('FLAG1')).toBe(true);
});
it('one registered flag false', () => {
const global = {location: {search: '?tfjsflags=FLAG1:false'}};
const env = new Environment(global);
env.registerFlag('FLAG1', () => true);
expect(env.get('FLAG1')).toBe(false);
});
it('two registered flags', () => {
const global = {location: {search: '?tfjsflags=FLAG1:true,FLAG2:200'}};
const env = new Environment(global);
env.registerFlag('FLAG1', () => false);
env.registerFlag('FLAG2', () => 100);
expect(env.get('FLAG1')).toBe(true);
expect(env.get('FLAG2')).toBe(200);
});
});
describe('flag registration and evaluation', () => {
it('one flag registered', () => {
const env = new Environment({});
const evalObject = {eval: () => true};
const spy = spyOn(evalObject, 'eval').and.callThrough();
env.registerFlag('FLAG1', () => evalObject.eval());
expect(env.get('FLAG1')).toBe(true);
expect(spy.calls.count()).toBe(1);
// Multiple calls to get do not call the evaluation function again.
expect(env.get('FLAG1')).toBe(true);
expect(spy.calls.count()).toBe(1);
});
it('multiple flags registered', () => {
const env = new Environment({});
const evalObject = {eval1: () => true, eval2: () => 100};
const spy1 = spyOn(evalObject, 'eval1').and.callThrough();
const spy2 = spyOn(evalObject, 'eval2').and.callThrough();
env.registerFlag('FLAG1', () => evalObject.eval1());
env.registerFlag('FLAG2', () => evalObject.eval2());
expect(env.get('FLAG1')).toBe(true);
expect(spy1.calls.count()).toBe(1);
expect(spy2.calls.count()).toBe(0);
expect(env.get('FLAG2')).toBe(100);
expect(spy1.calls.count()).toBe(1);
expect(spy2.calls.count()).toBe(1);
// Multiple calls to get do not call the evaluation function again.
expect(env.get('FLAG1')).toBe(true);
expect(env.get('FLAG2')).toBe(100);
expect(spy1.calls.count()).toBe(1);
expect(spy2.calls.count()).toBe(1);
});
it('setting overrides value', () => {
const env = new Environment({});
const evalObject = {eval: () => true};
const spy = spyOn(evalObject, 'eval').and.callThrough();
env.registerFlag('FLAG1', () => evalObject.eval());
expect(env.get('FLAG1')).toBe(true);
expect(spy.calls.count()).toBe(1);
env.set('FLAG1', false);
expect(env.get('FLAG1')).toBe(false);
expect(spy.calls.count()).toBe(1);
});
it('set hook is called', () => {
const env = new Environment({});
const evalObject = {eval: () => true, setHook: () => true};
const evalSpy = spyOn(evalObject, 'eval').and.callThrough();
const setHookSpy = spyOn(evalObject, 'setHook').and.callThrough();
env.registerFlag(
'FLAG1', () => evalObject.eval(), () => evalObject.setHook());
expect(env.get('FLAG1')).toBe(true);
expect(evalSpy.calls.count()).toBe(1);
expect(setHookSpy.calls.count()).toBe(0);
env.set('FLAG1', false);
expect(env.get('FLAG1')).toBe(false);
expect(evalSpy.calls.count()).toBe(1);
expect(setHookSpy.calls.count()).toBe(1);
});
});
describe('environment.getQueryParams', () => {
it('basic', () => {
expect(environment.getQueryParams('?a=1&b=hi&f=animal'))
.toEqual({'a': '1', 'b': 'hi', 'f': 'animal'});
});
});