using System.Linq;
using Catel.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Catel.Test.Data
{
[TestClass]
public class PropertyDataManagerTest
{
[TestMethod]
public void GetProperties()
{
PropertyDataManager propertyDataManager = new PropertyDataManager();
RegisterProperty(propertyDataManager, "stringProperty", "defaultValue");
RegisterProperty(propertyDataManager, "objectProperty", new object());
RegisterProperty(propertyDataManager, "intProperty", 1);
var registeredProperties = propertyDataManager.GetProperties(typeof(PropertyDataManagerTest));
var keys = registeredProperties.Keys.ToArray();
Assert.AreEqual(3, registeredProperties.Count);
Assert.AreEqual("stringProperty", keys[0]);
Assert.AreEqual("objectProperty", keys[1]);
Assert.AreEqual("intProperty", keys[2]);
}
[TestMethod]
public void RegisterProperty_SinglePropertyRegistration()
{
PropertyDataManager propertyDataManager = new PropertyDataManager();
RegisterProperty(propertyDataManager, "stringProperty", "defaultValue");
var registeredProperties = propertyDataManager.GetProperties(typeof(PropertyDataManagerTest));
Assert.AreEqual(1, registeredProperties.Count);
Assert.AreEqual("defaultValue", registeredProperties["stringProperty"].GetDefaultValue());
}
[TestMethod]
[ExpectedException(typeof(PropertyAlreadyRegisteredException))]
public void RegisterProperty_DoublePropertyRegistration()
{
PropertyDataManager propertyDataManager = new PropertyDataManager();
RegisterProperty(propertyDataManager, "stringProperty", "defaultValue");
RegisterProperty(propertyDataManager, "stringProperty", "defaultValue");
}
[TestMethod]
public void IsPropertyRegistered_RegisteredProperty()
{
PropertyDataManager propertyDataManager = new PropertyDataManager();
RegisterProperty(propertyDataManager, "stringProperty", "defaultValue");
Assert.IsTrue(propertyDataManager.IsPropertyRegistered(typeof(PropertyDataManagerTest), "stringProperty"));
}
[TestMethod]
public void IsPropertyRegistered_UnregisteredProperty()
{
PropertyDataManager propertyDataManager = new PropertyDataManager();
Assert.IsFalse(propertyDataManager.IsPropertyRegistered(typeof(PropertyDataManagerTest), "stringProperty"));
}
[TestMethod]
public void GetPropertyData_RegisteredProperty()
{
PropertyDataManager propertyDataManager = new PropertyDataManager();
RegisterProperty(propertyDataManager, "stringProperty", "defaultValue");
var propertyData = propertyDataManager.GetPropertyData(typeof(PropertyDataManagerTest), "stringProperty");
Assert.IsNotNull(propertyData);
Assert.AreEqual("stringProperty", propertyData.Name);
}
[TestMethod]
[ExpectedException(typeof(PropertyNotRegisteredException))]
public void GetPropertyData_UnregisteredProperty()
{
PropertyDataManager propertyDataManager = new PropertyDataManager();
var propertyData = propertyDataManager.GetPropertyData(typeof(PropertyDataManagerTest), "stringProperty");
}
[TestMethod]
public void MapXmlNameToPropertyName_EqualPropertyAndXml()
{
ObjectWithXmlMappings objectWithXmlMappings = new ObjectWithXmlMappings();
string propertyName = ObjectWithXmlMappings.PropertyDataManager.MapXmlNameToPropertyName(typeof(ObjectWithXmlMappings), "PropertyWithoutMapping");
Assert.AreEqual("PropertyWithoutMapping", propertyName);
}
[TestMethod]
public void MapXmlNameToPropertyName_DifferentPropertyAndXml()
{
ObjectWithXmlMappings objectWithXmlMappings = new ObjectWithXmlMappings();
string propertyName = ObjectWithXmlMappings.PropertyDataManager.MapXmlNameToPropertyName(typeof(ObjectWithXmlMappings), "MappedXmlProperty");
Assert.AreEqual("PropertyWithMapping", propertyName);
}
[TestMethod]
public void MapPropertyNameToXmlName_EqualPropertyAndXml()
{
ObjectWithXmlMappings objectWithXmlMappings = new ObjectWithXmlMappings();
string xmlName = ObjectWithXmlMappings.PropertyDataManager.MapPropertyNameToXmlName(typeof(ObjectWithXmlMappings), "PropertyWithoutMapping");
Assert.AreEqual("PropertyWithoutMapping", xmlName);
}
[TestMethod]
public void MapPropertyNameToXmlName_DifferentPropertyAndXml()
{
ObjectWithXmlMappings objectWithXmlMappings = new ObjectWithXmlMappings();
string xmlName = ObjectWithXmlMappings.PropertyDataManager.MapPropertyNameToXmlName(typeof(ObjectWithXmlMappings), "PropertyWithMapping");
Assert.AreEqual("MappedXmlProperty", xmlName);
}
#region Helper methods
/// <summary>
/// Registers the property with the property data manager. This is a shortcut method so the PropertyData doesn't have
/// to be declared every time.
/// </summary>
/// <typeparam name="T">Type of the property.</typeparam>
/// <param name="propertyDataManager">The property data manager.</param>
/// <param name="name">The name.</param>
/// <param name="defaultValue">The default value.</param>
private static void RegisterProperty<T>(PropertyDataManager propertyDataManager, string name, T defaultValue)
{
propertyDataManager.RegisterProperty(typeof(PropertyDataManagerTest), name, new PropertyData(name, typeof(T), defaultValue, false, null, false, false, false));
}
#endregion
}
}