Mercurial > pub > bltoolkit
comparison UnitTests/CS/Common/ConvertTest.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 #if ORACLE | |
3 using Oracle.DataAccess.Types; | |
4 #endif | |
5 using System.Reflection; | |
6 using BLToolkit.Common; | |
7 using BLToolkit.Reflection; | |
8 | |
9 using NUnit.Framework; | |
10 | |
11 using Convert = BLToolkit.Common.Convert; | |
12 | |
13 namespace Common | |
14 { | |
15 [TestFixture] | |
16 public class ConvertTest | |
17 { | |
18 [Test] | |
19 public void StringTest() | |
20 { | |
21 string testStr = "123"; | |
22 | |
23 Assert.AreEqual(123, Convert.ToSByte(testStr)); | |
24 Assert.AreEqual(123, Convert.ToInt16(testStr)); | |
25 Assert.AreEqual(123, Convert.ToInt32(testStr)); | |
26 Assert.AreEqual(123, Convert.ToInt64(testStr)); | |
27 | |
28 Assert.AreEqual(123, Convert.ToByte(testStr)); | |
29 Assert.AreEqual(123, Convert.ToUInt16(testStr)); | |
30 Assert.AreEqual(123, Convert.ToUInt32(testStr)); | |
31 Assert.AreEqual(123, Convert.ToUInt64(testStr)); | |
32 | |
33 Assert.AreEqual(123.0m, Convert.ToDecimal(testStr)); | |
34 Assert.AreEqual(123.0f, Convert.ToSingle(testStr)); | |
35 Assert.AreEqual(123.0, Convert.ToDouble(testStr)); | |
36 | |
37 string zeroStr = "0"; | |
38 | |
39 Assert.AreEqual(0, Convert.ToSByte((string)null)); | |
40 Assert.AreEqual(0, Convert.ToSByte(zeroStr)); | |
41 Assert.AreEqual(0, Convert.ToInt16(zeroStr)); | |
42 Assert.AreEqual(0, Convert.ToInt32(zeroStr)); | |
43 Assert.AreEqual(0, Convert.ToInt64(zeroStr)); | |
44 | |
45 Assert.AreEqual(0, Convert.ToByte(zeroStr)); | |
46 Assert.AreEqual(0, Convert.ToUInt16(zeroStr)); | |
47 Assert.AreEqual(0, Convert.ToUInt32(zeroStr)); | |
48 Assert.AreEqual(0, Convert.ToUInt64(zeroStr)); | |
49 | |
50 Assert.AreEqual(0.0m, Convert.ToDecimal(zeroStr)); | |
51 Assert.AreEqual(0.0f, Convert.ToSingle(zeroStr)); | |
52 Assert.AreEqual(0.0, Convert.ToDouble(zeroStr)); | |
53 | |
54 Assert.IsTrue(Convert.ToBoolean("True")); | |
55 Assert.IsTrue(Convert.ToBoolean("true")); | |
56 | |
57 Assert.IsFalse(Convert.ToBoolean("false")); | |
58 Assert.IsFalse(Convert.ToBoolean("FALSE")); | |
59 Assert.IsFalse(Convert.ToBoolean((string)null)); | |
60 | |
61 Assert.AreEqual('T', Convert.ToChar("T")); | |
62 Assert.AreEqual(0, Convert.ToChar((string)null)); | |
63 | |
64 Assert.AreEqual(DateTime.Today, Convert.ToDateTime(DateTime.Today.ToString())); | |
65 Assert.AreEqual(DateTime.MinValue, Convert.ToDateTime((string)null)); | |
66 | |
67 Assert.AreEqual(TimeSpan.FromDays(123.0), Convert.ToTimeSpan("123")); | |
68 Assert.AreEqual(TimeSpan.MinValue, Convert.ToTimeSpan((string)null)); | |
69 | |
70 Assert.AreEqual(typeof(int).GUID, Convert.ToGuid(typeof(int).GUID.ToString())); | |
71 Assert.AreEqual(Guid.Empty, Convert.ToGuid((string)null)); | |
72 | |
73 Assert.AreEqual(typeof(int), Convert.ToType("System.Int32, mscorlib")); | |
74 Assert.IsNull(Convert.ToType((string)null)); | |
75 } | |
76 | |
77 private static readonly Type[] NumericTypes = | |
78 { | |
79 typeof(SByte), | |
80 typeof(Byte), | |
81 typeof(Int16), | |
82 typeof(UInt16), | |
83 typeof(Int32), | |
84 typeof(UInt32), | |
85 typeof(Int64), | |
86 typeof(UInt64), | |
87 typeof(Single), | |
88 typeof(Double), | |
89 typeof(Decimal), | |
90 }; | |
91 | |
92 private const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Static; | |
93 | |
94 [Test] | |
95 public void NumericTest() | |
96 { | |
97 TypeHelper helper = new TypeHelper(typeof(Convert)); | |
98 IConvertible src = 123; | |
99 | |
100 // All types from SByte to Decimal can convert to each other | |
101 // | |
102 for (int from = 0; from < NumericTypes.Length; ++from) | |
103 { | |
104 Type typeFrom = NumericTypes[from]; | |
105 object test = src.ToType(typeFrom, null); | |
106 | |
107 for (int to = 0; to < NumericTypes.Length; ++to) | |
108 { | |
109 if (from == to) | |
110 continue; | |
111 | |
112 Type typeTo = NumericTypes[to]; | |
113 MethodInfo mi = helper.GetMethod("To" + typeTo.Name, bindingFlags, typeFrom); | |
114 | |
115 Assert.IsNotNull(mi, string.Format("Missed To{0}({1})", typeTo.Name, typeFrom.Name)); | |
116 Assert.AreEqual(123, mi.Invoke(null, new object[] { test })); | |
117 } | |
118 } | |
119 } | |
120 | |
121 [Test] | |
122 public void DateTimeTest() | |
123 { | |
124 Assert.AreEqual(DateTime.MinValue + TimeSpan.FromDays(1), Convert.ToDateTime(1.0)); | |
125 Assert.AreEqual(TimeSpan.FromDays(1), Convert.ToTimeSpan(1.0)); | |
126 | |
127 Assert.AreEqual(DateTime.MinValue + TimeSpan.FromTicks(1), Convert.ToDateTime(1L)); | |
128 Assert.AreEqual(TimeSpan.FromTicks(1), Convert.ToTimeSpan(1L)); | |
129 } | |
130 | |
131 [Test] | |
132 public void InterfaceTest() | |
133 { | |
134 Assert.AreEqual(123, ConvertTo<IConvertible>.From(123).ToByte(null)); | |
135 } | |
136 | |
137 [Test] | |
138 public void EnumTest() | |
139 { | |
140 // Enum-to-enum. | |
141 // | |
142 Assert.AreEqual(BindingFlags.DeclaredOnly, | |
143 ConvertTo<BindingFlags>.From(CallingConventions.VarArgs)); | |
144 | |
145 // Enum-to-nullable byte. | |
146 // | |
147 Assert.AreEqual((byte?)BindingFlags.DeclaredOnly, | |
148 ConvertTo<byte?>.From(BindingFlags.DeclaredOnly)); | |
149 | |
150 // Nullable Enum-to-nullable enum. | |
151 // | |
152 Assert.AreEqual((CallingConventions?)CallingConventions.VarArgs, | |
153 ConvertTo<CallingConventions?>.From((BindingFlags?)BindingFlags.DeclaredOnly)); | |
154 | |
155 // Integer-to-enum. | |
156 // | |
157 Assert.AreEqual(BindingFlags.DeclaredOnly, | |
158 ConvertTo<BindingFlags>.From((int)BindingFlags.DeclaredOnly)); | |
159 | |
160 // Integer-to-nullable enum. | |
161 // | |
162 Assert.AreEqual(BindingFlags.DeclaredOnly, | |
163 ConvertTo<BindingFlags?>.From((int)BindingFlags.DeclaredOnly)); | |
164 | |
165 } | |
166 | |
167 [Test] | |
168 public void ByteArrayTest() | |
169 { | |
170 Byte[] bytes; | |
171 object value; | |
172 | |
173 bytes = new byte[] {123}; | |
174 value = (byte)123; | |
175 Assert.AreEqual(bytes, Convert.ToByteArray(value)); | |
176 Assert.AreEqual(value, Convert.ToByte(bytes)); | |
177 | |
178 bytes = new byte[] {123, 0}; | |
179 value = (short)123; | |
180 Assert.AreEqual(bytes, Convert.ToByteArray(value)); | |
181 Assert.AreEqual(value, Convert.ToInt16(bytes)); | |
182 | |
183 bytes = new byte[] {210, 2, 150, 73}; | |
184 value = 1234567890; | |
185 Assert.AreEqual(bytes, Convert.ToByteArray(value)); | |
186 Assert.AreEqual(value, Convert.ToInt32(bytes)); | |
187 | |
188 bytes = new byte[] {(byte) 't', (byte) 's', (byte) 't'}; | |
189 value = "tst"; | |
190 Assert.AreEqual(bytes, Convert.ToByteArray(value)); | |
191 Assert.AreEqual(value, Convert.ToString(bytes)); | |
192 | |
193 bytes = new byte[] {12, 0, 0, 0, 0, 0, 0, 0, 192, 0, 0, 0, 0, 0, 0, 70}; | |
194 value = typeof(System.Runtime.InteropServices.ComTypes.IStream).GUID; | |
195 Assert.AreEqual(bytes, Convert.ToByteArray(value)); | |
196 Assert.AreEqual(value, Convert.ToGuid(bytes)); | |
197 | |
198 bytes = new byte[] {210, 10, 31, 235, 140, 169, 84, 171, 0, 0, 0, 0, 0, 0, 0, 0}; | |
199 value = 12345678901234567890m; | |
200 Assert.AreEqual(bytes, Convert.ToByteArray(value)); | |
201 Assert.AreEqual(value, Convert.ToDecimal(bytes)); | |
202 | |
203 bytes = new byte[] {121, 233, 246, 66}; | |
204 value = 123.456f; | |
205 Assert.AreEqual(bytes, Convert.ToByteArray(value)); | |
206 Assert.AreEqual(value, Convert.ToSingle(bytes)); | |
207 | |
208 bytes = new byte[] {119, 190, 159, 26, 47, 221, 94, 64}; | |
209 value = 123.456; | |
210 Assert.AreEqual(bytes, Convert.ToByteArray(value)); | |
211 Assert.AreEqual(value, Convert.ToDouble(bytes)); | |
212 } | |
213 | |
214 [Test] | |
215 public void ConvertT() | |
216 { | |
217 decimal d = ConvertTo<decimal>.From(123); | |
218 Assert.AreEqual(123.0m, d); | |
219 } | |
220 | |
221 #if ORACLE | |
222 [Test] | |
223 public void TypeCast() | |
224 { | |
225 // Note that method BLToolkit.Common.Convert.ToDecimal(OracleNumber p) does not exist. | |
226 // | |
227 decimal d = ConvertTo<decimal>.From(new OracleDecimal(123)); | |
228 Assert.AreEqual(123.0m, d); | |
229 | |
230 // Method BLToolkit.Common.Convert.ToOracleString(string p) does not exist too. | |
231 // | |
232 OracleString s = ConvertTo<OracleString>.From("test"); | |
233 Assert.AreEqual("test", s.Value); | |
234 } | |
235 #endif | |
236 } | |
237 } |