コード例 #1
0
        public void CanBind_List()
        {
            var context = Substitute.For<IModelBinderContext>();
            context.ModelType.Returns(typeof(IEnumerable<int>));
            var binder = new ArrayModelBinder();

            Assert.False(binder.CanBind(context));
        }
コード例 #2
0
        public void CanBind_Primitive()
        {
            var context = Substitute.For<IModelBinderContext>();
            context.ModelType.Returns(typeof(int));
            var binder = new ArrayModelBinder();

            Assert.False(binder.CanBind(context));
        }
コード例 #3
0
        public void CanBind_IntArray()
        {
            var context = Substitute.For<IModelBinderContext>();
            context.ModelType.Returns(typeof (int[]));
            var binder = new ArrayModelBinder();

            Assert.True(binder.CanBind(context));
        }
コード例 #4
0
 public void Bind_IntArray_Gap()
 {
     var provider = new ValueProvider() { Parameters = { new Parameter("ages[2]", "3"), new Parameter("ages[0]", "20") } };
     var context = new ModelBinderContext(typeof(int[]), "ages", "", provider);
     context.RootBinder = new PrimitiveModelBinder();
     
     var binder = new ArrayModelBinder();
     
     Assert.Throws<ModelBindingException>(() => binder.Bind(context));
 }
コード例 #5
0
        public void Bind_IntArray_Index()
        {
            var provider = new ValueProvider() { Parameters = { new Parameter("ages[1]", "3"), new Parameter("ages[0]", "20") } };
            var context = new ModelBinderContext(typeof(int[]), "ages", "", provider);
            context.RootBinder = new PrimitiveModelBinder();
            var binder = new ArrayModelBinder();
            var actual = (int[])binder.Bind(context);

            Assert.NotNull(actual);
            Assert.Equal(3, actual[1]);
            Assert.Equal(20, actual[0]);
        }
コード例 #6
0
        public void Bind_ViewModelArray_Index()
        {
            var provider = new ValueProvider
                {
                    Parameters =
                        {
                            new Parameter("user[1].FirstName", "jonas"),
                            new Parameter("user[0].FirstName", "Arne"),
                            new Parameter("user[0].Age", "32"),
                            new Parameter("user[1].Age", "23")
                        }
                };
            var mapper = new ModelMapper();
            mapper.Clear();
            mapper.AddBinder(new PrimitiveModelBinder());
            mapper.AddBinder(new ClassBinder());
            var context = new ModelBinderContext(typeof(UserViewModel[]), "user", "", provider) {RootBinder = mapper};

            var binder = new ArrayModelBinder();
            var actual = (UserViewModel[])binder.Bind(context);

            Assert.NotNull(actual);
            Assert.Equal("jonas", actual[1].FirstName);
            Assert.Equal("Arne", actual[0].FirstName);
            Assert.Equal(23, actual[1].Age);
            Assert.Equal(32, actual[0].Age);
        }
コード例 #7
0
        public void Bind_ViewModelArray_Gap()
        {
            var provider = new ValueProvider
            {
                Parameters =
                        {
                            new Parameter("user[1].FirstName", "jonas"),
                            new Parameter("user[3].FirstName", "Arne"),
                            new Parameter("user[0].Age", "32"),
                            new Parameter("user[1].Age", "23")
                        }
            };
            var mapper = new ModelMapper();
            mapper.Clear();
            mapper.AddBinder(new PrimitiveModelBinder());
            mapper.AddBinder(new ClassBinder());
            var context = new ModelBinderContext(typeof(UserViewModel[]), "user", "", provider) { RootBinder = mapper };

            var binder = new ArrayModelBinder();

            Assert.Throws<ModelBindingException>(() => binder.Bind(context));
        }