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.
- Loading branch information
Showing
39 changed files
with
2,189 additions
and
131 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
*.arm -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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,72 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using Bicep.Core.IntegrationTests.UnitSamples; | ||
using Bicep.Core.Parser; | ||
using Bicep.Core.Samples; | ||
using Bicep.Core.UnitTests.Json; | ||
using Bicep.Core.UnitTests.Serialization; | ||
using Bicep.Core.UnitTests.Utils; | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Bicep.Core.IntegrationTests | ||
{ | ||
[TestClass] | ||
public class LexerTests | ||
{ | ||
public TestContext? TestContext { get; set; } | ||
|
||
[DataTestMethod] | ||
[UnitSamplesDataSource] | ||
public void LexerShouldRoundtrip(string displayName, string contents) | ||
[DynamicData(nameof(GetData), DynamicDataSourceType.Method, DynamicDataDisplayNameDeclaringType = typeof(DataSet), DynamicDataDisplayName = nameof(DataSet.GetDisplayName))] | ||
public void LexerShouldRoundtrip(DataSet dataSet) | ||
{ | ||
var lexer = new Parser.Lexer(new SlidingTextWindow(contents)); | ||
var lexer = new Lexer(new SlidingTextWindow(dataSet.Bicep)); | ||
lexer.Lex(); | ||
|
||
var serialized = new StringBuilder(); | ||
WriteTokens(serialized, lexer.GetTokens()); | ||
new TokenWriter(serialized).WriteTokens(lexer.GetTokens()); | ||
|
||
serialized.ToString().Should().Be(contents, "because the lexer should not lose information"); | ||
serialized.ToString().Should().Be(dataSet.Bicep, "because the lexer should not lose information"); | ||
} | ||
|
||
[DataTestMethod] | ||
[UnitSamplesDataSource] | ||
public void LexerShouldProduceValidTokenLocations(string displayName, string contents) | ||
[DynamicData(nameof(GetData), DynamicDataSourceType.Method, DynamicDataDisplayNameDeclaringType = typeof(DataSet), DynamicDataDisplayName = nameof(DataSet.GetDisplayName))] | ||
public void LexerShouldProduceValidTokenLocations(DataSet dataSet) | ||
{ | ||
var lexer = new Parser.Lexer(new SlidingTextWindow(contents)); | ||
var lexer = new Lexer(new SlidingTextWindow(dataSet.Bicep)); | ||
lexer.Lex(); | ||
|
||
foreach (Token token in lexer.GetTokens()) | ||
{ | ||
// lookup the text of the token in original contents by token's span | ||
string tokenText = contents[new Range(token.Span.Position, token.Span.Position + token.Span.Length)]; | ||
string tokenText = dataSet.Bicep[new Range(token.Span.Position, token.Span.Position + token.Span.Length)]; | ||
|
||
tokenText.Should().Be(token.Text, "because token text at location should match original contents at the same location"); | ||
} | ||
} | ||
|
||
private void WriteTokens(StringBuilder buffer, IEnumerable<Token> tokens) | ||
[DataTestMethod] | ||
[DynamicData(nameof(GetData), DynamicDataSourceType.Method, DynamicDataDisplayNameDeclaringType = typeof(DataSet), DynamicDataDisplayName = nameof(DataSet.GetDisplayName))] | ||
public void LexerShouldProduceExpectedTokens(DataSet dataSet) | ||
{ | ||
foreach (Token token in tokens) | ||
{ | ||
WriteToken(buffer, token); | ||
} | ||
var lexer = new Lexer(new SlidingTextWindow(dataSet.Bicep)); | ||
lexer.Lex(); | ||
|
||
var actual = JToken.FromObject(lexer.GetTokens().Select(token => new TokenItem(token)), DataSetSerialization.CreateSerializer()); | ||
FileHelper.SaveResultFile(this.TestContext!, $"{dataSet.Name}_Tokens_Actual.json", actual.ToString(Formatting.Indented)); | ||
|
||
var expected = JToken.Parse(dataSet.Tokens); | ||
FileHelper.SaveResultFile(this.TestContext!, $"{dataSet.Name}_Tokens_Expected.json", expected.ToString(Formatting.Indented)); | ||
|
||
JsonAssert.AreEqual(expected, actual, this.TestContext!, $"{dataSet.Name}_Tokens_Delta.json"); | ||
} | ||
private void WriteToken(StringBuilder buffer, Token token) | ||
|
||
private static IEnumerable<object[]> GetData() | ||
{ | ||
buffer.Append(token.LeadingTrivia); | ||
buffer.Append(token.Text); | ||
buffer.Append(token.TrailingTrivia); | ||
return DataSets.AllDataSets.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
62 changes: 0 additions & 62 deletions
62
src/Bicep.Core.IntegrationTests/UnitSamples/UnitSamplesDataSource.cs
This file was deleted.
Oops, something went wrong.
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,57 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Remove="Empty\Bicep.arm" /> | ||
<None Remove="Empty\Errors.err" /> | ||
<None Remove="Empty\Errors.json" /> | ||
<None Remove="Empty\Tokens.json" /> | ||
<None Remove="InvalidParameters_LF\Bicep.arm" /> | ||
<None Remove="InvalidParameters_LF\Errors.json" /> | ||
<None Remove="InvalidParameters_LF\Tokens.json" /> | ||
<None Remove="InvalidParameters_CRLF\Bicep.arm" /> | ||
<None Remove="InvalidParameters_CRLF\Errors.err" /> | ||
<None Remove="InvalidParameters_CRLF\Errors.json" /> | ||
<None Remove="InvalidParameters_CRLF\Tokens.json" /> | ||
<None Remove="InvalidParameters_CRLF\Tokens.lexdump" /> | ||
<None Remove="Parameters_LF\Bicep.arm" /> | ||
<None Remove="Parameters_LF\Errors.json" /> | ||
<None Remove="Parameters_LF\Tokens.json" /> | ||
<None Remove="Parameters_CRLF\Bicep.arm" /> | ||
<None Remove="Parameters_CRLF\Errors.json" /> | ||
<None Remove="Parameters_CRLF\Tokens.json" /> | ||
<None Remove="Parameters_CRLF\Tokens.lexdump" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<EmbeddedResource Include="Empty\Bicep.arm" /> | ||
<EmbeddedResource Include="Empty\Errors.json" /> | ||
<EmbeddedResource Include="Empty\Tokens.json" /> | ||
<EmbeddedResource Include="InvalidParameters_LF\Bicep.arm" /> | ||
<EmbeddedResource Include="InvalidParameters_LF\Errors.json" /> | ||
<EmbeddedResource Include="InvalidParameters_LF\Tokens.json" /> | ||
<EmbeddedResource Include="InvalidParameters_CRLF\Bicep.arm" /> | ||
<EmbeddedResource Include="InvalidParameters_CRLF\Errors.json" /> | ||
<EmbeddedResource Include="InvalidParameters_CRLF\Tokens.json" /> | ||
<EmbeddedResource Include="Parameters_LF\Bicep.arm" /> | ||
<EmbeddedResource Include="Parameters_LF\Errors.json" /> | ||
<EmbeddedResource Include="Parameters_LF\Tokens.json" /> | ||
<EmbeddedResource Include="Parameters_CRLF\Bicep.arm" /> | ||
<EmbeddedResource Include="Parameters_CRLF\Errors.json" /> | ||
<EmbeddedResource Include="Parameters_CRLF\Tokens.json" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" Version="5.10.3" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" /> | ||
<PackageReference Include="coverlet.collector" Version="1.2.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.