Merge branch 'master' into develop

This commit is contained in:
Kevin Giszewski
2014-06-20 08:41:49 -04:00
3 changed files with 57 additions and 2 deletions
@@ -6,6 +6,7 @@ using System.Text;
using System.Threading.Tasks;
using Archetype.PropertyConverters;
using NUnit.Framework;
using Archetype.Tests.Serialization;
namespace Archetype.Tests.Models
{
@@ -34,6 +35,18 @@ namespace Archetype.Tests.Models
Assert.That(propertyValue == "Box 1 Title");
}
[Test]
public void Can_Get_Fieldset_Property_Default_Value_By_Alias()
{
var converter = new ArchetypeValueConverter();
var result = (Archetype.Models.ArchetypeModel)converter.ConvertDataToSource(null, _sampleJson, false);
var fieldset = result.Fieldsets.First();
var propertyValue = fieldset.GetValue<string>("noSuchProperty", "noSuchProperty default value");
Assert.That(propertyValue == "noSuchProperty default value");
}
[Test]
public void Can_Convert_Property_Value_Types()
{
@@ -47,6 +60,24 @@ namespace Archetype.Tests.Models
Assert.That(fieldset.GetValue<string>("blurb") == "A blurb here");
}
[Test]
public void Can_Convert_Property_Default_Value_Types()
{
var converter = new ArchetypeValueConverter();
var result = (Archetype.Models.ArchetypeModel)converter.ConvertDataToSource(null, _sampleJson, false);
var fieldset = result.Fieldsets.First();
Assert.That(fieldset.GetValue<int>("noSuchInteger", 1234) == 1234);
Assert.That(fieldset.GetValue<bool>("noSuchBoolean", true) == true);
Assert.That(fieldset.GetValue<bool>("noSuchBoolean", false) == false);
Assert.That(fieldset.GetValue<string>("noSuchString", "noSuchString default value") == "noSuchString default value");
var contactDetails = fieldset.GetValue<ContactDetails>("noSuchContact", new ContactDetails { Address = "some street, some city", Name = "someone" });
Assert.That(contactDetails.Address == "some street, some city");
Assert.That(contactDetails.Name == "someone");
}
[Test]
public void Returns_String_When_No_Type_Specified()
{
@@ -29,12 +29,36 @@ namespace Archetype.Models
{
var property = GetProperty(propertyAlias);
if (property == null || property.Value == null || string.IsNullOrEmpty(property.Value.ToString()))
if (IsEmptyProperty(property))
{
return default(T);
}
return property.GetValue<T>();
}
// issue 142: support default T value supplied by caller
// this code would look nicer if the two GetValue<T>() methods had one common implementation.
// however, this would require GetValue<T>(string propertyAlias) to call the common implementation
// with a default(T) value, which could in theory result in a performance hit, if T for some reason
// is costly to instantiate.
public T GetValue<T>(string propertyAlias, T defaultValue)
{
var property = GetProperty(propertyAlias);
if (IsEmptyProperty(property))
{
return defaultValue;
}
return property.GetValue<T>();
}
private bool IsEmptyProperty(ArchetypePropertyModel property)
{
return (property == null || property.Value == null || string.IsNullOrEmpty(property.Value.ToString()));
}
public bool HasProperty(string propertyAlias)
{
return GetProperty(propertyAlias) != null;
@@ -34,7 +34,7 @@ namespace Archetype.PropertyConverters
if (!sourceString.DetectIsJson())
return defaultValue;
using (var timer = DisposableTimer.DebugDuration<ArchetypeValueConverter>(string.Format("ConvertDataToSource ({0})", propertyType.PropertyTypeAlias)))
using (var timer = DisposableTimer.DebugDuration<ArchetypeValueConverter>(string.Format("ConvertDataToSource ({0})", propertyType != null ? propertyType.PropertyTypeAlias : "null")))
{
var archetype = ArchetypeHelper.Instance.DeserializeJsonToArchetype(sourceString,
(propertyType != null ? propertyType.DataTypeId : -1),