[8121] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8121] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using System.Reflection;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.PluginInfrastructure;
|
---|
| 28 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 29 |
|
---|
| 30 | namespace 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) {
|
---|
[8154] | 58 | var parameterType = parameter.GetType().GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IConstrainedValueParameter<>));
|
---|
| 59 | if (parameterType == null) continue;
|
---|
[8121] | 60 |
|
---|
[8154] | 61 | var parameterGenericTypeArgument = parameterType.GetGenericArguments().First();
|
---|
| 62 | var parameterGenericTypeDefinition = typeof(IConstrainedValueParameter<>);
|
---|
[8121] | 63 |
|
---|
[8154] | 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.");
|
---|
[8121] | 85 | }
|
---|
| 86 | }
|
---|
| 87 | Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | }
|
---|