Exemple #1
0
        public void TestGetValueIfDictionaryHasDecimalValues()
        {
            var dict = new Dictionary <string, decimal>
            {
                ["Key1"] = 27.67m,
                ["Key2"] = 64m,
            };

            var provider = new DictionaryValueProvider <decimal>();

            Assert.AreEqual(dict["Key1"], provider.GetValue("Key1", dict));
            Assert.AreEqual(dict["Key2"], provider.GetValue("Key2", dict));
        }
        public void GetValue_ThrowsIfKeyIsNull()
        {
            // Arrange
            DictionaryValueProvider <object> valueProvider = new DictionaryValueProvider <object>(_backingStore, null);

            // Act & assert
            Assert.ThrowsArgumentNull(
                delegate { valueProvider.GetValue(null); }, "key");
        }
        public void GetValue_ReturnsNullIfKeyNotFound() {
            // Arrange
            DictionaryValueProvider<object> valueProvider = new DictionaryValueProvider<object>(_backingStore, null);

            // Act
            ValueProviderResult vpResult = valueProvider.GetValue("nineteen.eighty");

            // Assert
            Assert.IsNull(vpResult);
        }
        public void GetValue_ReturnsNullIfKeyNotFound()
        {
            // Arrange
            DictionaryValueProvider <object> valueProvider = new DictionaryValueProvider <object>(_backingStore, null);

            // Act
            ValueProviderResult vpResult = valueProvider.GetValue("nineteen.eighty");

            // Assert
            Assert.Null(vpResult);
        }
        public void GetValue() {
            // Arrange
            CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
            DictionaryValueProvider<object> valueProvider = new DictionaryValueProvider<object>(_backingStore, culture);

            // Act
            ValueProviderResult vpResult = valueProvider.GetValue("nineteen.eighty.four");

            // Assert
            Assert.IsNotNull(vpResult);
            Assert.AreEqual(new DateTime(1984, 1, 1), vpResult.RawValue);
            Assert.AreEqual("01/01/1984 00:00:00", vpResult.AttemptedValue);
            Assert.AreEqual(culture, vpResult.Culture);
        }
        public void GetValue()
        {
            // Arrange
            CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
            DictionaryValueProvider <object> valueProvider = new DictionaryValueProvider <object>(_backingStore, culture);

            // Act
            ValueProviderResult vpResult = valueProvider.GetValue("nineteen.eighty.four");

            // Assert
            Assert.NotNull(vpResult);
            Assert.Equal(new DateTime(1984, 1, 1), vpResult.RawValue);
            Assert.Equal("01/01/1984 00:00:00", vpResult.AttemptedValue);
            Assert.Equal(culture, vpResult.Culture);
        }
Exemple #7
0
        public void TestGetValue()
        {
            var dict = new Dictionary <string, object>
            {
                ["StrParam"]  = "value1",
                ["IntParam"]  = 5,
                ["BoolParam"] = true,
                ["GuidParam"] = Guid.NewGuid(),
            };

            var provider = new DictionaryValueProvider <object>();

            Assert.AreEqual(dict["StrParam"], provider.GetValue("StrParam", dict));
            Assert.AreEqual(dict["IntParam"], provider.GetValue("IntParam", dict));
            Assert.AreEqual(dict["BoolParam"], provider.GetValue("BoolParam", dict));
            Assert.AreEqual(dict["GuidParam"], provider.GetValue("GuidParam", dict));

            ExceptionAssert.Throws <ArgumentException>(() => provider.GetValue(null, dict));
            ExceptionAssert.Throws <KeyNotFoundException>(() => provider.GetValue(" StrParam ", dict), "Key \" StrParam \" was not found in dictionary");
            ExceptionAssert.Throws <KeyNotFoundException>(() => provider.GetValue("strParam", dict), "Key \"strParam\" was not found in dictionary");
            ExceptionAssert.Throws <KeyNotFoundException>(() => provider.GetValue("BadParam", dict), "Key \"BadParam\" was not found in dictionary");
        }
        //=== Helpers

        /// <summary> Looks for value provider result. </summary>
        private object FindValue(ModelBindingContext bindingContext)
        {
            ValueProviderCollection vpc = bindingContext.ValueProvider as ValueProviderCollection;

            foreach (object vp in vpc)
            {
                if (vp is FormValueProvider)
                {
                    FormValueProvider fvp = (FormValueProvider)vp;

                    ValueProviderResult vpr = fvp.GetValue(bindingContext.ModelName);

                    if (vpr != null)
                    {
                        return(vpr);
                    }
                }
                else if (vp is DictionaryValueProvider <object> )
                {
                    DictionaryValueProvider <object> dvp = (DictionaryValueProvider <object>)vp;

                    ValueProviderResult vpr = dvp.GetValue(bindingContext.ModelName);

                    if (vpr != null)
                    {
                        return(vpr);
                    }
                    else if (dvp.ContainsPrefix(bindingContext.ModelName))
                    {
                        return(vp);
                    }
                }
            }

            return(null);
        }
 public bool GetValue(string key, Func <object, bool> matchingValueAction)
 {
     return(_provider.GetValue(key, matchingValueAction));
 }
        public void GetValue_ThrowsIfKeyIsNull() {
            // Arrange
            DictionaryValueProvider<object> valueProvider = new DictionaryValueProvider<object>(_backingStore, null);

            // Act & assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                    valueProvider.GetValue(null);
                }, "key");
        }
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            //DictionaryValueProvider<object> provider = this.GetDictionaryValueProvider(bindingContext);
            //ValueProviderResult value = provider.GetValue(bindingContext.ModelName);

            object provider = this.FindValue(bindingContext);

            // Only proceed if value is indeed stringified
            if (provider == null)
            {
                return(null);
            }

            if (provider is ValueProviderResult)
            {
                ValueProviderResult result = (ValueProviderResult)provider;

                return(this.ParseFrom(bindingContext, result));
            }
            else if (provider is DictionaryValueProvider <object> )
            {
                // Only JObject from here
                if (bindingContext.ModelType != typeof(JObject))
                {
                    if (this.ModelType != null)
                    {
                        if (bindingContext.ModelType != this.ModelType)
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        return(null);
                    }
                }

                DictionaryValueProvider <object> dvp = (DictionaryValueProvider <object>)provider;

                IDictionary <string, string> keys = dvp.GetKeysFromPrefix(bindingContext.ModelName);
                if (keys.Count == 0)
                {
                    return(null);
                }

                // TODO: build recursively (now supports only single layer
                JObject jObj = new JObject();

                foreach (KeyValuePair <string, string> pair in keys)
                {
                    //string name = pair.Key;
                    //string value = pair.Value;

                    //ValueProviderResult vpr1 = dvp.GetValue(name);
                    ValueProviderResult value = dvp.GetValue(pair.Value);

                    if (value != null)
                    {
                        //    string attempted = value.AttemptedValue;
                        //    object rawValue = value.RawValue;

                        jObj[pair.Key] = value.RawValue != null?JToken.FromObject(value.RawValue) : null;
                    }

                    //if (dvp.ContainsPrefix(value))
                    //{
                    //    IDictionary<string, string> keys2 = dvp.GetKeysFromPrefix(value);
                    //}
                }

                return(jObj);
            }

            return(null);
        }