Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/UnitTests/StorableAttributeTests.cs @ 1956

Last change on this file since 1956 was 1938, checked in by epitzer, 15 years ago

Resolve name clashes of overridden and shadowed properties (#659)

File size: 2.4 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      Dictionary<string, DataMemberAccessor> accessors = StorableAttribute.GetStorableAccessors(t);
59      Assert.IsTrue(accessors.ContainsKey("TestProperty"));
60      Assert.IsTrue(accessors.ContainsKey("x"));
61      Assert.IsFalse(accessors.ContainsKey("y"));
62      object o = new object();
63      t.o = o;
64      Assert.AreSame(accessors["TestProperty"].Get(), o);
65      t.x = 12;
66      Assert.AreEqual(accessors["x"].Get(), 12);
67      t.y = 312890;
68      accessors["TestProperty"].Set(null);
69      Assert.IsNull(t.o);
70      accessors["x"].Set(123);
71      Assert.AreEqual(t.x, 123);
72    }
73
74    [TestMethod]
75    public void TestInheritance() {
76      New n = new New();
77      var accessors = StorableAttribute.GetStorableAccessors(n);
78      var accessDict = new Dictionary<string, DataMemberAccessor>();
79      foreach (var accessor in accessors) // assert uniqueness
80        accessDict.Add(accessor.Key, accessor.Value);
81      Assert.IsTrue(accessDict.ContainsKey(typeof(New).FullName + ".Name"));
82      Assert.IsTrue(accessDict.ContainsKey(typeof(Override).FullName + ".Name"));
83      Assert.AreEqual("New", accessDict[typeof(New).FullName + ".Name"].Get());
84      Assert.AreEqual("Override", accessDict[typeof(Override).FullName + ".Name"].Get());
85    }
86
87  }
88
89}
Note: See TracBrowser for help on using the repository browser.