public void FromLambdaExpressionPropertyExpressionFromClosureValue() { using (MockModelMetadataProvider provider = new MockModelMetadataProvider()) { // Arrange DummyContactModel model = new DummyContactModel { FirstName = "Test" }; ViewDataDictionary <object> viewData = new ViewDataDictionary <object>(); provider.Expect(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(); } }
public void FromStringExpressionNonNullItemFoundOnPropertyOfItemInViewData() { using (MockModelMetadataProvider provider = new MockModelMetadataProvider()) { // Arrange DummyModelContainer model = new DummyModelContainer { Model = new DummyContactModel() }; ViewDataDictionary viewData = new ViewDataDictionary(); viewData["Object"] = model; provider.Expect(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(); } }
public void FromStringExpressionItemNotFoundInViewData() { using (MockModelMetadataProvider provider = new MockModelMetadataProvider()) { // Arrange ViewDataDictionary viewData = new ViewDataDictionary(); provider.Expect(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(); } }
public void FromLambdaExpressionNullReferenceExceptionsInPropertyExpressionPreserveAllExpressionInformation() { using (MockModelMetadataProvider provider = new MockModelMetadataProvider()) { // Arrange ViewDataDictionary <DummyContactModel> viewData = new ViewDataDictionary <DummyContactModel>(); provider.Expect(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(); } }
public void FromLambdaExpressionSingleParameterClassIndexer() { using (MockModelMetadataProvider provider = new MockModelMetadataProvider()) { // Arrange DummyContactModel model = new DummyContactModel(); 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("Indexed into 42", accessor()); Assert.AreEqual(typeof(string), type); }) .Returns(() => null) .Verifiable(); // Act ModelMetadata.FromLambdaExpression <DummyContactModel, string>(m => m[42], viewData); // Assert provider.Verify(); } }
public void FromLambdaExpressionFieldExpressionFromClosureValue() { using (MockModelMetadataProvider provider = new MockModelMetadataProvider()) { // Arrange DummyContactModel model = new DummyContactModel(); ViewDataDictionary <object> viewData = new ViewDataDictionary <object>(); provider.Expect(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(); } }
public void FromStringExpressionNonNullItemFoundAtRootOfViewData() { using (MockModelMetadataProvider provider = new MockModelMetadataProvider()) { // Arrange object model = new object(); ViewDataDictionary viewData = new ViewDataDictionary(); viewData["Object"] = model; provider.Expect(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(); } }
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.Expect(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(); } }