private void AssertDependency(string code)
        {
            var(assembly, diagnostics) = RAssert.Compile(code);
            var type = assembly.GetTypes().Single(x => x.Name == "TestClass");

            var givenNamesProperty = type.GetProperty("GivenNames");
            var familyNameProperty = type.GetProperty("FamilyName");

            var instance = (INotifyPropertyChanged)Activator.CreateInstance(type);

            var count = 0;

            instance.PropertyChanged += (sender, e) =>
            {
                if (e.PropertyName == "FullName")
                {
                    count++;
                }
            };

            givenNamesProperty.SetValue(instance, "Cameron");
            familyNameProperty.SetValue(instance, "MacFarland");

            Assert.Equal(2, count);
        }
        public void MultipleClasses()
        {
            var code = @"using System.ComponentModel;
public class PropertyChangedBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public class ExampleClass : PropertyChangedBase
{
    public CustomStruct Custom { get; set; }
}
public struct CustomStruct
{
    public CustomStruct(int value)
    {
        Value = value;
    }

    public int Value { get; }

    public override bool Equals(object obj) => Value.Equals(obj);
    public override int GetHashCode() => Value.GetHashCode();
}
";

            RAssert.Compile(code);
        }
        public void WrongHelperType()
        {
            var code = @"using System.ComponentModel;
public class TestClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(object name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name.ToString()));
	public int Property { get; set; }
}
";

            RAssert.PropertyChangedNotFired(code, 1);
        }
Exemple #4
0
#pragma warning disable xUnit1026 // Theory methods should use all of their parameters
        public void SingleClass_ComplexTypes <T>(string type, T value1, T value2)
#pragma warning restore xUnit1026 // Theory methods should use all of their parameters
        {
            var code = $@"using System.ComponentModel;
using Conan.Plugin.PropertyChanged.Tests;
public class TestClass : INotifyPropertyChanged
{{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
	public {type} Property {{ get; set; }}
}}";

            RAssert.PropertyChangedFired(code, value1);
        }
Exemple #5
0
#pragma warning disable xUnit1026 // Theory methods should use all of their parameters
        public void BeforeAfterHelper <T>(string type, T value1, T value2)
#pragma warning restore xUnit1026 // Theory methods should use all of their parameters
        {
            var code = $@"using System.ComponentModel;
using Conan.Plugin.PropertyChanged.Tests;
public class TestClass : INotifyPropertyChanged
{{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name, object before, object after) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
	public {type} Property {{ get; set; }}
}}";

            RAssert.ChangeGuardDoesDetectChange(code, value1, value2);
        }
        public void NormalPropertyNotConverted()
        {
            var code = @"using System.ComponentModel;
public class TestClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));

    private int _property;
    public int Property { get => _property; set => _property = value; }
}
";

            RAssert.PropertyChangedNotFired(code, 1);
        }
        public void MissingHelper()
        {
            var code = @"using System.ComponentModel;
public class TestClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
	public int Property { get; set; }
}
";

            var(assembly, diagnostics) = RAssert.Compile(code);

            Assert.Single(diagnostics);
            Assert.Equal("PC0001", diagnostics[0].Id);
        }
        public void WrongHelperType()
        {
            var code = @"using System.ComponentModel;
public class TestClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(object name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name.ToString()));
	public int Property { get; set; }
}
";

            var(assembly, diagnostics) = RAssert.Compile(code);

            Assert.Single(diagnostics);
            Assert.Equal("PC0002", diagnostics[0].Id);
        }