-
Notifications
You must be signed in to change notification settings - Fork 727
/
UserDefinedTypeTests.cs
1595 lines (1381 loc) · 57.3 KB
/
UserDefinedTypeTests.cs
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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Diagnostics.CodeAnalysis;
using Bicep.Core.CodeAction;
using Bicep.Core.Diagnostics;
using Bicep.Core.UnitTests;
using Bicep.Core.UnitTests.Assertions;
using Bicep.Core.UnitTests.Utils;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
namespace Bicep.Core.IntegrationTests;
[TestClass]
public class UserDefinedTypeTests
{
[NotNull]
public TestContext? TestContext { get; set; }
[TestMethod]
public void Type_declarations_are_enabled()
{
var result = CompilationHelper.Compile(@"
type myString = string
");
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
}
[TestMethod]
public void Inline_object_types_are_enabled()
{
var result = CompilationHelper.Compile(@"
param complexParam {
property1: string
property2: string
}
");
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
}
[TestMethod]
public void Inline_type_literals_are_enabled()
{
var result = CompilationHelper.Compile(@"
param thirtyThreeParam 33
");
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
}
[TestMethod]
public void Inline_union_types_are_enabled()
{
var result = CompilationHelper.Compile(@"
param oneOfSeveralStrings 'this one'|'that one'|'perhaps this one instead'
");
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
}
[TestMethod]
public void Nullable_types_are_enabled()
{
var result = CompilationHelper.Compile(@"
param nullableString string?
");
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
}
[TestMethod]
public void Namespaces_cannot_be_used_as_types()
{
var result = CompilationHelper.Compile(@"
param azParam az
");
result.Should().ContainDiagnostic("BCP306", DiagnosticLevel.Error, "The name \"az\" refers to a namespace, not to a type.");
}
[TestMethod]
public void Namespaces_cannot_be_assigned_to_types()
{
var result = CompilationHelper.Compile(@"
type sysAlias = sys
");
result.Should().ContainDiagnostic("BCP306", DiagnosticLevel.Error, "The name \"sys\" refers to a namespace, not to a type.");
}
[TestMethod]
public void Masked_types_still_accessible_via_qualified_reference()
{
var result = CompilationHelper.Compile(@"
type string = int
param stringParam string = 'foo'
");
result.Should().HaveDiagnostics(new[] {
("no-unused-params", DiagnosticLevel.Warning, "Parameter \"stringParam\" is declared but never used."),
("BCP033", DiagnosticLevel.Error, "Expected a value of type \"int\" but the provided value is of type \"'foo'\"."),
});
// fix by fully-qualifying
result = CompilationHelper.Compile(@"
type string = int
param stringParam sys.string = 'foo'
");
result.Should().HaveDiagnostics(new[] {
("no-unused-params", DiagnosticLevel.Warning, "Parameter \"stringParam\" is declared but never used."),
});
}
[TestMethod]
public void Constraint_decorators_prohibited_on_type_refs()
{
var result = CompilationHelper.Compile(@"
@minLength(3)
@maxLength(5)
@description('A string with a bunch of constraints')
type constrainedString = string
@minValue(3)
@maxValue(5)
@description('An int with a bunch of constraints')
type constrainedInt = int
@minLength(3)
@maxLength(5)
@description('A type alias with a bunch of constraints pointing to another type alias')
type constrainedStringAlias = constrainedString
@minValue(3)
@maxValue(5)
@description('A type alias with a bunch of constraints pointing to another type alias')
type constrainedIntAlias = constrainedInt
@minLength(3)
@maxLength(5)
@secure()
@allowed(['fizz', 'buzz', 'pop'])
@description('A parameter with a bunch of constraints that uses a type alias')
param stringParam constrainedString
@minValue(3)
@maxValue(5)
@allowed([3, 4, 5])
@description('A parameter with a bunch of constraints that uses a type alias')
param intParam constrainedInt
");
result.Should().HaveDiagnostics(new[] {
("BCP308", DiagnosticLevel.Error, "The decorator \"minLength\" may not be used on statements whose declared type is a reference to a user-defined type."),
("BCP308", DiagnosticLevel.Error, "The decorator \"maxLength\" may not be used on statements whose declared type is a reference to a user-defined type."),
("BCP308", DiagnosticLevel.Error, "The decorator \"minValue\" may not be used on statements whose declared type is a reference to a user-defined type."),
("BCP308", DiagnosticLevel.Error, "The decorator \"maxValue\" may not be used on statements whose declared type is a reference to a user-defined type."),
("BCP308", DiagnosticLevel.Error, "The decorator \"minLength\" may not be used on statements whose declared type is a reference to a user-defined type."),
("BCP308", DiagnosticLevel.Error, "The decorator \"maxLength\" may not be used on statements whose declared type is a reference to a user-defined type."),
("BCP308", DiagnosticLevel.Error, "The decorator \"secure\" may not be used on statements whose declared type is a reference to a user-defined type."),
("BCP308", DiagnosticLevel.Error, "The decorator \"allowed\" may not be used on statements whose declared type is a reference to a user-defined type."),
("no-unused-params", DiagnosticLevel.Warning, "Parameter \"stringParam\" is declared but never used."),
("BCP308", DiagnosticLevel.Error, "The decorator \"minValue\" may not be used on statements whose declared type is a reference to a user-defined type."),
("BCP308", DiagnosticLevel.Error, "The decorator \"maxValue\" may not be used on statements whose declared type is a reference to a user-defined type."),
("BCP308", DiagnosticLevel.Error, "The decorator \"allowed\" may not be used on statements whose declared type is a reference to a user-defined type."),
("no-unused-params", DiagnosticLevel.Warning, "Parameter \"intParam\" is declared but never used."),
});
}
[TestMethod]
public void Allowed_decorator_may_not_be_used_on_literal_and_union_typed_parameters()
{
var result = CompilationHelper.Compile(@"
@allowed([true])
param trueParam true
@allowed([false])
param falseParam !true
@allowed([1])
param oneParam 1
@allowed([-1])
param negativeOneParam -1
@allowed([{fizz: 'buzz'}])
param fizzBuzzParam {fizz: 'buzz'}
@allowed(['fizz'])
param fizzParam 'fizz'
@allowed(['fizz', 'buzz', 'pop'])
param fizzBuzzPopParam 'fizz'|'buzz'|'pop'
");
result.Should().HaveDiagnostics(new[] {
("BCP295", DiagnosticLevel.Error, "The 'allowed' decorator may not be used on targets of a union or literal type. The allowed values for this parameter or type definition will be derived from the union or literal type automatically."),
("no-unused-params", DiagnosticLevel.Warning, "Parameter \"trueParam\" is declared but never used."),
("BCP295", DiagnosticLevel.Error, "The 'allowed' decorator may not be used on targets of a union or literal type. The allowed values for this parameter or type definition will be derived from the union or literal type automatically."),
("no-unused-params", DiagnosticLevel.Warning, "Parameter \"falseParam\" is declared but never used."),
("BCP295", DiagnosticLevel.Error, "The 'allowed' decorator may not be used on targets of a union or literal type. The allowed values for this parameter or type definition will be derived from the union or literal type automatically."),
("no-unused-params", DiagnosticLevel.Warning, "Parameter \"oneParam\" is declared but never used."),
("BCP295", DiagnosticLevel.Error, "The 'allowed' decorator may not be used on targets of a union or literal type. The allowed values for this parameter or type definition will be derived from the union or literal type automatically."),
("no-unused-params", DiagnosticLevel.Warning, "Parameter \"negativeOneParam\" is declared but never used."),
("BCP295", DiagnosticLevel.Error, "The 'allowed' decorator may not be used on targets of a union or literal type. The allowed values for this parameter or type definition will be derived from the union or literal type automatically."),
("no-unused-params", DiagnosticLevel.Warning, "Parameter \"fizzBuzzParam\" is declared but never used."),
("BCP295", DiagnosticLevel.Error, "The 'allowed' decorator may not be used on targets of a union or literal type. The allowed values for this parameter or type definition will be derived from the union or literal type automatically."),
("no-unused-params", DiagnosticLevel.Warning, "Parameter \"fizzParam\" is declared but never used."),
("BCP295", DiagnosticLevel.Error, "The 'allowed' decorator may not be used on targets of a union or literal type. The allowed values for this parameter or type definition will be derived from the union or literal type automatically."),
("no-unused-params", DiagnosticLevel.Warning, "Parameter \"fizzBuzzPopParam\" is declared but never used."),
});
}
[TestMethod]
public void Unions_that_incorporate_their_parent_object_do_not_blow_the_stack()
{
var blockedBecauseOfCycle = CompilationHelper.Compile(@"
type anObject = {
recur: {foo: 'bar'}|anObject
}
");
blockedBecauseOfCycle.Should().HaveDiagnostics(new[] {
("BCP298", DiagnosticLevel.Error, "This type definition includes itself as required component, which creates a constraint that cannot be fulfilled."),
("BCP293", DiagnosticLevel.Error, "All members of a union type declaration must be literal values."),
});
var blockedBecauseOfUnionSemantics = CompilationHelper.Compile(@"
type anObject = {
recur: {foo: 'bar'}|anObject?
}
");
blockedBecauseOfUnionSemantics.Should().HaveDiagnostics(new[] {
("BCP293", DiagnosticLevel.Error, "All members of a union type declaration must be literal values."),
});
}
[TestMethod]
public void Unary_operations_that_incorporate_their_parent_object_do_not_blow_the_stack()
{
var blockedBecauseOfCycle = CompilationHelper.Compile(@"
type anObject = {
recur: !anObject
}
");
blockedBecauseOfCycle.Should().HaveDiagnostics(new[] {
("BCP285", DiagnosticLevel.Error, "The type expression could not be reduced to a literal value."),
});
}
[TestMethod]
public void Arrays_that_incorporate_their_parent_object_do_not_blow_the_stack()
{
var blockedBecauseOfCycle = CompilationHelper.Compile(@"
type anObject = {
recur: anObject[]
}
");
blockedBecauseOfCycle.Should().HaveDiagnostics(new[] {
("BCP298", DiagnosticLevel.Error, "This type definition includes itself as required component, which creates a constraint that cannot be fulfilled."),
});
var permitted = CompilationHelper.Compile(@"
type anObject = {
recur: (anObject?)[]
}
");
permitted.Should().NotHaveAnyDiagnostics();
permitted = CompilationHelper.Compile(@"
type anArray = (anArray?)[]
");
permitted.Should().NotHaveAnyDiagnostics();
permitted = CompilationHelper.Compile(@"
type anArray = anArray[]?
");
permitted.Should().NotHaveAnyDiagnostics();
}
[TestMethod]
public void Cyclic_nullables_do_not_blow_the_stack()
{
var blockedBecauseOfCycle = CompilationHelper.Compile(@"
type nullable = nullable?
");
blockedBecauseOfCycle.Should().HaveDiagnostics(new[] {
("BCP298", DiagnosticLevel.Error, "This type definition includes itself as required component, which creates a constraint that cannot be fulfilled."),
});
}
[TestMethod]
public void Tuples_that_incorporate_their_parent_object_do_not_blow_the_stack()
{
var blockedBecauseOfCycle = CompilationHelper.Compile(@"
type anObject = {
recur: [anObject]
}
");
blockedBecauseOfCycle.Should().HaveDiagnostics(new[] {
("BCP298", DiagnosticLevel.Error, "This type definition includes itself as required component, which creates a constraint that cannot be fulfilled."),
});
var permitted = CompilationHelper.Compile(@"
type anObject = {
recur: [anObject]?
}
");
permitted.Should().NotHaveAnyDiagnostics();
permitted = CompilationHelper.Compile(@"
type anObject = {
recur: [anObject?]
}
");
permitted.Should().NotHaveAnyDiagnostics();
permitted = CompilationHelper.Compile(@"
type aTuple = [aTuple?]
");
permitted.Should().NotHaveAnyDiagnostics();
permitted = CompilationHelper.Compile(@"
type aTuple = [aTuple]?
");
permitted.Should().NotHaveAnyDiagnostics();
}
[TestMethod]
public void Objects_that_incorporate_their_parent_object_do_not_blow_the_stack()
{
var blockedBecauseOfCycle = CompilationHelper.Compile(@"
type anObject = {
recurEventually: {
recurNow: anObject
}
}
");
blockedBecauseOfCycle.Should().HaveDiagnostics(new[] {
("BCP298", DiagnosticLevel.Error, "This type definition includes itself as required component, which creates a constraint that cannot be fulfilled."),
});
var permitted = CompilationHelper.Compile(@"
type anObject = {
recurEventually: {
recurNow: anObject?
}
}
");
permitted.Should().NotHaveAnyDiagnostics();
permitted = CompilationHelper.Compile(@"
type anObject = {
recurEventually: {
recurNow: anObject
}?
}
");
permitted.Should().NotHaveAnyDiagnostics();
permitted = CompilationHelper.Compile(@"
type anObject = {
recurEventually: {
recurNow: anObject
}
}?
");
permitted.Should().NotHaveAnyDiagnostics();
}
[TestMethod]
public void Cyclic_check_understands_nullability_modifiers()
{
var blockedBecauseOfCycle = CompilationHelper.Compile(@"
type anObject = {
recurEventually: {
recurNow: anObject!
}
}?
");
blockedBecauseOfCycle.Should().HaveDiagnostics(new[] {
("BCP298", DiagnosticLevel.Error, "This type definition includes itself as required component, which creates a constraint that cannot be fulfilled."),
});
blockedBecauseOfCycle = CompilationHelper.Compile(@"
type anObject = {
recurEventually: {
recurNow: anObject
}
}?!
");
blockedBecauseOfCycle.Should().HaveDiagnostics(new[] {
("BCP298", DiagnosticLevel.Error, "This type definition includes itself as required component, which creates a constraint that cannot be fulfilled."),
});
var permitted = CompilationHelper.Compile(@"
type anObject = {
recurEventually: {
recurNow: anObject!?
}
}?
");
permitted.Should().NotHaveAnyDiagnostics();
}
[TestMethod]
public void Warning_should_be_shown_when_reading_unknown_properties_on_unsealed_objects()
{
var result = CompilationHelper.Compile(@"
param anObject {}
output prop string = anObject.prop
");
result.Should().HaveDiagnostics(new[] {
("BCP187", DiagnosticLevel.Warning, "The property \"prop\" does not exist in the resource or type definition, although it might still be valid. If this is an inaccuracy in the documentation, please report it to the Bicep Team."),
});
}
[TestMethod]
public void Warning_should_be_shown_when_setting_unknown_properties_on_unsealed_objects()
{
var result = CompilationHelper.Compile(@"
#disable-next-line no-unused-params
param anObject {} = {prop: 'someVal'}
#disable-next-line no-unused-params
param anotherObject object = {prop: 'someVal'}
");
result.Should().HaveDiagnostics(new[] {
("BCP037", DiagnosticLevel.Warning, "The property \"prop\" is not allowed on objects of type \"{ }\". No other properties are allowed."),
});
}
[TestMethod]
public void Empty_object_should_be_distinguishable_from_untyped_object_in_compiled_JSON()
{
var result = CompilationHelper.Compile("""
type emptyObject = {}
type untypedObect = object
""");
result.Template.Should().HaveValueAtPath("definitions.emptyObject.properties", new JObject());
result.Template.Should().NotHaveValueAtPath("definitions.untypedObject.properties");
}
[TestMethod]
public void Error_should_be_shown_when_setting_unknown_properties_that_do_not_match_additional_properties_type()
{
var result = CompilationHelper.Compile(@"
#disable-next-line no-unused-params
param aDict {
*: int
} = {prop: 'someVal'}
");
result.Should().HaveDiagnostics(new[] {
("BCP036", DiagnosticLevel.Error, @"The property ""prop"" expected a value of type ""int"" but the provided value is of type ""'someVal'""."),
});
result = CompilationHelper.Compile(@"
#disable-next-line no-unused-params
param aDict {
*: string
} = {prop: 'someVal'}
");
result.Should().NotHaveAnyDiagnostics();
}
[TestMethod]
public void Additional_properties_may_be_used_alongside_named_properties()
{
var result = CompilationHelper.Compile(@"
#disable-next-line no-unused-params
param aDict {
knownProp: int
*: string
} = {
knownProp: 21
prop: 'someVal'
}
");
result.Should().NotHaveAnyDiagnostics();
}
[TestMethod]
public void Constraint_decorators_can_be_used_on_nullably_typed_params()
{
var result = CompilationHelper.Compile(@"
@minLength(3)
@maxLength(10)
@secure()
#disable-next-line no-unused-params
param constrainedString string?
@minValue(3)
@maxValue(10)
type constrainedInt = int?
@minLength(3)
@maxLength(10)
type constrainedArray = array?
@sealed()
@secure()
#disable-next-line no-unused-params
param sealedObject {}?
");
result.Should().NotHaveAnyDiagnostics();
}
[TestMethod]
public void Nullably_typed_values_can_be_used_as_nonnullable_outputs_with_postfix_assertion()
{
var templateWithPossiblyNullDeref = @"
param foos (null | { bar: { baz: { quux: 'quux' } } })[]
output quux string = foos[0].bar.baz.quux
";
var templateWithNonNullAssertion = @"
param foos (null | { bar: { baz: { quux: 'quux' } } })[]
output quux string = foos[0]!.bar.baz.quux
";
var templateWithSafeDeref = @"
param foos (null | { bar: { baz: { quux: 'quux' } } })[]
output quux string = foos[0].?bar.baz.quux
";
var result = CompilationHelper.Compile(templateWithPossiblyNullDeref);
result.Should().HaveDiagnostics(new[]
{
("BCP318", DiagnosticLevel.Warning, @"The value of type ""null | { bar: { baz: { quux: 'quux' } } }"" may be null at the start of the deployment, which would cause this access expression (and the overall deployment with it) to fail."),
});
result.Diagnostics.Single().Should().BeAssignableTo<IFixable>();
var fixAlternatives = new HashSet<string> { templateWithNonNullAssertion, templateWithSafeDeref };
foreach (var fix in result.Diagnostics.Single().As<IFixable>().Fixes)
{
fix.Replacements.Should().HaveCount(1);
var replacement = fix.Replacements.Single();
var actualText = templateWithPossiblyNullDeref.Remove(replacement.Span.Position, replacement.Span.Length);
actualText = actualText.Insert(replacement.Span.Position, replacement.Text);
fixAlternatives.Remove(actualText);
}
fixAlternatives.Should().BeEmpty();
result = CompilationHelper.Compile(templateWithNonNullAssertion);
result.Should().NotHaveAnyDiagnostics();
result.Should().HaveTemplateWithOutput("quux", "[parameters('foos')[0].bar.baz.quux]");
}
[TestMethod]
public void Error_should_be_emitted_when_setting_a_default_value_on_a_nullable_parameter()
{
var result = CompilationHelper.Compile(@"
#disable-next-line no-unused-params
param myParam string? = 'foo'
");
result.Should().HaveDiagnostics(new[] {
("BCP326", DiagnosticLevel.Error, "Nullable-typed parameters may not be assigned default values. They have an implicit default of 'null' that cannot be overridden."),
});
}
[TestMethod]
public void Tuples_with_a_literal_index_use_type_at_index()
{
var result = CompilationHelper.Compile(
("main.bicep", @"
var myArray = ['foo', 'bar']
module mod './mod.bicep' = {
name: 'mod'
params: {
myParam: myArray[0]
}
}
"),
("mod.bicep", @"
param myParam 'foo'
"));
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Template.Should().NotBeNull();
}
[TestMethod]
public void Tuples_with_a_literal_union_index_use_type_at_indices()
{
var result = CompilationHelper.Compile(
("main.bicep", @"
param index 0 | 1
var myArray = ['foo', 'bar', 'baz']
module mod './mod.bicep' = {
name: 'mod'
params: {
myParam: myArray[index]
}
}
"),
("mod.bicep", @"
param myParam 'foo' | 'bar'
"));
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Template.Should().NotBeNull();
}
[TestMethod]
public void Constraint_decorators_permitted_on_outputs()
{
var result = CompilationHelper.Compile(@"
@minLength(3)
@maxLength(5)
@description('A string with a bunch of constraints')
output foo string = 'foo'
");
result.Should().NotHaveAnyDiagnostics();
}
[TestMethod]
public void User_defined_types_may_be_used_with_outputs()
{
var result = CompilationHelper.Compile(@"
@minLength(3)
@maxLength(4)
type constrainedString = string
output arrayOfConstrainedStrings constrainedString[] = ['fizz', 'buzz', 'pop']
");
result.Should().NotHaveAnyDiagnostics();
}
public void Type_aliases_incorporate_modifiers_into_type()
{
var result = CompilationHelper.Compile(@"
@maxLength(2)
type shortString = string
param myString shortString = 'foo'
");
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[]
{
("BCP332", DiagnosticLevel.Error, "The provided value (whose length will always be greater than or equal to 3) is too long to assign to a target for which the maximum allowable length is 2."),
});
}
[TestMethod]
public void Impossible_integer_domains_raise_descriptive_error()
{
var result = CompilationHelper.Compile(@"
@minValue(1)
@maxValue(0)
param myParam int
");
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[]
{
("BCP331", DiagnosticLevel.Error, "A type's \"minValue\" must be less than or equal to its \"maxValue\", but a minimum of 1 and a maximum of 0 were specified."),
});
}
[TestMethod]
public void Impossible_array_length_domains_raise_descriptive_error()
{
var result = CompilationHelper.Compile(@"
@minLength(1)
@maxLength(0)
param myParam array
");
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[]
{
("BCP331", DiagnosticLevel.Error, "A type's \"minLength\" must be less than or equal to its \"maxLength\", but a minimum of 1 and a maximum of 0 were specified."),
});
}
[TestMethod]
public void Impossible_string_length_domains_raise_descriptive_error()
{
var result = CompilationHelper.Compile(@"
@minLength(1)
@maxLength(0)
param myParam string
");
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[]
{
("BCP331", DiagnosticLevel.Error, "A type's \"minLength\" must be less than or equal to its \"maxLength\", but a minimum of 1 and a maximum of 0 were specified."),
});
}
[TestMethod]
public void Duplicate_property_names_should_raise_descriptive_diagnostic()
{
var result = CompilationHelper.Compile("""
type foo = {
bar: bool
bar: string
}
""");
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[]
{
("BCP025", DiagnosticLevel.Error, "The property \"bar\" is declared multiple times in this object. Remove or rename the duplicate properties.")
});
}
[TestMethod]
public void Union_types_with_single_normalized_member_compile_without_error()
{
var result = CompilationHelper.Compile("""
type union = 'a' | 'a'
""");
result.Should().NotHaveAnyDiagnostics();
result.Template.Should().NotBeNull();
result.Template!.Should().HaveValueAtPath("definitions.union", JToken.Parse("""
{
"type": "string",
"allowedValues": ["a"]
}
"""));
}
[TestMethod]
public void Nullable_union_types_do_not_include_null_in_allowed_values_constraint()
{
var result = CompilationHelper.Compile("""
type union = 'a' | 'b' | 'c' | null
type unionWithOneMember = null | 'a'
""");
result.Should().NotHaveAnyDiagnostics();
result.Template.Should().NotBeNull();
result.Template!.Should().HaveValueAtPath("definitions.union", JToken.Parse("""
{
"type": "string",
"allowedValues": ["a", "b", "c"],
"nullable": true
}
"""));
result.Template!.Should().HaveValueAtPath("definitions.unionWithOneMember", JToken.Parse("""
{
"type": "string",
"allowedValues": ["a"],
"nullable": true
}
"""));
}
[TestMethod]
public void Param_with_null_in_allowedValues_constraint_can_be_loaded()
{
var result = CompilationHelper.Compile(
("main.bicep", """
module mod 'mod.json' = {
name: 'mod'
params: {
foo: 'foo'
}
}
"""),
("mod.json", """
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"languageVersion": "2.0",
"contentVersion": "1.0.0.0",
"parameters": {
"foo": {
"type": "string",
"allowedValues": ["foo", "bar", "baz", null]
}
},
"resources": []
}
"""));
result.Should().NotHaveAnyDiagnostics();
}
[TestMethod]
public void Self_reference_permitted_in_object_type_additional_properties()
{
var result = CompilationHelper.Compile("""
type anObject = {
id: int
flag: bool
someData: string
*: anObject
}
""");
result.Should().NotHaveAnyDiagnostics();
}
// https://github.com/azure/bicep/issues/12070
[TestMethod]
public void Self_property_deref_does_not_blow_the_stack()
{
var result = CompilationHelper.Compile("""
type anObject = {
property: anObject.property
}
""");
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[]
{
("BCP298", DiagnosticLevel.Error, "This type definition includes itself as required component, which creates a constraint that cannot be fulfilled."),
("BCP062", DiagnosticLevel.Error, "The referenced declaration with name \"anObject\" is not valid."),
});
}
// https://github.com/azure/bicep/issues/12070
[TestMethod]
public void Self_array_access_does_not_blow_the_stack()
{
var result = CompilationHelper.Compile("""
type anObject = {
property: anObject['property']
}
""");
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[]
{
("BCP298", DiagnosticLevel.Error, "This type definition includes itself as required component, which creates a constraint that cannot be fulfilled."),
("BCP062", DiagnosticLevel.Error, "The referenced declaration with name \"anObject\" is not valid."),
});
}
[TestMethod]
[Timeout(5_000)]
public void Parsing_incomplete_tuple_type_expressions_halts()
{
var result = CompilationHelper.Compile("""
type myType = {
name: [string
}
""");
result.Template.Should().BeNull();
}
[TestMethod]
public void Resource_derived_type_should_compile_successfully()
{
var result = CompilationHelper.Compile(new UnitTests.ServiceBuilder().WithFeatureOverrides(new(TestContext, ResourceDerivedTypesEnabled: true)),
"""
type myType = resource<'Microsoft.Storage/storageAccounts@2022-09-01'>.name
""");
result.Template.Should().HaveValueAtPath("definitions", JToken.Parse($$"""
{
"myType": {
"type": "string",
"metadata": {
"{{LanguageConstants.MetadataResourceDerivedTypePropertyName}}": "Microsoft.Storage/storageAccounts@2022-09-01#properties/name"
}
}
}
"""));
}
[TestMethod]
public void Resource_derived_type_should_compile_successfully_with_namespace_qualified_syntax()
{
var result = CompilationHelper.Compile(new UnitTests.ServiceBuilder().WithFeatureOverrides(new(TestContext, ResourceDerivedTypesEnabled: true)),
"""
var resource = 'foo'
type myType = sys.resource<'Microsoft.Storage/storageAccounts@2022-09-01'>.name
""");
result.Template.Should().HaveValueAtPath("definitions", JToken.Parse($$"""
{
"myType": {
"type": "string",
"metadata": {
"{{LanguageConstants.MetadataResourceDerivedTypePropertyName}}": "Microsoft.Storage/storageAccounts@2022-09-01#properties/name"
}
}
}
"""));
}
[TestMethod]
public void Param_with_resource_derived_type_can_be_loaded()
{
var result = CompilationHelper.Compile(new UnitTests.ServiceBuilder().WithFeatureOverrides(new(TestContext, ResourceDerivedTypesEnabled: true)),
("main.bicep", """
param location string = resourceGroup().location
module mod 'mod.json' = {
name: 'mod'
params: {
foo: {
bar: {
name: 'acct'
location: location
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
}
properties: {
unknownProperty: false
}
}
}
}
}
"""),
("mod.json", $$"""
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"languageVersion": "2.0",
"contentVersion": "1.0.0.0",
"parameters": {
"foo": {
"type": "object",
"additionalProperties": {
"type": "object",
"metadata": {
"{{LanguageConstants.MetadataResourceDerivedTypePropertyName}}": "Microsoft.Storage/storageAccounts@2022-09-01"
}
}
}
},
"resources": []
}
"""));
result.Should().NotHaveAnyCompilationBlockingDiagnostics();
result.Should().HaveDiagnostics(new[]
{
("BCP037", DiagnosticLevel.Warning, """The property "unknownProperty" is not allowed on objects of type "StorageAccountPropertiesCreateParametersOrStorageAccountProperties". Permissible properties include "accessTier", "allowBlobPublicAccess", "allowCrossTenantReplication", "allowedCopyScope", "allowSharedKeyAccess", "azureFilesIdentityBasedAuthentication", "customDomain", "defaultToOAuthAuthentication", "dnsEndpointType", "encryption", "immutableStorageWithVersioning", "isHnsEnabled", "isLocalUserEnabled", "isNfsV3Enabled", "isSftpEnabled", "keyPolicy", "largeFileSharesState", "minimumTlsVersion", "networkAcls", "publicNetworkAccess", "routingPreference", "sasPolicy", "supportsHttpsTrafficOnly"."""),
});
}
[TestMethod]
public void Param_with_resource_derived_type_property_can_be_loaded()
{
var result = CompilationHelper.Compile(new UnitTests.ServiceBuilder().WithFeatureOverrides(new(TestContext, ResourceDerivedTypesEnabled: true)),
("main.bicep", """
@minLength(2)
param saName string
module mod 'mod.json' = {
name: 'mod'
params: {
saName: saName
connectionParameterType: 'sting'
ipRuleAction: 'Deny'
}
}
"""),
("mod.json", $$"""
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"languageVersion": "2.0",
"contentVersion": "1.0.0.0",
"parameters": {
"saName": {
"type": "string",
"metadata": {
"{{LanguageConstants.MetadataResourceDerivedTypePropertyName}}": "Microsoft.Storage/storageAccounts@2022-09-01#properties/name"
}
},
"connectionParameterType": {