-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContradictingTests.cs
127 lines (111 loc) · 3.92 KB
/
ContradictingTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ContradictingTests.cs" company="Hukano">
// Copyright (c) Hukano. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Sundew.DiscriminatedUnions.Test
{
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sundew.DiscriminatedUnions.Analyzer;
using VerifyCS = Sundew.DiscriminatedUnions.Test.CSharpCodeFixVerifier<
Sundew.DiscriminatedUnions.Analyzer.SundewDiscriminatedUnionsAnalyzer,
Sundew.DiscriminatedUnions.CodeFixes.SundewDiscriminatedUnionsCodeFixProvider,
Sundew.DiscriminatedUnions.Analyzer.SundewDiscriminatedUnionSwitchWarningSuppressor>;
[TestClass]
public class ContradictingTests
{
[TestMethod]
public async Task
Given_SwitchExpressionInEnabledNullableContext_When_ValueIsNotNullAndAllCasesAndNullCaseAreHandled_Then_HasUnreachableNullCaseIsReported()
{
var test = $@"#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using Sundew.DiscriminatedUnions;
namespace ConsoleApplication1
{{
public class DiscriminatedUnionSymbolAnalyzerTests
{{
public int Switch(Option<int> option)
{{
return option switch
{{
Option<int>.Some some => some.Value,
Option<int>.None => 0,
null => -1,
}};
}}
}}
[Sundew.DiscriminatedUnions.DiscriminatedUnion]
public abstract record Option<T>
where T : notnull
{{
private Option()
{{ }}
public sealed record Some(T Value) : Option<T>;
public sealed record None : Option<T>;
}}
}}";
await VerifyCS.VerifyAnalyzerAsync(
test,
VerifyCS.Diagnostic(SundewDiscriminatedUnionsAnalyzer.HasUnreachableNullCaseRule)
.WithSpan(16, 24, 21, 22));
}
[TestMethod]
public async Task
Given_SwitchExpressionInEnabledNullableContext_When_ValueComeFromAMethodAndIsNotNullAndAllCasesAndNullCaseAreHandled_Then_HasUnreachableNullCaseIsReported()
{
var test = $@"#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using Sundew.DiscriminatedUnions;
namespace ConsoleApplication1
{{
public class DiscriminatedUnionSymbolAnalyzerTests
{{
public int Switch()
{{
var option = Compute(""test"");
return option switch
{{
Option<int>.Some some => some.Value,
Option<int>.None => 0,
null => -1,
}};
}}
private static Option<int> Compute(string test)
{{
if (test == ""test"")
{{
return new Option<int>.Some(45);
}}
return new Option<int>.None();
}}
}}
[Sundew.DiscriminatedUnions.DiscriminatedUnion]
public abstract record Option<T>
where T : notnull
{{
private Option()
{{ }}
public sealed record Some(T Value) : Option<T>;
public sealed record None : Option<T>;
}}
}}";
await VerifyCS.VerifyAnalyzerAsync(
test,
VerifyCS.Diagnostic(SundewDiscriminatedUnionsAnalyzer.HasUnreachableNullCaseRule)
.WithSpan(17, 20, 22, 18));
}
}
}