Skip to content

Commit

Permalink
Increase test coverage for records in SA1500, SA1502, and SA1508
Browse files Browse the repository at this point in the history
See #3272
  • Loading branch information
sharwell committed Dec 15, 2021
1 parent b5b91d7 commit 3cce367
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,89 @@ namespace StyleCop.Analyzers.Test.CSharp9.LayoutRules
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp8.LayoutRules;
using StyleCop.Analyzers.Test.Helpers;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.LayoutRules.SA1500BracesForMultiLineStatementsMustNotShareLine,
StyleCop.Analyzers.LayoutRules.SA1500CodeFixProvider>;

public class SA1500CSharp9UnitTests : SA1500CSharp8UnitTests
{
[Fact]
[Theory]
[MemberData(nameof(CommonMemberData.RecordTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
[WorkItem(3272, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3272")]
public async Task TestSingleLineRecordAsync()
public async Task TestSingleLineRecordAsync(string keyword)
{
var testCode = @"namespace TestNamespace
{
public record TestRecord;
}
var testCode = $@"namespace TestNamespace
{{
public {keyword} TestRecord;
}}
";

await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, testCode, CancellationToken.None).ConfigureAwait(false);
}

[Theory]
[MemberData(nameof(CommonMemberData.RecordTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
[WorkItem(3272, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3272")]
public async Task TestSingleLineRecordWithParameterAsync(string keyword)
{
var testCode = $@"namespace TestNamespace
{{
public {keyword} TestRecord(int Count);
}}
";

await new CSharpTest
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
TestCode = testCode,
FixedCode = testCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}

[Theory]
[MemberData(nameof(CommonMemberData.RecordTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
[WorkItem(3272, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3272")]
public async Task TestMultiLineRecordAsync(string keyword)
{
var testCode = $@"namespace TestNamespace
{{
public {keyword} TestRecord
{{
public int Count {{ get; init; }}
}}
}}
";

await new CSharpTest
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
TestCode = testCode,
FixedCode = testCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}

[Theory]
[MemberData(nameof(CommonMemberData.RecordTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
[WorkItem(3272, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3272")]
public async Task TestMultiLineRecordWithParameterAsync(string keyword)
{
var testCode = $@"namespace TestNamespace
{{
public {keyword} TestRecord(int Count)
{{
public int Count2 {{ get; init; }} = 0;
}}
}}
";

await new CSharpTest
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
TestCode = testCode,
FixedCode = testCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,89 @@ namespace StyleCop.Analyzers.Test.CSharp9.LayoutRules
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp8.LayoutRules;
using StyleCop.Analyzers.Test.Helpers;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.LayoutRules.SA1502ElementMustNotBeOnASingleLine,
StyleCop.Analyzers.LayoutRules.SA1502CodeFixProvider>;

public class SA1502CSharp9UnitTests : SA1502CSharp8UnitTests
{
[Fact]
[Theory]
[MemberData(nameof(CommonMemberData.RecordTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
[WorkItem(3272, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3272")]
public async Task TestSingleLineRecordAsync()
public async Task TestSingleLineRecordAsync(string keyword)
{
var testCode = @"namespace TestNamespace
{
public record TestRecord;
}
var testCode = $@"namespace TestNamespace
{{
public {keyword} TestRecord;
}}
";

await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, testCode, CancellationToken.None).ConfigureAwait(false);
}

[Theory]
[MemberData(nameof(CommonMemberData.RecordTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
[WorkItem(3272, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3272")]
public async Task TestSingleLineRecordWithParameterAsync(string keyword)
{
var testCode = $@"namespace TestNamespace
{{
public {keyword} TestRecord(int Count);
}}
";

await new CSharpTest
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
TestCode = testCode,
FixedCode = testCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}

[Theory]
[MemberData(nameof(CommonMemberData.RecordTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
[WorkItem(3272, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3272")]
public async Task TestMultiLineRecordAsync(string keyword)
{
var testCode = $@"namespace TestNamespace
{{
public {keyword} TestRecord
{{
public int Count {{ get; init; }}
}}
}}
";

await new CSharpTest
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
TestCode = testCode,
FixedCode = testCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}

[Theory]
[MemberData(nameof(CommonMemberData.RecordTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
[WorkItem(3272, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3272")]
public async Task TestMultiLineRecordWithParameterAsync(string keyword)
{
var testCode = $@"namespace TestNamespace
{{
public {keyword} TestRecord(int Count)
{{
public int Count2 {{ get; init; }} = 0;
}}
}}
";

await new CSharpTest
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
TestCode = testCode,
FixedCode = testCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,89 @@ namespace StyleCop.Analyzers.Test.CSharp9.LayoutRules
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp8.LayoutRules;
using StyleCop.Analyzers.Test.Helpers;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.LayoutRules.SA1508ClosingBracesMustNotBePrecededByBlankLine,
StyleCop.Analyzers.LayoutRules.SA1508CodeFixProvider>;

public class SA1508CSharp9UnitTests : SA1508CSharp8UnitTests
{
[Fact]
[Theory]
[MemberData(nameof(CommonMemberData.RecordTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
[WorkItem(3272, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3272")]
public async Task TestSingleLineRecordAsync()
public async Task TestSingleLineRecordAsync(string keyword)
{
var testCode = @"namespace TestNamespace
{
public record TestRecord;
}
var testCode = $@"namespace TestNamespace
{{
public {keyword} TestRecord;
}}
";

await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, testCode, CancellationToken.None).ConfigureAwait(false);
}

[Theory]
[MemberData(nameof(CommonMemberData.RecordTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
[WorkItem(3272, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3272")]
public async Task TestSingleLineRecordWithParameterAsync(string keyword)
{
var testCode = $@"namespace TestNamespace
{{
public {keyword} TestRecord(int Count);
}}
";

await new CSharpTest
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
TestCode = testCode,
FixedCode = testCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}

[Theory]
[MemberData(nameof(CommonMemberData.RecordTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
[WorkItem(3272, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3272")]
public async Task TestMultiLineRecordAsync(string keyword)
{
var testCode = $@"namespace TestNamespace
{{
public {keyword} TestRecord
{{
public int Count {{ get; init; }}
}}
}}
";

await new CSharpTest
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
TestCode = testCode,
FixedCode = testCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}

[Theory]
[MemberData(nameof(CommonMemberData.RecordTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
[WorkItem(3272, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3272")]
public async Task TestMultiLineRecordWithParameterAsync(string keyword)
{
var testCode = $@"namespace TestNamespace
{{
public {keyword} TestRecord(int Count)
{{
public int Count2 {{ get; init; }} = 0;
}}
}}
";

await new CSharpTest
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
TestCode = testCode,
FixedCode = testCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ public static IEnumerable<object[]> DataTypeDeclarationKeywords
}
}

public static IEnumerable<object[]> RecordTypeDeclarationKeywords
{
get
{
if (LightupHelpers.SupportsCSharp9)
{
yield return new[] { "record" };
}

if (LightupHelpers.SupportsCSharp10)
{
yield return new[] { "record class" };
yield return new[] { "record struct" };
}
}
}

public static IEnumerable<object[]> TypeDeclarationKeywords
{
get
Expand Down

0 comments on commit 3cce367

Please sign in to comment.