blob: bc1285400921f40aec4840d169d50f7aef6b9763 [file] [log] [blame]
Adrià Vilanova Martínezd7c19de2024-09-27 23:26:01 +02001/* eslint-disable no-sparse-arrays */
2import { expect, test } from '@jest/globals';
3import * as protojs from './protojs.js';
4
5const objectLike1 = {
6 '2': [1, 2, 3],
7};
8
9const arrayLike1 = [undefined, [1, 2, 3]];
10
11const rObjectLike1 = [, undefined, [, 1, 2, 3]];
12
13const objectLike2 = {
14 '1': {
15 '1': 12,
16 '2': [
17 {
18 '6': [false, false, false],
19 '7': {
20 '1': false,
21 '2': 'Hola',
22 },
23 },
24 {
25 '6': [true, true, false],
26 '7': {
27 '1': false,
28 '2': 'Test',
29 },
30 },
31 {
32 '6': [],
33 '7': {
34 '1': true,
35 '2': 'Bye',
36 },
37 },
38 ],
39 '3': 1663,
40 },
41};
42
43const arrayLike2 = [
44 [
45 12,
46 [
47 [
48 undefined,
49 undefined,
50 undefined,
51 undefined,
52 undefined,
53 [false, false, false],
54 [false, 'Hola'],
55 ],
56 [
57 undefined,
58 undefined,
59 undefined,
60 undefined,
61 undefined,
62 [true, true, false],
63 [false, 'Test'],
64 ],
65 [
66 undefined,
67 undefined,
68 undefined,
69 undefined,
70 undefined,
71 [],
72 [true, 'Bye'],
73 ],
74 ],
75 1663,
76 ],
77];
78
79const rObjectLike2 = [
80 ,
81 [
82 ,
83 12,
84 [
85 ,
86 [
87 ,
88 undefined,
89 undefined,
90 undefined,
91 undefined,
92 undefined,
93 [, false, false, false],
94 [, false, 'Hola'],
95 ],
96 [
97 ,
98 undefined,
99 undefined,
100 undefined,
101 undefined,
102 undefined,
103 [, true, true, false],
104 [, false, 'Test'],
105 ],
106 [
107 ,
108 undefined,
109 undefined,
110 undefined,
111 undefined,
112 undefined,
113 [],
114 [, true, 'Bye'],
115 ],
116 ],
117 1663,
118 ],
119];
120
121test('can convert object-like to array-like', () => {
122 // [ object-like input, array-like verified output ]
123 const entries = [
124 [objectLike1, arrayLike1],
125 [rObjectLike1, arrayLike1],
126 [objectLike2, arrayLike2],
127 [rObjectLike2, arrayLike2],
128 ];
129
130 entries.forEach(([input, verifiedOutput]) => {
131 const converted = protojs.inverseCorrectArrayKeys(input);
132 expect(converted).toStrictEqual(verifiedOutput);
133 });
134});
135
136test('can convert array-like to object-like', () => {
137 // [ array-like input, object-like verified output ]
138 const entries = [
139 [arrayLike1, rObjectLike1],
140 [arrayLike2, rObjectLike2],
141 ];
142
143 entries.forEach(([input, verifiedOutput]) => {
144 const converted = protojs.correctArrayKeys(input);
145 expect(converted).toStrictEqual(verifiedOutput);
146 });
147});
148
149test('empty object can be converted to array-like form', () => {
150 const object = {
151 1: {},
152 };
153 const converted = protojs.inverseCorrectArrayKeys(object);
154 expect(converted).toStrictEqual([[]]);
155});