Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VNS/HeuristicLab/3.3/Tests/CloningConstructorTest.cs @ 5740

Last change on this file since 5740 was 5306, checked in by mkommend, 14 years ago

Corrected HL3.3 tests project to test all assemblies and not only assemblies that contain a Plugin class (ticket #1351).

File size: 2.3 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.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)
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}
Note: See TracBrowser for help on using the repository browser.