Skip to content

Increase test coverage for records in SA1500, SA1502, and SA1508 #3421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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