|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Microsoft.CodeAnalysis; |
| 4 | +using Microsoft.CodeAnalysis.CSharp; |
| 5 | +using Xunit; |
| 6 | +using Xunit.Analyzers; |
| 7 | +using Verify = CSharpVerifier<Xunit.Analyzers.FactAttributeDerivedClassesShouldProvideSourceInformationConstructor>; |
| 8 | +using Verify_v3_Pre300 = CSharpVerifier<FactAttributeDerivedClassesShouldProvideSourceInformationConstructorTests.Analyzer_v3_Pre300>; |
| 9 | + |
| 10 | +public class FactAttributeDerivedClassesShouldProvideSourceInformationConstructorTests |
| 11 | +{ |
| 12 | + [Fact] |
| 13 | + public async Task v2_OlderV3_DoesNotTrigger() |
| 14 | + { |
| 15 | + var code = /* lang=c#-test */ """ |
| 16 | + using Xunit; |
| 17 | +
|
| 18 | + public class MyFactAttribute : FactAttribute { } |
| 19 | +
|
| 20 | + public class MyTheoryAttribute : TheoryAttribute { } |
| 21 | + """; |
| 22 | + |
| 23 | + await Verify.VerifyAnalyzerV2(code); |
| 24 | + await Verify_v3_Pre300.VerifyAnalyzerV3(code); |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public async Task v3_Triggers() |
| 30 | + { |
| 31 | +#if ROSLYN_LATEST |
| 32 | + var code = /* lang=c#-test */ """ |
| 33 | + using System.Runtime.CompilerServices; |
| 34 | + using Xunit; |
| 35 | +
|
| 36 | + public class {|xUnit3003:MyFactAttribute|} : FactAttribute { } |
| 37 | +
|
| 38 | + public class MyFactWithCtorArgs([CallerFilePath] string? foo = null, [CallerLineNumber] int bar = -1) |
| 39 | + : FactAttribute(foo, bar) |
| 40 | + { } |
| 41 | +
|
| 42 | + public class {|xUnit3003:MyTheoryAttribute|} : TheoryAttribute { } |
| 43 | +
|
| 44 | + public class MyTheoryWithCtorArgs( |
| 45 | + int x, |
| 46 | + string y, |
| 47 | + [CallerFilePath] string? sourceFilePath = null, |
| 48 | + [CallerLineNumber] int sourceLineNumber = -1) |
| 49 | + : TheoryAttribute(sourceFilePath, sourceLineNumber) |
| 50 | + { } |
| 51 | + """; |
| 52 | + |
| 53 | + await Verify.VerifyAnalyzerV3(LanguageVersion.CSharp12, code); |
| 54 | +#else |
| 55 | + var code = /* lang=c#-test */ """ |
| 56 | + using System.Runtime.CompilerServices; |
| 57 | + using Xunit; |
| 58 | +
|
| 59 | + public class {|xUnit3003:MyFactAttribute|} : FactAttribute { } |
| 60 | +
|
| 61 | + public class MyFactWithCtorArgs : FactAttribute |
| 62 | + { |
| 63 | + public MyFactWithCtorArgs([CallerFilePath] string? foo = null, [CallerLineNumber] int bar = -1) |
| 64 | + : base(foo, bar) |
| 65 | + { } |
| 66 | + } |
| 67 | +
|
| 68 | + public class {|xUnit3003:MyTheoryAttribute|} : TheoryAttribute { } |
| 69 | +
|
| 70 | + public class MyTheoryWithCtorArgs : TheoryAttribute |
| 71 | + { |
| 72 | + public MyTheoryWithCtorArgs(int x, string y, [CallerFilePath] string? foo = null, [CallerLineNumber] int bar = -1) |
| 73 | + : base(foo, bar) |
| 74 | + { } |
| 75 | + } |
| 76 | + """; |
| 77 | + |
| 78 | + await Verify.VerifyAnalyzerV3(LanguageVersion.CSharp8, code); |
| 79 | +#endif |
| 80 | + } |
| 81 | + |
| 82 | + internal class Analyzer_v3_Pre300 : FactAttributeDerivedClassesShouldProvideSourceInformationConstructor |
| 83 | + { |
| 84 | + protected override XunitContext CreateXunitContext(Compilation compilation) => |
| 85 | + XunitContext.ForV3(compilation, new Version(2, 999, 999)); |
| 86 | + } |
| 87 | +} |
0 commit comments