Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Tests/HeuristicLab-3.3/ParameterVisibilityTest.cs @ 17197

Last change on this file since 17197 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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    private TestContext testContextInstance;
34    public TestContext TestContext {
35      get { return testContextInstance; }
36      set { testContextInstance = value; }
37    }
38
39    [TestMethod]
40    [TestCategory("General")]
41    [TestCategory("Essential")]
42    [TestProperty("Time", "long")]
43    public void TestParameterVisibility() {
44      StringBuilder errorMessage = new StringBuilder();
45
46      foreach (var parameterizedItem in ApplicationManager.Manager.GetInstances<IParameterizedItem>()) {
47        foreach (var parameter in parameterizedItem.Parameters) {
48          var parameterType = parameter.GetType().GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IConstrainedValueParameter<>));
49          if (parameterType == null) continue;
50
51          var parameterGenericTypeArgument = parameterType.GetGenericArguments().First();
52          var parameterGenericTypeDefinition = typeof(IConstrainedValueParameter<>);
53
54          var paramProperty = parameterizedItem.GetType().GetProperty(parameter.Name + "Parameter",
55            BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);
56          var valueProperty = parameterizedItem.GetType().GetProperty(parameter.Name,
57                                                                 BindingFlags.GetProperty | BindingFlags.Instance |
58                                                                 BindingFlags.Public);
59          if (paramProperty == null)
60            errorMessage.Append(Environment.NewLine + parameterizedItem.GetType() +
61              ": public property " + parameter.Name + "Parameter is missing.");
62          else if (paramProperty.PropertyType.GetGenericTypeDefinition() != parameterGenericTypeDefinition)
63            errorMessage.Append(Environment.NewLine + parameterizedItem.GetType() +
64              ": public property " + parameter.Name + "Parameter type must be " + parameterGenericTypeDefinition.Name);
65          else if (paramProperty.PropertyType.GetGenericArguments().First() != parameterGenericTypeArgument)
66            errorMessage.Append(Environment.NewLine + parameterizedItem.GetType() +
67              ": public property " + parameter.Name + "Parameter generic type argument does not match the generic type argument of the parameter.");
68
69          if (valueProperty == null)
70            TestContext.WriteLine(parameterizedItem.GetType() + ": public property " + parameter.Name + " is missing.");
71          else if (valueProperty.PropertyType != parameterGenericTypeArgument) {
72            TestContext.WriteLine(parameterizedItem.GetType() + ": " + parameter.Name + " property type does not match the generic type argument of the parameter.");
73          } else if (!valueProperty.CanRead || !valueProperty.CanWrite)
74            TestContext.WriteLine(parameterizedItem.GetType() + ": public property " + parameter.Name + " must have a getter and a setter.");
75        }
76      }
77      Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
78    }
79  }
80}
Note: See TracBrowser for help on using the repository browser.