Skip to content

Commit 5e9b24d

Browse files
committed
OptionsUtil test
1 parent e111ec1 commit 5e9b24d

File tree

1 file changed

+228
-0
lines changed

1 file changed

+228
-0
lines changed
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
/**
2+
* @license
3+
* Copyright 2025 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import {expect} from "chai";
19+
20+
import {ParseContext} from "../../../src/api/parse_context";
21+
import {OptionsUtil} from "../../../src/core/options_util";
22+
import {
23+
UserDataSource
24+
} from "../../../src/lite-api/user_data_reader";
25+
import {testUserDataReader} from "../../util/helpers";
26+
27+
describe.only('OptionsUtil', () => {
28+
let context: ParseContext | undefined;
29+
beforeEach(async () => {
30+
context = testUserDataReader(false).createContext(UserDataSource.Argument, 'beforeEach');
31+
});
32+
33+
afterEach(async () => {
34+
context = undefined;
35+
});
36+
37+
it('should support known options', () => {
38+
const optionsUtil = new OptionsUtil({
39+
fooBar: {
40+
serverName: 'foo_bar',
41+
},
42+
});
43+
const proto = optionsUtil.getOptionsProto(
44+
context!, {
45+
fooBar: 'recommended',
46+
});
47+
48+
expect(proto).deep.equal({
49+
'foo_bar': {
50+
stringValue: 'recommended',
51+
},
52+
});
53+
});
54+
55+
it('should support unknown options', () => {
56+
const optionsUtil = new OptionsUtil({});
57+
const proto = optionsUtil.getOptionsProto(
58+
context!,
59+
{},
60+
{baz: 'foo'}
61+
);
62+
63+
expect(proto).to.deep.equal({
64+
baz: {
65+
stringValue: 'foo',
66+
},
67+
});
68+
});
69+
70+
it('should support unknown nested options', () => {
71+
const optionsUtil = new OptionsUtil({});
72+
const proto = optionsUtil.getOptionsProto(
73+
context!,
74+
{},
75+
{'foo.bar': 'baz'}
76+
);
77+
78+
expect(proto).to.deep.equal({
79+
foo: {
80+
mapValue: {
81+
fields: {
82+
bar: {stringValue: 'baz'},
83+
},
84+
},
85+
},
86+
});
87+
});
88+
89+
it('should support options override', () => {
90+
const optionsUtil = new OptionsUtil({
91+
indexMode: {
92+
serverName: 'index_mode',
93+
},
94+
});
95+
const proto = optionsUtil.getOptionsProto(
96+
context!,
97+
{
98+
indexMode: 'recommended',
99+
},
100+
{
101+
'index_mode': 'baz',
102+
}
103+
);
104+
105+
expect(proto).to.deep.equal({
106+
'index_mode': {
107+
stringValue: 'baz',
108+
},
109+
});
110+
});
111+
112+
it('should support options override of nested field', () => {
113+
const optionsUtil = new OptionsUtil({
114+
foo: {
115+
serverName: 'foo',
116+
nestedOptions: {
117+
bar: {
118+
serverName: 'bar',
119+
},
120+
waldo: {
121+
serverName: 'waldo',
122+
},
123+
},
124+
},
125+
});
126+
const proto = optionsUtil.getOptionsProto(
127+
context!,
128+
{
129+
foo: {bar: 'yep', waldo: 'found'},
130+
},
131+
{
132+
'foo.bar': 123,
133+
'foo.baz': true,
134+
}
135+
);
136+
137+
expect(proto).to.deep.equal({
138+
foo: {
139+
mapValue: {
140+
fields: {
141+
bar: {
142+
integerValue: '123',
143+
},
144+
waldo: {
145+
stringValue: 'found',
146+
},
147+
baz: {
148+
booleanValue: true,
149+
},
150+
},
151+
},
152+
},
153+
});
154+
});
155+
156+
it('will replace a nested object if given a new object', () => {
157+
const optionsUtil = new OptionsUtil({
158+
foo: {
159+
serverName: 'foo',
160+
nestedOptions: {
161+
bar: {
162+
serverName: 'bar',
163+
},
164+
waldo: {
165+
serverName: 'waldo',
166+
},
167+
},
168+
},
169+
});
170+
const proto = optionsUtil.getOptionsProto(
171+
context!,
172+
{
173+
foo: {bar: 'yep', waldo: 'found'},
174+
},
175+
{
176+
foo: {
177+
bar: 123,
178+
},
179+
}
180+
);
181+
182+
expect(proto).to.deep.equal({
183+
foo: {
184+
mapValue: {
185+
fields: {
186+
bar: {
187+
integerValue: '123',
188+
},
189+
},
190+
},
191+
},
192+
});
193+
});
194+
195+
it('will replace a top level property that is not an object if given a nested field with dot notation', () => {
196+
const optionsUtil = new OptionsUtil({
197+
foo: {
198+
serverName: 'foo',
199+
},
200+
});
201+
202+
const proto = optionsUtil.getOptionsProto(
203+
context!,
204+
{
205+
foo: 'bar',
206+
},
207+
{
208+
'foo.bar': '123',
209+
'foo.waldo': true,
210+
}
211+
);
212+
213+
expect(proto).to.deep.equal({
214+
foo: {
215+
mapValue: {
216+
fields: {
217+
bar: {
218+
stringValue: '123',
219+
},
220+
waldo: {
221+
booleanValue: true,
222+
},
223+
},
224+
},
225+
},
226+
});
227+
});
228+
});

0 commit comments

Comments
 (0)