Exemple #1
0
        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);
        }
Exemple #2
0
        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);
        }
Exemple #3
0
        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);
        }
Exemple #4
0
        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);
        }
Exemple #5
0
        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);
        }
Exemple #6
0
        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);
        }
Exemple #7
0
        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);
            }
Exemple #12
0
        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);
        }
Exemple #13
0
            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);
            }
Exemple #15
0
        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);
        }
Exemple #16
0
        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);
        }
Exemple #17
0
        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);
        }
Exemple #18
0
        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);
        }
Exemple #19
0
        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);
        }
Exemple #20
0
        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);
        }
Exemple #21
0
        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);
        }
Exemple #22
0
        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);
        }
Exemple #23
0
        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);
        }
Exemple #24
0
        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);
        }
Exemple #25
0
            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);
            }
Exemple #26
0
        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);
        }
Exemple #27
0
        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);
        }
Exemple #28
0
        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);
        }
Exemple #29
0
            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);
            }
Exemple #30
0
        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);
        }