public void IsValidPropertyReturnsTrueIfNoErrors()
        {
            // Arrange
            ModelStateDictionary dictionary = new ModelStateDictionary()
            {
                {
                    "foo",
                    new ModelState()
                    {
                        Value = HtmlHelperTest.GetValueProviderResult("bar", "bar")
                    }
                },
                {
                    "baz",
                    new ModelState()
                    {
                        Value = HtmlHelperTest.GetValueProviderResult("quux", "bar")
                    }
                }
            };

            // Act
            bool isValid = dictionary.IsValid;

            // Assert
            Assert.True(isValid);
        }
        public void IsValidPropertyReturnsFalseIfErrors()
        {
            // Arrange
            ModelState errorState = new ModelState()
            {
                Value = HtmlHelperTest.GetValueProviderResult("quux", "quux")
            };

            errorState.Errors.Add("some error");
            ModelStateDictionary dictionary = new ModelStateDictionary()
            {
                {
                    "foo",
                    new ModelState()
                    {
                        Value = HtmlHelperTest.GetValueProviderResult("bar", "bar")
                    }
                },
                { "baz", errorState }
            };

            // Act
            bool isValid = dictionary.IsValid;

            // Assert
            Assert.False(isValid);
        }
        public void SetAttemptedValueCreatesModelStateIfNotPresent()
        {
            // Arrange
            ModelStateDictionary dictionary = new ModelStateDictionary();

            // Act
            dictionary.SetModelValue("some key", HtmlHelperTest.GetValueProviderResult("some value", "some value"));

            // Assert
            Assert.Single(dictionary);
            ModelState modelState = dictionary["some key"];

            Assert.Empty(modelState.Errors);
            Assert.Equal("some value", modelState.Value.ConvertTo(typeof(string)));
        }
        public void ConstructorWithDictionaryParameter()
        {
            // Arrange
            ModelStateDictionary oldDictionary = new ModelStateDictionary()
            {
                { "foo", new ModelState()
                  {
                      Value = HtmlHelperTest.GetValueProviderResult("bar", "bar")
                  } }
            };

            // Act
            ModelStateDictionary newDictionary = new ModelStateDictionary(oldDictionary);

            // Assert
            Assert.Single(newDictionary);
            Assert.Equal("bar", newDictionary["foo"].Value.ConvertTo(typeof(string)));
        }
        public void SetAttemptedValueUsesExistingModelStateIfPresent()
        {
            // Arrange
            ModelStateDictionary dictionary = new ModelStateDictionary();

            dictionary.AddModelError("some key", "some error");
            Exception ex = new Exception();

            // Act
            dictionary.SetModelValue("some key", HtmlHelperTest.GetValueProviderResult("some value", "some value"));

            // Assert
            Assert.Single(dictionary);
            ModelState modelState = dictionary["some key"];

            Assert.Single(modelState.Errors);
            Assert.Equal("some error", modelState.Errors[0].ErrorMessage);
            Assert.Equal("some value", modelState.Value.ConvertTo(typeof(string)));
        }