[1614] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using HeuristicLab.Persistence.Core;
|
---|
| 4 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
[1823] | 5 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[1938] | 6 | using System.Reflection;
|
---|
[1614] | 7 |
|
---|
[2951] | 8 | namespace HeuristicLab.Persistence_33.Tests {
|
---|
[1614] | 9 |
|
---|
| 10 | class DemoClass {
|
---|
| 11 |
|
---|
| 12 | [Storable(Name = "TestProperty", DefaultValue = 12)]
|
---|
| 13 | public object o;
|
---|
| 14 |
|
---|
| 15 | [Storable]
|
---|
| 16 | public int x = 2;
|
---|
| 17 |
|
---|
| 18 | public int y;
|
---|
| 19 |
|
---|
| 20 | }
|
---|
| 21 |
|
---|
[1938] | 22 | class Base {
|
---|
| 23 | public string baseName;
|
---|
| 24 | [Storable]
|
---|
| 25 | public virtual string Name {
|
---|
| 26 | get { return "Base"; }
|
---|
| 27 | set { baseName = value; }
|
---|
| 28 | }
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | class Override : Base {
|
---|
| 32 | [Storable]
|
---|
| 33 | public override string Name {
|
---|
| 34 | get { return "Override"; }
|
---|
| 35 | set { base.Name = value; }
|
---|
| 36 | }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | [EmptyStorableClass]
|
---|
| 40 | class Intermediate : Override {
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | class New : Intermediate {
|
---|
| 44 | public string newName;
|
---|
| 45 | [Storable]
|
---|
| 46 | public new string Name {
|
---|
| 47 | get { return "New"; }
|
---|
| 48 | set { newName = value; }
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[1614] | 52 | [TestClass]
|
---|
| 53 | public class AttributeTest {
|
---|
| 54 |
|
---|
| 55 | [TestMethod]
|
---|
| 56 | public void SimpleStorableAttributeTest() {
|
---|
| 57 | DemoClass t = new DemoClass();
|
---|
[1960] | 58 | IEnumerable<DataMemberAccessor> accessorList = StorableAttribute.GetStorableAccessors(t);
|
---|
| 59 | Dictionary<string, DataMemberAccessor> accessors = new Dictionary<string, DataMemberAccessor>();
|
---|
| 60 | foreach (var a in accessorList)
|
---|
| 61 | accessors.Add(a.Name, a);
|
---|
[1938] | 62 | Assert.IsTrue(accessors.ContainsKey("TestProperty"));
|
---|
[1614] | 63 | Assert.IsTrue(accessors.ContainsKey("x"));
|
---|
| 64 | Assert.IsFalse(accessors.ContainsKey("y"));
|
---|
| 65 | object o = new object();
|
---|
| 66 | t.o = o;
|
---|
[1938] | 67 | Assert.AreSame(accessors["TestProperty"].Get(), o);
|
---|
[1614] | 68 | t.x = 12;
|
---|
| 69 | Assert.AreEqual(accessors["x"].Get(), 12);
|
---|
| 70 | t.y = 312890;
|
---|
[1938] | 71 | accessors["TestProperty"].Set(null);
|
---|
[1614] | 72 | Assert.IsNull(t.o);
|
---|
| 73 | accessors["x"].Set(123);
|
---|
| 74 | Assert.AreEqual(t.x, 123);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[1938] | 77 | [TestMethod]
|
---|
| 78 | public void TestInheritance() {
|
---|
| 79 | New n = new New();
|
---|
| 80 | var accessors = StorableAttribute.GetStorableAccessors(n);
|
---|
| 81 | var accessDict = new Dictionary<string, DataMemberAccessor>();
|
---|
| 82 | foreach (var accessor in accessors) // assert uniqueness
|
---|
[1960] | 83 | accessDict.Add(accessor.Name, accessor);
|
---|
[1938] | 84 | Assert.IsTrue(accessDict.ContainsKey(typeof(New).FullName + ".Name"));
|
---|
| 85 | Assert.IsTrue(accessDict.ContainsKey(typeof(Override).FullName + ".Name"));
|
---|
| 86 | Assert.AreEqual("New", accessDict[typeof(New).FullName + ".Name"].Get());
|
---|
| 87 | Assert.AreEqual("Override", accessDict[typeof(Override).FullName + ".Name"].Get());
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[1614] | 90 | }
|
---|
| 91 |
|
---|
| 92 | }
|
---|