Exemplo n.º 1
0
        public void Should_return_the_default_if_array_value_missing()
        {
            GetProperty <A, int> getter = SafeProperty <A> .GetGetProperty(x => x.Values[0]);

            var obj = new A();

            int value = getter.Get(obj);

            Assert.AreEqual(0, value);
        }
Exemplo n.º 2
0
        public void Should_return_nothing_if_object_is_null()
        {
            GetProperty <A, int> getter = SafeProperty <A> .GetGetProperty(x => x.TheBs[0].Value);

            A obj = null;

            int value = getter.Get(obj);

            Assert.AreEqual(0, value);
        }
Exemplo n.º 3
0
        public void Should_return_default_if_item_missing()
        {
            GetProperty <A, int> getter = SafeProperty <A> .GetGetProperty(x => x.TheBs[0].Value);

            A obj = null;

            int value = getter.Get(obj);

            Assert.AreEqual(0, value);
        }
Exemplo n.º 4
0
        public void Should_return_default_if_the_array_is_null()
        {
            GetProperty <A, int> getter = SafeProperty <A> .GetGetProperty(x => x.TheBs[0].Value);

            var obj = new A();

            int value = getter.Get(obj);

            Assert.AreEqual(0, value);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
        public void Should_return_default_value_for_null_object_on_value_type_references()
        {
            GetProperty <A, int> getValue = SafeProperty <A> .GetGetProperty(x => x.Id);

            const int expected = 0;

            A obj = null;

            int value = getValue.Get(obj);

            Assert.AreEqual(expected, value);
        }
Exemplo n.º 7
0
        public void Should_return_nothing_if_list_is_empty()
        {
            GetProperty <A, int> getter = SafeProperty <A> .GetGetProperty(x => x.TheBs[0].Value);

            var obj = new A
            {
                TheBs = new List <B>()
            };

            int value = getter.Get(obj);

            Assert.AreEqual(0, value);
        }
Exemplo n.º 8
0
        public void Should_go_way_deep()
        {
            Expression <Func <WayOuterClass, object> > accessor = o => o.Outer.Inner.Value;

            SafeProperty writer = accessor.CreateSafeProperty();

            const string expected = "Hello";

            var subject = new WayOuterClass();

            writer.Set(subject, 0, expected);

            subject.Outer.Inner.Value.ShouldEqual(expected);
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
0
        public void Should_create_a_backing_list_for_list_based_properties()
        {
            Expression <Func <OuterClass, object> > accessor = o => o.Inners[0].Value;

            SafeProperty writer = accessor.CreateSafeProperty();

            const string expected = "Hello";

            var subject = new OuterClass();

            writer.Set(subject, 0, expected);

            subject.Inners.ShouldNotBeNull();
            subject.Inners[0].Value.ShouldEqual(expected);
        }
Exemplo n.º 12
0
        public void Should_work_for_reference_types()
        {
            GetProperty <A, string> getValue = SafeProperty <A> .GetGetProperty(x => x.Value);

            const string expected = "123";

            var obj = new A
            {
                Value = expected
            };

            string value = getValue.Get(obj);

            Assert.AreEqual(expected, value);
        }
Exemplo n.º 13
0
        public void Should_return_the_value_for_value_types()
        {
            GetProperty <A, int> getValue = SafeProperty <A> .GetGetProperty(x => x.Id);

            const int expected = 123;

            var obj = new A
            {
                Id = expected
            };

            int value = getValue.Get(obj);

            Assert.AreEqual(expected, value);
        }
Exemplo n.º 14
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);
        }
Exemplo n.º 15
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);
        }
Exemplo n.º 16
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);
        }
Exemplo n.º 17
0
        public void Should_create_a_setter_for_a_simple_value()
        {
            Expression <Func <OuterClass, object> > accessor = o => o.Value;


            SafeProperty writer = accessor.CreateSafeProperty();


            var subject = new OuterClass();

            const string expected = "Hello";

            writer.Set(subject, 0, expected);

            subject.Value.ShouldEqual(expected);
        }
Exemplo n.º 18
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);
        }
Exemplo n.º 19
0
        public void Should_retrieve_a_property_value_when_present()
        {
            GetProperty <A, string> getValue = SafeProperty <A> .GetGetProperty(x => x.TheB.TheC.Value);

            var subject = new A
            {
                TheB = new B
                {
                    TheC = new C
                    {
                        Value = "123"
                    }
                }
            };

            Assert.AreEqual("123", getValue.Get(subject));
        }
Exemplo n.º 20
0
        public void oiuwer()
        {
            GetProperty <A, string> getter = SafeProperty <A> .GetGetProperty(x => x.TheB.Value);

            var subject = new A
            {
                TheB = new B
                {
                    Value = "123"
                }
            };
            string id = null;

            getter(subject, value => { id = value; });

            Assert.AreEqual("123", id);
        }
Exemplo n.º 21
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);
        }
Exemplo n.º 22
0
        public void Should_return_the_nested_value()
        {
            GetProperty <A, string> getValue = SafeProperty <A> .GetGetProperty(x => x.TheB.Value);

            const string expected = "123";

            var obj = new A
            {
                TheB = new B
                {
                    Value = expected
                }
            };

            string value = getValue.Get(obj);

            Assert.AreEqual(expected, value);
        }
Exemplo n.º 23
0
        public void Should_return_nothing_if_list_entry_is_null()
        {
            GetProperty <A, int> getter = SafeProperty <A> .GetGetProperty(x => x.TheBs[0].Value);

            var obj = new A
            {
                TheBs = new List <B>
                {
                    null
                }
            };

            Assert.AreEqual(1, obj.TheBs.Count);

            int value = getter.Get(obj);

            Assert.AreEqual(0, value);
        }
Exemplo n.º 24
0
        public void Should_return_the_value_if_it_exists()
        {
            GetProperty <A, int> getter = SafeProperty <A> .GetGetProperty(x => x.TheBs[0].Value);

            var obj = new A
            {
                TheBs = new List <B>
                {
                    new B
                    {
                        Value = 27
                    }
                }
            };

            int value = getter.Get(obj);

            Assert.AreEqual(27, value);
        }
Exemplo n.º 25
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);
        }
Exemplo n.º 26
0
        private object[] InitializeIndexerProperty(object context, EvaluationContext evalContext)
        {
            object[] indices = ResolveArguments(evalContext);

            if (indexer == null)
            {
                lock (this)
                {
                    if (indexer == null)
                    {
                        Type   contextType     = context.GetType();
                        Type[] argTypes        = ReflectionUtils.GetTypes(indices);
                        var    indexerProperty = GetIndexerPropertyInfo(contextType, argTypes);

                        indexer = new SafeProperty(indexerProperty);
                    }
                }
            }

            return(indices);
        }
Exemplo n.º 27
0
 public SafeGetNestedPropertyRunner()
 {
     _accessor = SafeProperty <A> .GetGetProperty(x => x.TheB.TheC.Value);
 }
Exemplo n.º 28
0
 public void Once()
 {
     _getter = SafeProperty <A> .GetGetProperty(x => x.Id);
 }
Exemplo n.º 29
0
 public void Should_handle_method_expressions_with_exception()
 {
     Assert.Throws <ArgumentException>(() => SafeProperty <A> .GetGetProperty(x => GetString(x)));
 }
Exemplo n.º 30
0
 public void Should_handle_bad_expressions()
 {
     Assert.Throws <ArgumentException>(() => SafeProperty <A> .GetGetProperty(x => true));
 }