1 | using System;
|
---|
2 | using System.Collections;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Reflection;
|
---|
6 | using System.Text;
|
---|
7 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
8 | using HeuristicLab.PluginInfrastructure;
|
---|
9 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
10 |
|
---|
11 | namespace HeuristicLab_33.Tests {
|
---|
12 | [TestClass]
|
---|
13 | public class StorableConstructorTest {
|
---|
14 |
|
---|
15 | // Use ClassInitialize to run code before running the first test in the class
|
---|
16 | [ClassInitialize]
|
---|
17 | public static void MyClassInitialize(TestContext testContext) {
|
---|
18 | PluginLoader.Assemblies.Any();
|
---|
19 | }
|
---|
20 |
|
---|
21 | [TestMethod]
|
---|
22 | public void TestStorableConstructor() {
|
---|
23 | StringBuilder errorMessage = new StringBuilder();
|
---|
24 |
|
---|
25 | foreach (Type storableType in ApplicationManager.Manager.GetTypes(typeof(object))
|
---|
26 | .Where(t => StorableClassAttribute.IsStorableClass(t))) {
|
---|
27 | //test only types contained in HL plugin assemblies
|
---|
28 | if (!PluginLoader.Assemblies.Contains(storableType.Assembly)) continue;
|
---|
29 |
|
---|
30 | IEnumerable<ConstructorInfo> ctors = storableType.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
|
---|
31 | ConstructorInfo storableConstructor = ctors.Where(c => c.GetParameters().Count() == 1 && c.GetParameters().First().ParameterType == typeof(bool)).FirstOrDefault();
|
---|
32 | if (storableConstructor == null) errorMessage.Append(Environment.NewLine + storableType.ToString() + ": No storable constructor is defined.");
|
---|
33 | else {
|
---|
34 | if (storableType.IsSealed && !storableConstructor.IsPrivate)
|
---|
35 | errorMessage.Append(Environment.NewLine + storableType.ToString() + ": Storable constructor must be private in sealed classes.");
|
---|
36 | else if (!storableType.IsSealed && !(storableConstructor.IsFamily || storableConstructor.IsPublic))
|
---|
37 | errorMessage.Append(Environment.NewLine + storableType.ToString() + ": Storable constructor must be protected (can be public in rare cases).");
|
---|
38 | }
|
---|
39 | }
|
---|
40 | Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
|
---|
41 | }
|
---|
42 | }
|
---|
43 | }
|
---|