Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Tests/HeuristicLab-3.3/ParameterVisibilityTest.cs @ 11321

Last change on this file since 11321 was 8206, checked in by gkronber, 12 years ago

#1847: merged r8084:8205 from trunk into GP move operators branch

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