コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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);
        }