1/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import {ObjectUtils} from './object_utils';
18
19describe('ObjectUtils', () => {
20  it('getField()', () => {
21    const obj = {
22      child0: {
23        key0: 'value0',
24      },
25      child1: [{key1: 'value1'}, 10],
26    };
27
28    expect(ObjectUtils.getProperty(obj, 'child0')).toEqual({key0: 'value0'});
29    expect(ObjectUtils.getProperty(obj, 'child0.key0')).toEqual('value0');
30    expect(ObjectUtils.getProperty(obj, 'child1')).toEqual([
31      {key1: 'value1'},
32      10,
33    ]);
34    expect(ObjectUtils.getProperty(obj, 'child1[0]')).toEqual({key1: 'value1'});
35    expect(ObjectUtils.getProperty(obj, 'child1[0].key1')).toEqual('value1');
36    expect(ObjectUtils.getProperty(obj, 'child1[1]')).toEqual(10);
37  });
38
39  it('setField()', () => {
40    const obj = {};
41
42    ObjectUtils.setProperty(obj, 'child0.key0', 'value0');
43    expect(obj).toEqual({
44      child0: {
45        key0: 'value0',
46      },
47    });
48
49    ObjectUtils.setProperty(obj, 'child1[0].key1', 'value1');
50    ObjectUtils.setProperty(obj, 'child1[1]', 10);
51    expect(obj).toEqual({
52      child0: {
53        key0: 'value0',
54      },
55      child1: [{key1: 'value1'}, 10],
56    });
57  });
58});
59