|
| 1 | +// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +namespace StyleCop.Analyzers.Test.LayoutRules |
| 5 | +{ |
| 6 | + using System.Threading; |
| 7 | + using System.Threading.Tasks; |
| 8 | + using StyleCop.Analyzers.LayoutRules; |
| 9 | + using TestHelper; |
| 10 | + using Xunit; |
| 11 | + |
| 12 | + /// <summary> |
| 13 | + /// Unit tests for the operators part of <see cref="SA1502ElementMustNotBeOnASingleLine"/>. |
| 14 | + /// </summary> |
| 15 | + public partial class SA1502UnitTests : CodeFixVerifier |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Verifies that valid operators will pass without diagnostic. |
| 19 | + /// </summary> |
| 20 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 21 | + [Fact] |
| 22 | + public async Task TestValidOperatorsAsync() |
| 23 | + { |
| 24 | + var testCode = @"public class TestClass |
| 25 | +{ |
| 26 | + public static TestClass operator +(TestClass value) |
| 27 | + { |
| 28 | + return value; |
| 29 | + } |
| 30 | +
|
| 31 | + public static explicit operator TestClass(int value) |
| 32 | + { |
| 33 | + return new TestClass(); |
| 34 | + } |
| 35 | +}"; |
| 36 | + |
| 37 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Verifies that operators with their blocks on the same line will trigger a diagnostic. |
| 42 | + /// </summary> |
| 43 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 44 | + [Fact] |
| 45 | + public async Task TestOperatorsOnSingleLineAsync() |
| 46 | + { |
| 47 | + var testCode = @"public class TestClass |
| 48 | +{ |
| 49 | + public static TestClass operator +(TestClass value) { return value; } |
| 50 | +
|
| 51 | + public static explicit operator TestClass(int value) { return new TestClass(); } |
| 52 | +}"; |
| 53 | + |
| 54 | + var fixedCode = @"public class TestClass |
| 55 | +{ |
| 56 | + public static TestClass operator +(TestClass value) |
| 57 | + { |
| 58 | + return value; |
| 59 | + } |
| 60 | +
|
| 61 | + public static explicit operator TestClass(int value) |
| 62 | + { |
| 63 | + return new TestClass(); |
| 64 | + } |
| 65 | +}"; |
| 66 | + |
| 67 | + DiagnosticResult[] expected = |
| 68 | + { |
| 69 | + this.CSharpDiagnostic().WithLocation(3, 57), |
| 70 | + this.CSharpDiagnostic().WithLocation(5, 58) |
| 71 | + }; |
| 72 | + |
| 73 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 74 | + await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 75 | + await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false); |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Verifies that operators with their blocks on the next line will trigger a diagnostic. |
| 80 | + /// </summary> |
| 81 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 82 | + [Fact] |
| 83 | + public async Task TestOperatorsWithBlockOnNextLineAsync() |
| 84 | + { |
| 85 | + var testCode = @"public class TestClass |
| 86 | +{ |
| 87 | + public static TestClass operator +(TestClass value) |
| 88 | + { return value; } |
| 89 | +
|
| 90 | + public static explicit operator TestClass(int value) |
| 91 | + { return new TestClass(); } |
| 92 | +}"; |
| 93 | + |
| 94 | + var fixedCode = @"public class TestClass |
| 95 | +{ |
| 96 | + public static TestClass operator +(TestClass value) |
| 97 | + { |
| 98 | + return value; |
| 99 | + } |
| 100 | +
|
| 101 | + public static explicit operator TestClass(int value) |
| 102 | + { |
| 103 | + return new TestClass(); |
| 104 | + } |
| 105 | +}"; |
| 106 | + |
| 107 | + DiagnosticResult[] expected = |
| 108 | + { |
| 109 | + this.CSharpDiagnostic().WithLocation(4, 9), |
| 110 | + this.CSharpDiagnostic().WithLocation(7, 9) |
| 111 | + }; |
| 112 | + |
| 113 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 114 | + await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 115 | + await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false); |
| 116 | + } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// Verifies that operators with an expression body will pass without diagnostic. |
| 120 | + /// </summary> |
| 121 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 122 | + [Fact] |
| 123 | + public async Task TestOperatorsWithExpressionBodyAsync() |
| 124 | + { |
| 125 | + var testCode = @"public class TestClass |
| 126 | +{ |
| 127 | + public static TestClass operator +(TestClass value) => value; |
| 128 | +
|
| 129 | + public static explicit operator TestClass(int value) => new TestClass(); |
| 130 | +}"; |
| 131 | + |
| 132 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments