forked from Azure/bicep
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cqthyy/params integration unit tests (Azure#7352)
* Bicep parameter file array & object unit tests * created bicepparam sample file for integration test, unit test fixes * add integration tests first iteration * comment cleanup * Bicep param file integration test fixes * changed SyntaxCollectorVisitor to public helper class, added more test cases for integration test * error message fix * one more error message fix * semantic token test fixes * integration test baseline update * additional integration test baseline update * additional integration test baseline update * additional integration test baseline update * additional integration test baseline update Co-authored-by: Cathy Cao <[email protected]>
- Loading branch information
Showing
12 changed files
with
621 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
*.bicep -text | ||
*.ts text eol=lf | ||
*.cs text eol=lf | ||
*.sh text eol=lf | ||
/src/Bicep.Core.Samples/Files/**/Assets/**/*.txt -text | ||
|
||
*.bicep -text | ||
*.bicepparam -text | ||
*.ts text eol=lf | ||
*.cs text eol=lf | ||
*.sh text eol=lf | ||
/src/Bicep.Core.Samples/Files/**/Assets/**/*.txt -text |
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,77 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
using Bicep.Core.UnitTests.Assertions; | ||
using Bicep.Core.Parsing; | ||
using Bicep.Core.Samples; | ||
using Bicep.Core.UnitTests.Utils; | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System.Linq; | ||
using System; | ||
|
||
namespace Bicep.Core.IntegrationTests | ||
{ | ||
[TestClass] | ||
public class ParamsParserTests | ||
{ | ||
[NotNull] | ||
public TestContext? TestContext { get; set; } | ||
|
||
[DataTestMethod] | ||
[DynamicData(nameof(GetParamData), DynamicDataSourceType.Method, DynamicDataDisplayNameDeclaringType = typeof(DataSet), DynamicDataDisplayName = nameof(DataSet.GetDisplayName))] | ||
[TestCategory(BaselineHelper.BaselineTestCategory)] | ||
public void Params_Parser_should_produce_expected_syntax(DataSet dataSet) | ||
{ | ||
if (dataSet.BicepParam is null) | ||
{ | ||
throw new InvalidOperationException($"Expected {nameof(dataSet.BicepParam)} to be non-null"); | ||
} | ||
|
||
var program = ParamsParserHelper.ParamsParse(dataSet.BicepParam); | ||
var syntaxList = SyntaxCollectorVisitorHelper.SyntaxCollectorVisitor.Build(program); | ||
var syntaxByParent = syntaxList.ToLookup(x => x.Parent); | ||
|
||
string getLoggingString(SyntaxCollectorVisitorHelper.SyntaxCollectorVisitor.SyntaxItem data) | ||
{ | ||
// Build a visual graph with lines to help understand the syntax hierarchy | ||
var graphPrefix = ""; | ||
if (data.Depth > 0) | ||
{ | ||
var lastSibling = syntaxByParent[data.Parent].Last(); | ||
var isLast = data.Syntax == lastSibling.Syntax; | ||
|
||
graphPrefix = string.Concat(Enumerable.Repeat("| ", data.Depth - 1)); | ||
graphPrefix += isLast switch { | ||
true => "└─", | ||
_ => "├─", | ||
}; | ||
} | ||
|
||
return data.Syntax switch { | ||
Token token => $"{graphPrefix}Token({token.Type}) |{OutputHelper.EscapeWhitespace(token.Text)}|", | ||
_ => $"{graphPrefix}{data.Syntax.GetType().Name}", | ||
}; | ||
} | ||
|
||
TextSpan getSpan(SyntaxCollectorVisitorHelper.SyntaxCollectorVisitor.SyntaxItem data) => data.Syntax.Span; | ||
|
||
var sourceTextWithDiags = DataSet.AddDiagsToParamSourceText(dataSet, syntaxList, getSpan, getLoggingString); | ||
var resultsFile = FileHelper.SaveResultFile(this.TestContext, Path.Combine(dataSet.Name, DataSet.TestFileMainParamSyntax), sourceTextWithDiags); | ||
|
||
sourceTextWithDiags.Should().EqualWithLineByLineDiffOutput( | ||
TestContext, | ||
dataSet.ParamSyntax ?? throw new InvalidOperationException($"Expected {nameof(dataSet.ParamSyntax)} to be non-null."), | ||
expectedLocation: DataSet.GetBaselineUpdatePath(dataSet, DataSet.TestFileMainParamSyntax), | ||
actualLocation: resultsFile); | ||
} | ||
|
||
private static IEnumerable<object[]> GetParamData() | ||
{ | ||
return DataSets.ParamDataSets.ToDynamicTestData(); | ||
} | ||
|
||
} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
src/Bicep.Core.IntegrationTests/SyntaxCollectorVisitorHelper.cs
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,44 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using Bicep.Core.Syntax; | ||
|
||
namespace Bicep.Core.IntegrationTests | ||
{ | ||
public class SyntaxCollectorVisitorHelper | ||
{ | ||
public class SyntaxCollectorVisitor : SyntaxVisitor | ||
{ | ||
public record SyntaxItem(SyntaxBase Syntax, SyntaxBase? Parent, int Depth); | ||
|
||
private readonly IList<SyntaxItem> syntaxList = new List<SyntaxItem>(); | ||
private SyntaxBase? parent = null; | ||
private int depth = 0; | ||
|
||
private SyntaxCollectorVisitor() | ||
{ | ||
} | ||
|
||
public static ImmutableArray<SyntaxItem> Build(ProgramSyntax syntax) | ||
{ | ||
var visitor = new SyntaxCollectorVisitor(); | ||
visitor.Visit(syntax); | ||
|
||
return visitor.syntaxList.ToImmutableArray(); | ||
} | ||
|
||
protected override void VisitInternal(SyntaxBase syntax) | ||
{ | ||
syntaxList.Add(new(Syntax: syntax, Parent: parent, Depth: depth)); | ||
|
||
var prevParent = parent; | ||
parent = syntax; | ||
depth++; | ||
base.VisitInternal(syntax); | ||
depth--; | ||
parent = prevParent; | ||
} | ||
} | ||
} | ||
} |
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
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.