public void NotDisposingFieldAssignedInReturnMethodExpressionBody() { var testCode = @" namespace RoslynSandbox { using System; using System.IO; public class Foo { private Stream stream; public IDisposable Meh() => ↓this.stream = File.OpenRead(string.Empty); } }"; //// var fixedCode = @" ////using System; ////using System.IO; ////public class Foo ////{ //// private Stream stream; //// public IDisposable Meh() //// { //// this.stream?.Dispose(); //// return this.stream = File.OpenRead(string.Empty); //// } ////}"; // Not implementing the fix for now, not a common case. AnalyzerAssert.NoFix <IDISP003DisposeBeforeReassigning, DisposeBeforeAssignCodeFixProvider>(testCode); }
public void FixDoesNotMatchAnalyzer() { var code = @" namespace RoslynSandbox { class Foo { private readonly int ↓_value; } }"; var fixedCode = @" namespace RoslynSandbox { class Foo { private readonly int value; } }"; var expected = "Analyzer Gu.Roslyn.Asserts.Tests.NoErrorAnalyzer does not produce diagnostics fixable by Gu.Roslyn.Asserts.Tests.CodeFixes.DontUseUnderscoreCodeFixProvider.\r\n" + "The analyzer produces the following diagnostics: {NoError}\r\n" + "The code fix supports the following diagnostics: {SA1309, ID1, ID2}"; var exception = Assert.Throws <AssertException>(() => AnalyzerAssert.NoFix <NoErrorAnalyzer, DontUseUnderscoreCodeFixProvider>(code, fixedCode)); Assert.AreEqual(expected, exception.Message); }
public void InsideIfNegatedTrySet() { var testCode = @" namespace RoslynSandbox.Client { public class ViewModel : RoslynSandbox.Core.ViewModelBase { private string name; public string Greeting => $""Hello {this.Name}""; public string Name { get { return this.name; } set { if (!this.TrySet(ref this.name, value)) { ↓this.OnPropertyChanged(nameof(this.Greeting)); } } } } }"; AnalyzerAssert.NoFix <InvocationAnalyzer, CheckIfDifferentBeforeNotifyFixProvider>(ExpectedDiagnostic, new[] { ViewModelBaseCode, testCode }); }
public void TwoClassOneErrorNoFix() { var barCode = @" namespace RoslynSandbox { class Bar { private readonly int value; } }"; var code = @" namespace RoslynSandbox { class Foo { private readonly int ↓_value; } }"; AnalyzerAssert.NoFix <FieldNameMustNotBeginWithUnderscore, NoCodeFixProvider>(barCode, code); AnalyzerAssert.NoFix <FieldNameMustNotBeginWithUnderscore, NoCodeFixProvider>(new List <string> { barCode, code }); AnalyzerAssert.NoFix(new FieldNameMustNotBeginWithUnderscore(), new NoCodeFixProvider(), barCode, code); var expectedDiagnostic = ExpectedDiagnostic.Create(FieldNameMustNotBeginWithUnderscore.DiagnosticId); AnalyzerAssert.NoFix <FieldNameMustNotBeginWithUnderscore, NoCodeFixProvider>(expectedDiagnostic, barCode, code); AnalyzerAssert.NoFix(new FieldNameMustNotBeginWithUnderscore(), new NoCodeFixProvider(), expectedDiagnostic, barCode, code); }
public void TwoClassesOneErrorCodeFixFixedTheCode() { var barCode = @" namespace RoslynSandbox { class Bar { private readonly int value; } }"; var code = @" namespace RoslynSandbox { class Foo { private readonly int ↓_value; } }"; var exception = Assert.Throws <AssertException>(() => AnalyzerAssert.NoFix <FieldNameMustNotBeginWithUnderscore, DontUseUnderscoreCodeFixProvider>(barCode, code)); var expected = "Expected code to have no fixable diagnostics.\r\n" + "The following actions were registered:\r\n" + "Rename to: value\r\n"; CodeAssert.AreEqual(expected, exception.Message); }
public void NoCodeFixForHasMessage() { var testCode = TestUtility.WrapInTestMethod(@" var actual = ""Can we consider string as message?...""; Assert.That(actual, ↓Has.Message.EqualTo(2);"); AnalyzerAssert.NoFix(analyzer, fix, expectedDiagnostic, testCode); }
public void NoCodeFixForCountWhenLengthPropertyDoesNotExist() { var testCode = TestUtility.WrapInTestMethod(@" var actual = new [] {1, 2, 3}.Where(i => i > 1); Assert.That(actual, ↓Has.Count.EqualTo(2));", additionalUsings: "using System.Linq;"); AnalyzerAssert.NoFix(analyzer, fix, expectedDiagnostic, testCode); }
public void NoCodeFixForRandomMissingProperty() { var testCode = TestUtility.WrapInTestMethod(@" var actual = new List<int> { 1, 2, 3 }; Assert.That(actual, ↓Has.Property(""Whatever"").EqualTo(1));", additionalUsings: "using System.Collections.Generic;"); AnalyzerAssert.NoFix(analyzer, fix, expectedDiagnostic, testCode); }
public void NoCodeFixForCombinedAssertThat() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" public void Test() {{ var expected = 5; Assert.That(↓4, Is.Not.EqualTo(3).And.Not.EqualTo(5)); }}"); AnalyzerAssert.NoFix(analyzer, fix, expectedDiagnostic, code); }
public void NoCodeFixForNonSymmetricAssertThat(string isConstraint) { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" public void Test() {{ var expected = 5; Assert.That(↓4, Is.{isConstraint}(expected)); }}"); AnalyzerAssert.NoFix(analyzer, fix, expectedDiagnostic, code); }
public void SingleClassOneErrorEmptyFix() { var code = @" namespace RoslynSandbox { class Foo { private readonly int ↓_value; } }"; AnalyzerAssert.NoFix <FieldNameMustNotBeginWithUnderscore, EmptyCodeFixProvider>(code); }
public void IgnoresWhenBaseIsMouseGesture() { var testCode = @" namespace RoslynSandBox { using System.Windows.Input; public class CustomGesture : MouseGesture { ↓public int Foo { get; set; } } }"; AnalyzerAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, testCode); }
public void NoFixWhenBaseHasInternalOnPropertyChanged() { var testCode = @" namespace RoslynSandBox { using System.Windows.Input; public class CustomGesture : MouseGesture { ↓public int Foo { get; set; } } }"; AnalyzerAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, testCode); }
public void AssemblyGetTypeNoFix(string call) { var code = @" namespace RoslynSandbox { using System; public class C { public static object Get => typeof(C).Assembly.GetType(↓""MISSING""); } }".AssertReplace("typeof(C).Assembly.GetType(↓\"MISSING\")", call); AnalyzerAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, code); }
// [TestCase("RoslynSandbox.MISSING")] public void TypeGetTypeNoFix(string type) { var code = @" namespace RoslynSandbox { using System; public class C { public static object Get => Type.GetType(↓""MISSING""); } }".AssertReplace("MISSING", type); AnalyzerAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, code); }
public void ParameterlessGeneric() { var code = @" namespace RoslynSandbox { public class C { public static void M<T>() => typeof(C).GetMethod(nameof(C.M)).↓Invoke(null, null); } }"; var message = "Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true."; AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic.WithMessage(message), code); AnalyzerAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic.WithMessage(message), code); }
public void NullArgument(string testCase) { var testCode = @" namespace RoslynSandbox { using NUnit.Framework; public class FooTests { [TestCase(""x"", ""y"", null)] public void Test(string x, string y, string z) { } } }".AssertReplace("[TestCase(\"x\", \"y\", null)]", testCase); AnalyzerAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, testCode); }
public void ArgumentIsNullAndParameterIsInt() { var testCode = @" namespace RoslynSandbox { using NUnit.Framework; public class FooTests { [TestCase(↓null)] public void Test(int obj) { } } }"; AnalyzerAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, testCode); }
public void WhenInterfaceIsMissing() { var testCode = @" namespace RoslynSandbox { using System.IO; public class Foo { ↓private readonly Stream stream = File.OpenRead(string.Empty); public void Dispose() { } } }"; AnalyzerAssert.NoFix <IDISP006ImplementIDisposable, ImplementIDisposableCodeFixProvider>(testCode); }
public void WhenInterfaceIsMissing() { var testCode = @" namespace RoslynSandbox { using System.IO; public class Foo { ↓private readonly Stream stream = File.OpenRead(string.Empty); public void Dispose() { } } }"; AnalyzerAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, testCode); }
public void NoFixWhen(string before) { var code = @" namespace AspBox { using Microsoft.AspNetCore.Mvc; [ApiController] public class OrdersController : Controller { [HttpGet(""api/orders/{id}"")] public IActionResult GetId(string id) { return this.Ok(id); } } }".AssertReplace("\"api/orders/{id}\"", before); AnalyzerAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, code); }
public void NoFixForArgument() { var testCode = @" namespace RoslynSandbox { using System; public class Foo { internal static string Bar() { return Meh(↓new Disposable()); } private static string Meh(IDisposable stream) => stream.ToString(); } }"; AnalyzerAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, new[] { DisposableCode, testCode }); }
public void NoFixForArgument() { var testCode = @" namespace RoslynSandbox { using System.IO; public class Foo { internal static string Bar() { return Meh(↓File.OpenRead(string.Empty)); } private static string Meh(Stream stream) => stream.ToString(); } }"; AnalyzerAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, testCode); }
public void NegatedCheckReturn(TestCase check) { var testCode = @" namespace RoslynSandbox { using System; using System.ComponentModel; using System.Runtime.CompilerServices; public class ViewModel : INotifyPropertyChanged { private string bar; public event PropertyChangedEventHandler PropertyChanged; public string Bar { get { return this.bar; } set { if (!Equals(value, this.bar)) { return; } this.bar = value; ↓this.OnPropertyChanged(new PropertyChangedEventArgs(nameof(Bar))); } } protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) { this.PropertyChanged?.Invoke(this, e); } } }"; testCode = testCode.AssertReplace("Equals(value, this.bar)", check.Call) .AssertReplace("string", check.Type); AnalyzerAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, testCode); }
public void AutoPropertyExplicitNameHandlesRecursionInInvoker() { var testCode = @" namespace RoslynSandbox { using System.ComponentModel; public class Foo : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; ↓public int Bar { get; set; } protected virtual void OnPropertyChanged(string propertyName) { this.OnPropertyChanged(propertyName); } } }"; AnalyzerAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, testCode); }
public void OperatorNotEqualsReturn() { var testCode = @" namespace RoslynSandbox { using System.ComponentModel; using System.Runtime.CompilerServices; public class ViewModel : INotifyPropertyChanged { private int bar; public event PropertyChangedEventHandler PropertyChanged; public int Bar { get { return this.bar; } set { if (value != this.bar) { return; } this.bar = value; ↓this.OnPropertyChanged(new PropertyChangedEventArgs(nameof(Bar))); } } protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) { this.PropertyChanged?.Invoke(this, e); } } }"; AnalyzerAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, testCode); }
public void SingleClassOneErrorNoFix() { var code = @" namespace RoslynSandbox { class Foo { private readonly int ↓_value; } }"; var analyzer = new FieldNameMustNotBeginWithUnderscore(); var expectedDiagnostic = ExpectedDiagnostic.Create(FieldNameMustNotBeginWithUnderscore.DiagnosticId); AnalyzerAssert.NoFix <FieldNameMustNotBeginWithUnderscore, NoCodeFixProvider>(code); AnalyzerAssert.NoFix <FieldNameMustNotBeginWithUnderscore, NoCodeFixProvider>(expectedDiagnostic, code); AnalyzerAssert.NoFix <FieldNameMustNotBeginWithUnderscore, NoCodeFixProvider>((IReadOnlyList <string>) new[] { code }); AnalyzerAssert.NoFix(analyzer, new NoCodeFixProvider(), code); AnalyzerAssert.NoFix(analyzer, new NoCodeFixProvider(), expectedDiagnostic, code); AnalyzerAssert.NoFix(analyzer, new NoCodeFixProvider(), expectedDiagnostic, new List <string> { code }); AnalyzerAssert.NoFix(analyzer, new NoCodeFixProvider(), new[] { code }, CodeFactory.DefaultCompilationOptions(analyzer, expectedDiagnostic, AnalyzerAssert.SuppressedDiagnostics), AnalyzerAssert.MetadataReferences); }
public void TwoClassOneErrorEmptyFix() { var barCode = @" namespace RoslynSandbox { class Bar { private readonly int value; } }"; var code = @" namespace RoslynSandbox { class Foo { private readonly int ↓_value; } }"; AnalyzerAssert.NoFix <FieldNameMustNotBeginWithUnderscore, EmptyCodeFixProvider>(barCode, code); AnalyzerAssert.NoFix <FieldNameMustNotBeginWithUnderscore, EmptyCodeFixProvider>((IReadOnlyList <string>) new[] { barCode, code }); }
public void ConstructorIgnoredIfAnyNamed() { var testCode = @" namespace RoslynSandbox { public class Foo { public Foo(int a, int b, int c, int d) { this.A = a; this.B = b; this.C = c; this.D = d; } public int A { get; } public int B { get; } public int C { get; } public int D { get; } private Foo Create(int a, int b, int c, int d) { return new Foo↓( a, b, c, d: d); } } }"; AnalyzerAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, testCode); }
public void ConstructorIgnoredIfAnyNamed() { var testCode = @" namespace RoslynSandbox { public class Foo { public Foo(int a, int b, int c, int d) { this.A = a; this.B = b; this.C = c; this.D = d; } public int A { get; } public int B { get; } public int C { get; } public int D { get; } private Foo Create(int a, int b, int c, int d) { return new Foo↓( a, b, c, d: d); } } }"; AnalyzerAssert.NoFix <GU0001NameArguments, NameArgumentsCodeFixProvider>(testCode); }