|
| 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 PublicApiAnalyzer.Test.ApiDesign |
| 5 | +{ |
| 6 | + using System.Collections.Generic; |
| 7 | + using System.Threading; |
| 8 | + using System.Threading.Tasks; |
| 9 | + using Microsoft.CodeAnalysis.CodeFixes; |
| 10 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 11 | + using PublicApiAnalyzer.ApiDesign; |
| 12 | + using TestHelper; |
| 13 | + using Xunit; |
| 14 | + |
| 15 | + public class DeclarePublicAPIAnalyzerTests : CodeFixVerifier |
| 16 | + { |
| 17 | + private string shippedText; |
| 18 | + private string unshippedText; |
| 19 | + |
| 20 | + [Fact] |
| 21 | + public async Task SimpleMissingTypeAsync() |
| 22 | + { |
| 23 | + var source = @" |
| 24 | +public class C |
| 25 | +{ |
| 26 | +} |
| 27 | +"; |
| 28 | + |
| 29 | + this.shippedText = string.Empty; |
| 30 | + this.unshippedText = string.Empty; |
| 31 | + |
| 32 | + var expected = this.CSharpDiagnostic(DeclarePublicAPIAnalyzer.DeclareNewApiRule).WithArguments("C").WithLocation(2, 14); |
| 33 | + await this.VerifyCSharpDiagnosticAsync(source, expected, CancellationToken.None).ConfigureAwait(false); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public async Task SimpleMissingMemberAsync() |
| 38 | + { |
| 39 | + var source = @" |
| 40 | +public class C |
| 41 | +{ |
| 42 | + public int Field; |
| 43 | + public int Property { get; set; } |
| 44 | + public void Method() { } |
| 45 | +} |
| 46 | +"; |
| 47 | + |
| 48 | + this.shippedText = string.Empty; |
| 49 | + this.unshippedText = string.Empty; |
| 50 | + |
| 51 | + DiagnosticResult[] expected = |
| 52 | + { |
| 53 | + // Test0.cs(2,14): error RS0016: Symbol 'C' is not part of the declared API. |
| 54 | + this.CSharpDiagnostic(DeclarePublicAPIAnalyzer.DeclareNewApiRule).WithArguments("C").WithLocation(2, 14), |
| 55 | + |
| 56 | + // Test0.cs(4,16): error RS0016: Symbol 'Field' is not part of the declared API. |
| 57 | + this.CSharpDiagnostic(DeclarePublicAPIAnalyzer.DeclareNewApiRule).WithArguments("Field").WithLocation(4, 16), |
| 58 | + |
| 59 | + // Test0.cs(5,27): error RS0016: Symbol 'Property.get' is not part of the declared API. |
| 60 | + this.CSharpDiagnostic(DeclarePublicAPIAnalyzer.DeclareNewApiRule).WithArguments("Property.get").WithLocation(5, 27), |
| 61 | + |
| 62 | + // Test0.cs(5,32): error RS0016: Symbol 'Property.set' is not part of the declared API. |
| 63 | + this.CSharpDiagnostic(DeclarePublicAPIAnalyzer.DeclareNewApiRule).WithArguments("Property.set").WithLocation(5, 32), |
| 64 | + |
| 65 | + // Test0.cs(6,17): error RS0016: Symbol 'Method' is not part of the declared API. |
| 66 | + this.CSharpDiagnostic(DeclarePublicAPIAnalyzer.DeclareNewApiRule).WithArguments("Method").WithLocation(6, 17) |
| 67 | + }; |
| 68 | + |
| 69 | + await this.VerifyCSharpDiagnosticAsync(source, expected, CancellationToken.None).ConfigureAwait(false); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public async Task SimpleMemberAsync() |
| 74 | + { |
| 75 | + var source = @" |
| 76 | +public class C |
| 77 | +{ |
| 78 | + public int Field; |
| 79 | + public int Property { get; set; } |
| 80 | + public void Method() { } |
| 81 | +} |
| 82 | +"; |
| 83 | + |
| 84 | + this.shippedText = @" |
| 85 | +C |
| 86 | +C.Field -> int |
| 87 | +C.Property.get -> int |
| 88 | +C.Property.set -> void |
| 89 | +C.Method() -> void |
| 90 | +"; |
| 91 | + this.unshippedText = string.Empty; |
| 92 | + |
| 93 | + await this.VerifyCSharpDiagnosticAsync(source, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 94 | + } |
| 95 | + |
| 96 | + [Fact] |
| 97 | + public async Task SplitBetweenShippedUnshippedAsync() |
| 98 | + { |
| 99 | + var source = @" |
| 100 | +public class C |
| 101 | +{ |
| 102 | + public int Field; |
| 103 | + public int Property { get; set; } |
| 104 | + public void Method() { } |
| 105 | +} |
| 106 | +"; |
| 107 | + |
| 108 | + this.shippedText = @" |
| 109 | +C |
| 110 | +C.Field -> int |
| 111 | +C.Property.get -> int |
| 112 | +C.Property.set -> void |
| 113 | +"; |
| 114 | + this.unshippedText = @" |
| 115 | +C.Method() -> void |
| 116 | +"; |
| 117 | + |
| 118 | + await this.VerifyCSharpDiagnosticAsync(source, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 119 | + } |
| 120 | + |
| 121 | + [Fact] |
| 122 | + public async Task EnumSplitBetweenFilesAsync() |
| 123 | + { |
| 124 | + var source = @" |
| 125 | +public enum E |
| 126 | +{ |
| 127 | + V1 = 1, |
| 128 | + V2 = 2, |
| 129 | + V3 = 3, |
| 130 | +} |
| 131 | +"; |
| 132 | + |
| 133 | + this.shippedText = @" |
| 134 | +E |
| 135 | +E.V1 = 1 -> E |
| 136 | +E.V2 = 2 -> E |
| 137 | +"; |
| 138 | + |
| 139 | + this.unshippedText = @" |
| 140 | +E.V3 = 3 -> E |
| 141 | +"; |
| 142 | + |
| 143 | + await this.VerifyCSharpDiagnosticAsync(source, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 144 | + } |
| 145 | + |
| 146 | + [Fact] |
| 147 | + public async Task SimpleRemovedMemberAsync() |
| 148 | + { |
| 149 | + var source = @" |
| 150 | +public class C |
| 151 | +{ |
| 152 | + public int Field; |
| 153 | + public int Property { get; set; } |
| 154 | +} |
| 155 | +"; |
| 156 | + |
| 157 | + this.shippedText = @" |
| 158 | +C |
| 159 | +C.Field -> int |
| 160 | +C.Property.get -> int |
| 161 | +C.Property.set -> void |
| 162 | +C.Method() -> void |
| 163 | +"; |
| 164 | + |
| 165 | + this.unshippedText = $@" |
| 166 | +{DeclarePublicAPIAnalyzer.RemovedApiPrefix}C.Method() -> void |
| 167 | +"; |
| 168 | + |
| 169 | + await this.VerifyCSharpDiagnosticAsync(source, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 170 | + } |
| 171 | + |
| 172 | + [Fact] |
| 173 | + public async Task ApiFileShippedWithRemovedAsync() |
| 174 | + { |
| 175 | + var source = @" |
| 176 | +public class C |
| 177 | +{ |
| 178 | + public int Field; |
| 179 | + public int Property { get; set; } |
| 180 | +} |
| 181 | +"; |
| 182 | + |
| 183 | + this.shippedText = $@" |
| 184 | +C |
| 185 | +C.Field -> int |
| 186 | +C.Property.get -> int |
| 187 | +C.Property.set -> void |
| 188 | +{DeclarePublicAPIAnalyzer.RemovedApiPrefix}C.Method() -> void |
| 189 | +"; |
| 190 | + |
| 191 | + this.unshippedText = string.Empty; |
| 192 | + |
| 193 | + // error RS0024: The contents of the public API files are invalid: The shipped API file can't have removed members |
| 194 | + var expected = this.CSharpDiagnostic(DeclarePublicAPIAnalyzer.PublicApiFilesInvalid).WithArguments(DeclarePublicAPIAnalyzer.InvalidReasonShippedCantHaveRemoved); |
| 195 | + |
| 196 | + await this.VerifyCSharpDiagnosticAsync(source, expected, CancellationToken.None).ConfigureAwait(false); |
| 197 | + } |
| 198 | + |
| 199 | + [Fact] |
| 200 | + public async Task DuplicateSymbolInSameAPIFileAsync() |
| 201 | + { |
| 202 | + var source = @" |
| 203 | +public class C |
| 204 | +{ |
| 205 | + public int Field; |
| 206 | + public int Property { get; set; } |
| 207 | +} |
| 208 | +"; |
| 209 | + |
| 210 | + this.shippedText = @" |
| 211 | +C |
| 212 | +C.Field -> int |
| 213 | +C.Property.get -> int |
| 214 | +C.Property.set -> void |
| 215 | +C.Property.get -> int |
| 216 | +"; |
| 217 | + |
| 218 | + this.unshippedText = string.Empty; |
| 219 | + |
| 220 | + // Warning RS0025: The symbol 'C.Property.get -> int' appears more than once in the public API files. |
| 221 | + var expected = this.CSharpDiagnostic(DeclarePublicAPIAnalyzer.DuplicateSymbolInApiFiles) |
| 222 | + .WithArguments("C.Property.get -> int") |
| 223 | + .WithLocation(DeclarePublicAPIAnalyzer.ShippedFileName, 6, 1) |
| 224 | + .WithLocation(DeclarePublicAPIAnalyzer.ShippedFileName, 4, 1); |
| 225 | + |
| 226 | + await this.VerifyCSharpDiagnosticAsync(source, expected, CancellationToken.None).ConfigureAwait(false); |
| 227 | + } |
| 228 | + |
| 229 | + [Fact] |
| 230 | + public async Task DuplicateSymbolInDifferentAPIFilesAsync() |
| 231 | + { |
| 232 | + var source = @" |
| 233 | +public class C |
| 234 | +{ |
| 235 | + public int Field; |
| 236 | + public int Property { get; set; } |
| 237 | +} |
| 238 | +"; |
| 239 | + |
| 240 | + this.shippedText = @" |
| 241 | +C |
| 242 | +C.Field -> int |
| 243 | +C.Property.get -> int |
| 244 | +C.Property.set -> void |
| 245 | +"; |
| 246 | + |
| 247 | + this.unshippedText = @" |
| 248 | +C.Property.get -> int"; |
| 249 | + |
| 250 | + // Warning RS0025: The symbol 'C.Property.get -> int' appears more than once in the public API files. |
| 251 | + var expected = this.CSharpDiagnostic(DeclarePublicAPIAnalyzer.DuplicateSymbolInApiFiles) |
| 252 | + .WithArguments("C.Property.get -> int") |
| 253 | + .WithLocation(DeclarePublicAPIAnalyzer.UnshippedFileName, 2, 1) |
| 254 | + .WithLocation(DeclarePublicAPIAnalyzer.ShippedFileName, 4, 1); |
| 255 | + |
| 256 | + await this.VerifyCSharpDiagnosticAsync(source, expected, CancellationToken.None).ConfigureAwait(false); |
| 257 | + } |
| 258 | + |
| 259 | + protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers() |
| 260 | + { |
| 261 | + yield return new DeclarePublicAPIAnalyzer(); |
| 262 | + } |
| 263 | + |
| 264 | + protected override CodeFixProvider GetCSharpCodeFixProvider() |
| 265 | + { |
| 266 | + return new DeclarePublicAPIFix(); |
| 267 | + } |
| 268 | + |
| 269 | + protected override string GetShippedPublicApi() |
| 270 | + { |
| 271 | + return this.shippedText; |
| 272 | + } |
| 273 | + |
| 274 | + protected override string GetUnshippedPublicApi() |
| 275 | + { |
| 276 | + return this.unshippedText; |
| 277 | + } |
| 278 | + } |
| 279 | +} |
0 commit comments