-
Notifications
You must be signed in to change notification settings - Fork 818
Expand file tree
/
Copy pathBaseParametersSymbolTests.cs
More file actions
221 lines (193 loc) · 9.69 KB
/
BaseParametersSymbolTests.cs
File metadata and controls
221 lines (193 loc) · 9.69 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Bicep.Core.Semantics;
using Bicep.Core.Syntax;
using Bicep.Core.TypeSystem.Types;
using Bicep.Core.UnitTests.Features;
using Bicep.Core.UnitTests.Utils;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Bicep.Core.UnitTests.Semantics
{
[TestClass]
public class BaseParametersSymbolTests
{
[TestMethod]
public void FileSymbol_should_include_base_parameters_symbol_when_extends_is_present()
{
var services = new ServiceBuilder()
.WithEmptyAzResources()
.WithFeatureOverrides(new(ExtendableParamFilesEnabled: true));
var files = new Dictionary<Uri, string>
{
[new Uri("file:///main.bicep")] = """
param one string = ''
param two string = ''
param three string = ''
""",
[new Uri("file:///shared.bicepparam")] = """
using none
param three = 'param three'
""",
[new Uri("file:///main.bicepparam")] = """
using 'main.bicep'
extends 'shared.bicepparam'
param one = 'param one'
param two = base.three
"""
};
var compilation = services.BuildCompilation(files, new Uri("file:///main.bicepparam"));
var model = compilation.GetEntrypointSemanticModel();
var baseSymbol = model.Root.Declarations.OfType<BaseParametersSymbol>().Single();
baseSymbol.Name.Should().Be(LanguageConstants.BaseIdentifier);
baseSymbol.ParentAssignments.Select(x => x.Name).Should().BeEquivalentTo(["three"]);
model.Root.Declarations.Should().Contain(baseSymbol);
}
[TestMethod]
public void FileSymbol_should_not_include_base_parameters_symbol_when_extends_is_absent()
{
var services = new ServiceBuilder()
.WithEmptyAzResources()
.WithFeatureOverrides(new(ExtendableParamFilesEnabled: true));
var files = new Dictionary<Uri, string>
{
[new Uri("file:///main.bicep")] = """
param one string = ''
param two string = ''
param three string = ''
""",
[new Uri("file:///main.bicepparam")] = """
using 'main.bicep'
param one = 'param one'
param two = 'param two'
"""
};
var compilation = services.BuildCompilation(files, new Uri("file:///main.bicepparam"));
var model = compilation.GetEntrypointSemanticModel();
model.Root.Declarations.OfType<BaseParametersSymbol>().Should().BeEmpty();
model.Root.Declarations.Should().NotContain(x => x.Name == LanguageConstants.BaseIdentifier);
}
[TestMethod]
public void Base_parameters_symbol_should_include_all_inherited_assignments()
{
var services = new ServiceBuilder()
.WithEmptyAzResources()
.WithFeatureOverrides(new(ExtendableParamFilesEnabled: true));
var files = new Dictionary<Uri, string>
{
[new Uri("file:///main.bicep")] = """
param one string = ''
param two string = ''
param three string = ''
param four string = ''
""",
[new Uri("file:///shared.bicepparam")] = """
using none
param three = 'param three'
param four = 'param four'
""",
[new Uri("file:///main.bicepparam")] = """
using 'main.bicep'
extends 'shared.bicepparam'
param one = 'param one'
param two = base.three
"""
};
var compilation = services.BuildCompilation(files, new Uri("file:///main.bicepparam"));
var model = compilation.GetEntrypointSemanticModel();
var baseSymbol = model.Root.Declarations.OfType<BaseParametersSymbol>().Single();
baseSymbol.ParentAssignments.Select(x => x.Name).Should().BeEquivalentTo(["three", "four"]);
}
[TestMethod]
public void Base_variable_access_should_have_object_type_with_read_only_parent_properties()
{
var services = new ServiceBuilder()
.WithEmptyAzResources()
.WithFeatureOverrides(new(ExtendableParamFilesEnabled: true));
var files = new Dictionary<Uri, string>
{
[new Uri("file:///main.bicep")] = """
param one string = ''
param two string = ''
param three string = ''
param four string = ''
""",
[new Uri("file:///shared.bicepparam")] = """
using none
param three = 'param three'
param four = 'param four'
""",
[new Uri("file:///main.bicepparam")] = """
using 'main.bicep'
extends 'shared.bicepparam'
param one = 'param one'
param two = base.three
"""
};
var compilation = services.BuildCompilation(files, new Uri("file:///main.bicepparam"));
var model = compilation.GetEntrypointSemanticModel();
var twoAssignment = model.Root.ParameterAssignments.Single(x => x.Name == "two");
var baseAccess = ((PropertyAccessSyntax)twoAssignment.DeclaringParameterAssignment.Value).BaseExpression
.Should().BeOfType<VariableAccessSyntax>().Subject;
var baseType = model.GetTypeInfo(baseAccess).Should().BeOfType<ObjectType>().Subject;
baseType.Properties.Should().ContainKeys("three", "four");
baseType.Properties["three"].Flags.Should().HaveFlag(TypePropertyFlags.ReadOnly);
baseType.Properties["four"].Flags.Should().HaveFlag(TypePropertyFlags.ReadOnly);
}
[TestMethod]
public void Base_variable_access_should_not_throw_when_inherited_params_include_object_and_array_values()
{
var services = new ServiceBuilder()
.WithEmptyAzResources()
.WithFeatureOverrides(new(ExtendableParamFilesEnabled: true));
var files = new Dictionary<Uri, string>
{
[new Uri("file:///main.bicep")] = """
param one string = ''
param two string = ''
param three string = ''
param four object = {
name: 'four'
value: 'four'
}
param five array = [
{
name: 'five'
value: 'five'
}
]
""",
[new Uri("file:///shared.bicepparam")] = """
using none
param three = 'param three'
param four = {
name: 'param four'
}
param five = [
{
name: 'param five'
}
]
""",
[new Uri("file:///main.bicepparam")] = """
using 'main.bicep'
extends 'shared.bicepparam'
param one = 'param one'
param two = base.three
param five = []
"""
};
var compilation = services.BuildCompilation(files, new Uri("file:///main.bicepparam"));
var model = compilation.GetEntrypointSemanticModel();
FluentActions.Invoking(() => model.GetAllDiagnostics().ToArray()).Should().NotThrow();
var baseAccess = ((PropertyAccessSyntax)model.Root.ParameterAssignments
.Single(x => x.Name == "two")
.DeclaringParameterAssignment
.Value).BaseExpression;
var baseType = model.GetTypeInfo(baseAccess).Should().BeOfType<ObjectType>().Subject;
baseType.Properties.Should().ContainKeys("three", "four", "five");
baseType.Properties["four"].Flags.Should().HaveFlag(TypePropertyFlags.ReadOnly);
baseType.Properties["five"].Flags.Should().HaveFlag(TypePropertyFlags.ReadOnly);
}
}
}