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