public static void ConstructorSettingFieldPrefixedByUnderscore() { var code = @" namespace N { public class C { private readonly int _a; private readonly int _b; private readonly int _c; private readonly int _d; public C(int a, int b, int c, int d) { _a = a; _b = b; _c = c; _d = d; } } }"; RoslynAssert.Valid(Analyzer, code); }
public static void ExplicitImplementationCalculatedBeforeGetSet() { var iValue = @" namespace N { interface IValue { object Value { get; } } }"; var c = @" namespace N { public class C : IValue { object IValue.Value => this.Value; public int Value { get; set; } } }"; RoslynAssert.Valid(Analyzer, iValue, c); }
public static void ExplicitImplementationGetOnlyIndexer() { var iValue = @" namespace N { interface IValue { object this[int index] { get; } } }"; var c = @" namespace N { public class C : IValue { public int this[int index] => index; object IValue.this[int index] => index; } }"; RoslynAssert.Valid(Analyzer, iValue, c); }
public void DependencyPropertyRegisterAttached() { var testCode = @" namespace RoslynSandbox { using System; using System.Collections.ObjectModel; using System.Windows; public static class Foo { public static readonly DependencyProperty BarProperty = DependencyProperty.RegisterAttached( ""Bar"", typeof(int), typeof(Foo), new PropertyMetadata(default(int))); public static void SetBar(FrameworkElement element, int value) { element.SetValue(BarProperty, value); } public static int GetBar(FrameworkElement element) { return (int)element.GetValue(BarProperty); } public static void Meh(FrameworkElement element) { element.SetValue(BarProperty, ↓1.0); } } }"; RoslynAssert.Diagnostics(Analyzer, ExpectedDiagnostic, testCode); }
public static void IntWhenTypeIsEnum() { var e = @" namespace N { public enum E { M1, M2 } }"; var code = @" namespace N { using System.Windows; using System.Windows.Controls; public class FooControl : Control { /// <summary>Identifies the <see cref=""E""/> dependency property.</summary> public static readonly DependencyProperty EProperty = DependencyProperty.Register( nameof(E), typeof(E), typeof(FooControl), new PropertyMetadata(↓1)); public E E { get => (E) this.GetValue(EProperty); set => this.SetValue(EProperty, value); } } }"; RoslynAssert.Diagnostics(Analyzer, ExpectedDiagnostic, e, code); }
public static void AssignFieldViaParameter() { var code = @" namespace N { public class C { private Disposable disposable; public C() { var temp = new Disposable(); M(temp); } private void M(Disposable disposable) { this.disposable = disposable; } } }"; RoslynAssert.Valid(Analyzer, Descriptor, Disposable, code); }
public void AddOwnerTextElementFontSize() { var testCode = @" namespace RoslynSandbox { using System.Windows; using System.Windows.Documents; public class FooControl : FrameworkElement { public static readonly DependencyProperty FontSizeProperty = TextElement.FontSizeProperty.AddOwner(typeof(FooControl)); public double FontSize { get => (double)this.GetValue(FontSizeProperty); set => this.SetValue(FontSizeProperty, value); } public void Update(int i) => this.SetValue(FontSizeProperty, ↓i); } }"; RoslynAssert.Diagnostics(Analyzer, ExpectedDiagnostic, testCode); }
public static void TypeOfDictionaryGetMethodNameOfHashSetAdd() { var testCode = @" namespace RoslynSandbox { using System.Collections.Generic; public class C { public C() { var member = typeof(Dictionary<string, object>).GetMethod(nameof(HashSet<string>.Add)); } private static int Add(int x, int y) => x + y; } }"; var fixedCode = @" namespace RoslynSandbox { using System.Collections.Generic; public class C { public C() { var member = typeof(Dictionary<string, object>).GetMethod(nameof(Dictionary<string, object>.Add)); } private static int Add(int x, int y) => x + y; } }"; RoslynAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, testCode, fixedCode); }
public static void WhenString(string before, string after) { 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); var fixedCode = @" 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}\"", after); RoslynAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, code, fixedCode); }
public static void ThisGetTypeGetMethodNameOfHashSetAddUnderscore() { var testCode = @" namespace RoslynSandbox { using System.Collections.Generic; public class C { public C() { var member = GetType().GetMethod(nameof(HashSet<string>.Add)); } private int Add(int x, int y) => x + y; } }"; var fixedCode = @" namespace RoslynSandbox { using System.Collections.Generic; public class C { public C() { var member = GetType().GetMethod(nameof(Add)); } private int Add(int x, int y) => x + y; } }"; RoslynAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, testCode, fixedCode); }
public static void TypeOfConsoleGetMethodNameOfStaticWriteLine() { var testCode = @" namespace RoslynSandbox { using System; public class C { public C() { _ = typeof(Console).GetMethod(nameof(WriteLine), Type.EmptyTypes); } public bool WriteLine { get; set; } } }"; var fixedCode = @" namespace RoslynSandbox { using System; public class C { public C() { _ = typeof(Console).GetMethod(nameof(Console.WriteLine), Type.EmptyTypes); } public bool WriteLine { get; set; } } }"; RoslynAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, testCode, fixedCode); }
public static void DisposingFieldInExpressionBodyDispose() { var disposableCode = @" namespace N { using System; class Disposable : IDisposable { public void Dispose() { } } }"; var code = @" namespace N { using System; class Goof : IDisposable { IDisposable _disposable; public void Create() => _disposable = new Disposable(); public void Dispose() => _disposable.Dispose(); } }"; RoslynAssert.Valid(Analyzer, disposableCode, code); }
public static void SubclassMvxViewModelAddUsing() { var before = @" namespace N { public class ↓C { public int P { get; set; } } }"; var after = @" namespace N { using MvvmCross.ViewModels; public class C : MvxViewModel { public int P { get; set; } } }"; RoslynAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, before, after, fixTitle: "Subclass MvvmCross.ViewModels.MvxViewModel and add using.", metadataReferences: MetadataReferences); }
public static void MissingMethodWhenInterface() { var iC = @" namespace N { public interface IC { } }"; var code = @" namespace N { public class C { public C() { var methodInfo = typeof(IC).GetMethod(↓""MISSING""); } } }"; RoslynAssert.Diagnostics(Analyzer, ExpectedDiagnostic, iC, code); }
public static void OnPropertyChangedWithEventArgs(string propertyName) { var code = @" namespace N { using System.ComponentModel; using System.Runtime.CompilerServices; public class C : INotifyPropertyChanged { private int p; public event PropertyChangedEventHandler PropertyChanged; public int P { get { return this.p; } set { if (value == this.p) return; this.p = value; #pragma warning disable INPC013 this.OnPropertyChanged(new PropertyChangedEventArgs(nameof(P))); #pragma warning restore INPC013 } } protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) { this.PropertyChanged?.Invoke(this, e); } } }".AssertReplace(@"nameof(P)", propertyName); RoslynAssert.Valid(Analyzer, code); }
public static void PropertyReturningObject() { var code = @" namespace N { public class C { public void M() { var p = P; } public object P { get { return new object(); } } } }"; RoslynAssert.Valid(Analyzer, code); }
public static void AssignFieldListAdd() { var code = @" namespace N { using System.Collections.Generic; public class C { private List<Disposable> disposables = new List<Disposable>(); public C() { for (var i = 0; i < 2; i++) { var item = new Disposable(); disposables.Add(item); } } } }"; RoslynAssert.Valid(Analyzer, Disposable, code); }
public static void IndexerReturningObject() { var code = @" namespace N { public class C { public void M() { var meh = this[0]; } public object this[int index] { get { return new object(); } } } }"; RoslynAssert.Valid(Analyzer, code); }
public void Message() { var testCode = @" namespace RoslynSandbox { using System.Windows; using System.Windows.Controls; public class FooControl : Control { static FooControl() { EventManager.RegisterClassHandler(typeof(TextBlock), SizeChangedEvent, new RoutedEventHandler(↓WrongName)); } private static void WrongName(object sender, RoutedEventArgs e) { throw new System.NotImplementedException(); } } }"; RoslynAssert.Diagnostics(Analyzer, ExpectedDiagnostic.WithMessage("Rename to OnSizeChanged to match the event."), testCode); }
public static void MethodReturningTaskFromResultOfDisposable() { var code = @" namespace N { using System; using System.Threading.Tasks; public class C { public void M() { CreateDisposableAsync(); } private static Task<IDisposable> CreateDisposableAsync() { return Task.FromResult<IDisposable>(new Disposable()); } } }"; RoslynAssert.Valid(Analyzer, DisposableCode, code); }
public void AddOwnerBorderBorderThicknessProperty() { var testCode = @" namespace RoslynSandbox { using System.Windows; using System.Windows.Controls; public class FooControl : FrameworkElement { public static readonly DependencyProperty BorderThicknessProperty = Border.BorderThicknessProperty.AddOwner(typeof(FooControl)); public Thickness BorderThickness { get => (Thickness)GetValue(BorderThicknessProperty); set => SetValue(BorderThicknessProperty, value); } public void Update(int i) => this.SetValue(BorderThicknessProperty, ↓i); } }"; RoslynAssert.Diagnostics(Analyzer, ExpectedDiagnostic, testCode); }
public static void CallingOverload() { var code = @" namespace N { using System.Threading; using System.Threading.Tasks; public class C { public Task M(string source, int expected, CancellationToken cancellationToken) { return this.M(source, new[] { expected }, cancellationToken); } public Task M(string source, int[] expected, CancellationToken cancellationToken) { return Task.FromResult(0); } } }"; RoslynAssert.Valid(Analyzer, code); }
public void WhenHasAttribute() { var testCode = @" namespace RoslynSandbox { using System.Windows; public class Foo { public static readonly DependencyProperty ValueProperty = DependencyProperty.RegisterAttached( ""Value"", typeof(int), typeof(Foo), new PropertyMetadata(default(int))); /// <summary>Helper for setting <see cref=""ValueProperty""/> on <paramref name=""element""/>.</summary> /// <param name=""element""><see cref=""DependencyObject""/> to set <see cref=""ValueProperty""/> on.</param> /// <param name=""value"">Value property value.</param> public static void SetValue(DependencyObject element, int value) { element.SetValue(ValueProperty, value); } /// <summary>Helper for getting <see cref=""ValueProperty""/> from <paramref name=""element""/>.</summary> /// <param name=""element""><see cref=""DependencyObject""/> to read <see cref=""ValueProperty""/> from.</param> /// <returns>Value property value.</returns> [AttachedPropertyBrowsableForType(typeof(DependencyObject))] public static int GetValue(DependencyObject element) { return (int)element.GetValue(ValueProperty); } } }"; RoslynAssert.Valid(Analyzer, testCode); }
public static void NotDisposingVariable() { var before = @" namespace N { using System.IO; public class C { public void M() { var stream = File.OpenRead(string.Empty); ↓stream = File.OpenRead(string.Empty); } } }"; var after = @" namespace N { using System.IO; public class C { public void M() { var stream = File.OpenRead(string.Empty); stream?.Dispose(); stream = File.OpenRead(string.Empty); } } }"; RoslynAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, before, after); RoslynAssert.FixAll(Analyzer, Fix, ExpectedDiagnostic, before, after); }
public static void SubclassPropertyChangedBaseAddUsing() { var before = @" namespace N { public class ↓C { public int P { get; set; } } }"; var after = @" namespace N { using Caliburn.Micro; public class C : PropertyChangedBase { public int P { get; set; } } }"; RoslynAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, before, after, fixTitle: "Subclass Caliburn.Micro.PropertyChangedBase and add using.", metadataReferences: MetadataReferences); }
public static void BaseCall() { var before = @" namespace N { using Gu.Reactive; public class ValueCondition : Condition { public ValueCondition(C1 c1) : base↓( () => c1.Value == 2, c1.ObservePropertyChangedSlim(x => x.Value)) { } } }"; var after = @" namespace N { using Gu.Reactive; public class ValueCondition : Condition { public ValueCondition(C1 c1) : base( c1.ObservePropertyChangedSlim(x => x.Value), () => c1.Value == 2) { } } }"; RoslynAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, new[] { C1, before }, after); }
public static void ExplicitImplementationGetOnly() { var iValue = @" namespace N { interface IValue { object Value { get; } } }"; var c = @" namespace N { public class C : IValue { public int Value { get; } = 5; object IValue.Value { get; } = 5; } }"; RoslynAssert.Valid(Analyzer, iValue, c); }
public void ReadOnlyDependencyProperty() { var testCode = @" namespace RoslynSandbox { using System.Windows; using System.Windows.Controls; public class FooControl : Control { private static readonly DependencyPropertyKey BarPropertyKey = DependencyProperty.RegisterReadOnly( ""Bar"", typeof(int), typeof(FooControl), new PropertyMetadata(default(int))); public static readonly DependencyProperty BarProperty = BarPropertyKey.DependencyProperty; public int Bar { get { return (int)this.GetValue(BarProperty); } protected set { this.SetValue(BarPropertyKey, value); ↓SideEffect(); } } private void SideEffect() { } } }"; RoslynAssert.Diagnostics(Analyzer, ExpectedDiagnostic, testCode); }
public static void WithExpectedDiagnosticWithWrongId() { var code = @" namespace N { class C { private readonly int value; } }"; var expected = "FieldNameMustNotBeginWithUnderscore does not produce a diagnostic with ID 'WRONG'.\r\n" + "FieldNameMustNotBeginWithUnderscore.SupportedDiagnostics: 'SA1309'.\r\n" + "The expected diagnostic is: 'WRONG'."; var expectedDiagnostic = ExpectedDiagnostic.Create("WRONG"); var analyzer = new FieldNameMustNotBeginWithUnderscore(); var exception = Assert.Throws <AssertException>(() => RoslynAssert.Diagnostics(analyzer, expectedDiagnostic, code)); Assert.AreEqual(expected, exception.Message); exception = Assert.Throws <AssertException>(() => RoslynAssert.Diagnostics(analyzer, new[] { expectedDiagnostic }, code)); Assert.AreEqual(expected, exception.Message); }
public static void ConstructorSettingField() { var code = @" namespace N { public class C { private readonly int a; private readonly int b; private readonly int c; private readonly int d; public C(int a, int b, int c, int d) { this.a = a; this.b = b; this.c = c; this.d = d; } } }"; RoslynAssert.Valid(Analyzer, code); }