private static object Create <TNotifier, TProperty>(Expression <Func <TNotifier, TProperty> > propertyPath)
            where TNotifier : class, INotifyPropertyChanged
        {
            var properties   = PropertyPathParser.GetPath(propertyPath);
            var errorBuilder = new StringBuilder();
            var parts        = new INotifyingGetter[properties.Count];

            for (var i = 0; i < properties.Count; i++)
            {
                if (TryGetError(properties, i, out string errorMessage))
                {
                    errorBuilder.Append(errorMessage);
                    errorBuilder.AppendLine();
                }

                if (errorBuilder.Length == 0)
                {
                    var property = properties[i];
                    var item     = Getter.GetOrCreate(property);
                    parts[i] = (INotifyingGetter)item;
                }
            }

            if (errorBuilder.Length != 0)
            {
                return(errorBuilder.ToString());
            }

            return(new NotifyingPath <TNotifier, TProperty>(parts));
        }
Exemple #2
0
        public void ThrowsOnNullProp()
        {
            // ReSharper disable once ObjectCreationAsStatement
            var exception = Assert.Throws <ArgumentNullException>(() => Getter.GetOrCreate(null));

            Assert.AreEqual("Value cannot be null.\r\nParameter name: property", exception.Message);
        }
Exemple #3
0
        public void GetValueGenericStruct()
        {
            var source = new StructLevel {
                Name = "meh"
            };
            var getter = (Getter <StructLevel, string>)Getter.GetOrCreate(typeof(StructLevel).GetProperty("Name"));

            Assert.AreEqual("meh", getter.GetValue(source));
        }
Exemple #4
0
        public void GetValueGenericStruct()
        {
            var source = new StructLevel {
                Name = "meh"
            };
            var getter = (Getter <StructLevel, string>)Getter.GetOrCreate(typeof(StructLevel).GetProperty(nameof(StructLevel.Name), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly));

            Assert.AreEqual("meh", getter.GetValue(source));
        }
Exemple #5
0
        public void GetValueGeneric()
        {
            var source = new Fake <int> {
                Value = 1
            };
            var getter = Getter.GetOrCreate(typeof(Fake <int>).GetProperty(nameof(Fake <int> .Value), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly));

            Assert.AreEqual(1, getter.GetValue(source));
            var genericGetter = (Getter <Fake <int>, int>)getter;

            Assert.AreEqual(1, genericGetter.GetValue(source));
        }
Exemple #6
0
        public void GetValue()
        {
            var source = new Fake {
                Name = "meh"
            };
            var getter = Getter.GetOrCreate(typeof(Fake).GetProperty(nameof(Fake.Name), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly));

            Assert.AreEqual("meh", getter.GetValue(source));
            var genericGetter = (Getter <Fake, string>)getter;

            Assert.AreEqual("meh", genericGetter.GetValue(source));
        }
Exemple #7
0
        public void GetValue()
        {
            var source = new Fake {
                Name = "meh"
            };
            var getter = Getter.GetOrCreate(typeof(Fake).GetProperty("Name"));

            Assert.AreEqual("meh", getter.GetValue(source));
            var genericGetter = (Getter <Fake, string>)getter;

            Assert.AreEqual("meh", genericGetter.GetValue(source));
        }
Exemple #8
0
        public void GetValueGeneric()
        {
            var source = new Fake <int> {
                Value = 1
            };
            var getter = Getter.GetOrCreate(typeof(Fake <int>).GetProperty("Value"));

            Assert.AreEqual(1, getter.GetValue(source));
            var genericGetter = (Getter <Fake <int>, int>)getter;

            Assert.AreEqual(1, genericGetter.GetValue(source));
        }
Exemple #9
0
        public void ThrowsOnWriteOnly()
        {
            var propertyInfo = typeof(Fake).GetProperty("WriteOnly");

            Assert.NotNull(propertyInfo);
            //// ReSharper disable once ObjectCreationAsStatement
            var exception = Assert.Throws <ArgumentException>(() => Getter.GetOrCreate(propertyInfo));
            var expected  = "Property cannot be write only.\r\n" +
                            "The property Gu.Reactive.Tests.Helpers.Fake.WriteOnly does not have a getter.\r\n" +
                            "Parameter name: property";

            Assert.AreEqual(expected, exception.Message);
        }
Exemple #10
0
        public void Caching()
        {
            var getter1 = Getter.GetOrCreate(typeof(Fake).GetProperty(nameof(Fake.Name), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly));
            var getter2 = Getter.GetOrCreate(typeof(Fake).GetProperty(nameof(Fake.Name), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly));

            Assert.AreSame(getter1, getter2);

            var getter3 = Getter.GetOrCreate(typeof(Level).GetProperty(nameof(Level.Name), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly));
            var getter4 = Getter.GetOrCreate(typeof(Level).GetProperty(nameof(Level.Name), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly));

            Assert.AreSame(getter3, getter4);

            Assert.AreNotSame(getter1, getter3);
        }
Exemple #11
0
        public void Caching()
        {
            var getter1 = Getter.GetOrCreate(typeof(Fake).GetProperty("Name"));
            var getter2 = Getter.GetOrCreate(typeof(Fake).GetProperty("Name"));

            Assert.AreSame(getter1, getter2);

            var getter3 = Getter.GetOrCreate(typeof(Level).GetProperty("Name"));
            var getter4 = Getter.GetOrCreate(typeof(Level).GetProperty("Name"));

            Assert.AreSame(getter3, getter4);

            Assert.AreNotSame(getter1, getter3);
        }
Exemple #12
0
        public void GetValueGenerics()
        {
            var intFake = new Fake <int> {
                Value = 1
            };
            var intgetter = Getter.GetOrCreate(typeof(Fake <int>).GetProperty(nameof(Fake <int> .Value), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly));

            Assert.AreEqual(1, intgetter.GetValue(intFake));
            var genericintGetter = (Getter <Fake <int>, int>)intgetter;

            Assert.AreEqual(1, genericintGetter.GetValue(intFake));

            var doubleFake = new Fake <double> {
                Value = 1
            };
            var doublegetter = Getter.GetOrCreate(typeof(Fake <double>).GetProperty(nameof(Fake <double> .Value), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly));

            Assert.AreEqual(1, doublegetter.GetValue(doubleFake));
            var genericdoubleGetter = (Getter <Fake <double>, double>)doublegetter;

            Assert.AreEqual(1, genericdoubleGetter.GetValue(doubleFake));
        }
Exemple #13
0
        public void GetValueGenerics()
        {
            var intFake = new Fake <int> {
                Value = 1
            };
            var intgetter = Getter.GetOrCreate(typeof(Fake <int>).GetProperty("Value"));

            Assert.AreEqual(1, intgetter.GetValue(intFake));
            var genericintGetter = (Getter <Fake <int>, int>)intgetter;

            Assert.AreEqual(1, genericintGetter.GetValue(intFake));

            var doubleFake = new Fake <double> {
                Value = 1
            };
            var doublegetter = Getter.GetOrCreate(typeof(Fake <double>).GetProperty("Value"));

            Assert.AreEqual(1, doublegetter.GetValue(doubleFake));
            var genericdoubleGetter = (Getter <Fake <double>, double>)doublegetter;

            Assert.AreEqual(1, genericdoubleGetter.GetValue(doubleFake));
        }
Exemple #14
0
 public object GetterGetOrCreateProperty()
 {
     return(Getter.GetOrCreate(Property));
 }
Exemple #15
0
        public void FakeIsTrue()
        {
            var pathProperty = Getter.GetOrCreate(typeof(Fake).GetProperty(nameof(Fake.IsTrue), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly));

            Assert.IsInstanceOf <NotifyingGetter <Fake, bool> >(pathProperty);
        }
Exemple #16
0
        public void FakeIsTrue()
        {
            var pathProperty = Getter.GetOrCreate(typeof(Fake).GetProperty(nameof(Fake.IsTrue)));

            Assert.IsInstanceOf <NotifyingGetter <Fake, bool> >(pathProperty);
        }
Exemple #17
0
        public void FakeOfIntNext()
        {
            var pathProperty = Getter.GetOrCreate(typeof(Fake <int>).GetProperty(nameof(Fake <int> .Next), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly));

            Assert.IsInstanceOf <NotifyingGetter <Fake <int>, Level <int> > >(pathProperty);
        }
Exemple #18
0
        public void FakeOfIntNext()
        {
            var pathProperty = Getter.GetOrCreate(typeof(Fake <int>).GetProperty(nameof(Fake <int> .Next)));

            Assert.IsInstanceOf <NotifyingGetter <Fake <int>, Level <int> > >(pathProperty);
        }