public void TestSafePropertyFromFirstWithList()
        {
            List <SomeDemoObject> theList = new List <SomeDemoObject>();
            SomeDemoObject        obj     = new SomeDemoObject()
            {
                Integer = 3,
                String  = "ptv",
                Type    = "demo"
            };

            theList.Add(obj);
            theList.Add(new SomeDemoObject()
            {
                Integer = 4,
                String  = "vrk",
                Type    = "test"
            });

            int getValue = theList.SafePropertyFromFirst(x =>
            {
                return(x.Integer);
            });

            getValue.Should().Be(obj.Integer);
        }
        public void GetPropertyValueObject()
        {
            int    number   = 7;
            string txt      = "vrk ptv";
            string typeText = "Test";

            SomeDemoObject obj = new SomeDemoObject()
            {
                Integer = number,
                String  = txt,
                Type    = typeText
            };

            obj.SomeList.Add("ptv");
            obj.SomeList.Add("vrk");

            obj.GetPropertyObjectValue("Integer").Should().Be(number);
            obj.GetPropertyObjectValue("String").Should().Be(txt);
            obj.GetPropertyObjectValue("Type").Should().Be(typeText);

            IList <string> theList = obj.GetPropertyObjectValue("SomeList") as IList <string>;

            theList.Should().NotBeNullOrEmpty();
            theList.Count.Should().Be(2);
        }
        public void SetPropertyValueNullPropertyNameShouldThrow()
        {
            // if property name is null the code should handle it and throw and not silently fail/do nothing
            SomeDemoObject obj = new SomeDemoObject();
            Action         act = () => obj.SetPropertyValue(null, "something");

            act.ShouldThrowExactly <ArgumentNullException>("Property name is null.");
        }
        public void SetPropertyValueNullInstanceShouldThrow()
        {
            // if instance is null and calling the extension method causes currently TargetInvocationException
            // our implementation should throw argumentnullexception or invalidoperationexception because called on null instead of instance
            SomeDemoObject obj = null;
            Action         act = () => obj.SetPropertyValue("Type", "Test");

            act.ShouldThrowExactly <ArgumentException>("Calling SetPropertyValue on null instance.");
        }
        public void SetPropertyValueNotFoundPropertyNameShouldThrow()
        {
            // if invalid property name is given it should throw and not just silently skip the setting of the value
            // if silently just skipping the caller doesn't know that the value is not set
            SomeDemoObject obj = new SomeDemoObject();
            Action         act = () => obj.SetPropertyValue("NotFound", "something");

            act.ShouldThrowExactly <ArgumentException>("Invalid property name.");
        }
        public void SetPropertyValueIntUsingExpression()
        {
            int valueToSet = 6;

            SomeDemoObject obj = new SomeDemoObject();

            obj.SetPropertyValue(x => x.Integer, valueToSet);

            obj.Integer.Should().Be(valueToSet);
        }
        public void SetPropertyValueStringUsingExpression()
        {
            string valueToSet = "ptv";

            SomeDemoObject obj = new SomeDemoObject();

            obj.SetPropertyValue(x => x.String, valueToSet);

            obj.String.Should().Be(valueToSet);
        }
        public void TestSafeCallWithNullInstance()
        {
            SomeDemoObject obj = null;

            Action act = () => obj.SafeCall(sdo => {
                sdo.Integer = 100;
                sdo.String  = "Hello world";
                sdo.Type    = "Demo";
            });

            act.ShouldNotThrow();
        }
        public void PropertyNameIsNullOrEmptyOrWhitespace(string propertyName)
        {
            // if property name is null or empty the method should return the same object
            // currently works only when property name is empty string
            // should it work like null, "", would always return the same object

            SomeDemoObject obj = new SomeDemoObject()
            {
                Integer = 100, String = "somestring"
            };

            obj.GetItemValue(propertyName).Should().BeSameAs(obj);
        }
        public void TestSafeCallReturnWithNullInstance()
        {
            SomeDemoObject obj    = null;
            string         retval = "vrk"; // just initializing to something, we are expecting this to be null after the safecall

            // implementation returns default(T) when called instance is null, so retval should be null
            retval = obj.SafeCall(sdo =>
            {
                return(sdo.ToString());
            });

            retval.Should().BeNull();
        }
Beispiel #11
0
        public void TestWithObject()
        {
            // the formatter expects the value to be string
            TrimSpacesFormatter formatter = new TrimSpacesFormatter();
            SomeDemoObject      obj       = new SomeDemoObject()
            {
                Integer = 100,
                String  = "Some text",
                Type    = "Demo"
            };

            Action act = () => formatter.Format(obj);

            act.ShouldThrowExactly <PtvArgumentException>($"Expected value is string! Value {obj.ToString()} of type SomeDemoObject is not valid.");
        }
        public void TryGetIDictionaryValueWithNullThrows()
        {
            IDictionary <string, SomeDemoObject> dict = new Dictionary <string, SomeDemoObject>();

            SomeDemoObject obj = new SomeDemoObject()
            {
                Integer = 65
            };

            dict.Add("demo", obj);

            Action act = () => dict.TryGet(null);

            act.ShouldThrowExactly <ArgumentNullException>("Key cannot be null.");
        }
        public void TryGetIDictionaryValueByKeyNotFound(string keyValue)
        {
            IDictionary <string, SomeDemoObject> dict = new Dictionary <string, SomeDemoObject>();

            SomeDemoObject obj = new SomeDemoObject()
            {
                Integer = 65
            };

            dict.Add("demo", obj);

            var foundObj = dict.TryGet(keyValue);

            foundObj.Should().BeNull();
        }
        public void AddAndReturn()
        {
            List <SomeDemoObject> theList = new List <SomeDemoObject>();

            SomeDemoObject obj = new SomeDemoObject()
            {
                Integer = 8,
                String  = "text",
                Type    = "demo"
            };

            var returnedObj = theList.AddAndReturn(obj);

            returnedObj.Should().BeSameAs(obj);
        }
Beispiel #15
0
        public void TestWithObject()
        {
            // the formatter expects the value to be string
            MaxLengthFormatter formatter = new MaxLengthFormatter();
            SomeDemoObject     obj       = new SomeDemoObject()
            {
                Integer = 100,
                String  = "Some text",
                Type    = "Demo"
            };

            Action act = () => formatter.Format(obj, "Demo", new MockResolveManager());

            act.ShouldThrowExactly <PtvArgumentException>($"Expected value is string! Value {obj.ToString()} of type Demo is not valid.");
        }
        public void SetPropertyValueUsingInvalidExpressionShouldThrow()
        {
            string valueToSet = "ptv";

            SomeDemoObject obj = new SomeDemoObject();

            // invalid "memberacces" expression
            System.Linq.Expressions.Expression <Func <SomeDemoObject, string> > f = (sdo) => sdo.ToString();

            Action act = () => obj.SetPropertyValue(f, valueToSet);

            // currently if invalid expression is given the value is not set which can lead to cases that intrdocude hard to find bugs to our app
            // if implementation is changed to throw, change the expected exception type
            act.ShouldThrow <Exception>("Maybe this should throw because the value is not set. Currently just silently does nothing because of invalid memberexpression.");
        }
        public void TryGetDictionaryValueByKey()
        {
            Dictionary <string, SomeDemoObject> dict = new Dictionary <string, SomeDemoObject>();

            SomeDemoObject obj = new SomeDemoObject()
            {
                Integer = 65
            };

            dict.Add("demo", obj);

            var foundObj = dict.TryGet("demo");

            foundObj.Should().NotBeNull();
            foundObj.Should().BeSameAs(obj);
        }
        public void TestSafeCallReturnCalculation()
        {
            SomeDemoObject obj = new SomeDemoObject()
            {
                Integer = 9,
                String  = "vrk",
                Type    = "demo"
            };

            int calcResult = obj.SafeCall(sdo =>
            {
                return(sdo.Integer + 1);
            });

            calcResult.Should().Be(10);
        }
        public void TestSafeCall()
        {
            SomeDemoObject obj = new SomeDemoObject()
            {
                Integer = 9,
                String  = "vrk",
                Type    = "demo"
            };

            obj.SafeCall(sdo =>
            {
                sdo.Integer = 100;
            });

            obj.Integer.Should().Be(100);
        }
        public void GetPropertyValueObjectPropertyNameIsNullShouldThrow()
        {
            SomeDemoObject obj = new SomeDemoObject()
            {
                Integer = 8,
                String  = "vrk ptv",
                Type    = "Demo"
            };

            obj.SomeList.Add("ptv");
            obj.SomeList.Add("vrk");

            // extension should throw an ArgumentNullException
            Action act = () => obj.GetPropertyObjectValue(null);

            act.ShouldThrowExactly <ArgumentNullException>("Property name is null.");
        }
        public void SetPropertyValue()
        {
            // if invalid property name is given it should throw and not just silently skip the setting of the value
            // if silently just skipping the caller doesn't know that the value is not set
            SomeDemoObject obj = new SomeDemoObject()
            {
                Integer = 100,
                String  = "some text",
                Type    = "Demo"
            };

            obj.SetPropertyValue("Integer", 65);
            obj.SetPropertyValue("Type", "Test");

            obj.Integer.Should().Be(65);
            obj.Type.Should().Be("Test");
        }
        public void TestSafeCallReturn()
        {
            SomeDemoObject obj = new SomeDemoObject()
            {
                Integer = 9,
                String  = "vrk",
                Type    = "demo"
            };

            string str = obj.ToString();

            string strB = obj.SafeCall(sdo =>
            {
                return(sdo.ToString());
            });

            strB.Should().Be(str);
        }
        public void GetPropertyValueObjectPropertyMissingShouldThrow()
        {
            SomeDemoObject obj = new SomeDemoObject()
            {
                Integer = 8,
                String  = "vrk ptv",
                Type    = "Demo"
            };

            obj.SomeList.Add("ptv");
            obj.SomeList.Add("vrk");

            // extension should throw an argumenexception when a property is not found
            // why? because otherwise the caller doesn't know that the property is missing from the object
            Action act = () => obj.GetPropertyValue <SomeDemoObject, int>("NotFoundPropertyName");

            act.ShouldThrowExactly <ArgumentException>("Property not found from object.");
        }
        public void GetPropertyValueObjectInvalidPropertyNameShouldThrow(string propertyName)
        {
            SomeDemoObject obj = new SomeDemoObject()
            {
                Integer = 8,
                String  = "vrk ptv",
                Type    = "Demo"
            };

            obj.SomeList.Add("ptv");
            obj.SomeList.Add("vrk");

            // extension should throw an argumenexception when a known invalid propertyname is passed
            // why? because otherwise the caller doesn't know that the property is missing from the object
            Action act = () => obj.GetPropertyObjectValue(propertyName);

            act.ShouldThrowExactly <ArgumentException>("Known invalid property name.");
        }