Skip to content

Commit

Permalink
Upgraded the new test project to Microsoft.CodeAnalysis.CSharp.Worksp…
Browse files Browse the repository at this point in the history
…aces version 4.2.0-4.final
  • Loading branch information
bjornhellander committed Apr 30, 2022
1 parent d09a2e2 commit c933da1
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,33 @@

namespace StyleCop.Analyzers.Test.CSharp11.LayoutRules
{
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp10.LayoutRules;

using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.LayoutRules.SA1516ElementsMustBeSeparatedByBlankLine,
StyleCop.Analyzers.LayoutRules.SA1516CodeFixProvider>;

public class SA1516CSharp11UnitTests : SA1516CSharp10UnitTests
{
protected override DiagnosticResult[] GetExpectedResultTestUsingAndGlobalStatementSpacingInTopLevelProgram()
{
// NOTE: Roslyn bug fix. Earlier versions made diagnostics be reported twice.
return new[]
{
// /0/Test0.cs(3,1): warning SA1516: Elements should be separated by blank line
Diagnostic().WithLocation(0),
};
}

protected override DiagnosticResult[] GetExpectedResultTestGlobalStatementAndRecordSpacingInTopLevelProgram()
{
// NOTE: Roslyn bug fix. Earlier versions made diagnostics be reported twice.
return new[]
{
// /0/Test0.cs(2,1): warning SA1516: Elements should be separated by blank line
Diagnostic().WithLocation(0),
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,18 @@ namespace StyleCop.Analyzers.Test.CSharp11.MaintainabilityRules

public class SA1119CSharp11UnitTests : SA1119CSharp10UnitTests
{
// In earlier Roslyn versions, we ended up with an extra space between the opening brace
// and the identifier. Does not happen anymore.
protected override string GetFixedCodeTestParenthesisInInterpolatedStringThatShouldBeRemoved()
{
return @"class Foo
{
public void Bar()
{
bool flag = false;
string data = $""{flag}"";
}
}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,22 @@

namespace StyleCop.Analyzers.Test.CSharp11.NamingRules
{
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp10.NamingRules;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.NamingRules.SA1300ElementMustBeginWithUpperCaseLetter,
StyleCop.Analyzers.NamingRules.RenameToUpperCaseCodeFixProvider>;

public class SA1300CSharp11UnitTests : SA1300CSharp10UnitTests
{
protected override DiagnosticResult[] GetExpectedResultTestPositionalRecord1()
{
// NOTE: Roslyn bug fix. Earlier versions made diagnostics be reported twice.
return new[]
{
// /0/Test0.cs(2,15): warning SA1300: Element 'r' should begin with an uppercase letter
Diagnostic().WithLocation(0).WithArguments("r"),
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.0.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.2.0-4.final" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" PrivateAssets="all" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,19 @@ public async Task TestUsingAndGlobalStatementSpacingInTopLevelProgramAsync()
return 0;
";

await new CSharpTest(LanguageVersion.CSharp9)
var test = new CSharpTest(LanguageVersion.CSharp9)
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
TestState =
{
OutputKind = OutputKind.ConsoleApplication,
Sources = { testCode },
ExpectedDiagnostics =
{
// /0/Test0.cs(3,1): warning SA1516: Elements should be separated by blank line
Diagnostic().WithLocation(0),

// /0/Test0.cs(3,1): warning SA1516: Elements should be separated by blank line
Diagnostic().WithLocation(0),
},
},
FixedCode = fixedCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
};
var expectedDiagnostics = this.GetExpectedResultTestUsingAndGlobalStatementSpacingInTopLevelProgram();
test.TestState.ExpectedDiagnostics.AddRange(expectedDiagnostics);
await test.RunAsync(CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -95,24 +90,45 @@ public async Task TestGlobalStatementAndRecordSpacingInTopLevelProgramAsync()
record A();
";

await new CSharpTest(LanguageVersion.CSharp9)
var test = new CSharpTest(LanguageVersion.CSharp9)
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
TestState =
{
OutputKind = OutputKind.ConsoleApplication,
Sources = { testCode },
ExpectedDiagnostics =
{
// /0/Test0.cs(2,1): warning SA1516: Elements should be separated by blank line
Diagnostic().WithLocation(0),

// /0/Test0.cs(2,1): warning SA1516: Elements should be separated by blank line
Diagnostic().WithLocation(0),
},
},
FixedCode = fixedCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
};
var expectedDiagnostics = this.GetExpectedResultTestGlobalStatementAndRecordSpacingInTopLevelProgram();
test.TestState.ExpectedDiagnostics.AddRange(expectedDiagnostics);
await test.RunAsync(CancellationToken.None).ConfigureAwait(false);
}

protected virtual DiagnosticResult[] GetExpectedResultTestUsingAndGlobalStatementSpacingInTopLevelProgram()
{
// NOTE: Seems like a Roslyn bug made diagnostics be reported twice. Fixed in a later version.
return new[]
{
// /0/Test0.cs(3,1): warning SA1516: Elements should be separated by blank line
Diagnostic().WithLocation(0),

// /0/Test0.cs(3,1): warning SA1516: Elements should be separated by blank line
Diagnostic().WithLocation(0),
};
}

protected virtual DiagnosticResult[] GetExpectedResultTestGlobalStatementAndRecordSpacingInTopLevelProgram()
{
// NOTE: Seems like a Roslyn bug made diagnostics be reported twice. Fixed in a later version.
return new[]
{
// /0/Test0.cs(2,1): warning SA1516: Elements should be separated by blank line
Diagnostic().WithLocation(0),

// /0/Test0.cs(2,1): warning SA1516: Elements should be separated by blank line
Diagnostic().WithLocation(0),
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace StyleCop.Analyzers.Test.CSharp9.NamingRules
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp8.NamingRules;
using StyleCop.Analyzers.Test.Verifiers;
using Xunit;
Expand Down Expand Up @@ -40,20 +41,15 @@ public R(int a, int b)
}
";

await new CSharpTest(LanguageVersion.CSharp9)
var test = new CSharpTest(LanguageVersion.CSharp9)
{
ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssembliesNet50,
TestCode = testCode,
ExpectedDiagnostics =
{
// /0/Test0.cs(2,15): warning SA1300: Element 'r' should begin with an uppercase letter
Diagnostic().WithLocation(0).WithArguments("r"),

// /0/Test0.cs(2,15): warning SA1300: Element 'r' should begin with an uppercase letter
Diagnostic().WithLocation(0).WithArguments("r"),
},
FixedCode = fixedCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
};
var expectedDiagnostics = this.GetExpectedResultTestPositionalRecord1();
test.TestState.ExpectedDiagnostics.AddRange(expectedDiagnostics);
await test.RunAsync(CancellationToken.None).ConfigureAwait(false);
}

[Fact]
Expand Down Expand Up @@ -86,5 +82,18 @@ public R(int a, int b)
FixedCode = fixedCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}

protected virtual DiagnosticResult[] GetExpectedResultTestPositionalRecord1()
{
// NOTE: Seems like a Roslyn bug made diagnostics be reported twice. Fixed in a later version.
return new[]
{
// /0/Test0.cs(2,15): warning SA1300: Element 'r' should begin with an uppercase letter
Diagnostic().WithLocation(0).WithArguments("r"),

// /0/Test0.cs(2,15): warning SA1300: Element 'r' should begin with an uppercase letter
Diagnostic().WithLocation(0).WithArguments("r"),
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1042,14 +1042,9 @@ public void Bar()
string data = $""{(flag)}"";
}
}";
string fixedCode = @"class Foo
{
public void Bar()
{
bool flag = false;
string data = $""{ flag}"";
}
}";

string fixedCode = this.GetFixedCodeTestParenthesisInInterpolatedStringThatShouldBeRemoved();

DiagnosticResult[] expected =
{
Diagnostic(DiagnosticId).WithSpan(6, 26, 6, 32),
Expand Down Expand Up @@ -1477,5 +1472,19 @@ private static void Main()

await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

// In this version of Roslyn, we end up with an extra space between the opening brace
// and the identifier. Fixed in a later version.
protected virtual string GetFixedCodeTestParenthesisInInterpolatedStringThatShouldBeRemoved()
{
return @"class Foo
{
public void Bar()
{
bool flag = false;
string data = $""{ flag}"";
}
}";
}
}
}

0 comments on commit c933da1

Please sign in to comment.