Skip to content
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
@@ -0,0 +1,64 @@
// Copyright (c) Contributors to the New StyleCop Analyzers project.
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.Test.CSharp14.MaintainabilityRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using Xunit;
using static StyleCop.Analyzers.MaintainabilityRules.SA1119StatementMustNotUseUnnecessaryParenthesis;
using static StyleCop.Analyzers.Test.CSharp6.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.MaintainabilityRules.SA1119StatementMustNotUseUnnecessaryParenthesis,
StyleCop.Analyzers.MaintainabilityRules.SA1119CodeFixProvider>;

public partial class SA1119CSharp14UnitTests
{
[Fact]
public async Task TestFieldKeywordUnnecessaryParenthesisAsync()
{
var testCode = @"
public class TestClass
{
private int result;

public int Prop
{
get => field;
set
{
field = value;
this.result = {|#2:{|#0:(|}field{|#1:)|}|};
}
}
}
";

var fixedCode = @"
public class TestClass
{
private int result;

public int Prop
{
get => field;
set
{
field = value;
this.result = field;
}
}
}
";

DiagnosticResult[] expected =
{
Diagnostic(DiagnosticId).WithLocation(2),
Diagnostic(ParenthesesDiagnosticId).WithLocation(0),
Diagnostic(ParenthesesDiagnosticId).WithLocation(1),
};

await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ public static class TestClass
public int GetMyLength() => source.Length;
}
}
";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(true);
}

[Fact]
public async Task TestFieldKeywordDoesNotRequireThisPrefixAsync()
{
var testCode = @"
public class TestClass
{
public int Prop
{
get => field;
set => field = value;
}
}
";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,22 @@ public class MyClass
};
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(true);
}

[Fact]
public async Task TestFieldKeywordIsIgnoredAsync()
{
var testCode = @"
public class TestClass
{
public int Prop
{
get => field;
set =>field= value;
}
}
";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(true);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) Contributors to the New StyleCop Analyzers project.
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.Test.CSharp14.SpacingRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using Xunit;
using static StyleCop.Analyzers.SpacingRules.SA1003SymbolsMustBeSpacedCorrectly;
using static StyleCop.Analyzers.Test.CSharp6.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.SpacingRules.SA1003SymbolsMustBeSpacedCorrectly,
StyleCop.Analyzers.SpacingRules.SA1003CodeFixProvider>;

public partial class SA1003CSharp14UnitTests
{
[Fact]
public async Task TestFieldKeywordBinaryExpressionAsync()
{
var testCode = @"
public class TestClass
{
public int Prop
{
get => field{|#0:+|}field;
}
}
";

var fixedCode = @"
public class TestClass
{
public int Prop
{
get => field + field;
}
}
";

DiagnosticResult[] expected =
{
Diagnostic(DescriptorPrecededByWhitespace).WithLocation(0).WithArguments("+"),
Diagnostic(DescriptorFollowedByWhitespace).WithLocation(0).WithArguments("+"),
};

await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,47 @@ public class TestClass

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

[Fact]
public async Task TestFieldKeywordInvocationAsync()
{
var testCode = @"
public class TestClass
{
private int result;

public System.Func<int, int> Handler
{
get => field;
set
{
field = value;
this.result = field {|#0:(|}9);
}
}
}
";

var fixedCode = @"
public class TestClass
{
private int result;

public System.Func<int, int> Handler
{
get => field;
set
{
field = value;
this.result = field(9);
}
}
}
";

var expected = Diagnostic(DescriptorNotPreceded).WithLocation(0);

await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,47 @@ public class TestClass

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

[Fact]
public async Task TestFieldKeywordInvocationAsync()
{
var testCode = @"
public class TestClass
{
private int result;

public System.Func<int, int> Handler
{
get => field;
set
{
field = value;
this.result = field(9 {|#0:)|};
}
}
}
";

var fixedCode = @"
public class TestClass
{
private int result;

public System.Func<int, int> Handler
{
get => field;
set
{
field = value;
this.result = field(9);
}
}
}
";

var expected = Diagnostic(DescriptorNotPreceded).WithLocation(0);

await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(true);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (c) Contributors to the New StyleCop Analyzers project.
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.Test.CSharp14.SpacingRules
{
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using static StyleCop.Analyzers.SpacingRules.SA1010OpeningSquareBracketsMustBeSpacedCorrectly;
using static StyleCop.Analyzers.Test.CSharp6.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.SpacingRules.SA1010OpeningSquareBracketsMustBeSpacedCorrectly,
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;

public partial class SA1010CSharp14UnitTests
{
[Fact]
public async Task TestFieldIndexerAsync()
{
var testCode = @"
public class TestClass
{
private int result;

public int[] Items
{
get => field;
set
{
field = value;
this.result = field {|#0:[|}0];
}
}
}
";

var fixedCode = @"
public class TestClass
{
private int result;

public int[] Items
{
get => field;
set
{
field = value;
this.result = field[0];
}
}
}
";

var expected = Diagnostic(DescriptorNotPreceded).WithLocation(0);

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

[Fact]
public async Task TestFieldInCollectionExpressionAsync()
{
var testCode = @"
public class TestClass
{
private int[] items;

public int Value
{
get => field;
set
{
field = value;
this.items = {|#0:[|} field ];
}
}
}
";

var fixedCode = @"
public class TestClass
{
private int[] items;

public int Value
{
get => field;
set
{
field = value;
this.items = [field ];
}
}
}
";

var expected = Diagnostic(DescriptorNotFollowed).WithLocation(0);

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