[4660] | 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) {
|
---|
[5306] | 15 | PluginLoader.Assemblies.Any();
|
---|
[4660] | 16 | }
|
---|
| 17 |
|
---|
| 18 | [TestMethod]
|
---|
| 19 | public void TestCloningConstructor() {
|
---|
| 20 | StringBuilder errorMessage = new StringBuilder();
|
---|
| 21 |
|
---|
| 22 | foreach (Type deepCloneableType in ApplicationManager.Manager.GetTypes(typeof(IDeepCloneable))) {
|
---|
[5151] | 23 | //test only types contained in HL plugin assemblies
|
---|
[5306] | 24 | if (!PluginLoader.Assemblies.Contains(deepCloneableType.Assembly)) continue;
|
---|
[5151] | 25 |
|
---|
[4660] | 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)
|
---|
| 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.");
|
---|
[4661] | 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 | }
|
---|
[4660] | 46 | }
|
---|
| 47 | Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 | }
|
---|