|
| 1 | +using Microsoft.CodeAnalysis.Testing; |
| 2 | +using Xunit; |
| 3 | +using VerifyCS = Funcky.Analyzers.Test.CSharpAnalyzerVerifier<Funcky.Analyzers.OptionListPatternAnalyzer>; |
| 4 | + |
| 5 | +namespace Funcky.Analyzers.Test; |
| 6 | + |
| 7 | +public sealed class OptionListPatternTest |
| 8 | +{ |
| 9 | + // language=csharp |
| 10 | + private const string OptionStub = |
| 11 | + """ |
| 12 | + namespace Funcky.Monads |
| 13 | + { |
| 14 | + public readonly struct Option<T> |
| 15 | + { |
| 16 | + public int Count => throw null!; |
| 17 | +
|
| 18 | + public T this[int index] => throw null!; |
| 19 | + } |
| 20 | + } |
| 21 | + """; |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public async Task ErrorsWhenListPatternHasMoreThanOneElement() |
| 25 | + { |
| 26 | + // language=csharp |
| 27 | + const string inputCode = |
| 28 | + """ |
| 29 | + using Funcky.Monads; |
| 30 | +
|
| 31 | + class C |
| 32 | + { |
| 33 | + private void M(Option<string> option) |
| 34 | + { |
| 35 | + _ = option is ["foo", "bar"]; |
| 36 | + _ = option is [var foo, var bar, var baz]; |
| 37 | + _ = option is [var one, var two, var three, var four]; |
| 38 | + } |
| 39 | + } |
| 40 | + """; |
| 41 | + |
| 42 | + DiagnosticResult[] expectedDiagnostics = [ |
| 43 | + VerifyCS.Diagnostic().WithSpan(7, 23, 7, 37), |
| 44 | + VerifyCS.Diagnostic().WithSpan(8, 23, 8, 50), |
| 45 | + VerifyCS.Diagnostic().WithSpan(9, 23, 9, 62), |
| 46 | + ]; |
| 47 | + |
| 48 | + await VerifyCS.VerifyAnalyzerAsync(inputCode + Environment.NewLine + OptionStub, expectedDiagnostics); |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public async Task DoesNotErrorWhenUsingAListPatternWithZeroOrOneElements() |
| 53 | + { |
| 54 | + // language=csharp |
| 55 | + const string inputCode = |
| 56 | + """ |
| 57 | + using Funcky.Monads; |
| 58 | +
|
| 59 | + class C |
| 60 | + { |
| 61 | + private void M(Option<string> option) |
| 62 | + { |
| 63 | + _ = option is ["foo"]; |
| 64 | + _ = option is []; |
| 65 | + } |
| 66 | + } |
| 67 | + """; |
| 68 | + |
| 69 | + await VerifyCS.VerifyAnalyzerAsync(inputCode + Environment.NewLine + OptionStub); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public async Task UsingASlicePatternIsACompileError() |
| 74 | + { |
| 75 | + // language=csharp |
| 76 | + const string inputCode = |
| 77 | + """ |
| 78 | + using Funcky.Monads; |
| 79 | +
|
| 80 | + class C |
| 81 | + { |
| 82 | + private void M(Option<string> option) |
| 83 | + { |
| 84 | + _ = option is [..var slice]; |
| 85 | + } |
| 86 | + } |
| 87 | + """; |
| 88 | + |
| 89 | + DiagnosticResult[] expectedDiagnostics = [ |
| 90 | + DiagnosticResult.CompilerError("CS1503").WithSpan(7, 24, 7, 35).WithArguments("1", "System.Range", "int"), |
| 91 | + ]; |
| 92 | + |
| 93 | + await VerifyCS.VerifyAnalyzerAsync(inputCode + Environment.NewLine + OptionStub, expectedDiagnostics); |
| 94 | + } |
| 95 | +} |
0 commit comments