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)); }
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); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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); }
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); }
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); }
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)); }
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)); }
public object GetterGetOrCreateProperty() { return(Getter.GetOrCreate(Property)); }
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); }
public void FakeIsTrue() { var pathProperty = Getter.GetOrCreate(typeof(Fake).GetProperty(nameof(Fake.IsTrue))); Assert.IsInstanceOf <NotifyingGetter <Fake, bool> >(pathProperty); }
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); }
public void FakeOfIntNext() { var pathProperty = Getter.GetOrCreate(typeof(Fake <int>).GetProperty(nameof(Fake <int> .Next))); Assert.IsInstanceOf <NotifyingGetter <Fake <int>, Level <int> > >(pathProperty); }