comparison UnitTests/CS/TypeBuilder/GetSetValueAttributeTest.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Reflection;
3
4 using NUnit.Framework;
5
6 using BLToolkit.Reflection;
7 using BLToolkit.TypeBuilder;
8
9 namespace TypeBuilder
10 {
11 [TestFixture]
12 public class GetSetValueAttributeTest
13 {
14 public class Value
15 {
16 public Value(int value)
17 {
18 IntValue = value;
19 }
20
21 [SetValue, GetValue] public int IntValue;
22
23 public Value(float value)
24 {
25 FloatValue = value;
26 }
27
28 [SetValue, GetValue] public float FloatValue;
29
30 public Value(string value)
31 {
32 StrValue = value;
33 }
34
35 object _value;
36
37 [SetValue, GetValue]
38 public string StrValue
39 {
40 get { return (string)_value; }
41 set { _value = value; }
42 }
43
44 public Value(DayOfWeek value)
45 {
46 DayValue = value;
47 }
48
49 [SetValue, GetValue]
50 public DayOfWeek DayValue
51 {
52 get { return (DayOfWeek)_value; }
53 set { _value = value; }
54 }
55 }
56
57 public abstract class TestObject1
58 {
59 [InstanceType(typeof(Value), 55)] public abstract int IntValue { get; set; }
60 [InstanceType(typeof(Value), (float)16)] public abstract float FloatValue { get; set; }
61 [InstanceType(typeof(Value), "test1")] public abstract string StrValue { get; set; }
62
63 [InstanceType(typeof(Value), DayOfWeek.Saturday)]
64 public abstract DayOfWeek DayValue { get; set; }
65 }
66
67 [Test]
68 public void Test()
69 {
70 TestObject1 o = (TestObject1)TypeAccessor.CreateInstance(typeof(TestObject1));
71
72 Assert.AreEqual(55, o.IntValue);
73 o.IntValue += 1;
74 Assert.AreEqual(56, o.IntValue);
75
76 Assert.AreEqual(16, o.FloatValue);
77 o.FloatValue += 1;
78 Assert.AreEqual(17, o.FloatValue);
79
80 Assert.AreEqual("test1", o.StrValue);
81 o.StrValue = "test2";
82 Assert.AreEqual("test2", o.StrValue);
83
84 Assert.AreEqual(DayOfWeek.Saturday, o.DayValue);
85 o.DayValue = DayOfWeek.Thursday;
86 Assert.AreEqual(DayOfWeek.Thursday, o.DayValue);
87 }
88
89 public struct ValueBox<T>
90 {
91 public ValueBox(InitContext ctx)
92 {
93 _value = (T)ctx.MemberParameters[0];
94 }
95
96 private T _value;
97
98 [GetValue] public T GetValue() { return _value; }
99 [SetValue] public void SetValue(T value) { _value = value; }
100 }
101
102 public struct ValueBox2<T>
103 {
104 private T _value;
105
106 [GetValue] public T GetValue([Parent] TestObject2 parent) { return _value; }
107 [SetValue] public void SetValue(T value, [Parent] object parent) { _value = value; }
108 }
109
110 public struct ValueBox3<T>
111 {
112 private T _value;
113
114 [GetValue] public T GetValue([Parent] object parent, [PropertyInfo] PropertyInfo pi) { return _value; }
115 [SetValue] public void SetValue(T value, [PropertyInfo] PropertyInfo pi) { _value = value; }
116 }
117
118 public abstract class TestObject2
119 {
120 [InstanceType(typeof(ValueBox<int>), 27)]
121 public abstract int IntValue { get; set; }
122 [InstanceType(typeof(ValueBox2<float>))]
123 public abstract float FloatValue { get; set; }
124 [InstanceType(typeof(ValueBox3<string>))]
125 public abstract string StrValue { get; set; }
126 }
127
128 [Test]
129 public void MethodTest()
130 {
131 TestObject2 o = TypeAccessor.CreateInstance<TestObject2>();
132
133 Assert.AreEqual(27, o.IntValue);
134 o.IntValue += 8;
135 Assert.AreEqual(35, o.IntValue);
136
137 o.FloatValue = 0.1f;
138 o.FloatValue *= 2.0f;
139 Assert.AreEqual(0.2f, o.FloatValue);
140
141 o.StrValue = "foo";
142 o.StrValue += "Bar";
143 Assert.AreEqual("fooBar", o.StrValue);
144 }
145
146 }
147 }