Skip to content

Commit a3fc3f9

Browse files
committed
Cleanup and separation of serialisation tests
1 parent cc9a207 commit a3fc3f9

File tree

5 files changed

+380
-319
lines changed

5 files changed

+380
-319
lines changed

Serialisation_Tests/DataSource.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using BH.Engine.Base;
2+
using BH.Engine.Diffing;
3+
using BH.Engine.Reflection;
4+
using BH.Engine.Serialiser;
5+
using BH.Engine.Test;
6+
using BH.oM.Base;
7+
using BH.oM.Base.Attributes;
8+
using BH.oM.Test;
9+
using BH.oM.Test.Results;
10+
using NUnit.Framework;
11+
using System;
12+
using System.Collections.Generic;
13+
using System.Data;
14+
using System.IO;
15+
using System.Linq;
16+
using System.Reflection;
17+
using System.Text;
18+
using System.Threading.Tasks;
19+
20+
namespace BH.Tests.Serialisation
21+
{
22+
public static class DataSource
23+
{
24+
25+
public static IEnumerable<Type> OmTypes()
26+
{
27+
return OmTypesToTest(Setup.Query.CurrentAssemblies());
28+
}
29+
30+
public static IEnumerable<MethodBase> EngineMethods()
31+
{
32+
return EngineMethodsToTest(Setup.Query.CurrentAssemblies());
33+
}
34+
35+
/*************************************/
36+
37+
public static List<Type> OmTypesToTest(List<Assembly> assembliesToTest)
38+
{
39+
assembliesToTest = assembliesToTest.Where(x => x.IsOmAssembly()).ToList();
40+
41+
// It feels like the BHoMTypeList method should already return a clean list of Type but it doesn't at the moment
42+
return assembliesToTest.SelectMany(a => a.GetTypes().Where(x => {
43+
return typeof(IObject).IsAssignableFrom(x)
44+
&& !x.IsAbstract
45+
&& !x.IsDeprecated()
46+
&& !x.GetProperties().Select(p => p.PropertyType.Namespace).Any(n => !n.StartsWith("BH.") && !n.StartsWith("System"));
47+
})).ToList();
48+
}
49+
50+
/*************************************/
51+
52+
public static List<MethodInfo> EngineMethodsToTest(List<Assembly> assembliesToTest)
53+
{
54+
assembliesToTest = assembliesToTest.Where(x => x.IsEngineAssembly()).ToList();
55+
return BH.Engine.Base.Query.BHoMMethodList().Where(x => assembliesToTest.Any(a => x.DeclaringType.Assembly == a)).ToList();
56+
}
57+
58+
/*************************************/
59+
60+
}
61+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using BH.Engine.Base;
2+
using BH.Engine.Diffing;
3+
using BH.Engine.Reflection;
4+
using BH.Engine.Serialiser;
5+
using BH.Engine.Test;
6+
using BH.oM.Base;
7+
using BH.oM.Base.Attributes;
8+
using BH.oM.Test;
9+
using BH.oM.Test.Results;
10+
using NUnit.Framework;
11+
using System;
12+
using System.Collections.Generic;
13+
using System.Data;
14+
using System.IO;
15+
using System.Linq;
16+
using System.Reflection;
17+
using System.Text;
18+
using System.Threading.Tasks;
19+
20+
namespace BH.Tests.Serialisation
21+
{
22+
public class MethodSerialisation
23+
{
24+
25+
[TestCaseSource(typeof(DataSource), nameof(DataSource.EngineMethods))]
26+
public void ToFromJson(MethodBase method)
27+
{
28+
TestResult result = MethodToFromJson(method);
29+
30+
Assert.That(result.Status, Is.EqualTo(TestStatus.Pass), result.FullMessage(3, TestStatus.Warning));
31+
Assert.Pass($"Passing method serialisation test for {method.IToText(true)} from Assembly {method.DeclaringType.Assembly.FullName}");
32+
}
33+
34+
/*************************************/
35+
36+
37+
//Below is copy pasted from Verification solution in BHoM_Engine
38+
39+
public static TestResult MethodToFromJson(MethodBase method)
40+
{
41+
string methodDescription = method.IToText(true);
42+
43+
// To Json
44+
string json = "";
45+
try
46+
{
47+
Engine.Base.Compute.ClearCurrentEvents();
48+
json = method.ToJson();
49+
}
50+
catch (Exception e)
51+
{
52+
Engine.Base.Compute.RecordError(e.Message);
53+
}
54+
55+
if (string.IsNullOrWhiteSpace(json))
56+
return new TestResult
57+
{
58+
Description = methodDescription,
59+
Status = TestStatus.Error,
60+
Message = $"Error: Failed to convert method {methodDescription} to json.",
61+
Information = Engine.Base.Query.CurrentEvents().Select(x => x.ToEventMessage()).ToList<ITestInformation>()
62+
};
63+
64+
// From Json
65+
MethodInfo copy = null;
66+
try
67+
{
68+
Engine.Base.Compute.ClearCurrentEvents();
69+
copy = Engine.Serialiser.Convert.FromJson(json) as MethodInfo;
70+
}
71+
catch (Exception e)
72+
{
73+
Engine.Base.Compute.RecordError(e.Message);
74+
}
75+
76+
if (!method.IsEqual(copy))
77+
return new TestResult
78+
{
79+
Description = methodDescription,
80+
Status = TestStatus.Error,
81+
Message = $"Error: Method {methodDescription} is not equal to the original after serialisation.",
82+
Information = Engine.Base.Query.CurrentEvents().Select(x => x.ToEventMessage()).ToList<ITestInformation>()
83+
};
84+
85+
// All test objects passed the test
86+
return Engine.Test.Create.PassResult(methodDescription);
87+
}
88+
89+
/*************************************/
90+
}
91+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
using BH.Engine.Base;
2+
using BH.Engine.Diffing;
3+
using BH.Engine.Reflection;
4+
using BH.Engine.Serialiser;
5+
using BH.Engine.Test;
6+
using BH.oM.Base;
7+
using BH.oM.Base.Attributes;
8+
using BH.oM.Test;
9+
using BH.oM.Test.Results;
10+
using NUnit.Framework;
11+
using System;
12+
using System.Collections.Generic;
13+
using System.Data;
14+
using System.IO;
15+
using System.Linq;
16+
using System.Reflection;
17+
using System.Text;
18+
using System.Threading.Tasks;
19+
20+
namespace BH.Tests.Serialisation
21+
{
22+
public class ObjectSerialisation
23+
{
24+
[TestCaseSource(typeof(DataSource), nameof(DataSource.OmTypes))]
25+
public void ToFromJson(Type oMType)
26+
{
27+
TestResult result = ObjectToFromJson(oMType);
28+
29+
Assert.That(result.Status, Is.EqualTo(TestStatus.Pass), result.FullMessage(3, TestStatus.Warning));
30+
Assert.Pass($"Passing object serialisation test for {oMType.FullName} from Assembly {oMType.Assembly.FullName}");
31+
}
32+
33+
//Below is copy pasted from Verification solution in BHoM_Engine
34+
35+
public static TestResult ObjectToFromJson(Type type)
36+
{
37+
string typeDescription = type.IToText(true);
38+
39+
// Create the test objects of the given type
40+
List<object> testObjects = new List<object>();
41+
if (testObjects.Count == 0)
42+
{
43+
object dummy = null;
44+
try
45+
{
46+
Engine.Base.Compute.ClearCurrentEvents();
47+
dummy = Engine.Test.Compute.DummyObject(type);
48+
}
49+
catch (Exception e)
50+
{
51+
Engine.Base.Compute.RecordWarning(e.Message);
52+
}
53+
54+
if (dummy == null)
55+
return new TestResult
56+
{
57+
Description = typeDescription,
58+
Status = TestStatus.Warning,
59+
Message = $"Warning: Failed to create a dummy object of type {typeDescription}.",
60+
Information = Engine.Base.Query.CurrentEvents().Select(x => x.ToEventMessage()).ToList<ITestInformation>()
61+
};
62+
else
63+
testObjects.Add(dummy);
64+
}
65+
66+
// Test each object in the list
67+
foreach (object testObject in testObjects)
68+
{
69+
// To Json
70+
string json = "";
71+
try
72+
{
73+
Engine.Base.Compute.ClearCurrentEvents();
74+
json = testObject.ToJson();
75+
}
76+
catch (Exception e)
77+
{
78+
Engine.Base.Compute.RecordError(e.Message);
79+
}
80+
81+
if (string.IsNullOrWhiteSpace(json))
82+
return new TestResult
83+
{
84+
Description = typeDescription,
85+
Status = TestStatus.Error,
86+
Message = $"Error: Failed to convert object of type {typeDescription} to json.",
87+
Information = Engine.Base.Query.CurrentEvents().Select(x => x.ToEventMessage()).ToList<ITestInformation>()
88+
};
89+
90+
// From Json
91+
object copy = null;
92+
try
93+
{
94+
Engine.Base.Compute.ClearCurrentEvents();
95+
copy = Engine.Serialiser.Convert.FromJson(json);
96+
}
97+
catch (Exception e)
98+
{
99+
Engine.Base.Compute.RecordError(e.Message);
100+
}
101+
102+
bool isEqual;
103+
104+
try
105+
{
106+
isEqual = testObject.IsEqual(copy);
107+
}
108+
catch (Exception e)
109+
{
110+
BH.Engine.Base.Compute.RecordWarning(e, $"Crashed when trying to compare objects.");
111+
112+
return new TestResult
113+
{
114+
Description = typeDescription,
115+
Status = TestStatus.Warning,
116+
Message = $"Warning: Failed to compare objects of type {typeDescription}.",
117+
Information = Engine.Base.Query.CurrentEvents().Select(x => x.ToEventMessage()).ToList<ITestInformation>()
118+
};
119+
}
120+
121+
if (!isEqual)
122+
return new TestResult
123+
{
124+
Description = typeDescription,
125+
Status = TestStatus.Error,
126+
Message = $"Error: Object of type {typeDescription} is not equal to the original after serialisation.",
127+
Information = Engine.Base.Query.CurrentEvents().Select(x => x.ToEventMessage()).ToList<ITestInformation>()
128+
};
129+
}
130+
131+
// All test objects passed the test
132+
return Engine.Test.Create.PassResult(typeDescription);
133+
}
134+
135+
}
136+
}

0 commit comments

Comments
 (0)