Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Persistence/UnitTests/StorableAttributeTests.cs @ 4593

Last change on this file since 4593 was 1960, checked in by epitzer, 16 years ago

Refactoring of storables. (#603)

File size: 2.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using HeuristicLab.Persistence.Core;
4using Microsoft.VisualStudio.TestTools.UnitTesting;
5using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
6using System.Reflection;
7
8namespace HeuristicLab.Persistence.Test {
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
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
52  [TestClass]
53  public class AttributeTest {
54
55    [TestMethod]
56    public void SimpleStorableAttributeTest() {
57      DemoClass t = new DemoClass();
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);
62      Assert.IsTrue(accessors.ContainsKey("TestProperty"));
63      Assert.IsTrue(accessors.ContainsKey("x"));
64      Assert.IsFalse(accessors.ContainsKey("y"));
65      object o = new object();
66      t.o = o;
67      Assert.AreSame(accessors["TestProperty"].Get(), o);
68      t.x = 12;
69      Assert.AreEqual(accessors["x"].Get(), 12);
70      t.y = 312890;
71      accessors["TestProperty"].Set(null);
72      Assert.IsNull(t.o);
73      accessors["x"].Set(123);
74      Assert.AreEqual(t.x, 123);
75    }
76
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
83        accessDict.Add(accessor.Name, accessor);
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
90  }
91
92}
Note: See TracBrowser for help on using the repository browser.