Пример #1
0
        public async Task NegatedCheck(EqualsItem check)
        {
            var testCode = @"
    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))
                {
                    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);
            await this.VerifyHappyPathAsync(testCode).ConfigureAwait(false);
        }
Пример #2
0
        public async Task NegatedCheck(EqualsItem check)
        {
            var testCode = @"
    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);
            var expected = this.CSharpDiagnostic().WithLocationIndicated(ref testCode).WithMessage("Check if value is different before notifying.");

            await this.VerifyCSharpDiagnosticAsync(testCode, expected).ConfigureAwait(false);

            await this.VerifyCSharpFixAsync(testCode, testCode).ConfigureAwait(false);
        }
Пример #3
0
        public async Task Check(EqualsItem check)
        {
            var testCode = @"
using System;
using System.ComponentModel;

public class ViewModel : INotifyPropertyChanged
{
    private Foo bar;

    public event PropertyChangedEventHandler PropertyChanged;

    public Foo 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);
            var expected = this.CSharpDiagnostic().WithLocationIndicated(ref testCode).WithMessage("Check if value is different using ReferenceEquals before notifying.");

            await this.VerifyCSharpDiagnosticAsync(new[] { FooCode, testCode }, expected).ConfigureAwait(false);

            var fixedCode = @"
using System;
using System.ComponentModel;

public class ViewModel : INotifyPropertyChanged
{
    private Foo bar;

    public event PropertyChangedEventHandler PropertyChanged;

    public Foo 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);
    }
}";

            fixedCode = check.FixedCall == null
                            ? fixedCode.AssertReplace("Equals(value, this.bar)", check.Call)
                            : fixedCode.AssertReplace("Equals(value, this.bar)", check.FixedCall);

            await this.VerifyCSharpFixAsync(new[] { FooCode, testCode }, new[] { FooCode, fixedCode }, allowNewCompilerDiagnostics : true).ConfigureAwait(false);
        }