using System; using System.Collections.Generic; using DelftTools.Shell.Gui; using DelftTools.TestUtils; using DelftTools.Utils.PropertyBag.Dynamic; using DeltaShell.Gui; using DeltaShell.Gui.Forms.PropertyGridView; using NUnit.Framework; using Rhino.Mocks; namespace DeltaShell.Tests.Gui.Forms.PropertyGrid { [TestFixture] public class PropertyResolverTest { # region GetObjectProperties tests [Test] public void GetObjectProperties_WhenNoPropertyInfoIsFound_ReturnNull() { // Assert Assert.IsNull(PropertyResolver.GetObjectProperties(new List(), 1.0)); } [Test] public void GetObjectProperties_WhenOnePropertyInfoIsFound_ReturnDynamicPropertyBagContainingOnlyThatPropertiesObject() { // Setup var propertyInfos = new List { new PropertyInfo>() }; // Call var objectProperties = PropertyResolver.GetObjectProperties(propertyInfos, new A()); // Assert Assert.IsTrue(objectProperties is DynamicPropertyBag); Assert.AreSame(typeof(SimpleProperties), ((DynamicPropertyBag) objectProperties).GetContentType()); } [Test] public void GetObjectProperties_WhenOnePropertyInfoIsFoundButAdditionalChecksFail_ReturnNull() { // Setup var propertyInfos = new List { new PropertyInfo>() { AdditionalDataCheck = o => false } }; // Call var objectProperties = PropertyResolver.GetObjectProperties(propertyInfos, new A()); // Assert Assert.IsNull(objectProperties); } [Test] public void GetObjectProperties_WhenTwoPropertyInfoAreFoundOneWithAdditionalCheckOneWithBetterType_ReturnPropertiesObjectMatchingAdditionCheck() { // Setup var propertyInfos = new List { new PropertyInfo>() { AdditionalDataCheck = o => false }, new PropertyInfo>() // specifically for C }; // Call var objectProperties = PropertyResolver.GetObjectProperties(propertyInfos, new C()); //we ask for C // Assert Assert.AreSame(typeof(SimpleProperties), ((DynamicPropertyBag)objectProperties).GetContentType(), "we got A, expected C"); } [Test] public void GetObjectProperties_BasedOnDirectObjectTypeMatch_ReturnObjectPropertiesMatchingTypeD() { // Setup var propertyInfos = new List { new PropertyInfo>(), new PropertyInfo>() }; // Call var objectProperties = PropertyResolver.GetObjectProperties(propertyInfos, new D()); // Setup Assert.IsTrue(objectProperties is DynamicPropertyBag); Assert.AreSame(typeof(SimpleProperties), ((DynamicPropertyBag)objectProperties).GetContentType()); } [Test] public void GetObjectProperties_BasedOnDerivedObjectTypeMatch_ReturnObjectPropertiesForBaseClass() { // Setup var propertyInfos = new List { new PropertyInfo>(), new PropertyInfo>() }; // Call var objectProperties = PropertyResolver.GetObjectProperties(propertyInfos, new D()); // Assert Assert.IsTrue(objectProperties is DynamicPropertyBag); Assert.AreSame(typeof(SimpleProperties), ((DynamicPropertyBag)objectProperties).GetContentType()); } [Test] public void GetObjectProperties_BasedOnDerivedObjectTypeMatchAndAdditionalDataCheck_ReturnObjectPropertiesForBaseClassMatchingAdditionCheck() { // Setup var propertyInfos = new List { new PropertyInfo> { AdditionalDataCheck = o => true }, new PropertyInfo> { AdditionalDataCheck = o => true } }; // Call var objectProperties = PropertyResolver.GetObjectProperties(propertyInfos, new D()); // Assert Assert.IsTrue(objectProperties is DynamicPropertyBag); Assert.AreSame(typeof(SimpleProperties), ((DynamicPropertyBag)objectProperties).GetContentType()); } [Test] public void GetObjectProperties_BasedOnMatchingAdditionalDataCheck_ReturnMatchingWithAdditionalDataCheck() { // Setup var propertyInfos = new List { new PropertyInfo> { AdditionalDataCheck = o => true }, // Additional data check which will be matched new PropertyInfo>() }; // Call var objectProperties = PropertyResolver.GetObjectProperties(propertyInfos, new B()); // Assert Assert.IsTrue(objectProperties is DynamicPropertyBag); Assert.AreSame(typeof(SimpleProperties), ((DynamicPropertyBag)objectProperties).GetContentType()); } [Test] public void GetObjectProperties_BasedOnMismatchingAdditionalDataCheck_ReturnFallBackPropertiesObject() { // Setup var propertyInfos = new List { new PropertyInfo> { AdditionalDataCheck = o => false }, // Additional data check which will not be matched new PropertyInfo>() }; // Call var objectProperties = PropertyResolver.GetObjectProperties(propertyInfos, new B()); // Assert Assert.IsTrue(objectProperties is DynamicPropertyBag); Assert.AreSame(typeof(OtherSimpleProperties), ((DynamicPropertyBag)objectProperties).GetContentType()); } [Test] public void GetObjectProperties_BasedOnDerivedPropertyTypeMatch_ReturnDerivedObjectPropertiesClass() { // Setup var propertyInfos = new List { new PropertyInfo>(), new PropertyInfo>() }; // Call var objectProperties = PropertyResolver.GetObjectProperties(propertyInfos, new B()); // Assert Assert.IsTrue(objectProperties is DynamicPropertyBag); Assert.AreSame(typeof(DerivedSimpleProperties), ((DynamicPropertyBag)objectProperties).GetContentType()); } [Test] public void GetObjectProperties_BasedOnDerivedPropertyTypeMatchAndAdditionalDataCheck_ReturnDerivedObjectProperties() { // Setup var propertyInfos = new List { new PropertyInfo> { AdditionalDataCheck = o => true }, new PropertyInfo> { AdditionalDataCheck = o => true } }; // Call var objectProperties = PropertyResolver.GetObjectProperties(propertyInfos, new B()); // Assert Assert.IsTrue(objectProperties is DynamicPropertyBag); Assert.AreSame(typeof(DerivedSimpleProperties), ((DynamicPropertyBag)objectProperties).GetContentType()); } [Test] public void GetObjectProperties_WhenMultiplePropertyObjectsAreFound_ReturnNull() { // Setup var propertyInfos = new List { new PropertyInfo>(), new PropertyInfo>() }; // Call var objectProperties = PropertyResolver.GetObjectProperties(propertyInfos, new B()); Assert.IsNull(objectProperties); } [Test] public void GetObjectProperties_TakingAllPropertyInfoPropertiesIntoAccount_ReturnDerivedObjectPropertiesMatchingDataCheck() { // Setup var propertyInfos = new List { new PropertyInfo> { AdditionalDataCheck = o => true }, // D is not assignable from C: no candidate new PropertyInfo> { AdditionalDataCheck = o => true }, // A is less specific than C: candidate but not the most specific one new PropertyInfo> { AdditionalDataCheck = o => false }, // Additional data check is false: no candidate new PropertyInfo> { AdditionalDataCheck = o => true }, // SimpleProperties is less specific than DerivedSimpleProperties: candidate but not the most specific one new PropertyInfo> { AdditionalDataCheck = o => true } // Most specific! }; // Call var objectProperties = PropertyResolver.GetObjectProperties(propertyInfos, new C()); // Assert Assert.IsTrue(objectProperties is DynamicPropertyBag); Assert.AreSame(typeof(DerivedSimpleProperties), ((DynamicPropertyBag)objectProperties).GetContentType()); } # endregion # region Nested types /* * A * ___^___ * | | * B C * | * D * * * * SimpleProperties OtherSimpleProperties * ^ * | * DerivedSimpleProperties * */ internal class A {} private class B : A {} internal class C : A {} private class D : C {} internal class SimpleProperties : ObjectProperties {} private class DerivedSimpleProperties : SimpleProperties {} private class OtherSimpleProperties : ObjectProperties {} # endregion } }