Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactoring/HeuristicLab/3.3/Tests/CloningConstructorTest.cs @ 4661

Last change on this file since 4661 was 4661, checked in by mkommend, 13 years ago

Added check for existence of the clone method in CloningConstructorTest (ticket #922).

File size: 2.2 KB
Line 
1using System;
2using System.Linq;
3using System.Reflection;
4using System.Text;
5using HeuristicLab.Common;
6using HeuristicLab.PluginInfrastructure;
7using Microsoft.VisualStudio.TestTools.UnitTesting;
8
9namespace 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.");
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        }
43      }
44      Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
45    }
46  }
47}
Note: See TracBrowser for help on using the repository browser.