Commit b7d1f4a3 authored by Michael Herndon's avatar Michael Herndon

PROTO: flex

 - add tests for simple dictionary conversion.
 - add decimal to IsDataType check.
 - when interface like IList, IDictionary, has a child value of object.
   ensure that each value that isn't null is checked for
   its type value.
parent a9e7b1e5
......@@ -19,6 +19,7 @@ namespace NerdyMishka.Reflection
public static Type[] DataTypes { get; set; } = new []{
typeof(string),
typeof(decimal),
typeof(DateTime),
typeof(TimeSpan),
typeof(char[]),
......
......@@ -20,8 +20,18 @@ namespace NerdyMishka.Reflection
this.PropertyInfo = info;
this.CanWrite = info.CanWrite;
this.CanRead = info.CanRead;
this.IsStatic = info.GetMethod.IsStatic;
this.IsPublic = info.GetMethod.IsPublic || info.SetMethod.IsPublic;
if(info.GetMethod != null)
{
this.IsStatic = info.GetMethod.IsStatic;
this.IsPublic = info.GetMethod.IsPublic;
}
if(info.SetMethod != null)
{
this.IsStatic = this.IsStatic ? this.IsStatic : info.SetMethod.IsStatic;
this.IsPublic = this.IsPublic ? this.IsPublic : info.SetMethod.IsPublic;
}
this.IsPrivate = !this.IsPublic;
this.IsInstance = !this.IsStatic;
this.ClrType = info.PropertyType;
......
......@@ -145,9 +145,15 @@ namespace NerdyMishka.Extensions.Flex
if(childType == null)
childType = typeof(object).AsTypeInfo();
bool updateType = childType.ClrType == typeof(object) || type.ClrType.IsGenericType;
for(var i = 0; i < list.Count; i++)
{
var nextItem = list[i];
if(updateType && nextItem != null)
childType = nextItem.GetType().AsTypeInfo();
var nextValue = this.Visit(nextItem, null, childType);
if(nextValue == null)
{
......@@ -178,10 +184,17 @@ namespace NerdyMishka.Extensions.Flex
if(childType == null)
childType = typeof(object).AsTypeInfo();
bool updateType = childType.ClrType == typeof(object) || type.ClrType.IsGenericType;
foreach(var key in dictionary.Keys)
{
var symbol = this.ConvertToSymbol(key.ToString());
var value = dictionary[key];
if(updateType && value != null)
childType = value.GetType().AsTypeInfo();
var nextNode = this.Visit(value, null, childType);
if(nextNode == null)
{
......
......@@ -9,25 +9,61 @@ using YamlDotNet.RepresentationModel;
namespace Tests
{
public class YamlObjectWriterTests
{
{
[Fact]
public static void VisitDictionary_Simple()
{
var dictionary = new Dictionary<string, object>() {
{"Name", "Crash Bandicoot"},
{"Age", 20},
{"Price", 43234.30M }
};
var typeInfo = dictionary.GetType().AsTypeInfo();
var writer = new YamlObjectWriter();
var node = writer.VisitDictionary(dictionary, null, typeInfo);
Assert.NotNull(node);
Assert.IsType<YamlMappingNode>(node);
var map = (YamlMappingNode)node;
foreach(var set in map)
{
var key = ((YamlScalarNode)set.Key).Value;
key = key[0].ToString().ToUpperInvariant() + key.Substring(1);
var found = dictionary.TryGetValue(key, out object value);
Assert.True(found, $"key {key} not found");
Assert.Equal(value.ToString(), ((YamlScalarNode)set.Value).Value);
}
}
[Fact]
public static void VisitArray()
public static void VisitArray_Simple()
{
var configuration = new SimpleConfigurationWithList();
var typeInfo = configuration.Values.GetType().AsTypeInfo();
var list = new List<string>() {
"1",
"2",
"3",
"4",
"5",
"6"
};
var typeInfo = list.GetType().AsTypeInfo();
var writer = new YamlObjectWriter();
var node = writer.VisitArray(configuration.Values, null, typeInfo);
var node = writer.VisitArray(list, null, typeInfo);
Assert.NotNull(node);
Assert.IsType(typeof(YamlSequenceNode), node);
Assert.IsType<YamlSequenceNode>(node);
var list = (YamlSequenceNode)node;
var set = (YamlSequenceNode)node;
Assert.Equal(list.Children.Count, configuration.Values.Count);
Assert.Equal(set.Children.Count, list.Count);
Assert.Collection(list.Children,
Assert.Collection(set.Children,
(v) => Assert.Equal("1", ((YamlScalarNode)v).Value),
(v) => Assert.Equal("2", ((YamlScalarNode)v).Value),
(v) => Assert.Equal("3", ((YamlScalarNode)v).Value),
......@@ -35,7 +71,6 @@ namespace Tests
(v) => Assert.Equal("5", ((YamlScalarNode)v).Value),
(v) => Assert.Equal("6", ((YamlScalarNode)v).Value)
);
}
......@@ -59,7 +94,7 @@ namespace Tests
continue;
}
Assert.IsType(typeof(YamlScalarNode), node);
Assert.IsType<YamlScalarNode>(node);
var scalar = (YamlScalarNode)node;
switch(value)
......@@ -97,7 +132,7 @@ namespace Tests
continue;
}
Assert.IsType(typeof(YamlScalarNode), node);
Assert.IsType<YamlScalarNode>(node);
var scalar = (YamlScalarNode)node;
switch(propertyInfo.Name)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment