Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/25/15 13:59:23 (8 years ago)
Author:
pfleck
Message:

#2525

  • Changed unit test that it also lists types that have a StorableConstructor but are not marked as StorableClass.
  • Put both storable tests in a single StorableTest.cs.
  • Added missing StorableClassAttributes.
Location:
trunk/sources/HeuristicLab.Tests/HeuristicLab-3.3
Files:
1 deleted
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Tests/HeuristicLab-3.3/StorableTest.cs

    r13391 r13395  
    2121
    2222using System;
    23 using System.Collections;
    2423using System.Collections.Generic;
    2524using System.Linq;
    2625using System.Reflection;
    2726using System.Text;
     27using HeuristicLab.Common;
    2828using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2929using HeuristicLab.PluginInfrastructure;
     
    3232namespace HeuristicLab.Tests {
    3333  [TestClass]
    34   public class StorableConstructorTest {
     34  public class StorableTest {
    3535    [TestMethod]
    3636    [TestCategory("General")]
     
    5151        else {
    5252          if (storableType.IsSealed && !storableConstructor.IsPrivate)
    53             errorMessage.Append(Environment.NewLine + storableType.ToString() + ": Storable constructor must be private in sealed classes.");
     53            errorMessage.Append(Environment.NewLine + storableType.Namespace + "." + storableType.GetPrettyName() + ": Storable constructor must be private in sealed classes.");
    5454          else if (!storableType.IsSealed && !(storableConstructor.IsFamily || storableConstructor.IsPublic))
    55             errorMessage.Append(Environment.NewLine + storableType.ToString() + ": Storable constructor must be protected (can be public in rare cases).");
     55            errorMessage.Append(Environment.NewLine + storableType.Namespace + "." + storableType.GetPrettyName() + ": Storable constructor must be protected (can be public in rare cases).");
     56        }
     57      }
     58      Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
     59    }
     60
     61    [TestMethod]
     62    [TestCategory("General")]
     63    [TestCategory("Essential")]
     64    [TestProperty("Time", "short")]
     65    public void TestStorableClass() {
     66      var errorMessage = new StringBuilder();
     67
     68      foreach (var type in ApplicationManager.Manager.GetTypes(typeof(object), onlyInstantiable: false, includeGenericTypeDefinitions: true)
     69        .Where(t => t.Namespace != null && !t.Namespace.Contains(".Tests"))
     70        .Where(t => !StorableClassAttribute.IsStorableClass(t))) {
     71        var members = type.GetMembers(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly);
     72        var storableConstructor = members.SingleOrDefault(m => Attribute.IsDefined(m, typeof(StorableConstructorAttribute), inherit: false));
     73        var storableMembers = members.Where(m => Attribute.IsDefined(m, typeof(StorableAttribute), inherit: false));
     74
     75        if (storableConstructor != null) {
     76          errorMessage.Append(Environment.NewLine + type.Namespace + "." + type.GetPrettyName() + ": Contains a storable constructor but is not a storable class.");
     77        } else if (storableMembers.Any()) {
     78          errorMessage.Append(Environment.NewLine + type.Namespace + "." + type.GetPrettyName() + ": Contains at least one storable member but is not a storable class.");
    5679        }
    5780      }
Note: See TracChangeset for help on using the changeset viewer.