Free cookie consent management tool by TermsFeed Policy Generator

source: branches/gteufl/HeuristicLab.Tests/HeuristicLab-3.3/ParameterVisibilityTest.cs @ 11063

Last change on this file since 11063 was 9783, checked in by mkommend, 11 years ago

#2088: Adapted HL-3.3.Tests, Algorithms.DataAnalysis.Tests, and Collections.Test.

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Linq;
24using System.Reflection;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.PluginInfrastructure;
28using Microsoft.VisualStudio.TestTools.UnitTesting;
29
30namespace HeuristicLab.Tests {
31  [TestClass]
32  public class ParameterVisibilityTest {
33    // Use ClassInitialize to run code before running the first test in the class
34    [ClassInitialize]
35    public static void MyClassInitialize(TestContext testContext) {
36      PluginLoader.Assemblies.Any();
37    }
38
39    private TestContext testContextInstance;
40    public TestContext TestContext {
41      get { return testContextInstance; }
42      set { testContextInstance = value; }
43    }
44
45    [TestMethod]
46    [TestCategory("General")]
47    [TestCategory("Essential")]
48    [TestProperty("Time", "long")]
49    public void TestParameterVisibility() {
50      StringBuilder errorMessage = new StringBuilder();
51
52      foreach (var parameterizedItem in ApplicationManager.Manager.GetInstances<IParameterizedItem>()) {
53        foreach (var parameter in parameterizedItem.Parameters) {
54          var parameterType = parameter.GetType().GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IConstrainedValueParameter<>));
55          if (parameterType == null) continue;
56
57          var parameterGenericTypeArgument = parameterType.GetGenericArguments().First();
58          var parameterGenericTypeDefinition = typeof(IConstrainedValueParameter<>);
59
60          var paramProperty = parameterizedItem.GetType().GetProperty(parameter.Name + "Parameter",
61            BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);
62          var valueProperty = parameterizedItem.GetType().GetProperty(parameter.Name,
63                                                                 BindingFlags.GetProperty | BindingFlags.Instance |
64                                                                 BindingFlags.Public);
65          if (paramProperty == null)
66            errorMessage.Append(Environment.NewLine + parameterizedItem.GetType() +
67              ": public property " + parameter.Name + "Parameter is missing.");
68          else if (paramProperty.PropertyType.GetGenericTypeDefinition() != parameterGenericTypeDefinition)
69            errorMessage.Append(Environment.NewLine + parameterizedItem.GetType() +
70              ": public property " + parameter.Name + "Parameter type must be " + parameterGenericTypeDefinition.Name);
71          else if (paramProperty.PropertyType.GetGenericArguments().First() != parameterGenericTypeArgument)
72            errorMessage.Append(Environment.NewLine + parameterizedItem.GetType() +
73              ": public property " + parameter.Name + "Parameter generic type argument does not match the generic type argument of the parameter.");
74
75          if (valueProperty == null)
76            TestContext.WriteLine(parameterizedItem.GetType() + ": public property " + parameter.Name + " is missing.");
77          else if (valueProperty.PropertyType != parameterGenericTypeArgument) {
78            TestContext.WriteLine(parameterizedItem.GetType() + ": " + parameter.Name + " property type does not match the generic type argument of the parameter.");
79          } else if (!valueProperty.CanRead || !valueProperty.CanWrite)
80            TestContext.WriteLine(parameterizedItem.GetType() + ": public property " + parameter.Name + " must have a getter and a setter.");
81        }
82      }
83      Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
84    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.