|
| 1 | +using Gu.Roslyn.Asserts; |
| 2 | +using Microsoft.CodeAnalysis.CodeFixes; |
| 3 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 4 | +using NUnit.Analyzers.Constants; |
| 5 | +using NUnit.Analyzers.UseSpecificConstraint; |
| 6 | +using NUnit.Framework; |
| 7 | + |
| 8 | +namespace NUnit.Analyzers.Tests.UseSpecificConstraint |
| 9 | +{ |
| 10 | + public class UseSpecificConstraintCodeFixTests |
| 11 | + { |
| 12 | + private static readonly DiagnosticAnalyzer analyzer = new UseSpecificConstraintAnalyzer(); |
| 13 | + private static readonly CodeFixProvider fix = new UseSpecificConstraintCodeFix(); |
| 14 | + private static readonly ExpectedDiagnostic expectedDiagnostic = |
| 15 | + ExpectedDiagnostic.Create(AnalyzerIdentifiers.UseSpecificConstraint); |
| 16 | + |
| 17 | + private static readonly string[][] EqualToSpecificConstraint = |
| 18 | + [ |
| 19 | + ["false", "False"], |
| 20 | +#if NUNIT4 |
| 21 | + ["default(bool)", "Default"], |
| 22 | +#else |
| 23 | + ["default(bool)", "False"], |
| 24 | +#endif |
| 25 | + ["true", "True"], |
| 26 | + |
| 27 | + ["null", "Null"], |
| 28 | + ["default(object)", "Null"], |
| 29 | + ["default(string)", "Null"], |
| 30 | +#if NUNIT4 |
| 31 | + ["default(int)", "Default"], |
| 32 | +#endif |
| 33 | + ]; |
| 34 | + |
| 35 | + [TestCaseSource(nameof(EqualToSpecificConstraint))] |
| 36 | + public void AnalyzeForSpecificConstraint(string literal, string constraint) => AnalyzeForEqualTo(literal, constraint); |
| 37 | + |
| 38 | +#if NUNIT4 |
| 39 | + [Test] |
| 40 | + public void AnalyzeForIsDefault() => AnalyzeForEqualTo("default", "Default", |
| 41 | + Settings.Default.WithAllowedCompilerDiagnostics(AllowedCompilerDiagnostics.WarningsAndErrors)); |
| 42 | +#endif |
| 43 | + |
| 44 | + private static void AnalyzeForEqualTo(string literal, string constraint, Settings? settings = null) |
| 45 | + { |
| 46 | + AnalyzeForEqualTo("Is", string.Empty, literal, constraint, settings); |
| 47 | + AnalyzeForEqualTo("Is", ".And.Not.Empty", literal, constraint, settings); |
| 48 | + AnalyzeForEqualTo("Is.Not", string.Empty, literal, constraint, settings); |
| 49 | + AnalyzeForEqualTo("Is.EqualTo(0).Or", string.Empty, literal, constraint, settings); |
| 50 | + } |
| 51 | + |
| 52 | + private static void AnalyzeForEqualTo(string prefix, string suffix, string literal, string constraint, Settings? settings = null) |
| 53 | + { |
| 54 | + var testCode = TestUtility.WrapInTestMethod( |
| 55 | + $"Assert.That(false, ↓{prefix}.EqualTo({literal}){suffix});"); |
| 56 | + |
| 57 | + var fixedCode = TestUtility.WrapInTestMethod( |
| 58 | + $"Assert.That(false, {prefix}.{constraint}{suffix});"); |
| 59 | + |
| 60 | + RoslynAssert.CodeFix(analyzer, fix, |
| 61 | + expectedDiagnostic.WithMessage($"Replace 'Is.EqualTo({literal})' with 'Is.{constraint}' constraint"), |
| 62 | + testCode, fixedCode, |
| 63 | + settings: settings); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments