[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) {
|
---|
| 15 | PluginLoader.pluginAssemblies.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 | bool found = false;
|
---|
| 24 | foreach (ConstructorInfo constructor in deepCloneableType.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) {
|
---|
| 25 | ParameterInfo[] parameters = constructor.GetParameters();
|
---|
| 26 | if (parameters.Length == 2 && parameters[0].ParameterType == deepCloneableType && parameters[1].ParameterType == typeof(Cloner)) {
|
---|
| 27 | found = true;
|
---|
| 28 | if (deepCloneableType.IsSealed && !constructor.IsPrivate)
|
---|
| 29 | errorMessage.Append(Environment.NewLine + deepCloneableType.ToString() + ": Cloning constructor must be private in sealed classes.");
|
---|
| 30 | else if (!deepCloneableType.IsSealed && !constructor.IsFamily)
|
---|
| 31 | errorMessage.Append(Environment.NewLine + deepCloneableType.ToString() + ": Cloning constructor must be protected.");
|
---|
| 32 | break;
|
---|
| 33 | }
|
---|
| 34 | }
|
---|
| 35 | if (!found)
|
---|
| 36 | errorMessage.Append(Environment.NewLine + deepCloneableType.ToString() + ": No cloning constructor is defined.");
|
---|
[4661] | 37 |
|
---|
| 38 | if (!deepCloneableType.IsAbstract) {
|
---|
| 39 | MethodInfo cloneMethod = deepCloneableType.GetMethod("Clone", new Type[] { typeof(Cloner) });
|
---|
| 40 | if (cloneMethod == null)
|
---|
| 41 | errorMessage.Append(Environment.NewLine + deepCloneableType.ToString() + ": No virtual cloning method is defined.");
|
---|
| 42 | }
|
---|
[4660] | 43 | }
|
---|
| 44 | Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 | }
|
---|