-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArrayType.test.ts
More file actions
279 lines (223 loc) · 8.76 KB
/
ArrayType.test.ts
File metadata and controls
279 lines (223 loc) · 8.76 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import convertToSchema from '../convert';
import type { Diagnostic } from '../interfaces';
import { ArrayType } from './ArrayType';
import { BooleanType } from './BooleanType';
import { MissingType } from './MissingType';
import { NullType } from './NullType';
import { NumberType } from './NumberType';
import { ObjectType } from './ObjectType';
import { StringType } from './StringType';
import type { UnionType } from './UnionType';
describe('ArrayType simple test case', () => {
describe('constructor', () => {
it('has type Array', () => {
const b1 = new ArrayType();
expect(b1.type).toBe('Array');
});
it('has counter set to 1', () => {
const b1 = new ArrayType();
expect(b1.counter).toBe(1);
});
it('has a tag', () => {
const b1 = new ArrayType({ counter: 1, tag: 'someTag' });
expect(b1.tag).toBe('someTag');
});
});
describe('#combine', () => {
it('combines with ArrayType', () => {
const b1 = new ArrayType();
const b2 = new ArrayType();
const combined = b1.combine(b2);
expect(combined.type).toBe('Array');
expect(combined.counter).toBe(2);
});
it('combines lengths', () => {
const b1 = new ArrayType({ stats: { lengths: { 1: 1 } } });
const b2 = new ArrayType({ stats: { lengths: { 2: 1 } } });
const b3 = new ArrayType({ stats: { lengths: { 2: 1 } } });
let combined = b1.combine(b2) as ArrayType;
combined = combined.combine(b3) as ArrayType;
expect(combined.type).toBe('Array');
expect(combined.counter).toBe(3);
expect(combined.stats).toStrictEqual({ lengths: { 1: 1, 2: 2 } });
});
it('combine does not mutate inputs', () => {
const b1 = new ArrayType();
const b2 = new ArrayType();
b1.combine(b2);
expect(b1.type).toBe('Array');
expect(b2.type).toBe('Array');
expect(b1.counter).toBe(1);
expect(b2.counter).toBe(1);
});
it('combine can be chained', () => {
const combined = new ArrayType()
.combine(new ArrayType())
.combine(new ArrayType())
.combine(new ArrayType())
.combine(new ArrayType())
.combine(new ArrayType())
.combine(new ArrayType())
.combine(new ArrayType())
.combine(new ArrayType())
.combine(new ArrayType());
expect(combined.type).toBe('Array');
expect(combined.counter).toBe(10);
});
it('can combine with NullType', () => {
const combined: any = new ArrayType().combine(new NullType());
expect(combined.type).toBe('Union');
expect(combined.counter).toBe(2);
expect(combined.types.Array.counter).toBe(1);
expect(combined.types.Null.counter).toBe(1);
});
it('can combine with NumberType', () => {
const combined = new ArrayType().combine(new NumberType());
expect(combined.type).toBe('Union');
expect(combined.counter).toBe(2);
expect(combined.types.Array.counter).toBe(1);
expect(combined.types.Number.counter).toBe(1);
});
it('can combine with StringType', () => {
const combined = new ArrayType().combine(new StringType());
expect(combined.type).toBe('Union');
expect(combined.counter).toBe(2);
expect(combined.types.Array.counter).toBe(1);
expect(combined.types.String.counter).toBe(1);
});
it('can combine with ObjectType', () => {
const combined = new ArrayType().combine(new ObjectType());
expect(combined.type).toBe('Union');
expect(combined.counter).toBe(2);
expect(combined.types.Array.counter).toBe(1);
expect(combined.types.Object.counter).toBe(1);
});
it('can combine with BooleanType', () => {
const combined = new ArrayType().combine(new BooleanType());
expect(combined.type).toBe('Union');
expect(combined.counter).toBe(2);
expect(combined.types.Array.counter).toBe(1);
expect(combined.types.Boolean.counter).toBe(1);
});
it('can combine with MissingType', () => {
const combined = new ArrayType().combine(new MissingType());
expect(combined.type).toBe('Union');
expect(combined.counter).toBe(2);
expect(combined.types.Array.counter).toBe(1);
expect(combined.types.Missing.counter).toBe(1);
});
});
});
describe('Simple Array Type test case', () => {
it('defines correct schema for string arrays', () => {
const converted = convertToSchema(['someText', 'someText'], undefined, {
collectStatistics: {
array: true,
},
}) as ArrayType;
expect(converted.type).toBe('Array');
expect(converted.types.String).toBeDefined();
expect(converted.types.String.counter).toBe(1);
expect(converted.counter).toBe(1);
expect(converted.stats).toStrictEqual({ lengths: { 2: 1 } });
});
it('ignore array length by default', () => {
const converted = convertToSchema(['someText', 'someText']) as ArrayType;
expect(converted.type).toBe('Array');
expect(converted.types.String).toBeDefined();
expect(converted.types.String.counter).toBe(1);
expect(converted.counter).toBe(1);
expect(converted.stats).toStrictEqual({ lengths: {} });
});
it('defines correct schema for boolean arrays', () => {
const converted = convertToSchema([true, true]) as ArrayType;
expect(converted.type).toBe('Array');
expect(converted.types.Boolean).toBeDefined();
expect(converted.types.Boolean.counter).toBe(1);
expect(converted.counter).toBe(1);
});
it('defines correct schema for null arrays', () => {
const converted = convertToSchema([null, null, null]) as ArrayType;
expect(converted.type).toBe('Array');
expect(converted.types.Null).toBeDefined();
expect(converted.types.Null.counter).toBe(1);
expect(converted.counter).toBe(1);
});
it('defines correct schema for number arrays', () => {
const converted = convertToSchema([123, 42, 6]) as ArrayType;
expect(converted.type).toBe('Array');
expect(converted.types.Number).toBeDefined();
expect(converted.types.Number.counter).toBe(1);
expect(converted.counter).toBe(1);
});
it('defines correct schema for array arrays', () => {
const converted = convertToSchema([[234], [24], [23]]) as ArrayType;
expect(converted.type).toBe('Array');
expect(converted.types.Array).toBeDefined();
expect(converted.types.Array.counter).toBe(1);
expect(converted.counter).toBe(1);
});
it('defines correct schema for object arrays', () => {
const converted = convertToSchema([
{ count: 123 },
{ count: 42 },
{ count: 234 },
]) as ArrayType;
expect(converted.type).toBe('Array');
expect(converted.types.Object).toBeDefined();
expect(converted.types.Object.counter).toBe(1);
expect(converted.counter).toBe(1);
});
it('defines correct schema for multi-type arrays', () => {
const converted = convertToSchema(['someText', 123, null]) as ArrayType;
expect(converted.type).toBe('Array');
expect(converted.types.String).toBeDefined();
expect(converted.types.String.counter).toBe(1);
expect(converted.types.Number).toBeDefined();
expect(converted.types.Number.counter).toBe(1);
expect(converted.types.Null).toBeDefined();
expect(converted.types.Null.counter).toBe(1);
expect(converted.counter).toBe(1);
});
it('merges child object schemas into one', () => {
const converted = convertToSchema([
{ count: 123 },
{ count: 42, opt: true },
{ count: 234 },
]) as ArrayType;
expect(converted.type).toBe('Array');
expect(converted.types.Object).toBeDefined();
expect(converted.types.Object.type).toBe('Object');
expect(converted.types.Object.counter).toBe(1);
const sub = converted.types.Object as ObjectType;
expect(sub.schema.count.type).toBe('Number');
expect(sub.schema.count.counter).toBe(1);
expect(sub.schema.opt.type).toBe('Union');
const opt = sub.schema.opt as UnionType;
expect(opt.types.Missing).toBeDefined();
expect(opt.types.Missing.counter).toBe(1);
expect(opt.types.Boolean).toBeDefined();
expect(opt.types.Boolean.counter).toBe(1);
expect(converted.counter).toBe(1);
});
it('diagnoses empty array schema even when only provided with empty arrays', () => {
const firstModel = convertToSchema([]) as ArrayType;
const secondModel = convertToSchema([]) as ArrayType;
const combined = firstModel.combine(secondModel);
expect(combined.type).toBe('Array');
expect(combined.types.Missing).toBeDefined();
expect(combined.types.Missing.counter).toBe(2);
expect(combined.counter).toBe(2);
const diagnostics = combined.diagnose();
const expectedDiags: Diagnostic[] = [
{
id: 'emptyArray',
title: 'Empty Array',
type: 'Array',
path: [],
affected: 2,
},
];
expect(diagnostics).toEqual(expectedDiags);
});
});