|
8 | 8 |
|
9 | 9 | import unittest
|
10 | 10 |
|
11 |
| -from iptest import IronPythonTestCase, big, run_test, skipUnlessIronPython |
| 11 | +from iptest import IronPythonTestCase, big, is_mono, run_test, skipUnlessIronPython |
12 | 12 |
|
13 | 13 | @skipUnlessIronPython()
|
14 | 14 | class IronMathTest(IronPythonTestCase):
|
15 | 15 | def setUp(self):
|
16 | 16 | super(IronMathTest, self).setUp()
|
17 | 17 |
|
18 |
| - import clr |
| 18 | + import clr, System |
19 | 19 | from System import IFormatProvider
|
20 | 20 | class myFormatProvider(IFormatProvider):
|
21 |
| - def ToString():pass |
| 21 | + def ToString(): |
| 22 | + pass |
| 23 | + def GetFormat(self, formatType): |
| 24 | + return System.Globalization.NumberFormatInfo.InvariantInfo |
22 | 25 |
|
23 | 26 | self.p = myFormatProvider()
|
24 | 27 |
|
@@ -247,6 +250,51 @@ def CheckDwordConversions(bigint, dwords):
|
247 | 250 | CheckDwordConversions(big(1<<31) + 9, [0x80000009])
|
248 | 251 | CheckDwordConversions(big(1<<32), [0x00000000, 0x00000001])
|
249 | 252 |
|
| 253 | + def test_to_type_conversions(self): |
| 254 | + from System import Decimal, Double, Single |
| 255 | + from System import Int64, UInt64, Int32, UInt32, Int16, UInt16, Byte, SByte |
| 256 | + from System import Boolean, Char, DateTime, Object, Enum, DateTimeKind |
| 257 | + |
| 258 | + val = 1 |
| 259 | + for i in [big(val), Int32(val)]: |
| 260 | + self.assertEqual(i.ToDecimal(self.p), val) |
| 261 | + self.assertIsInstance(i.ToDecimal(self.p), Decimal) |
| 262 | + self.assertEqual(i.ToDouble(self.p), val) |
| 263 | + self.assertIsInstance(i.ToDouble(self.p), Double) |
| 264 | + self.assertEqual(i.ToSingle(self.p), val) |
| 265 | + self.assertIsInstance(i.ToSingle(self.p), Single) |
| 266 | + self.assertEqual(i.ToInt64(self.p), val) |
| 267 | + self.assertIsInstance(i.ToInt64(self.p), Int64) |
| 268 | + self.assertEqual(i.ToUInt64(self.p), val) |
| 269 | + self.assertIsInstance(i.ToUInt64(self.p), UInt64) |
| 270 | + self.assertEqual(i.ToInt32(self.p), val) |
| 271 | + self.assertIsInstance(i.ToInt32(self.p), Int32) |
| 272 | + self.assertEqual(i.ToUInt32(self.p), val) |
| 273 | + self.assertIsInstance(i.ToUInt32(self.p), UInt32) |
| 274 | + self.assertEqual(i.ToInt16(self.p), val) |
| 275 | + self.assertIsInstance(i.ToInt16(self.p), Int16) |
| 276 | + self.assertEqual(i.ToUInt16(self.p), val) |
| 277 | + self.assertIsInstance(i.ToUInt16(self.p), UInt16) |
| 278 | + self.assertEqual(i.ToByte(self.p), val) |
| 279 | + self.assertIsInstance(i.ToByte(self.p), Byte) |
| 280 | + self.assertEqual(i.ToSByte(self.p), val) |
| 281 | + self.assertIsInstance(i.ToSByte(self.p), SByte) |
| 282 | + self.assertEqual(i.ToBoolean(self.p), val) |
| 283 | + self.assertIsInstance(i.ToBoolean(self.p), Boolean) |
| 284 | + self.assertEqual(i.ToChar(self.p), Char(val)) |
| 285 | + self.assertEqual(i.ToString(self.p), str(val)) |
| 286 | + self.assertRaisesRegex(TypeError, r"Invalid cast from '\w+' to 'DateTime'", i.ToDateTime, self.p) |
| 287 | + |
| 288 | + for t in [Decimal, Double, Single, Int64, UInt64, Int32, UInt32, Int16, UInt16, Byte, SByte, Boolean, Char, str]: |
| 289 | + self.assertEqual(i.ToType(t, self.p), t(i)) |
| 290 | + |
| 291 | + self.assertEqual(i.ToType(Object, self.p), i) |
| 292 | + self.assertIsInstance(i.ToType(Object, self.p), Object) |
| 293 | + self.assertRaisesRegex(TypeError, r"Invalid cast from '\w+' to 'DateTime'", i.ToType, DateTime, self.p) |
| 294 | + self.assertRaisesRegex(TypeError, r"Invalid cast from '[\w.]+' to 'System.DateTimeKind'\.", i.ToType, DateTimeKind, self.p) |
| 295 | + if not is_mono: |
| 296 | + self.assertRaisesRegex(TypeError, r"Unable to cast object of type '[\w.]+' to type 'System.Enum'\.", i.ToType, Enum, self.p) |
| 297 | + |
250 | 298 | def test_misc(self):
|
251 | 299 | from System import ArgumentException, ArgumentNullException
|
252 | 300 | from System.Numerics import BigInteger
|
|
0 commit comments