public static void FixDoesNotMatchAnalyzer() { var code = @" namespace N { class C { private readonly int ↓_value; } }"; var fixedCode = @" namespace N { class C { private readonly int value; } }"; var expected = "NopAnalyzer does not produce diagnostics fixable by DoNotUseUnderscoreFix.\r\n" + "NopAnalyzer.SupportedDiagnostics: 'IdWithNoFix'.\r\n" + "DoNotUseUnderscoreFix.FixableDiagnosticIds: {SA1309, SA1309a, SA1309b}."; var analyzer = new NopAnalyzer(Descriptors.IdWithNoFix); var fix = new DoNotUseUnderscoreFix(); var exception = Assert.Throws <AssertException>(() => RoslynAssert.NoFix(analyzer, fix, code, fixedCode)); Assert.AreEqual(expected, exception.Message); }
public static void UsedByMoreThanOnePropertyMatchingNeither() { var code = @" namespace N { using System.Windows; using System.Windows.Controls; public class FooControl : Control { /// <summary>Identifies the <see cref=""Bar""/> dependency property.</summary> public static readonly DependencyProperty BarProperty = DependencyProperty.Register( nameof(Bar), typeof(int), typeof(FooControl), new PropertyMetadata(default(int), Meh, CoerceBar)); /// <summary>Identifies the <see cref=""Baz""/> dependency property.</summary> public static readonly DependencyProperty BazProperty = DependencyProperty.Register( nameof(Baz), typeof(int), typeof(FooControl), new PropertyMetadata(default(int), Meh, ↓CoerceBar)); public int Bar { get => (int)this.GetValue(BarProperty); set => this.SetValue(BarProperty, value); } public int Baz { get => (int)this.GetValue(BazProperty); set => this.SetValue(BazProperty, value); } private static void Meh(DependencyObject d, DependencyPropertyChangedEventArgs e) { if ((int)e.NewValue > 0) { d.ClearValue(BackgroundProperty); } } private static object CoerceBar(DependencyObject d, object baseValue) { if (baseValue is int i && i < 0) { return 0; } return baseValue; } } }"; RoslynAssert.Diagnostics(Analyzer, ExpectedDiagnostic, code); RoslynAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, code); }
public static void WhenNotContainingType() { var before = @" namespace N { using System.Windows; public static class ResourceKeys { public static readonly ComponentResourceKey FooKey = new ComponentResourceKey( ↓typeof(string), $""{typeof(ResourceKeys).FullName}.{nameof(FooKey)}""); } }"; var after = @" namespace N { using System.Windows; public static class ResourceKeys { public static readonly ComponentResourceKey FooKey = new ComponentResourceKey( typeof(ResourceKeys), $""{typeof(ResourceKeys).FullName}.{nameof(FooKey)}""); } }"; RoslynAssert.NoFix(Analyzer, ComponentResourceKeyFix, ExpectedDiagnostic, before); RoslynAssert.CodeFix(Analyzer, UseContainingTypeFix, ExpectedDiagnostic.WithMessage("Use containing type: ResourceKeys."), before, after); }
public static void TwoDocumentsOneErrorCodeFixFixedTheCode() { var barCode = @" namespace N { class Bar { private readonly int value; } }"; var code = @" namespace N { class C { private readonly int ↓_value; } }"; var analyzer = new FieldNameMustNotBeginWithUnderscore(); var fix = new DoNotUseUnderscoreFix(); var exception = Assert.Throws <AssertException>(() => RoslynAssert.NoFix(analyzer, fix, 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 static void WhenNoAnalyzerDiagnosticsButCompilerError() { var code = @" namespace N { class C { private readonly int ↓value = SYNTAX_ERROR; } }"; var analyzer = new FieldNameMustNotBeginWithUnderscore(); var fix = new CodeFixes.NoFix(); var expectedDiagnostic = ExpectedDiagnostic.Create(FieldNameMustNotBeginWithUnderscore.DiagnosticId); var expected = "Expected and actual diagnostics do not match.\r\n" + "Expected:\r\n" + " SA1309 \r\n" + " at line 5 and character 29 in file C.cs | private readonly int ↓value = SYNTAX_ERROR;\r\n" + "Actual:\r\n" + " CS0103 The name 'SYNTAX_ERROR' does not exist in the current context\r\n" + " at line 5 and character 37 in file C.cs | private readonly int value = ↓SYNTAX_ERROR;\r\n"; var exception = Assert.Throws <AssertException>(() => RoslynAssert.NoFix(analyzer, fix, code)); Assert.AreEqual(expected, exception.Message); exception = Assert.Throws <AssertException>(() => RoslynAssert.NoFix(analyzer, fix, code, string.Empty)); Assert.AreEqual(expected, exception.Message); exception = Assert.Throws <AssertException>(() => RoslynAssert.NoFix(analyzer, fix, expectedDiagnostic, code)); Assert.AreEqual(expected, exception.Message); exception = Assert.Throws <AssertException>(() => RoslynAssert.NoFix(analyzer, fix, expectedDiagnostic, code, string.Empty)); Assert.AreEqual(expected, exception.Message); }
public static void InsideIfNegatedTrySet() { var code = @" namespace N.Client { public class C : N.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)); } } } } }"; RoslynAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, new[] { ViewModelBaseCode, code }); }
public static void TwoDocumentsOneErrorNoFix() { var barCode = @" namespace N { class Bar { private readonly int value; } }"; var code = @" namespace N { class C { private readonly int ↓_value; } }"; var analyzer = new FieldNameMustNotBeginWithUnderscore(); var fix = new CodeFixes.NoFix(); RoslynAssert.NoFix(analyzer, fix, barCode, code); var expectedDiagnostic = ExpectedDiagnostic.Create(FieldNameMustNotBeginWithUnderscore.DiagnosticId); RoslynAssert.NoFix(analyzer, fix, expectedDiagnostic, barCode, code); }
public void WhenNoArguments() { var testCode = @" namespace RoslynSandbox { using System.Windows; public static class ResourceKeys { public static readonly ComponentResourceKey FooKey = new ComponentResourceKey(); } }"; var fixedCode = @" namespace RoslynSandbox { using System.Windows; public static class ResourceKeys { public static readonly ComponentResourceKey FooKey = new ComponentResourceKey(typeof(ResourceKeys), nameof(FooKey)); } }"; RoslynAssert.NoFix(Analyzer, UseContainingTypeFix, ExpectedDiagnostic, testCode); RoslynAssert.CodeFix(Analyzer, ComponentResourceKeyFix, ExpectedDiagnostic, testCode, fixedCode); }
public static void ParameterNameFoo() { var code = @" namespace N { using Gu.Roslyn.Asserts; using NUnit.Framework; public static class Valid { private static readonly PlaceholderAnalyzer Analyzer = new PlaceholderAnalyzer(); [Test] public static void M() { var c = @"" namespace N { class C { c(int ↓foo) { } } }""; RoslynAssert.Valid(Analyzer, c); } } }"; RoslynAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, Code.PlaceholderAnalyzer, code); }
public static void ClassNamedFooWithMethodNamedC() { var code = @" namespace N { using Gu.Roslyn.Asserts; using NUnit.Framework; public static class Valid { private static readonly PlaceholderAnalyzer Analyzer = new PlaceholderAnalyzer(); [Test] public static void M() { var foo = @"" namespace N { class ↓Foo { public int C() => 1; } }""; RoslynAssert.Valid(Analyzer, foo); } } }"; RoslynAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, Code.PlaceholderAnalyzer, code); }
public static void WhenNoArguments() { var before = @" namespace N { using System.Windows; public static class ResourceKeys { public static readonly ComponentResourceKey FooKey = new ComponentResourceKey↓(); } }"; var after = @" namespace N { using System.Windows; public static class ResourceKeys { public static readonly ComponentResourceKey FooKey = new ComponentResourceKey(typeof(ResourceKeys), nameof(FooKey)); } }"; RoslynAssert.NoFix(Analyzer, UseContainingTypeFix, ExpectedDiagnostic, before); RoslynAssert.CodeFix(Analyzer, ComponentResourceKeyFix, ExpectedDiagnostic, before, after); }
public void NoCodeFixForHasMessage() { var testCode = TestUtility.WrapInTestMethod(@" var actual = ""Can we consider string as message?...""; Assert.That(actual, ↓Has.Message.EqualTo(2));"); RoslynAssert.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;"); RoslynAssert.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;"); RoslynAssert.NoFix(analyzer, fix, expectedDiagnostic, testCode); }
public static void DuplicateId() { var expected = "SyntaxNodeAnalyzer.SupportedDiagnostics has more than one descriptor with ID 'ID1'."; var analyzer = new SyntaxNodeAnalyzer(Descriptors.Id1, Descriptors.Id1Duplicate); var fix = new DuplicateIdFix(); var expectedDiagnostic = ExpectedDiagnostic.Create(Descriptors.Id1); var exception = Assert.Throws <AssertException>(() => RoslynAssert.NoFix(analyzer, fix, expectedDiagnostic, string.Empty)); Assert.AreEqual(expected, exception.Message); }
public void NoCodeFixForCombinedAssertThat() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" public void Test() {{ var expected = 5; Assert.That(↓4, Is.Not.EqualTo(3).And.Not.EqualTo(5)); }}"); RoslynAssert.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)); }}"); RoslynAssert.NoFix(analyzer, fix, expectedDiagnostic, code); }
public void WhenCallbackMatchesOtherProperty() { var testCode = @" namespace RoslynSandbox { using System.Windows; using System.Windows.Controls; public class FooControl : Control { /// <summary>Identifies the <see cref=""Bar""/> dependency property.</summary> public static readonly DependencyProperty BarProperty = DependencyProperty.Register( nameof(Bar), typeof(double), typeof(FooControl), new FrameworkPropertyMetadata(1d), ValidateBar); /// <summary>Identifies the <see cref=""Baz""/> dependency property.</summary> public static readonly DependencyProperty BazProperty = DependencyProperty.Register( nameof(Baz), typeof(double), typeof(FooControl), new FrameworkPropertyMetadata(1d), ↓ValidateBar); public double Bar { get => (double)this.GetValue(BarProperty); set => this.SetValue(BarProperty, value); } public double Baz { get => (double)this.GetValue(BazProperty); set => this.SetValue(BazProperty, value); } private static bool ValidateBar(object value) { if (value is double d) { return d > 0; } return false; } } }"; RoslynAssert.Diagnostics(Analyzer, ExpectedDiagnostic.WithMessage("Method 'ValidateBar' should be named 'ValidateBaz'"), testCode); RoslynAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, testCode); }
public static void NoFixWhenBaseHasInternalOnPropertyChanged() { var code = @" namespace N { using System.Windows.Input; public class CustomGesture : MouseGesture { public int ↓Foo { get; set; } } }"; RoslynAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, code); }
public static void IgnoresWhenBaseIsMouseGesture() { var code = @" namespace N { using System.Windows.Input; public class CustomGesture : MouseGesture { ↓public int Foo { get; set; } } }"; RoslynAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, code); }
public static 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); RoslynAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, code); }
// [TestCase("RoslynSandbox.MISSING")] public static void TypeGetTypeNoFix(string type) { var code = @" namespace RoslynSandbox { using System; public class C { public static object Get => Type.GetType(↓""MISSING""); } }".AssertReplace("MISSING", type); RoslynAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, code); }
public static void NullArgument(string testCase) { var testCode = @" namespace N { using NUnit.Framework; public class C { [TestCase(""x"", ""y"", null)] public void M(string x, string y, string z) { } } }".AssertReplace("[TestCase(\"x\", \"y\", null)]", testCase); RoslynAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, testCode); }
public static void ArgumentIsNullAndParameterIsInt() { var testCode = @" namespace N { using NUnit.Framework; public class C { [TestCase(↓null)] public void M(int obj) { } } }"; RoslynAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, testCode); }
public static void FieldWhenInterfaceIsMissing() { var code = @" namespace N { using System.IO; public class C { ↓private readonly Stream stream = File.OpenRead(string.Empty); public void Dispose() { } } }"; RoslynAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, code); }
public static void PropertyWhenInterfaceIsMissing() { var code = @" namespace N { using System.IO; public class C { ↓public Stream Stream { get; } = File.OpenRead(string.Empty); public void Dispose() { } } }"; RoslynAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, code); }
public static void SingleDocumentOneErrorNoFix() { var code = @" namespace N { class C { private readonly int ↓_value; } }"; var analyzer = new FieldNameMustNotBeginWithUnderscore(); var fix = new CodeFixes.NoFix(); var expectedDiagnostic = ExpectedDiagnostic.Create(FieldNameMustNotBeginWithUnderscore.DiagnosticId); RoslynAssert.NoFix(analyzer, fix, code); RoslynAssert.NoFix(analyzer, new CodeFixes.NoFix(), expectedDiagnostic, code); RoslynAssert.NoFix(analyzer, new CodeFixes.NoFix(), expectedDiagnostic, new List <string> { code }); }
public static void NoFixWhen(string template) { var code = @" namespace AspBox { using Microsoft.AspNetCore.Mvc; [ApiController] public class OrdersController : Controller { [HttpGet(""api/orders/{id}"")] public IActionResult GetId(byte id) { return this.Ok(id); } } }".AssertReplace("\"api/orders/{id}\"", template); RoslynAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, code); }
public static void NegatedCheckAssignReturn(string type, string expression) { var code = @" namespace N { using System; using System.ComponentModel; using System.Runtime.CompilerServices; public class C : INotifyPropertyChanged { private int bar; public event PropertyChangedEventHandler PropertyChanged; public int Bar { get { return this.bar; } set { if (!Equals(value, this.bar)) { return; this.bar = value; } ↓this.OnPropertyChanged(); } } protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } }".AssertReplace("Equals(value, this.bar)", expression) .AssertReplace("int", type); RoslynAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, code); }
public static void AutoPropertyExplicitNameHandlesRecursionInInvoker() { var code = @" namespace N { 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); } } }"; RoslynAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, code); }