-
Notifications
You must be signed in to change notification settings - Fork 742
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a5e1cb9
commit 3b87b56
Showing
41 changed files
with
1,009 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using Bicep.Core.UnitTests.Assertions; | ||
using Bicep.Core.UnitTests.Utils; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Bicep.Core.IntegrationTests; | ||
|
||
[TestClass] | ||
public class SpreadTests | ||
{ | ||
[TestMethod] | ||
public void Spread_operator_results_in_correct_codegen() | ||
{ | ||
var result = CompilationHelper.Compile(""" | ||
var other = { | ||
bar: [1, ...[2, 3], 4] | ||
} | ||
|
||
output test object = { | ||
foo: 'foo' | ||
...other | ||
baz: 'baz' | ||
} | ||
"""); | ||
|
||
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics(); | ||
result.Template.Should().HaveValueAtPath("$.variables['other']['bar']", "[flatten(createArray(createArray(1), createArray(2, 3), createArray(4)))]"); | ||
result.Template.Should().HaveValueAtPath("$.outputs['test'].value", "[shallowMerge(createArray(createObject('foo', 'foo'), variables('other'), createObject('baz', 'baz')))]"); | ||
|
||
var evaluated = TemplateEvaluator.Evaluate(result.Template); | ||
evaluated.Should().HaveJsonAtPath("$.outputs['test'].value", """ | ||
{ | ||
"foo": "foo", | ||
"bar": [ | ||
1, | ||
2, | ||
3, | ||
4 | ||
], | ||
"baz": "baz" | ||
} | ||
"""); | ||
} | ||
|
||
[TestMethod] | ||
public void Spread_operator_works_on_single_line() | ||
{ | ||
var result = CompilationHelper.Compile(""" | ||
param other object | ||
|
||
var test = { foo: 'foo', ...other, baz: 'baz' } | ||
"""); | ||
|
||
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics(); | ||
} | ||
|
||
[TestMethod] | ||
public void Array_spread_cannot_be_used_inside_object() | ||
{ | ||
var result = CompilationHelper.Compile(""" | ||
var other = ['bar'] | ||
|
||
var test = { | ||
foo: 'foo' | ||
...other | ||
baz: 'baz' | ||
} | ||
"""); | ||
|
||
result.ExcludingLinterDiagnostics().Should().ContainDiagnostic( | ||
"BCP397", Diagnostics.DiagnosticLevel.Error, """The spread operator "..." can only be used in this context for an expression assignable to type "object"."""); | ||
} | ||
|
||
[TestMethod] | ||
public void Object_spread_cannot_be_used_inside_array() | ||
{ | ||
var result = CompilationHelper.Compile(""" | ||
var other = { | ||
bar: 'bar' | ||
} | ||
|
||
var test = [ | ||
'foo' | ||
...other | ||
'baz' | ||
] | ||
"""); | ||
|
||
result.ExcludingLinterDiagnostics().Should().ContainDiagnostic( | ||
"BCP397", Diagnostics.DiagnosticLevel.Error, """The spread operator "..." can only be used in this context for an expression assignable to type "array"."""); | ||
} | ||
|
||
[TestMethod] | ||
public void Spread_works_with_any() | ||
{ | ||
var result = CompilationHelper.Compile(""" | ||
var badObj = { | ||
...any(['foo']) | ||
} | ||
|
||
var badArray = [ | ||
...any({ foo: 'foo' }) | ||
] | ||
"""); | ||
|
||
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics(); | ||
|
||
// this will result in a runtime failure, but at least the codegen is correct. | ||
result.Template.Should().HaveValueAtPath("$.variables['badObj']", "[shallowMerge(createArray(createArray('foo')))]"); | ||
result.Template.Should().HaveValueAtPath("$.variables['badArray']", "[flatten(createArray(createObject('foo', 'foo')))]"); | ||
} | ||
|
||
[TestMethod] | ||
public void Spread_is_blocked_in_resource_body() | ||
{ | ||
var result = CompilationHelper.Compile(""" | ||
var other = { location: 'westus' } | ||
|
||
resource foo 'Microsoft.Storage/storageAccounts@2023-01-01' = { | ||
name: 'foo' | ||
...other | ||
} | ||
"""); | ||
|
||
result.ExcludingLinterDiagnostics().Should().ContainDiagnostic( | ||
"BCP396", Diagnostics.DiagnosticLevel.Error, """The spread operator "..." is not permitted in this location."""); | ||
} | ||
|
||
[TestMethod] | ||
public void Object_spread_edge_cases() | ||
{ | ||
var result = CompilationHelper.Compile(""" | ||
output test1 object = { | ||
a: 0 | ||
...{ a: 1, b: 0 } | ||
c: 0 | ||
} | ||
|
||
output test2 object = { | ||
'ABC': 0 | ||
...{ 'aBC': 1 } | ||
} | ||
|
||
output test3 object = { | ||
foo: 'bar' | ||
...{ foo: null } | ||
} | ||
"""); | ||
|
||
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics(); | ||
|
||
var evaluated = TemplateEvaluator.Evaluate(result.Template); | ||
evaluated.Should().HaveJsonAtPath("$.outputs['test1'].value", """ | ||
{ | ||
"a": 1, | ||
"b": 0, | ||
"c": 0 | ||
} | ||
"""); | ||
evaluated.Should().HaveJsonAtPath("$.outputs['test2'].value", """ | ||
{ | ||
"ABC": 1 | ||
} | ||
"""); | ||
evaluated.Should().HaveJsonAtPath("$.outputs['test3'].value", """ | ||
{ | ||
"foo": null | ||
} | ||
"""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.