Skip to content

Commit

Permalink
Refactored tests (Azure#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
majastrz committed Jun 16, 2020
1 parent 1560669 commit 71d2a3a
Show file tree
Hide file tree
Showing 39 changed files with 2,189 additions and 131 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.arm -text
9 changes: 8 additions & 1 deletion Bicep.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bicep.Core.IntegrationTests
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bicep.Core.UnitTests", "src\Bicep.Core.UnitTests\Bicep.Core.UnitTests.csproj", "{166B8503-54F0-452F-88C5-1430BF05A604}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bicep.LangServer.UnitTests", "Bicep.LangServer.UnitTests\Bicep.LangServer.UnitTests.csproj", "{F7B0F06D-56CE-4F4A-8EF9-ED6EC3D77EE7}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bicep.LangServer.UnitTests", "Bicep.LangServer.UnitTests\Bicep.LangServer.UnitTests.csproj", "{F7B0F06D-56CE-4F4A-8EF9-ED6EC3D77EE7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bicep.Core.Samples", "src\Bicep.Core.Samples\Bicep.Core.Samples.csproj", "{A2E4B91C-3B7F-4CB1-B482-BE40F7709EB5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -65,6 +67,10 @@ Global
{F7B0F06D-56CE-4F4A-8EF9-ED6EC3D77EE7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F7B0F06D-56CE-4F4A-8EF9-ED6EC3D77EE7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F7B0F06D-56CE-4F4A-8EF9-ED6EC3D77EE7}.Release|Any CPU.Build.0 = Release|Any CPU
{A2E4B91C-3B7F-4CB1-B482-BE40F7709EB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A2E4B91C-3B7F-4CB1-B482-BE40F7709EB5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A2E4B91C-3B7F-4CB1-B482-BE40F7709EB5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A2E4B91C-3B7F-4CB1-B482-BE40F7709EB5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -78,6 +84,7 @@ Global
{087ADE61-3D39-4884-8258-40D7A7DD833F} = {FE323E78-E865-46E2-859A-E4F6FB312C0F}
{166B8503-54F0-452F-88C5-1430BF05A604} = {FE323E78-E865-46E2-859A-E4F6FB312C0F}
{F7B0F06D-56CE-4F4A-8EF9-ED6EC3D77EE7} = {76F6CBAF-FE81-4B56-B0DB-DE6FD7174771}
{A2E4B91C-3B7F-4CB1-B482-BE40F7709EB5} = {FE323E78-E865-46E2-859A-E4F6FB312C0F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {21F77282-91E7-4304-B1EF-FADFA4F39E37}
Expand Down
12 changes: 2 additions & 10 deletions src/Bicep.Core.IntegrationTests/Bicep.Core.IntegrationTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<None Remove="UnitSamples\Invalid\InvalidParameters.arm" />
<None Remove="UnitSamples\Valid\Parameters.arm" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="UnitSamples\Invalid\InvalidParameters.arm" />
<EmbeddedResource Include="UnitSamples\Valid\Parameters.arm" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
Expand All @@ -28,6 +18,8 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Bicep.Core.Samples\Bicep.Core.Samples.csproj" />
<ProjectReference Include="..\Bicep.Core.UnitTests\Bicep.Core.UnitTests.csproj" />
<ProjectReference Include="..\Bicep.Core\Bicep.Core.csproj" />
</ItemGroup>

Expand Down
32 changes: 21 additions & 11 deletions src/Bicep.Core.IntegrationTests/CheckVisitorTests.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
using System.Collections.Generic;
using Bicep.Core.IntegrationTests.UnitSamples;
using System.Linq;
using System.Text;
using Bicep.Core.IntegrationTests.Utils;
using Bicep.Core.Parser;
using Bicep.Core.Samples;
using Bicep.Core.Syntax;
using Bicep.Core.UnitTests.Json;
using Bicep.Core.UnitTests.Serialization;
using Bicep.Core.UnitTests.Utils;
using Bicep.Core.Visitors;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Bicep.Core.IntegrationTests
{
[TestClass]
public class CheckVisitorTests
{
[DataTestMethod]
[UnitSamplesDataSource(includeInvalid: false)]
public void ValidProgram_ShouldProduceNoErrors(string displayName, string contents)
{
GetErrors(contents).Should().BeEmpty();
}
public TestContext? TestContext { get; set; }

[DataTestMethod]
[UnitSamplesDataSource(includeValid: false)]
public void InvalidProgram_ShouldProduceErrors(string displayName, string contents)
[DynamicData(nameof(GetData), DynamicDataSourceType.Method, DynamicDataDisplayNameDeclaringType = typeof(DataSet), DynamicDataDisplayName = nameof(DataSet.GetDisplayName))]
public void ProgramsShouldProduceExpectedErrors(DataSet dataSet)
{
var errors = GetErrors(contents);
errors.Should().NotBeEmpty();
var errors = GetErrors(dataSet.Bicep).Select(error => new ErrorItem(error, dataSet.Bicep));

var actual = JToken.FromObject(errors, DataSetSerialization.CreateSerializer());
FileHelper.SaveResultFile(this.TestContext!, $"{dataSet.Name}_Errors_Actual.json", actual.ToString(Formatting.Indented));

var expected = JToken.Parse(dataSet.Errors);
FileHelper.SaveResultFile(this.TestContext!, $"{dataSet.Name}_Errors_Expected.json", expected.ToString(Formatting.Indented));
JsonAssert.AreEqual(expected, actual, this.TestContext!, $"{dataSet.Name}_Errors_Delta.json");
}

private static IEnumerable<object[]> GetData() => DataSets.AllDataSets.ToDynamicTestData();

private static List<Error> GetErrors(string contents)
{
var program = ParserHelper.Parse(contents);
Expand Down
54 changes: 34 additions & 20 deletions src/Bicep.Core.IntegrationTests/LexerTests.cs
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();
}
}
}
14 changes: 10 additions & 4 deletions src/Bicep.Core.IntegrationTests/ParserTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Text;
using Bicep.Core.IntegrationTests.UnitSamples;
using Bicep.Core.IntegrationTests.Utils;
using Bicep.Core.Samples;
using Bicep.Core.Syntax;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand All @@ -11,10 +12,10 @@ namespace Bicep.Core.IntegrationTests
public class ParserTests
{
[DataTestMethod]
[UnitSamplesDataSource]
public void Files_ShouldRoundTripSuccessfully(string displayName, string contents)
[DynamicData(nameof(GetData), DynamicDataSourceType.Method, DynamicDataDisplayNameDeclaringType = typeof(DataSet), DynamicDataDisplayName = nameof(DataSet.GetDisplayName))]
public void FilesShouldRoundTripSuccessfully(DataSet dataSet)
{
RunRoundTripTest(contents);
RunRoundTripTest(dataSet.Bicep);
}

[DataTestMethod]
Expand All @@ -30,6 +31,11 @@ public void Oneliners_ShouldRoundTripSuccessfully(string contents)
RunRoundTripTest(contents);
}

private static IEnumerable<object[]> GetData()
{
return DataSets.AllDataSets.ToDynamicTestData();
}

private static void RunRoundTripTest(string contents)
{
var program = ParserHelper.Parse(contents);
Expand Down

This file was deleted.

57 changes: 57 additions & 0 deletions src/Bicep.Core.Samples/Bicep.Core.Samples.csproj
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>
Loading

0 comments on commit 71d2a3a

Please sign in to comment.