public void Should_set_first_level_property() { Expression <Func <Outer, string> > expression = o => o.Value; SafeProperty setter = SafeProperty.Create(expression); var outer = new Outer(); setter.Set(outer, 0, "Hello"); Assert.AreEqual("Hello", outer.Value); }
public void Should_not_throw_a_null_reference_exception() { Expression <Func <OuterClass, object> > accessor = o => o.Inner.Value; var writer = SafeProperty.Create(accessor); const string expected = "Hello"; var subject = new OuterClass(); writer.Set(subject, 0, expected); Assert.AreEqual(expected, subject.Inner.Value); }
public void Should_go_way_deep() { Expression <Func <WayOuterClass, object> > accessor = o => o.Outer.Inner.Value; var writer = SafeProperty.Create(accessor); const string expected = "Hello"; var subject = new WayOuterClass(); writer.Set(subject, 0, expected); Assert.AreEqual(expected, subject.Outer.Inner.Value); }
public void Should_set_third_level_value_with_one_array_indexer() { Expression <Func <Outer, string> > expression = o => o.Middles[0].Inner.Value; SafeProperty setter = SafeProperty.Create(expression); var outer = new Outer(); setter.Set(outer, 0, "Hello"); Assert.IsNotNull(outer.Middles); Assert.AreEqual(1, outer.Middles.Count); Assert.IsNotNull(outer.Middles[0].Inner); Assert.AreEqual("Hello", outer.Middles[0].Inner.Value); }
public void Should_create_a_list_for_an_indexed_property_value() { Expression <Func <WayOuterClass, int, object> > accessor = (o, index) => o.Outer.Inners[index].Value; var writer = SafeProperty.Create(accessor); const string expected = "Hello"; var subject = new WayOuterClass(); writer.Set(subject, 0, expected); Assert.IsNotNull(subject.Outer.Inners); Assert.AreEqual(expected, subject.Outer.Inners[0].Value); }
public void Should_create_a_backing_list_for_list_based_properties_way_deep() { Expression <Func <WayOuterClass, object> > accessor = o => o.Outer.Inners[0].Value; var writer = SafeProperty.Create(accessor); const string expected = "Hello"; var subject = new WayOuterClass(); writer.Set(subject, 0, expected); Assert.IsNotNull(subject.Outer.Inners); Assert.AreEqual(expected, subject.Outer.Inners[0].Value); }
public void Should_create_a_setter_for_a_simple_value() { Expression <Func <OuterClass, object> > accessor = o => o.Value; var writer = SafeProperty.Create(accessor); var subject = new OuterClass(); const string expected = "Hello"; writer.Set(subject, 0, expected); Assert.AreEqual(expected, subject.Value); }
public void Should_work_for_indexed_properties_way_deep() { Expression <Func <WayOuterClass, int, object> > accessor = (o, index) => o.Outers[index].Inner.Value; var writer = SafeProperty.Create(accessor); const string expected = "Hello"; var subject = new WayOuterClass(); writer.Set(subject, 1, expected); Assert.IsNotNull(subject.Outers); Assert.AreEqual(2, subject.Outers.Count); Assert.IsNotNull(subject.Outers[1].Inner); Assert.AreEqual(expected, subject.Outers[1].Inner.Value); }
public void Should_allow_property_to_already_be_set() { Expression <Func <OuterClass, object> > accessor = o => o.Inner.Value; var writer = SafeProperty.Create(accessor); const string expected = "Hello"; var subject = new OuterClass(); subject.Inner = new InnerClass { OtherValue = "Hi" }; writer.Set(subject, 0, expected); Assert.AreEqual(expected, subject.Inner.Value); Assert.AreEqual("Hi", subject.Inner.OtherValue); }