Skip to content

Commit

Permalink
#4: Added object/dictionary conversion and duck typing; Refactored in…
Browse files Browse the repository at this point in the history
…to multiple files
  • Loading branch information
ygoe committed Aug 22, 2019
1 parent 37ddd9e commit f5503cd
Show file tree
Hide file tree
Showing 6 changed files with 1,021 additions and 403 deletions.
169 changes: 169 additions & 0 deletions DeepConvert.Tests/DeepConvertTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,5 +354,174 @@ public void ChangeType_StringToBytes()
Assert.AreEqual(0xac, bytesArr[8]); // €
Assert.AreEqual(0x20, bytesArr[9]);
}

public class DataClass
{
public bool Flag { get; set; }
public int Number { get; set; }
public string Text { get; set; }
public int[] Numbers { get; set; }
public List<float> Floats { get; set; }
public SubClass Sub { get; set; }
}

public class DataClass2
{
public int flag { get; set; }
public long number { get; set; }
public string Text { get; set; }
public double[] Floats { get; set; }
public bool Add { get; set; } = true;
public SubClass2 Sub { get; set; }
}

public class SubClass
{
public bool IsInitialized { get; set; }
public Dictionary<string, string> Dict { get; set; }
}

public class SubClass2
{
public int IsInitialized { get; set; }
public SubDict2 Dict { get; set; }

public class SubDict2
{
public short A { get; set; }
public short B { get; set; }
public short C { get; set; }
}
}

[TestMethod]
public void ConvertToObject()
{
var dict = new Dictionary<object, object>
{
["flag"] = true,
["number"] = 42,
["Text"] = "Hello",
["Numbers"] = new[] { 1.0, 2.0, 3.6 },
["Floats"] = new[] { "3.14", "2.71" },
["Sub"] = new Dictionary<string, object>
{
["IsInitialized"] = true,
["Dict"] = new Dictionary<string, object>
{
["A"] = 1,
["B"] = 2,
["C"] = 3
}
}
};
//var data = DeepConvert.ConvertToObject<DataClass>(dict, new DeepConvertSettings { Provider = CultureInfo.InvariantCulture });
var data = DeepConvert.ChangeType<DataClass>(dict, new DeepConvertSettings { Provider = CultureInfo.InvariantCulture });

Assert.AreEqual(true, data.Flag);
Assert.AreEqual(42, data.Number);
Assert.AreEqual("Hello", data.Text);

Assert.AreEqual(3, data.Numbers.Length);
Assert.AreEqual(1, data.Numbers[0]);
Assert.AreEqual(2, data.Numbers[1]);
Assert.AreEqual(4, data.Numbers[2]);

Assert.AreEqual(2, data.Floats.Count);
Assert.AreEqual(3.14f, data.Floats[0]);
Assert.AreEqual(2.71f, data.Floats[1]);

Assert.AreEqual(true, data.Sub.IsInitialized);

Assert.AreEqual(3, data.Sub.Dict.Count);
Assert.AreEqual("1", data.Sub.Dict["A"]);
Assert.AreEqual("2", data.Sub.Dict["B"]);
Assert.AreEqual("3", data.Sub.Dict["C"]);
}

[TestMethod]
public void ConvertToDictionary()
{
var data = new DataClass
{
Flag = true,
Number = 42,
Text = "Hello",
Numbers = new[] { 1, 2, 4 },
Floats = new List<float> { 3.14f, 2.71f },
Sub = new SubClass
{
IsInitialized = true,
Dict = new Dictionary<string, string>
{
["A"] = "1",
["B"] = "2",
["C"] = "3"
}
}
};
//var dict = DeepConvert.ConvertToDictionary(data);
var dict = DeepConvert.ChangeType<Dictionary<string, object>>(data);

Assert.AreEqual(true, dict["Flag"]);
Assert.AreEqual(42, dict["Number"]);
Assert.AreEqual("Hello", dict["Text"]);

Assert.AreEqual(3, ((int[])dict["Numbers"]).Length);
Assert.AreEqual(1, ((int[])dict["Numbers"])[0]);
Assert.AreEqual(2, ((int[])dict["Numbers"])[1]);
Assert.AreEqual(4, ((int[])dict["Numbers"])[2]);

Assert.AreEqual(2, ((List<float>)dict["Floats"]).Count);
Assert.AreEqual(3.14f, ((List<float>)dict["Floats"])[0]);
Assert.AreEqual(2.71f, ((List<float>)dict["Floats"])[1]);

var sub = (SubClass)dict["Sub"];
Assert.AreEqual(true, sub.IsInitialized);

Assert.AreEqual(3, sub.Dict.Count);
Assert.AreEqual("1", sub.Dict["A"]);
Assert.AreEqual("2", sub.Dict["B"]);
Assert.AreEqual("3", sub.Dict["C"]);
}

[TestMethod]
public void ConvertBetweenObjects()
{
var data = new DataClass
{
Flag = true,
Number = 42,
Text = "Hello",
Numbers = new[] { 1, 2, 4 },
Floats = new List<float> { 3.14f, 2.71f },
Sub = new SubClass
{
IsInitialized = true,
Dict = new Dictionary<string, string>
{
["A"] = "1",
["B"] = "2",
["C"] = "3"
}
}
};
var data2 = DeepConvert.ChangeType<DataClass2>(data);

Assert.AreEqual(1, data2.flag);
Assert.AreEqual(42, data2.number);
Assert.AreEqual("Hello", data2.Text);
Assert.AreEqual(true, data2.Add);

Assert.AreEqual(2, data2.Floats.Length);
Assert.AreEqual(3.14f, data2.Floats[0]);
Assert.AreEqual(2.71f, data2.Floats[1]);

Assert.AreEqual(1, data2.Sub.IsInitialized);

Assert.AreEqual(1, data2.Sub.Dict.A);
Assert.AreEqual(2, data2.Sub.Dict.B);
Assert.AreEqual(3, data2.Sub.Dict.C);
}
}
}
Loading

0 comments on commit f5503cd

Please sign in to comment.