Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test #19

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft

test #19

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Groot/Groot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static IEnumerable<T> GetObjectFromCsv<T>(string filePath, bool autoMapFo
.ToList()
.ForEach(prop =>
{
prop.SetValue(tElem, Convert.ChangeType(x.Value, prop.PropertyType), null);
prop.SetValue(tElem, ChangeType(prop, x.Value), null);
});

}
Expand All @@ -70,6 +70,14 @@ public static IEnumerable<T> GetObjectFromCsv<T>(string filePath, bool autoMapFo

}


private static object ChangeType(PropertyInfo prop, string value)
{
if (prop.PropertyType.IsEnum)
return Enum.Parse(prop.PropertyType, value);

return Convert.ChangeType(value, prop.PropertyType);
}


}
}
3 changes: 3 additions & 0 deletions GrootUnitTest/GrootUnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@
<None Update="resources\grootTrees.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="resources\item.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
16 changes: 16 additions & 0 deletions GrootUnitTest/models/Item.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Groot;

namespace GrootUnitTest.models
{
public class Item
{
[GrootField("name")]
public string Name { get; set; }

[GrootField("amount")]
public int Amount { get; set; }

[GrootField("type")]
public ItemType Type { get; set; }
}
}
12 changes: 12 additions & 0 deletions GrootUnitTest/models/ItemType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Groot;

namespace GrootUnitTest.models
{
public enum ItemType
{
Plastic=1,
Wood=2,
Iron=3,
Fabric=4
};
}
4 changes: 4 additions & 0 deletions GrootUnitTest/resources/item.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name, amount, type
itema, 68, 1
itemb, 42, 2
itemc, 55, 1
37 changes: 37 additions & 0 deletions GrootUnitTest/test/GetObjectFromCsvTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace GrootUnitTest.test
[TestClass]
public class GetObjectFromCsvTest
{
private const string itemName = "itema";
private const string mappedFieldWithNoCustomAttr = "mappedFieldWithCustomAttr";

[TestMethod]
public void AutoMapForNoCustomAttr()
{
Expand Down Expand Up @@ -47,5 +50,39 @@ public void NoAutoMapForNoCustomAttr()
Assert.AreEqual(232.29M, paul.Height, "mappedFieldWithCustomAttr");

}

[TestMethod]
public void AutoMapForNoCustomAttrUsingEnum()
{
var path = Path.GetFullPath("resources/item.csv");
var types = Groot.Groot.GetObjectFromCsv<models.Item>(path);

Assert.AreEqual(3, types.Count(), "rows amount");

var itema = types.First(t => t.Name == itemName);

Assert.IsNotNull(itema);
Assert.AreEqual(itemName, itema.Name, mappedFieldWithNoCustomAttr);
Assert.AreEqual(68, itema.Amount, mappedFieldWithNoCustomAttr);
Assert.AreEqual(models.ItemType.Plastic, itema.Type, mappedFieldWithNoCustomAttr);

}

[TestMethod]
public void NoAutoMapForNoCustomAttrUsingEnum()
{
var path = Path.GetFullPath("resources/item.csv");
var types = Groot.Groot.GetObjectFromCsv<models.Item>(path, false);

Assert.AreEqual(3, types.Count(), "rows amount");

var itema = types.First(t => t.Name == itemName);

Assert.IsNotNull(itema);
Assert.AreEqual(itemName, itema.Name, mappedFieldWithNoCustomAttr);
Assert.AreEqual(68, itema.Amount, mappedFieldWithNoCustomAttr);
Assert.AreEqual(models.ItemType.Plastic, itema.Type, mappedFieldWithNoCustomAttr);

}
}
}