示例#1
0
        public void FromLambdaExpressionPropertyExpressionFromClosureValue()
        {
            using (MockModelMetadataProvider provider = new MockModelMetadataProvider()) {
                // Arrange
                DummyContactModel model = new DummyContactModel {
                    FirstName = "Test"
                };
                ViewDataDictionary <object> viewData = new ViewDataDictionary <object>();
                provider.Setup(p => p.GetMetadataForProperty(It.IsAny <Func <object> >(), It.IsAny <Type>(), It.IsAny <string>()))
                .Callback <Func <object>, Type, string>((accessor, type, propertyName) =>
                {
                    Assert.AreEqual("Test", accessor());
                    Assert.AreEqual(typeof(DummyContactModel), type);
                    Assert.AreEqual("FirstName", propertyName);
                })
                .Returns(() => null)
                .Verifiable();

                // Act
                ModelMetadata.FromLambdaExpression <object, string>(m => model.FirstName, viewData);

                // Assert
                provider.Verify();
            }
        }
示例#2
0
        public void FromStringExpressionNonNullItemFoundOnPropertyOfItemInViewData()
        {
            using (MockModelMetadataProvider provider = new MockModelMetadataProvider()) {
                // Arrange
                DummyModelContainer model = new DummyModelContainer {
                    Model = new DummyContactModel()
                };
                ViewDataDictionary viewData = new ViewDataDictionary();
                viewData["Object"] = model;
                provider.Setup(p => p.GetMetadataForProperty(It.IsAny <Func <object> >(), It.IsAny <Type>(), It.IsAny <string>()))
                .Callback <Func <object>, Type, string>((accessor, type, propertyName) =>
                {
                    Assert.AreSame(model.Model, accessor());
                    Assert.AreEqual(typeof(DummyModelContainer), type);
                    Assert.AreEqual("Model", propertyName);
                })
                .Returns(() => null)
                .Verifiable();

                // Act
                ModelMetadata.FromStringExpression("Object.Model", viewData);

                // Assert
                provider.Verify();
            }
        }
示例#3
0
        public void FromStringExpressionItemNotFoundInViewData()
        {
            using (MockModelMetadataProvider provider = new MockModelMetadataProvider()) {
                // Arrange
                ViewDataDictionary viewData = new ViewDataDictionary();
                provider.Setup(p => p.GetMetadataForType(It.IsAny <Func <object> >(), It.IsAny <Type>()))
                .Callback <Func <object>, Type>((accessor, type) =>
                {
                    Assert.IsNull(accessor);
                    Assert.AreEqual(typeof(string), type);            // Don't know the type, must fall back on string
                })
                .Returns(() => null)
                .Verifiable();

                // Act
                ModelMetadata.FromStringExpression("UnknownObject", viewData);

                // Assert
                provider.Verify();
            }
        }
示例#4
0
        public void FromLambdaExpressionSetsContainerTypeToDerivedMostType()   // Dev10 Bug #868619
        {
            using (MockModelMetadataProvider provider = new MockModelMetadataProvider()) {
                // Arrange
                ViewDataDictionary <DerivedModel> viewData = new ViewDataDictionary <DerivedModel>();
                provider.Setup(p => p.GetMetadataForProperty(It.IsAny <Func <object> >(), It.IsAny <Type>(), It.IsAny <string>()))
                .Callback <Func <object>, Type, string>((accessor, type, propertyName) =>
                {
                    Assert.IsNull(accessor());
                    Assert.AreEqual(typeof(DerivedModel), type);
                    Assert.AreEqual("MyProperty", propertyName);
                })
                .Returns(() => null)
                .Verifiable();

                // Act
                ModelMetadata.FromLambdaExpression <DerivedModel, string>(m => m.MyProperty, viewData);

                // Assert
                provider.Verify();
            }
        }
示例#5
0
        public void FromLambdaExpressionNullReferenceExceptionsInPropertyExpressionPreserveAllExpressionInformation()
        {
            using (MockModelMetadataProvider provider = new MockModelMetadataProvider()) {
                // Arrange
                ViewDataDictionary <DummyContactModel> viewData = new ViewDataDictionary <DummyContactModel>();
                provider.Setup(p => p.GetMetadataForProperty(It.IsAny <Func <object> >(), It.IsAny <Type>(), It.IsAny <string>()))
                .Callback <Func <object>, Type, string>((accessor, type, propertyName) =>
                {
                    Assert.IsNull(accessor());
                    Assert.AreEqual(typeof(DummyContactModel), type);
                    Assert.AreEqual("FirstName", propertyName);
                })
                .Returns(() => null)
                .Verifiable();

                // Act
                ModelMetadata.FromLambdaExpression <DummyContactModel, string>(m => m.FirstName, viewData);

                // Assert
                provider.Verify();
            }
        }
示例#6
0
        public void FromLambdaExpressionSingleParameterClassIndexer()
        {
            using (MockModelMetadataProvider provider = new MockModelMetadataProvider()) {
                // Arrange
                DummyContactModel model = new DummyContactModel();
                ViewDataDictionary <DummyContactModel> viewData = new ViewDataDictionary <DummyContactModel>(model);
                provider.Setup(p => p.GetMetadataForType(It.IsAny <Func <object> >(), It.IsAny <Type>()))
                .Callback <Func <object>, Type>((accessor, type) =>
                {
                    Assert.AreEqual("Indexed into 42", accessor());
                    Assert.AreEqual(typeof(string), type);
                })
                .Returns(() => null)
                .Verifiable();

                // Act
                ModelMetadata.FromLambdaExpression <DummyContactModel, string>(m => m[42], viewData);

                // Assert
                provider.Verify();
            }
        }
示例#7
0
        public void FromLambdaExpressionFieldExpressionFromClosureValue()
        {
            using (MockModelMetadataProvider provider = new MockModelMetadataProvider()) {
                // Arrange
                DummyContactModel           model    = new DummyContactModel();
                ViewDataDictionary <object> viewData = new ViewDataDictionary <object>();
                provider.Setup(p => p.GetMetadataForType(It.IsAny <Func <object> >(), It.IsAny <Type>()))
                .Callback <Func <object>, Type>((accessor, type) =>
                {
                    Assert.AreSame(model, accessor());
                    Assert.AreEqual(typeof(DummyContactModel), type);
                })
                .Returns(() => null)
                .Verifiable();

                // Act
                ModelMetadata.FromLambdaExpression <object, DummyContactModel>(m => model, viewData);

                // Assert
                provider.Verify();
            }
        }
示例#8
0
        public void FromStringExpressionNonNullItemFoundAtRootOfViewData()
        {
            using (MockModelMetadataProvider provider = new MockModelMetadataProvider()) {
                // Arrange
                object             model    = new object();
                ViewDataDictionary viewData = new ViewDataDictionary();
                viewData["Object"] = model;
                provider.Setup(p => p.GetMetadataForType(It.IsAny <Func <object> >(), It.IsAny <Type>()))
                .Callback <Func <object>, Type>((accessor, type) =>
                {
                    Assert.AreSame(model, accessor());
                    Assert.AreEqual(typeof(object), type);
                })
                .Returns(() => null)
                .Verifiable();

                // Act
                ModelMetadata.FromStringExpression("Object", viewData);

                // Assert
                provider.Verify();
            }
        }
示例#9
0
        public void FromLambdaExpressionSingleDimensionArrayIndex()
        {
            using (MockModelMetadataProvider provider = new MockModelMetadataProvider()) {
                // Arrange
                DummyContactModel model = new DummyContactModel {
                    Array = new[] { 4, 8, 15, 16, 23, 42 }
                };
                ViewDataDictionary <DummyContactModel> viewData = new ViewDataDictionary <DummyContactModel>(model);
                provider.Setup(p => p.GetMetadataForType(It.IsAny <Func <object> >(), It.IsAny <Type>()))
                .Callback <Func <object>, Type>((accessor, type) =>
                {
                    Assert.AreEqual(16, accessor());
                    Assert.AreEqual(typeof(int), type);
                })
                .Returns(() => null)
                .Verifiable();

                // Act
                ModelMetadata.FromLambdaExpression <DummyContactModel, int>(m => m.Array[3], viewData);

                // Assert
                provider.Verify();
            }
        }
示例#10
0
        public void FromLambdaExpressionFieldExpressionFromParameter()
        {
            using (MockModelMetadataProvider provider = new MockModelMetadataProvider()) {
                // Arrange
                DummyContactModel model = new DummyContactModel {
                    IntField = 42
                };
                ViewDataDictionary <DummyContactModel> viewData = new ViewDataDictionary <DummyContactModel>(model);
                provider.Expect(p => p.GetMetadataForType(It.IsAny <Func <object> >(), It.IsAny <Type>()))
                .Callback <Func <object>, Type>((accessor, type) =>
                {
                    Assert.AreEqual(42, accessor());
                    Assert.AreEqual(typeof(int), type);
                })
                .Returns(() => null)
                .Verifiable();

                // Act
                ModelMetadata.FromLambdaExpression <DummyContactModel, int>(m => m.IntField, viewData);

                // Assert
                provider.Verify();
            }
        }