[3742] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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 |
|
---|
| 22 | using System;
|
---|
[1614] | 23 | using System.Collections.Generic;
|
---|
| 24 | using HeuristicLab.Persistence.Core;
|
---|
| 25 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
[1823] | 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[1938] | 27 | using System.Reflection;
|
---|
[1614] | 28 |
|
---|
[2951] | 29 | namespace HeuristicLab.Persistence_33.Tests {
|
---|
[1614] | 30 |
|
---|
[3017] | 31 | [StorableClass]
|
---|
[1614] | 32 | class DemoClass {
|
---|
| 33 |
|
---|
| 34 | [Storable(Name = "TestProperty", DefaultValue = 12)]
|
---|
[3057] | 35 | public object o = null;
|
---|
[1614] | 36 |
|
---|
| 37 | [Storable]
|
---|
| 38 | public int x = 2;
|
---|
| 39 |
|
---|
[3057] | 40 | public int y = 0;
|
---|
[1614] | 41 | }
|
---|
| 42 |
|
---|
[3017] | 43 | [StorableClass]
|
---|
[1938] | 44 | class Base {
|
---|
| 45 | public string baseName;
|
---|
| 46 | [Storable]
|
---|
| 47 | public virtual string Name {
|
---|
| 48 | get { return "Base"; }
|
---|
| 49 | set { baseName = value; }
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[3017] | 53 | [StorableClass]
|
---|
[1938] | 54 | class Override : Base {
|
---|
| 55 | [Storable]
|
---|
| 56 | public override string Name {
|
---|
| 57 | get { return "Override"; }
|
---|
| 58 | set { base.Name = value; }
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[3017] | 62 | [StorableClass]
|
---|
[1938] | 63 | class Intermediate : Override {
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[3017] | 66 | [StorableClass]
|
---|
[1938] | 67 | class New : Intermediate {
|
---|
| 68 | public string newName;
|
---|
| 69 | [Storable]
|
---|
| 70 | public new string Name {
|
---|
| 71 | get { return "New"; }
|
---|
| 72 | set { newName = value; }
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[3025] | 76 | /* [TestClass]
|
---|
[1614] | 77 | public class AttributeTest {
|
---|
| 78 |
|
---|
| 79 | [TestMethod]
|
---|
| 80 | public void SimpleStorableAttributeTest() {
|
---|
| 81 | DemoClass t = new DemoClass();
|
---|
[1960] | 82 | IEnumerable<DataMemberAccessor> accessorList = StorableAttribute.GetStorableAccessors(t);
|
---|
| 83 | Dictionary<string, DataMemberAccessor> accessors = new Dictionary<string, DataMemberAccessor>();
|
---|
| 84 | foreach (var a in accessorList)
|
---|
| 85 | accessors.Add(a.Name, a);
|
---|
[1938] | 86 | Assert.IsTrue(accessors.ContainsKey("TestProperty"));
|
---|
[1614] | 87 | Assert.IsTrue(accessors.ContainsKey("x"));
|
---|
| 88 | Assert.IsFalse(accessors.ContainsKey("y"));
|
---|
| 89 | object o = new object();
|
---|
| 90 | t.o = o;
|
---|
[1938] | 91 | Assert.AreSame(accessors["TestProperty"].Get(), o);
|
---|
[1614] | 92 | t.x = 12;
|
---|
| 93 | Assert.AreEqual(accessors["x"].Get(), 12);
|
---|
| 94 | t.y = 312890;
|
---|
[1938] | 95 | accessors["TestProperty"].Set(null);
|
---|
[1614] | 96 | Assert.IsNull(t.o);
|
---|
| 97 | accessors["x"].Set(123);
|
---|
| 98 | Assert.AreEqual(t.x, 123);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[1938] | 101 | [TestMethod]
|
---|
| 102 | public void TestInheritance() {
|
---|
| 103 | New n = new New();
|
---|
| 104 | var accessors = StorableAttribute.GetStorableAccessors(n);
|
---|
| 105 | var accessDict = new Dictionary<string, DataMemberAccessor>();
|
---|
| 106 | foreach (var accessor in accessors) // assert uniqueness
|
---|
[1960] | 107 | accessDict.Add(accessor.Name, accessor);
|
---|
[1938] | 108 | Assert.IsTrue(accessDict.ContainsKey(typeof(New).FullName + ".Name"));
|
---|
| 109 | Assert.IsTrue(accessDict.ContainsKey(typeof(Override).FullName + ".Name"));
|
---|
| 110 | Assert.AreEqual("New", accessDict[typeof(New).FullName + ".Name"].Get());
|
---|
| 111 | Assert.AreEqual("Override", accessDict[typeof(Override).FullName + ".Name"].Get());
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[3025] | 114 | }*/
|
---|
[1614] | 115 |
|
---|
| 116 | }
|
---|