comparison UnitTests/CS/TypeBuilder/ParameterAttributeTest.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.Collections;
3 using System.Diagnostics.CodeAnalysis;
4 using BLToolkit.EditableObjects;
5 using BLToolkit.Mapping;
6 using BLToolkit.Reflection;
7 using BLToolkit.TypeBuilder;
8
9 using NUnit.Framework;
10
11 namespace TypeBuilder
12 {
13 [TestFixture]
14 public class ParameterAttributeTest
15 {
16 public abstract class AbstractObject
17 {
18 public AbstractObject(InitContext init)
19 {
20 if (init.MemberParameters != null && init.MemberParameters.Length == 1)
21 Field = (int)init.MemberParameters[0];
22 else
23 Field = 77;
24 }
25
26 public int Field;
27 }
28
29 public class InnerObject
30 {
31 public InnerObject(InitContext init)
32 {
33 if (init.MemberParameters != null && init.MemberParameters.Length == 1)
34 Field = (int)init.MemberParameters[0];
35 else
36 Field = 44;
37 }
38
39 public int Field;
40 }
41
42 public class TestField
43 {
44 public TestField()
45 {
46 Value = 10;
47 }
48
49 public TestField(int p1, float p2)
50 {
51 Value = p1 + (int)p2;
52 }
53
54 public TestField(TestField p1)
55 {
56 Value = 77;
57 }
58
59 public int Value;
60 }
61
62 [AttributeUsage(AttributeTargets.Property)]
63 public class TestParameterAttribute : ParameterAttribute
64 {
65 public TestParameterAttribute() : base(new TestField())
66 {
67 }
68 }
69
70 public abstract class TestObject1
71 {
72 [Parameter(10)] public abstract ArrayList List { get; set; }
73 [Parameter("t")] public abstract string Str { get; set; }
74 [Parameter(20)] public abstract string this[int i] { get; set; }
75 [Parameter(20, 30)] public abstract TestField Field1 { get; set; }
76 [TestParameter] public abstract TestField Field2 { get; set; }
77 [Parameter(55)] public abstract InnerObject InnerObject1 { get; set; }
78 [Parameter(54)] public abstract int? Int1 { get; set; }
79 [Parameter(null)] public abstract int? Int2 { get; set; }
80
81 [Parameter(2,2,2)] public abstract DateTime Date { get; set; }
82 [Parameter(222L)] public abstract Decimal Decimal1 { get; set; }
83 [Parameter(1, 0, 0, true, (byte)2)]
84 public abstract Decimal? Decimal2 { get; set; }
85 [Parameter(new int[]{2, 0, 0, 0})]
86 public abstract Decimal Decimal3 { get; set; }
87 [Parameter(22.05)] public abstract Decimal Decimal4 { get; set; }
88 [Parameter(null)] public abstract Decimal? Decimal5 { get; set; }
89 }
90
91 [Test]
92 public void ParamTest()
93 {
94 TestObject1 o = (TestObject1)TypeAccessor.CreateInstance(typeof(TestObject1));
95
96 Assert.That(o.List.Capacity, Is.EqualTo(10));
97 Assert.That(o.Str, Is.EqualTo("t"));
98 Assert.That(o.Field1.Value, Is.EqualTo(50));
99 Assert.That(o.Field2.Value, Is.EqualTo(77));
100 Assert.That(o.InnerObject1.Field, Is.EqualTo(55));
101 Assert.That(o.Int1, Is.EqualTo(54));
102 Assert.That(o.Int2, Is.EqualTo(null));
103 Assert.That(o.Date, Is.EqualTo(new DateTime(2,2,2)));
104 Assert.That(o.Decimal1, Is.EqualTo(222m));
105 Assert.That(o.Decimal2, Is.EqualTo(-0.01m));
106 Assert.That(o.Decimal3, Is.EqualTo(2m));
107 Assert.That(o.Decimal4, Is.EqualTo(22.05m));
108 Assert.That(o.Decimal5, Is.EqualTo(null));
109 }
110
111 public abstract class TestObject2 : EditableObject
112 {
113 protected TestObject2() {}
114 protected TestObject2(InitContext context) {}
115
116 [Parameter(10)] public abstract ArrayList List { get; set; }
117 [Parameter("t")] public abstract string Str { get; set; }
118 [Parameter(20)] public abstract string this[int i] { get; set; }
119 [Parameter(20, 30)] public abstract TestField Field1 { get; set; }
120 [TestParameter] public abstract TestField Field2 { get; set; }
121 [Parameter(55)] public abstract InnerObject InnerObject1 { get; set; }
122 [Parameter(54)] public abstract int? Int1 { get; set; }
123 [Parameter(null)] public abstract int? Int2 { get; set; }
124
125 [Parameter(2,2,2)] public abstract DateTime Date { get; set; }
126 [Parameter(222L)] public abstract Decimal Decimal1 { get; set; }
127 [Parameter(1, 0, 0, true, (byte)2)]
128 public abstract Decimal? Decimal2 { get; set; }
129 [Parameter(new int[]{2, 0, 0, 0})]
130 public abstract Decimal Decimal3 { get; set; }
131 [Parameter(22.05)] public abstract Decimal Decimal4 { get; set; }
132 [Parameter(null)] public abstract Decimal? Decimal5 { get; set; }
133 }
134
135 [Test]
136 public void EditablObjectParamTest()
137 {
138 TestObject2 o = (TestObject2)TypeAccessor.CreateInstance(typeof(TestObject2), new InitContext());
139
140 Assert.That(o.List.Capacity, Is.EqualTo(10));
141 Assert.That(o.Str, Is.EqualTo("t"));
142 Assert.That(o.Field1.Value, Is.EqualTo(50));
143 Assert.That(o.Field2.Value, Is.EqualTo(77));
144 Assert.That(o.InnerObject1.Field, Is.EqualTo(55));
145 Assert.That(o.Int1, Is.EqualTo(54));
146 Assert.That(o.Int2, Is.EqualTo(null));
147 Assert.That(o.Date, Is.EqualTo(new DateTime(2,2,2)));
148 Assert.That(o.Decimal1, Is.EqualTo(222m));
149 Assert.That(o.Decimal2, Is.EqualTo(-0.01m));
150 Assert.That(o.Decimal3, Is.EqualTo(2m));
151 Assert.That(o.Decimal4, Is.EqualTo(22.05m));
152 Assert.That(o.Decimal5, Is.EqualTo(null));
153 }
154 }
155 }