Beispiel #1
0
        public void BindsIntegersWithPrecisionLoss()
        {
            IBinder binder = new StrictModelBinder();
            var     result = binder.Bind(2.5678, typeof(int));

            Assert.Equal(3, result);

            result = binder.Bind(2.123, typeof(int));

            Assert.Equal(2, result);
        }
Beispiel #2
0
        public void BindsDoublesWithoutPrecisionLoss()
        {
            const double Expected = 2.5678;
            IBinder      binder   = new StrictModelBinder();
            var          result   = binder.Bind(Expected, typeof(double));

            Assert.Equal(Expected, result);

            result = binder.Bind(2, typeof(double));

            Assert.Equal(2.0, result);
        }
Beispiel #3
0
        public void BindsEnums()
        {
            IBinder binder = new StrictModelBinder();
            var     result = binder.Bind(2, typeof(TestEnum));

            Assert.Equal(TestEnum.C, result);
        }
Beispiel #4
0
        public void NullToValueTypeTheory(Type conversionType)
        {
            IBinder binder = new StrictModelBinder();

            //Throw Exception if we try and conver null to a ValueType
            Assert.Throws <ModelBindingException>(() => binder.Bind(null, conversionType));
        }
Beispiel #5
0
        public void BindsComplexObjects()
        {
            IBinder binder = new StrictModelBinder();
            var     obj    = new Dictionary <string, object>
            {
                { "anEnum", (int)TestEnum.C },
                { "aString", "SomeValue" },
                { "aBool", true },
                { "anInteger", 2.4 },
                { "aDouble", 2.6 }
            };

            var result = (TestObject)binder.Bind(obj, typeof(TestObject));

            Assert.Equal(TestEnum.C, result.AnEnum);
            Assert.Equal(obj["aString"], result.AString);
            Assert.Equal(obj["aBool"], result.ABool);
            Assert.Equal(2, result.AnInteger);
            Assert.Equal(obj["aDouble"], result.ADouble);
        }
Beispiel #6
0
        public void BindListOfNumbersToDoubleArray()
        {
            var doubleArrayType = typeof(double[]);

            IBinder binder = new StrictModelBinder();
            var     obj    = new List <object> {
                10, 20, 1.23
            };
            var result = binder.Bind(obj, doubleArrayType);

            Assert.NotNull(result);
            Assert.Equal(doubleArrayType, result.GetType());

            var arr = (double[])result;

            Assert.Equal(obj.Count, arr.Length);

            for (int i = 0; i < obj.Count; i++)
            {
                var expected = Convert.ToDouble(obj[i]);
                var actual   = arr[i];
                Assert.Equal(expected, actual);
            }
        }