-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
type_test.go
716 lines (579 loc) 路 33.2 KB
/
type_test.go
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
// Copyright 2016-2020, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package model
import (
"testing"
"github.com/hashicorp/hcl/v2"
"github.com/stretchr/testify/assert"
"github.com/zclconf/go-cty/cty"
)
func testTraverse(t *testing.T, receiver Traversable, traverser hcl.Traverser, expected Traversable, expectDiags bool) {
actual, diags := receiver.Traverse(traverser)
assert.Equal(t, expected, actual)
if expectDiags {
assert.Greater(t, len(diags), 0)
} else {
assert.Equal(t, 0, len(diags))
}
}
func TestDynamicType(t *testing.T) {
t.Parallel()
// Test that DynamicType is assignable to and from itself.
assert.True(t, DynamicType.AssignableFrom(DynamicType))
// Test that DynamicType is assignable from any type.
assert.True(t, DynamicType.AssignableFrom(BoolType))
assert.True(t, DynamicType.AssignableFrom(IntType))
assert.True(t, DynamicType.AssignableFrom(NumberType))
assert.True(t, DynamicType.AssignableFrom(StringType))
assert.True(t, DynamicType.AssignableFrom(NewOptionalType(BoolType)))
assert.True(t, DynamicType.AssignableFrom(NewOutputType(BoolType)))
assert.True(t, DynamicType.AssignableFrom(NewPromiseType(BoolType)))
assert.True(t, DynamicType.AssignableFrom(NewMapType(BoolType)))
assert.True(t, DynamicType.AssignableFrom(NewListType(BoolType)))
assert.True(t, DynamicType.AssignableFrom(NewUnionType(BoolType, IntType)))
assert.True(t, DynamicType.AssignableFrom(NewObjectType(map[string]Type{
"bool": BoolType,
"int": IntType,
})))
// Test that DynamicType is assignable to certain types and not assignable to others.
assert.True(t, NewOptionalType(DynamicType).AssignableFrom(DynamicType))
assert.True(t, NewOutputType(DynamicType).AssignableFrom(DynamicType))
assert.True(t, NewPromiseType(DynamicType).AssignableFrom(DynamicType))
assert.True(t, NewUnionType(BoolType, DynamicType).AssignableFrom(DynamicType))
assert.False(t, BoolType.AssignableFrom(DynamicType))
assert.False(t, IntType.AssignableFrom(DynamicType))
assert.False(t, NumberType.AssignableFrom(DynamicType))
assert.False(t, StringType.AssignableFrom(DynamicType))
assert.False(t, NewOptionalType(BoolType).AssignableFrom(DynamicType))
assert.False(t, NewOutputType(BoolType).AssignableFrom(DynamicType))
assert.False(t, NewPromiseType(BoolType).AssignableFrom(DynamicType))
assert.False(t, NewMapType(BoolType).AssignableFrom(DynamicType))
assert.False(t, NewListType(BoolType).AssignableFrom(DynamicType))
assert.False(t, NewUnionType(BoolType, IntType).AssignableFrom(DynamicType))
assert.False(t, NewObjectType(map[string]Type{
"bool": BoolType,
"int": IntType,
}).AssignableFrom(DynamicType))
// Test that DynamicType is convertible from any type.
assert.True(t, DynamicType.ConversionFrom(BoolType).Exists())
assert.True(t, DynamicType.ConversionFrom(IntType).Exists())
assert.True(t, DynamicType.ConversionFrom(NumberType).Exists())
assert.True(t, DynamicType.ConversionFrom(StringType).Exists())
assert.True(t, DynamicType.ConversionFrom(NewOptionalType(BoolType)).Exists())
assert.True(t, DynamicType.ConversionFrom(NewOutputType(BoolType)).Exists())
assert.True(t, DynamicType.ConversionFrom(NewPromiseType(BoolType)).Exists())
assert.True(t, DynamicType.ConversionFrom(NewMapType(BoolType)).Exists())
assert.True(t, DynamicType.ConversionFrom(NewListType(BoolType)).Exists())
assert.True(t, DynamicType.ConversionFrom(NewUnionType(BoolType, IntType)).Exists())
assert.True(t, DynamicType.ConversionFrom(NewObjectType(map[string]Type{
"bool": BoolType,
"int": IntType,
})).Exists())
// Test that DynamicType is convertible to any type.
assert.True(t, BoolType.ConversionFrom(DynamicType).Exists())
assert.True(t, IntType.ConversionFrom(DynamicType).Exists())
assert.True(t, NumberType.ConversionFrom(DynamicType).Exists())
assert.True(t, StringType.ConversionFrom(DynamicType).Exists())
assert.True(t, NewOptionalType(BoolType).ConversionFrom(DynamicType).Exists())
assert.True(t, NewOutputType(BoolType).ConversionFrom(DynamicType).Exists())
assert.True(t, NewPromiseType(BoolType).ConversionFrom(DynamicType).Exists())
assert.True(t, NewMapType(BoolType).ConversionFrom(DynamicType).Exists())
assert.True(t, NewListType(BoolType).ConversionFrom(DynamicType).Exists())
assert.True(t, NewUnionType(BoolType, IntType).ConversionFrom(DynamicType).Exists())
assert.True(t, NewObjectType(map[string]Type{
"bool": BoolType,
"int": IntType,
}).ConversionFrom(DynamicType).Exists())
// Test that traversals on DynamicType always succeed.
testTraverse(t, DynamicType, hcl.TraverseAttr{Name: "foo"}, DynamicType, false)
testTraverse(t, DynamicType, hcl.TraverseIndex{Key: cty.StringVal("foo")}, DynamicType, false)
testTraverse(t, DynamicType, hcl.TraverseIndex{Key: cty.NumberIntVal(0)}, DynamicType, false)
testTraverse(t, DynamicType, hcl.TraverseIndex{Key: encapsulateType(DynamicType)}, DynamicType, false)
}
func TestOptionalType(t *testing.T) {
t.Parallel()
typ := NewOptionalType(DynamicType)
// Test that creating an optional type with the same element type does not create a new type.
typ2 := NewOptionalType(DynamicType)
assert.EqualValues(t, typ, typ2)
// Test that creating an optional type with an element type that is also optional does not create a new type.
typ2 = NewOptionalType(typ)
assert.Equal(t, typ, typ2)
// Test that an optional type is assignable to and from itself.
assert.True(t, typ.AssignableFrom(typ))
// Test that an optional type is assignable from none.
assert.True(t, typ.AssignableFrom(NoneType))
// Test that an optional type is assignable from its element type.
assert.True(t, NewOptionalType(StringType).AssignableFrom(StringType))
// Test that an optional(T) is assignable from an U, where U is assignable to T.
assert.True(t, typ.AssignableFrom(BoolType))
// Test that an optional(T) is assignable from an optional(U), where U is assignable to T.
assert.True(t, typ.AssignableFrom(NewOptionalType(BoolType)))
// Test that traversing an optional(T) returns an optional(U), where U is the result of the inner traversal.
typ = NewOptionalType(NewMapType(StringType))
testTraverse(t, typ, hcl.TraverseAttr{Name: "foo"}, NewOptionalType(StringType), false)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.StringVal("foo")}, NewOptionalType(StringType), false)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.NumberIntVal(0)}, NewOptionalType(StringType), false)
testTraverse(t, typ, hcl.TraverseIndex{Key: encapsulateType(IntType)}, NewOptionalType(StringType), false)
}
func TestOutputType(t *testing.T) {
t.Parallel()
typ := NewOutputType(DynamicType)
// Test that creating an output type with the same element type does not create a new type.
typ2 := NewOutputType(typ.ElementType)
assert.EqualValues(t, typ, typ2)
// Test that creating an output type with an element type that is also an output does not create a new type.
typ2 = NewOutputType(typ)
assert.Equal(t, typ, typ2)
// Test that an output type is assignable to and from itself.
assert.True(t, typ.AssignableFrom(typ))
// Test that an output type is assignable from its element type.
assert.True(t, NewOutputType(StringType).AssignableFrom(StringType))
// Test that output(T) is assignable from U, where U is assignable to T.
assert.True(t, typ.AssignableFrom(BoolType))
// Test that output(T) is assignable from output(U), where U is assignable to T.
assert.True(t, typ.AssignableFrom(NewOutputType(BoolType)))
// Test that output(T) is assignable from promise(U), where U is assignable to T.
assert.True(t, typ.AssignableFrom(NewPromiseType(BoolType)))
// Test that output(T) is _not_ assignable from U, where U is not assignable to T.
assert.False(t, NewOutputType(BoolType).AssignableFrom(IntType))
// Test that output(T) is _not_ assignable from output(U), where U is not assignable to T.
assert.False(t, NewOutputType(BoolType).AssignableFrom(NewOutputType(IntType)))
// Test that output(T) is _not_ assignable from promise(U), where U is not assignable to T.
assert.False(t, NewOutputType(BoolType).AssignableFrom(NewPromiseType(IntType)))
// Test that traversing an output(T) returns an output(U), where U is the result of the inner traversal.
typ = NewOutputType(NewMapType(StringType))
testTraverse(t, typ, hcl.TraverseAttr{Name: "foo"}, NewOutputType(StringType), false)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.StringVal("foo")}, NewOutputType(StringType), false)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.NumberIntVal(0)}, NewOutputType(StringType), false)
testTraverse(t, typ, hcl.TraverseIndex{Key: encapsulateType(IntType)}, NewOutputType(StringType), false)
// Test that ResolveOutputs correctly handles nested outputs.
assert.Equal(t, NewOptionalType(BoolType), ResolveOutputs(NewOptionalType(NewOutputType(BoolType))))
assert.Equal(t, BoolType, ResolveOutputs(NewOutputType(BoolType)))
assert.Equal(t, BoolType, ResolveOutputs(NewPromiseType(BoolType)))
assert.Equal(t, NewMapType(BoolType), ResolveOutputs(NewMapType(NewOutputType(BoolType))))
assert.Equal(t, NewListType(BoolType), ResolveOutputs(NewListType(NewOutputType(BoolType))))
assert.Equal(t, NewUnionType(BoolType, IntType), ResolveOutputs(NewUnionType(NewOutputType(BoolType),
NewOutputType(IntType))))
assert.Equal(t, NewObjectType(map[string]Type{
"bool": BoolType,
"int": IntType,
}), ResolveOutputs(NewObjectType(map[string]Type{
"bool": NewOutputType(BoolType),
"int": NewOutputType(IntType),
})))
// Test that NewOutputType correctly handles nested outputs.
assert.Equal(t, NewOutputType(NewOptionalType(BoolType)), NewOutputType(NewOptionalType(NewOutputType(BoolType))))
assert.Equal(t, NewOutputType(BoolType), NewOutputType(NewOutputType(BoolType)))
assert.Equal(t, NewOutputType(BoolType), NewOutputType(NewPromiseType(BoolType)))
assert.Equal(t, NewOutputType(NewMapType(BoolType)), NewOutputType(NewMapType(NewOutputType(BoolType))))
assert.Equal(t, NewOutputType(NewListType(BoolType)), NewOutputType(NewListType(NewOutputType(BoolType))))
assert.Equal(t, NewOutputType(NewUnionType(BoolType, IntType)),
NewOutputType(NewUnionType(NewOutputType(BoolType), NewOutputType(IntType))))
assert.Equal(t, NewOutputType(NewObjectType(map[string]Type{
"bool": BoolType,
"int": IntType,
})), NewOutputType(NewObjectType(map[string]Type{
"bool": NewOutputType(BoolType),
"int": NewOutputType(IntType),
})))
}
func TestPromiseType(t *testing.T) {
t.Parallel()
typ := NewPromiseType(DynamicType)
// Test that creating an promise type with the same element type does not create a new type.
typ2 := NewPromiseType(typ.ElementType)
assert.EqualValues(t, typ, typ2)
// Test that creating an promise type with an element type that is also a promise does not create a new type.
typ2 = NewPromiseType(typ)
assert.Equal(t, typ, typ2)
// Test that a promise type is assignable to and from itself.
assert.True(t, typ.AssignableFrom(typ))
// Test that a promise type is assignable from its element type.
assert.True(t, NewPromiseType(StringType).AssignableFrom(StringType))
// Test that promise(T) is assignable from U, where U is assignable to T.
assert.True(t, typ.AssignableFrom(BoolType))
// Test that promise(T) is assignable from promise(U), where U is assignable to T.
assert.True(t, typ.AssignableFrom(NewPromiseType(BoolType)))
// Test that promise(T) is _not_ assignable from U, where U is not assignable to T.
assert.False(t, NewPromiseType(BoolType).AssignableFrom(IntType))
// Test that promise(T) is _not_ assignable from promise(U), where U is not assignable to T.
assert.False(t, NewPromiseType(BoolType).AssignableFrom(NewPromiseType(IntType)))
// Test that traversing an promise(T) returns an promise(U), where U is the result of the inner traversal.
typ = NewPromiseType(NewMapType(StringType))
testTraverse(t, typ, hcl.TraverseAttr{Name: "foo"}, NewPromiseType(StringType), false)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.StringVal("foo")}, NewPromiseType(StringType), false)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.NumberIntVal(0)}, NewPromiseType(StringType), false)
testTraverse(t, typ, hcl.TraverseIndex{Key: encapsulateType(IntType)}, NewPromiseType(StringType), false)
// Test that ResolvePromises correctly handles nested promises.
assert.Equal(t, NewOptionalType(BoolType), ResolvePromises(NewOptionalType(NewPromiseType(BoolType))))
assert.Equal(t, BoolType, ResolvePromises(NewPromiseType(BoolType)))
assert.Equal(t, BoolType, ResolvePromises(NewPromiseType(BoolType)))
assert.Equal(t, NewMapType(BoolType), ResolvePromises(NewMapType(NewPromiseType(BoolType))))
assert.Equal(t, NewListType(BoolType), ResolvePromises(NewListType(NewPromiseType(BoolType))))
assert.Equal(t, NewUnionType(BoolType, IntType),
ResolvePromises(NewUnionType(NewPromiseType(BoolType), NewPromiseType(IntType))))
assert.Equal(t, NewObjectType(map[string]Type{
"bool": BoolType,
"int": IntType,
}), ResolvePromises(NewObjectType(map[string]Type{
"bool": NewPromiseType(BoolType),
"int": NewPromiseType(IntType),
})))
// Test that NewPromiseType correctly handles nested promises.
assert.Equal(t, NewPromiseType(NewOptionalType(BoolType)), NewPromiseType(NewOptionalType(NewPromiseType(BoolType))))
assert.Equal(t, NewPromiseType(BoolType), NewPromiseType(NewPromiseType(BoolType)))
assert.Equal(t, NewPromiseType(BoolType), NewPromiseType(NewPromiseType(BoolType)))
assert.Equal(t, NewPromiseType(NewMapType(BoolType)), NewPromiseType(NewMapType(NewPromiseType(BoolType))))
assert.Equal(t, NewPromiseType(NewListType(BoolType)), NewPromiseType(NewListType(NewPromiseType(BoolType))))
assert.Equal(t, NewPromiseType(NewUnionType(BoolType, IntType)),
NewPromiseType(NewUnionType(NewPromiseType(BoolType), NewPromiseType(IntType))))
assert.Equal(t, NewPromiseType(NewObjectType(map[string]Type{
"bool": BoolType,
"int": IntType,
})), NewPromiseType(NewObjectType(map[string]Type{
"bool": NewPromiseType(BoolType),
"int": NewPromiseType(IntType),
})))
}
func TestMapType(t *testing.T) {
t.Parallel()
typ := NewMapType(DynamicType)
// Test that creating an map type with the same element type does not create a new type.
typ2 := NewMapType(typ.ElementType)
assert.EqualValues(t, typ, typ2)
// Test that a map type is assignable to and from itself.
assert.True(t, typ.AssignableFrom(typ))
// Test that map(T) is _not_ assignable from U, where U is not map(T).
assert.False(t, typ.AssignableFrom(BoolType))
// Test that map(T) is assignable from map(U), where U is assignable to T.
assert.True(t, typ.AssignableFrom(NewMapType(BoolType)))
// Test that map(T) is convertible from object(K_0=U_0, .., K_N=U_N) where unify(U_0, ..., U_N) is assignable to T.
assert.True(t, typ.ConversionFrom(NewObjectType(map[string]Type{
"foo": IntType,
"bar": NumberType,
"baz": StringType,
})).Exists())
// Test that map(T) is _not_ assignable from map(U), where U is not assignable to T.
assert.False(t, NewMapType(BoolType).AssignableFrom(NewMapType(IntType)))
// Test that traversing a map(T) with a type that is convertible to string returns T.
testTraverse(t, typ, hcl.TraverseAttr{Name: "foo"}, typ.ElementType, false)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.StringVal("foo")}, typ.ElementType, false)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.NumberIntVal(0)}, typ.ElementType, false)
testTraverse(t, typ, hcl.TraverseIndex{Key: encapsulateType(IntType)}, typ.ElementType, false)
// Test that traversing a map(T) with a type that is not convertible to string returns DynamicType and an error.
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.ListVal([]cty.Value{cty.NumberIntVal(0)})}, typ.ElementType, true)
testTraverse(t, typ, hcl.TraverseIndex{Key: encapsulateType(typ)}, typ.ElementType, true)
}
func TestListType(t *testing.T) {
t.Parallel()
typ := NewListType(DynamicType)
// Test that creating an list type with the same element type does not create a new type.
typ2 := NewListType(typ.ElementType)
assert.EqualValues(t, typ, typ2)
// Test that an list type is assignable to and from itself.
assert.True(t, typ.AssignableFrom(typ))
// Test that list(T) is _not_ assignable from U, where U is not list(T).
assert.False(t, typ.AssignableFrom(BoolType))
// Test that list(T) is assignable from list(U), where U is assignable to T.
assert.True(t, typ.AssignableFrom(NewListType(BoolType)))
// Test that list(T) is _not_ assignable from list(U), where U is not assignable to T.
assert.False(t, NewListType(BoolType).AssignableFrom(NewListType(IntType)))
// Test that traversing a list(T) with a type that is convertible to number returns T.
testTraverse(t, typ, hcl.TraverseAttr{Name: "0"}, typ.ElementType, false)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.StringVal("0")}, typ.ElementType, false)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.NumberIntVal(0)}, typ.ElementType, false)
testTraverse(t, typ, hcl.TraverseIndex{Key: encapsulateType(IntType)}, typ.ElementType, false)
// Test that traversing a list(T) with a type that is not convertible to number returns DynamicType and an error.
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.ListVal([]cty.Value{cty.NumberIntVal(0)})}, typ.ElementType, true)
testTraverse(t, typ, hcl.TraverseIndex{Key: encapsulateType(typ)}, typ.ElementType, true)
}
func TestSetType(t *testing.T) {
t.Parallel()
typ := NewSetType(DynamicType)
// Test that creating an set type with the same element type does not create a new type.
typ2 := NewSetType(typ.ElementType)
assert.EqualValues(t, typ, typ2)
// Test that an set type is assignable to and from itself.
assert.True(t, typ.AssignableFrom(typ))
// Test that set(T) is _not_ assignable from U, where U is not set(T).
assert.False(t, typ.AssignableFrom(BoolType))
// Test that set(T) is assignable from set(U), where U is assignable to T.
assert.True(t, typ.AssignableFrom(NewSetType(BoolType)))
// Test that set(T) is _not_ assignable from set(U), where U is not assignable to T.
assert.False(t, NewSetType(BoolType).AssignableFrom(NewSetType(IntType)))
// Test that traversing a set(T) fails.
testTraverse(t, typ, hcl.TraverseAttr{Name: "0"}, DynamicType, true)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.StringVal("0")}, DynamicType, true)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.NumberIntVal(0)}, DynamicType, true)
testTraverse(t, typ, hcl.TraverseIndex{Key: encapsulateType(IntType)}, DynamicType, true)
}
func TestUnionType(t *testing.T) {
t.Parallel()
typ := NewUnionType(BoolType, IntType, NumberType, StringType).(*UnionType)
// Test that creating a union with the same element types does not create a new type.
typ2 := NewUnionType(BoolType, IntType, NumberType, StringType).(*UnionType)
assert.EqualValues(t, typ, typ2)
// Test that creating a union with duplicated element types unifies all of the duplicated types.
assert.Equal(t, BoolType, NewUnionType(BoolType, BoolType))
assert.Equal(t, typ, NewUnionType(BoolType, IntType, IntType, NumberType, StringType))
// Test that a union type is assignable to and from itself.
assert.True(t, typ.AssignableFrom(typ))
// Test that a union type is assignable from each of its element types.
for _, et := range typ.ElementTypes {
assert.True(t, typ.AssignableFrom(et))
}
// Test that union(T_0, ..., T_N) is assignable from union(U_0, ..., U_M) if union(T_0, ..., T_N) is assignable
// from all of U_0 through U_M.
assert.True(t, NewUnionType(NoneType, StringType).ConversionFrom(typ).Exists())
// Test that union(T_0, ..., T_N) is _not_ assignable from union(U_0, ..., U_M) if union(T_0, ..., T_N) is not
// assignable from any of U_0 through U_M.
assert.False(t, typ.AssignableFrom(NewUnionType(BoolType, NewOptionalType(NumberType))))
// Test that traversing a union type fails if the element type cannot be traversed.
testTraverse(t, typ, hcl.TraverseAttr{Name: "foo"}, DynamicType, true)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.StringVal("foo")}, DynamicType, true)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.NumberIntVal(0)}, DynamicType, true)
testTraverse(t, typ, hcl.TraverseIndex{Key: encapsulateType(typ)}, DynamicType, true)
// Test that traversing a union type succeeds if some element type can be traversed.
typ = NewUnionType(typ, NewObjectType(map[string]Type{"foo": StringType}), NewListType(StringType)).(*UnionType)
testTraverse(t, typ, hcl.TraverseAttr{Name: "foo"}, StringType, false)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.StringVal("foo")}, StringType, false)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.NumberIntVal(0)}, StringType, false)
testTraverse(t, typ, hcl.TraverseIndex{Key: encapsulateType(StringType)}, StringType, false)
// Test that traversing a union type produces a union if more than one element can be traversed.
typ = NewUnionType(NewMapType(IntType), NewObjectType(map[string]Type{"foo": StringType})).(*UnionType)
testTraverse(t, typ, hcl.TraverseAttr{Name: "foo"}, NewUnionType(StringType, IntType), false)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.StringVal("foo")}, NewUnionType(StringType, IntType), false)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.NumberIntVal(0)}, IntType, false)
testTraverse(t, typ, hcl.TraverseIndex{Key: encapsulateType(IntType)}, NewUnionType(StringType, IntType), false)
}
func TestObjectType(t *testing.T) {
t.Parallel()
typ := NewObjectType(map[string]Type{
"foo": BoolType,
"bar": IntType,
"baz": NumberType,
"qux": NewOptionalType(BoolType),
})
// Test that creating a union with the same element types does not create a new type.
typ2 := NewObjectType(map[string]Type{
"foo": BoolType,
"bar": IntType,
"baz": NumberType,
"qux": NewOptionalType(BoolType),
})
assert.EqualValues(t, typ, typ2)
// Test that an object type is assignable to and from itself.
assert.True(t, typ.AssignableFrom(typ))
// Test that object(K_0=T_0, ..., K_N=T_N) is assignable from object(K_0=U_0, ..., K_N=U_N) if for each key K_i
// T_i is assignable from U_i.
assert.True(t, typ.ConversionFrom(NewObjectType(map[string]Type{
"foo": BoolType,
"bar": IntType,
"baz": IntType,
"qux": BoolType,
})).Exists())
// Test that object(K_0=T_0, ..., K_N=T_N) is assignable from object(K_0=U_0, ..., K_M=U_M) if M < N and for each
// key K_i where 0 <= i <= M, T_i is assignable from U_i and for each K_j where M < j <= N, T_j is optional.
assert.True(t, typ.ConversionFrom(NewObjectType(map[string]Type{
"foo": BoolType,
"bar": IntType,
"baz": NumberType,
})).Exists())
// Test that object(K_0=T_0, ..., K_N=T_N) is _unsafely_ convertible from object(L_0=U_0, ..., L_M=U_M) if there exists
// some key K_i a matching key K_i exists and T_i is unsafely convertible from U_i.
assert.Equal(t, UnsafeConversion, typ.ConversionFrom(NewObjectType(map[string]Type{
"foo": BoolType,
"bar": IntType,
"baz": NumberType,
"qux": StringType,
})))
assert.Equal(t, UnsafeConversion, typ.ConversionFrom(NewObjectType(map[string]Type{
"foo": BoolType,
"bar": IntType,
"baz": StringType,
})))
// Test that traversing an object type with a property name K_i returns T_i.
testTraverse(t, typ, hcl.TraverseAttr{Name: "foo"}, BoolType, false)
testTraverse(t, typ, hcl.TraverseAttr{Name: "bar"}, IntType, false)
testTraverse(t, typ, hcl.TraverseAttr{Name: "baz"}, NumberType, false)
testTraverse(t, typ, hcl.TraverseAttr{Name: "qux"}, NewOptionalType(BoolType), false)
// test case-insensitive attribute
testTraverse(t, typ, hcl.TraverseAttr{Name: "Qux"}, NewOptionalType(BoolType), true)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.StringVal("foo")}, BoolType, false)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.StringVal("bar")}, IntType, false)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.StringVal("baz")}, NumberType, false)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.StringVal("qux")}, NewOptionalType(BoolType), false)
// Test that traversing an object type with a dynamic value produces the union of the object's property types..
testTraverse(t, typ, hcl.TraverseIndex{Key: encapsulateType(StringType)}, NewUnionType(BoolType, IntType,
NumberType, NewOptionalType(BoolType)), false)
// Test that traversing an object type with any other type fails.
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.NumberIntVal(0)}, DynamicType, true)
testTraverse(t, typ, hcl.TraverseIndex{Key: encapsulateType(typ)}, DynamicType, true)
}
func TestInputType(t *testing.T) {
t.Parallel()
// Test that InputType(DynamicType) just returns DynamicType.
assert.Equal(t, DynamicType, InputType(DynamicType))
// Test that InputType(T) correctly recurses through constructed types. The result of InputType(T) should be
// union(innerInputType(T), output(innerInputType(T))), where innerInputType(T) recurses thorough constructed
// types.
assert.Equal(t, NewUnionType(BoolType, NewOutputType(BoolType)), InputType(BoolType))
assert.Equal(t, NewUnionType(
NewOptionalType(NewUnionType(BoolType, NewOutputType(BoolType))),
NewOutputType(NewOptionalType(BoolType))), InputType(NewOptionalType(BoolType)))
assert.Equal(t, NewUnionType(
NewPromiseType(NewUnionType(BoolType, NewOutputType(BoolType))),
NewOutputType(BoolType)), InputType(NewPromiseType(BoolType)))
assert.Equal(t, NewUnionType(
NewMapType(NewUnionType(BoolType, NewOutputType(BoolType))),
NewOutputType(NewMapType(BoolType))), InputType(NewMapType(BoolType)))
assert.Equal(t, NewUnionType(
NewListType(NewUnionType(BoolType, NewOutputType(BoolType))),
NewOutputType(NewListType(BoolType))), InputType(NewListType(BoolType)))
assert.Equal(t, NewUnionType(
NewUnionType(BoolType, IntType, NewOutputType(BoolType), NewOutputType(IntType)),
NewOutputType(NewUnionType(BoolType, IntType))),
InputType(NewUnionType(BoolType, IntType)))
assert.Equal(t, NewUnionType(
NewObjectType(map[string]Type{"foo": NewUnionType(BoolType, NewOutputType(BoolType))}),
NewOutputType(NewObjectType(map[string]Type{"foo": BoolType}))),
InputType(NewObjectType(map[string]Type{"foo": BoolType})))
assert.True(t, InputType(BoolType).ConversionFrom(BoolType).Exists())
assert.True(t, InputType(NumberType).ConversionFrom(NumberType).Exists())
}
func assertUnified(t *testing.T, expectedSafe, expectedUnsafe Type, types ...Type) {
actualSafe, actualUnsafe := UnifyTypes(types...)
assert.Equal(t, expectedSafe, actualSafe)
assert.Equal(t, expectedUnsafe, actualUnsafe)
// Reverse the types and ensure we get the same results.
for i, j := 0, len(types)-1; i < j; i, j = i+1, j-1 {
types[i], types[j] = types[j], types[i]
}
actualSafe2, actualUnsafe2 := UnifyTypes(types...)
assert.Equal(t, actualSafe, actualSafe2)
assert.Equal(t, actualUnsafe, actualUnsafe2)
}
func TestUnifyType(t *testing.T) {
t.Parallel()
// Number, int, and bool unify with string by preferring string.
assertUnified(t, StringType, StringType, NumberType, StringType)
assertUnified(t, StringType, StringType, IntType, StringType)
assertUnified(t, StringType, StringType, BoolType, StringType)
// Number and int unify by preferring number.
assertUnified(t, NumberType, NumberType, IntType, NumberType)
// Number or int and bool unify by preferring number or int.
assertUnified(t, NewUnionType(NumberType, BoolType), NumberType, BoolType, NumberType)
assertUnified(t, NewUnionType(IntType, BoolType), IntType, BoolType, IntType)
// Two collection types of the same kind unify according to the unification of their element types.
assertUnified(t, NewMapType(StringType), NewMapType(StringType), NewMapType(BoolType), NewMapType(StringType))
assertUnified(t, NewListType(StringType), NewListType(StringType), NewListType(BoolType), NewListType(StringType))
assertUnified(t, NewSetType(StringType), NewSetType(StringType), NewSetType(BoolType), NewSetType(StringType))
// List and set types unify by preferring the list type.
assertUnified(t, NewListType(StringType), NewListType(StringType), NewListType(StringType), NewSetType(BoolType))
assertUnified(t, NewListType(StringType), NewListType(StringType), NewListType(BoolType), NewSetType(StringType))
assert.True(t, StringType.ConversionFrom(NewOptionalType(NewUnionType(NewMapType(StringType), BoolType))).Exists())
// Map and object types unify by preferring the map type.
m0, m1 := NewObjectType(map[string]Type{"foo": StringType}), NewObjectType(map[string]Type{"foo": BoolType})
assertUnified(t, NewMapType(StringType), NewMapType(StringType), m0, NewMapType(BoolType))
assertUnified(t, NewMapType(StringType), NewMapType(StringType), m1, NewMapType(StringType))
// List or set and tuple types unify by preferring the list or set type.
t0, t1 := NewTupleType(NumberType, BoolType), NewTupleType(StringType, NumberType)
assertUnified(t, NewListType(StringType), NewListType(StringType), t0, NewListType(StringType))
assertUnified(t, NewListType(StringType), NewListType(StringType), t1, NewListType(BoolType))
assertUnified(t, NewUnionType(t0, NewSetType(StringType)), NewSetType(StringType), t0, NewSetType(StringType))
assertUnified(t, NewUnionType(t1, NewSetType(BoolType)), NewSetType(StringType), t1, NewSetType(BoolType))
// The dynamic type unifies with any other type by selecting the other type.
assertUnified(t, NewUnionType(BoolType, DynamicType), BoolType, BoolType, DynamicType)
// Object types unify by constructing a new object type whose attributes are the unification of the two input types.
m2 := NewObjectType(map[string]Type{"bar": StringType})
m3 := NewObjectType(map[string]Type{"foo": NewOptionalType(StringType), "bar": NewOptionalType(StringType)})
m4 := NewObjectType(map[string]Type{"foo": NewMapType(StringType), "bar": NewListType(StringType)})
m5 := NewObjectType(map[string]Type{
"foo": NewOptionalType(NewUnionType(NewMapType(StringType), StringType, NoneType)),
"bar": NewOptionalType(NewUnionType(NewListType(StringType), StringType, NoneType)),
})
assertUnified(t, m0, m0, m0, m1)
assertUnified(t, m3, m3, m0, m2)
assertUnified(t, m5, m5, m4, m2, m0, m1)
assertUnified(t, m5, m5, m4, m0, m2, m1)
// Tuple types unify by constructing a new tuple type whose element types are the unification of the corresponding
// element types.
t2 := NewTupleType(StringType, NumberType)
t3 := NewTupleType(StringType, IntType)
t4 := NewTupleType(NumberType, BoolType, StringType)
t5 := NewTupleType(NumberType, BoolType, NewOptionalType(StringType))
assertUnified(t, NewUnionType(t0, t1), t2, t0, t1)
assertUnified(t, t2, t2, t3, t1)
assertUnified(t, t5, t5, t4, t0)
//
// assertUnified(t, NewUnionType(BoolType, IntType), IntType, BoolType, IntType)
// assertUnified(t, NewOptionalType(NumberType), NewOptionalType(NumberType), IntType, NewOptionalType(NumberType))
// assertUnified(t, NewOptionalType(BoolType), NewOptionalType(BoolType), BoolType, NewOptionalType(BoolType))
// assertUnified(t, NewOutputType(BoolType), NewOutputType(BoolType), BoolType, NewOutputType(BoolType))
// assertUnified(t, NewPromiseType(BoolType), NewPromiseType(BoolType), BoolType, NewPromiseType(BoolType))
// assertUnified(t, AnyType, AnyType, BoolType, IntType, AnyType)
//
// assertUnified(t, BoolType, BoolType, DynamicType, BoolType)
// t0 := Type(NewObjectType(map[string]Type{"foo": IntType}))
// t1 := Type(NewObjectType(map[string]Type{"foo": IntType, "bar": NewOptionalType(NumberType)}))
//
// assert.Equal(t, NewMapType(AnyType), unifyTypes(NewMapType(AnyType), t0))
// assert.Equal(t, t1, unifyTypes(t0, t1))
// assert.Equal(t, t1, unifyTypes(t1, t0))
//
// t0 = NewOutputType(NumberType)
// t1 = NewOutputType(NewUnionType(NumberType, IntType))
// assert.Equal(t, t0, unifyTypes(t0, t1))
// assert.Equal(t, t0, unifyTypes(t1, t0))
}
func TestRecursiveObjectType(t *testing.T) {
t.Parallel()
makeType := func(prop string) Type {
props := map[string]Type{
prop: NewOutputType(IntType),
}
objType := NewObjectType(props)
props["sibling"] = objType
listType := NewListType(objType)
linkedListType := NewOptionalType(listType)
props["next"] = linkedListType
return linkedListType
}
linkedListType := makeType("data")
linkedListTypeEqual := makeType("data")
linkedListTypeNonEqual := makeType("data1")
// Equals
assert.True(t, linkedListType.Equals(linkedListTypeEqual))
assert.False(t, linkedListType.Equals(linkedListTypeNonEqual))
// String conversion
// Note: 'next' property is not visible because the string value is memoized at the time of Optional creation.
assert.Equal(t, "union(list(object({data = output(int), sibling = ...})), none)", linkedListType.String())
// Convert from another type
assert.Equal(t, UnsafeConversion, linkedListType.ConversionFrom(linkedListTypeNonEqual))
// Contains eventuals
hasOutputs, hasPromises := ContainsEventuals(linkedListType)
assert.True(t, hasOutputs)
assert.False(t, hasPromises)
// Resolving eventuals
resolvedLinkedListType := ResolveOutputs(linkedListType)
data := resolvedLinkedListType.(*UnionType).ElementTypes[0].(*ListType).ElementType.(*ObjectType).Properties["data"]
assert.True(t, data.Equals(IntType))
hasOutputs, _ = ContainsEventuals(resolvedLinkedListType)
assert.False(t, hasOutputs)
// InputType conversion
inputLinkedListType := InputType(resolvedLinkedListType)
hasOutputs, _ = ContainsEventuals(inputLinkedListType)
assert.True(t, hasOutputs)
}