1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using System.Reflection;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Common;
|
---|
6 | using HeuristicLab.PluginInfrastructure;
|
---|
7 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
8 |
|
---|
9 | namespace HeuristicLab_33.Tests {
|
---|
10 | [TestClass]
|
---|
11 | public class CloningConstructorTest {
|
---|
12 | // Use ClassInitialize to run code before running the first test in the class
|
---|
13 | [ClassInitialize]
|
---|
14 | public static void MyClassInitialize(TestContext testContext) {
|
---|
15 | PluginLoader.Assemblies.Any();
|
---|
16 | }
|
---|
17 |
|
---|
18 | [TestMethod]
|
---|
19 | public void TestCloningConstructor() {
|
---|
20 | StringBuilder errorMessage = new StringBuilder();
|
---|
21 |
|
---|
22 | foreach (Type deepCloneableType in ApplicationManager.Manager.GetTypes(typeof(IDeepCloneable))) {
|
---|
23 | //test only types contained in HL plugin assemblies
|
---|
24 | if (!PluginLoader.Assemblies.Contains(deepCloneableType.Assembly)) continue;
|
---|
25 |
|
---|
26 | bool found = false;
|
---|
27 | foreach (ConstructorInfo constructor in deepCloneableType.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) {
|
---|
28 | ParameterInfo[] parameters = constructor.GetParameters();
|
---|
29 | if (parameters.Length == 2 && parameters[0].ParameterType == deepCloneableType && parameters[1].ParameterType == typeof(Cloner)) {
|
---|
30 | found = true;
|
---|
31 | if (deepCloneableType.IsSealed && !constructor.IsPrivate)
|
---|
32 | errorMessage.Append(Environment.NewLine + deepCloneableType.ToString() + ": Cloning constructor must be private in sealed classes.");
|
---|
33 | else if (!deepCloneableType.IsSealed && !(constructor.IsFamily || constructor.IsPublic))
|
---|
34 | errorMessage.Append(Environment.NewLine + deepCloneableType.ToString() + ": Cloning constructor must be protected.");
|
---|
35 | break;
|
---|
36 | }
|
---|
37 | }
|
---|
38 | if (!found)
|
---|
39 | errorMessage.Append(Environment.NewLine + deepCloneableType.ToString() + ": No cloning constructor is defined.");
|
---|
40 |
|
---|
41 | if (!deepCloneableType.IsAbstract) {
|
---|
42 | MethodInfo cloneMethod = deepCloneableType.GetMethod("Clone", new Type[] { typeof(Cloner) });
|
---|
43 | if (cloneMethod == null)
|
---|
44 | errorMessage.Append(Environment.NewLine + deepCloneableType.ToString() + ": No virtual cloning method is defined.");
|
---|
45 | }
|
---|
46 | }
|
---|
47 | Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|