-
Notifications
You must be signed in to change notification settings - Fork 773
Expand file tree
/
Copy pathtest_conversion.py
More file actions
757 lines (520 loc) · 19.3 KB
/
test_conversion.py
File metadata and controls
757 lines (520 loc) · 19.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
"""Test CLR <-> Python type conversions."""
import operator
import pytest
import System
from Python.Test import ConversionTest, MethodResolutionInt, UnicodeString, CodecResetter
from Python.Runtime import PyObjectConversions
from Python.Runtime.Codecs import RawProxyEncoder
def test_bool_conversion():
"""Test bool conversion."""
ob = ConversionTest()
assert ob.BooleanField is False
assert ob.BooleanField == 0
ob.BooleanField = True
assert ob.BooleanField is True
assert ob.BooleanField == 1
ob.BooleanField = False
assert ob.BooleanField is False
assert ob.BooleanField == 0
with pytest.raises(TypeError):
ob.BooleanField = 1
with pytest.raises(TypeError):
ob.BooleanField = 0
with pytest.raises(TypeError):
ob.BooleanField = None
with pytest.raises(TypeError):
ob.BooleanField = ''
with pytest.raises(TypeError):
ob.BooleanField = System.Boolean(0)
with pytest.raises(TypeError):
ob.BooleanField = System.Boolean(1)
with pytest.raises(TypeError):
ob.BooleanField = System.Boolean('a')
def test_sbyte_conversion():
"""Test sbyte conversion."""
assert System.SByte.MaxValue == 127
assert System.SByte.MinValue == -128
ob = ConversionTest()
assert ob.SByteField == 0
ob.SByteField = 127
assert ob.SByteField == 127
ob.SByteField = -128
assert ob.SByteField == -128
ob.SByteField = System.SByte(127)
assert ob.SByteField == 127
ob.SByteField = System.SByte(-128)
assert ob.SByteField == -128
with pytest.raises(TypeError):
ConversionTest().SByteField = "spam"
with pytest.raises(TypeError):
ConversionTest().SByteField = None
with pytest.raises(OverflowError):
ConversionTest().SByteField = 128
with pytest.raises(OverflowError):
ConversionTest().SByteField = -129
with pytest.raises(OverflowError):
_ = System.SByte(128)
with pytest.raises(OverflowError):
_ = System.SByte(-129)
def test_byte_conversion():
"""Test byte conversion."""
assert System.Byte.MaxValue == 255
assert System.Byte.MinValue == 0
ob = ConversionTest()
assert ob.ByteField == 0
ob.ByteField = 255
assert ob.ByteField == 255
ob.ByteField = 0
assert ob.ByteField == 0
ob.ByteField = System.Byte(255)
assert ob.ByteField == 255
ob.ByteField = System.Byte(0)
assert ob.ByteField == 0
with pytest.raises(TypeError):
ConversionTest().ByteField = "spam"
with pytest.raises(TypeError):
ConversionTest().ByteField = None
with pytest.raises(OverflowError):
ConversionTest().ByteField = 256
with pytest.raises(OverflowError):
ConversionTest().ByteField = -1
with pytest.raises(OverflowError):
_ = System.Byte(256)
with pytest.raises(OverflowError):
_ = System.Byte(-1)
def test_char_conversion():
"""Test char conversion."""
assert System.Char.MaxValue == chr(65535)
assert System.Char.MinValue == chr(0)
ob = ConversionTest()
assert ob.CharField == u'A'
ob.CharField = 'B'
assert ob.CharField == u'B'
ob.CharField = u'B'
assert ob.CharField == u'B'
ob.CharField = 67
assert ob.CharField == u'C'
with pytest.raises(OverflowError):
ConversionTest().CharField = 65536
with pytest.raises(OverflowError):
ConversionTest().CharField = -1
with pytest.raises(TypeError):
ConversionTest().CharField = None
def test_int16_conversion():
"""Test int16 conversion."""
assert System.Int16.MaxValue == 32767
assert System.Int16.MinValue == -32768
ob = ConversionTest()
assert ob.Int16Field == 0
ob.Int16Field = 32767
assert ob.Int16Field == 32767
ob.Int16Field = -32768
assert ob.Int16Field == -32768
ob.Int16Field = System.Int16(32767)
assert ob.Int16Field == 32767
ob.Int16Field = System.Int16(-32768)
assert ob.Int16Field == -32768
with pytest.raises(TypeError):
ConversionTest().Int16Field = "spam"
with pytest.raises(TypeError):
ConversionTest().Int16Field = None
with pytest.raises(OverflowError):
ConversionTest().Int16Field = 32768
with pytest.raises(OverflowError):
ConversionTest().Int16Field = -32769
with pytest.raises(OverflowError):
_ = System.Int16(32768)
with pytest.raises(OverflowError):
_ = System.Int16(-32769)
def test_int32_conversion():
"""Test int32 conversion."""
assert System.Int32.MaxValue == 2147483647
assert System.Int32.MinValue == -2147483648
ob = ConversionTest()
assert ob.Int32Field == 0
ob.Int32Field = 2147483647
assert ob.Int32Field == 2147483647
ob.Int32Field = -2147483648
assert ob.Int32Field == -2147483648
ob.Int32Field = System.Int32(2147483647)
assert ob.Int32Field == 2147483647
ob.Int32Field = System.Int32(-2147483648)
assert ob.Int32Field == -2147483648
with pytest.raises(TypeError):
ConversionTest().Int32Field = "spam"
with pytest.raises(TypeError):
ConversionTest().Int32Field = None
with pytest.raises(OverflowError):
ConversionTest().Int32Field = 2147483648
with pytest.raises(OverflowError):
ConversionTest().Int32Field = -2147483649
with pytest.raises(OverflowError):
_ = System.Int32(2147483648)
with pytest.raises(OverflowError):
_ = System.Int32(-2147483649)
def test_int64_conversion():
"""Test int64 conversion."""
assert System.Int64.MaxValue == 9223372036854775807
assert System.Int64.MinValue == -9223372036854775808
ob = ConversionTest()
assert ob.Int64Field == 0
ob.Int64Field = 9223372036854775807
assert ob.Int64Field == 9223372036854775807
ob.Int64Field = -9223372036854775808
assert ob.Int64Field == -9223372036854775808
ob.Int64Field = System.Int64(9223372036854775807)
assert ob.Int64Field == 9223372036854775807
ob.Int64Field = System.Int64(-9223372036854775808)
assert ob.Int64Field == -9223372036854775808
with pytest.raises(TypeError):
ConversionTest().Int64Field = "spam"
with pytest.raises(TypeError):
ConversionTest().Int64Field = None
with pytest.raises(OverflowError):
ConversionTest().Int64Field = 9223372036854775808
with pytest.raises(OverflowError):
ConversionTest().Int64Field = -9223372036854775809
with pytest.raises(OverflowError):
_ = System.Int64(9223372036854775808)
with pytest.raises(OverflowError):
_ = System.Int64(-9223372036854775809)
def test_uint16_conversion():
"""Test uint16 conversion."""
assert System.UInt16.MaxValue == 65535
assert System.UInt16.MinValue == 0
ob = ConversionTest()
assert ob.UInt16Field == 0
ob.UInt16Field = 65535
assert ob.UInt16Field == 65535
ob.UInt16Field = -0
assert ob.UInt16Field == 0
ob.UInt16Field = System.UInt16(65535)
assert ob.UInt16Field == 65535
ob.UInt16Field = System.UInt16(0)
assert ob.UInt16Field == 0
with pytest.raises(TypeError):
ConversionTest().UInt16Field = "spam"
with pytest.raises(TypeError):
ConversionTest().UInt16Field = None
with pytest.raises(OverflowError):
ConversionTest().UInt16Field = 65536
with pytest.raises(OverflowError):
ConversionTest().UInt16Field = -1
with pytest.raises(OverflowError):
_ = System.UInt16(65536)
with pytest.raises(OverflowError):
_ = System.UInt16(-1)
def test_uint32_conversion():
"""Test uint32 conversion."""
assert System.UInt32.MaxValue == 4294967295
assert System.UInt32.MinValue == 0
ob = ConversionTest()
assert ob.UInt32Field == 0
ob.UInt32Field = 4294967295
assert ob.UInt32Field == 4294967295
ob.UInt32Field = -0
assert ob.UInt32Field == 0
ob.UInt32Field = System.UInt32(4294967295)
assert ob.UInt32Field == 4294967295
ob.UInt32Field = System.UInt32(0)
assert ob.UInt32Field == 0
with pytest.raises(TypeError):
ConversionTest().UInt32Field = "spam"
with pytest.raises(TypeError):
ConversionTest().UInt32Field = None
with pytest.raises(OverflowError):
ConversionTest().UInt32Field = 4294967296
with pytest.raises(OverflowError):
ConversionTest().UInt32Field = -1
with pytest.raises(OverflowError):
_ = System.UInt32(4294967296)
with pytest.raises(OverflowError):
_ = System.UInt32(-1)
def test_uint64_conversion():
"""Test uint64 conversion."""
assert System.UInt64.MaxValue == 18446744073709551615
assert System.UInt64.MinValue == 0
ob = ConversionTest()
assert ob.UInt64Field == 0
ob.UInt64Field = 18446744073709551615
assert ob.UInt64Field == 18446744073709551615
ob.UInt64Field = -0
assert ob.UInt64Field == 0
ob.UInt64Field = System.UInt64(18446744073709551615)
assert ob.UInt64Field == 18446744073709551615
ob.UInt64Field = System.UInt64(0)
assert ob.UInt64Field == 0
with pytest.raises(TypeError):
ConversionTest().UInt64Field = 0.5
with pytest.raises(TypeError):
ConversionTest().UInt64Field = "spam"
with pytest.raises(TypeError):
ConversionTest().UInt64Field = None
with pytest.raises(OverflowError):
ConversionTest().UInt64Field = 18446744073709551616
with pytest.raises(OverflowError):
ConversionTest().UInt64Field = -1
with pytest.raises(OverflowError):
_ = System.UInt64((18446744073709551616))
with pytest.raises(OverflowError):
_ = System.UInt64(-1)
def test_single_conversion():
"""Test single conversion."""
assert System.Single.MaxValue == pytest.approx(3.402823e38)
assert System.Single.MinValue == pytest.approx(-3.402823e38)
ob = ConversionTest()
assert ob.SingleField == 0.0
ob.SingleField = 3.402823e38
assert ob.SingleField == System.Single(3.402823e38)
ob.SingleField = -3.402823e38
assert ob.SingleField == System.Single(-3.402823e38)
with pytest.raises(TypeError):
ConversionTest().SingleField = "spam"
with pytest.raises(TypeError):
ConversionTest().SingleField = None
with pytest.raises(OverflowError):
ConversionTest().SingleField = 3.402824e38
with pytest.raises(OverflowError):
ConversionTest().SingleField = -3.402824e38
with pytest.raises(OverflowError):
_ = System.Single(3.402824e38)
with pytest.raises(OverflowError):
_ = System.Single(-3.402824e38)
def test_double_conversion():
"""Test double conversion."""
assert System.Double.MaxValue == 1.7976931348623157e308
assert System.Double.MinValue == -1.7976931348623157e308
ob = ConversionTest()
assert ob.DoubleField == 0.0
ob.DoubleField = 1.7976931348623157e308
assert ob.DoubleField == 1.7976931348623157e308
ob.DoubleField = -1.7976931348623157e308
assert ob.DoubleField == -1.7976931348623157e308
ob.DoubleField = System.Double(1.7976931348623157e308)
assert ob.DoubleField == 1.7976931348623157e308
ob.DoubleField = System.Double(-1.7976931348623157e308)
assert ob.DoubleField == -1.7976931348623157e308
with pytest.raises(TypeError):
ConversionTest().DoubleField = "spam"
with pytest.raises(TypeError):
ConversionTest().DoubleField = None
def test_decimal_conversion():
"""Test decimal conversion."""
from System import Decimal
max_d = Decimal.Parse("79228162514264337593543950335")
min_d = Decimal.Parse("-79228162514264337593543950335")
assert Decimal.ToInt64(Decimal(10)) == 10
ob = ConversionTest()
assert ob.DecimalField == Decimal(0)
ob.DecimalField = Decimal(10)
assert ob.DecimalField == Decimal(10)
ob.DecimalField = Decimal.One
assert ob.DecimalField == Decimal.One
ob.DecimalField = Decimal.Zero
assert ob.DecimalField == Decimal.Zero
ob.DecimalField = max_d
assert ob.DecimalField == max_d
ob.DecimalField = min_d
assert ob.DecimalField == min_d
with pytest.raises(TypeError):
ConversionTest().DecimalField = None
with pytest.raises(TypeError):
ConversionTest().DecimalField = "spam"
with pytest.raises(TypeError):
ConversionTest().DecimalField = 1
def test_string_conversion():
"""Test string / unicode conversion."""
ob = ConversionTest()
assert ob.StringField == "spam"
assert ob.StringField == u"spam"
ob.StringField = "eggs"
assert ob.StringField == "eggs"
assert ob.StringField == u"eggs"
ob.StringField = u"spam"
assert ob.StringField == "spam"
assert ob.StringField == u"spam"
ob.StringField = u'\uffff\uffff'
assert ob.StringField == u'\uffff\uffff'
ob.StringField = System.String("spam")
assert ob.StringField == "spam"
assert ob.StringField == u"spam"
ob.StringField = System.String(u'\uffff\uffff')
assert ob.StringField == u'\uffff\uffff'
ob.StringField = System.String("\ufeffbom")
assert ob.StringField == "\ufeffbom"
ob.StringField = None
assert ob.StringField is None
with pytest.raises(TypeError):
ConversionTest().StringField = 1
world = UnicodeString()
test_unicode_str = u"안녕"
assert test_unicode_str == str(world.value)
assert test_unicode_str == str(world.GetString())
assert test_unicode_str == str(world)
def test_interface_conversion():
"""Test interface conversion."""
from Python.Test import Spam, ISpam
ob = ConversionTest()
assert ISpam(ob.SpamField).GetValue() == "spam"
assert ob.SpamField.GetValue() == "spam"
ob.SpamField = Spam("eggs")
assert ISpam(ob.SpamField).GetValue() == "eggs"
assert ob.SpamField.GetValue() == "eggs"
# need to test spam subclass here.
ob.SpamField = None
assert ob.SpamField is None
with pytest.raises(TypeError):
ob = ConversionTest()
ob.SpamField = System.String("bad")
with pytest.raises(TypeError):
ob = ConversionTest()
ob.SpamField = System.Int32(1)
def test_object_conversion():
"""Test ob conversion."""
from Python.Test import Spam
ob = ConversionTest()
assert ob.ObjectField is None
ob.ObjectField = Spam("eggs")
assert ob.ObjectField.__class__.__name__ == "Spam"
assert ob.ObjectField.GetValue() == "eggs"
ob.ObjectField = None
assert ob.ObjectField is None
ob.ObjectField = System.String("spam")
assert ob.ObjectField == "spam"
ob.ObjectField = System.Int32(1)
assert ob.ObjectField == 1
# need to test subclass here
class Foo(object):
pass
ob.ObjectField = Foo
assert ob.ObjectField == Foo
class PseudoSeq:
def __getitem__(self, idx):
return 0
ob.ObjectField = PseudoSeq()
assert ob.ObjectField.__class__.__name__ == "PseudoSeq"
def test_null_conversion():
"""Test null conversion."""
import System
ob = ConversionTest()
ob.StringField = None
assert ob.StringField is None
ob.ObjectField = None
assert ob.ObjectField is None
ob.SpamField = None
assert ob.SpamField is None
pi = 22/7
assert ob.Echo[System.Double](pi) == pi
assert ob.Echo[System.DateTime](None) is None
# Primitive types and enums should not be set to null.
with pytest.raises(TypeError):
ConversionTest().Int32Field = None
with pytest.raises(TypeError):
ConversionTest().EnumField = None
def test_byte_array_conversion():
"""Test byte array conversion."""
ob = ConversionTest()
assert ob.ByteArrayField is None
ob.ByteArrayField = [0, 1, 2, 3, 4]
array = ob.ByteArrayField
assert len(array) == 5
assert array[0] == 0
assert array[4] == 4
value = b"testing"
ob.ByteArrayField = value
array = ob.ByteArrayField
for i, _ in enumerate(value):
assert array[i] == operator.getitem(value, i)
def test_sbyte_array_conversion():
"""Test sbyte array conversion."""
ob = ConversionTest()
assert ob.SByteArrayField is None
ob.SByteArrayField = [0, 1, 2, 3, 4]
array = ob.SByteArrayField
assert len(array) == 5
assert array[0] == 0
assert array[4] == 4
value = b"testing"
ob.SByteArrayField = value
array = ob.SByteArrayField
for i, _ in enumerate(value):
assert array[i] == operator.getitem(value, i)
def test_codecs():
"""Test codec registration from Python"""
class ListAsRawEncoder(RawProxyEncoder):
__namespace__ = "Python.Test"
def CanEncode(self, clr_type):
return clr_type.Name == "List`1" and clr_type.Namespace == "System.Collections.Generic"
list_raw_encoder = ListAsRawEncoder()
PyObjectConversions.RegisterEncoder(list_raw_encoder)
ob = ConversionTest()
l = ob.ListField
l.Add(42)
assert ob.ListField.Count == 1
CodecResetter.Reset()
def test_int_param_resolution_required():
"""Test resolution of `int` parameters when resolution is needed"""
mri = MethodResolutionInt()
data = list(mri.MethodA(0x1000, 10))
assert len(data) == 10
assert data[0] == 0
data = list(mri.MethodA(0x100000000, 10))
assert len(data) == 10
assert data[0] == 0
def test_iconvertible_conversion():
change_type = System.Convert.ChangeType
assert 1024 == change_type(1024, System.Int32)
assert 1024 == change_type(1024, System.Int64)
assert 1024 == change_type(1024, System.Int16)
def test_intptr_construction():
from System import IntPtr, UIntPtr, Int64, UInt64
from ctypes import sizeof, c_void_p
ptr_size = sizeof(c_void_p)
max_intptr = 2 ** (ptr_size * 8 - 1) - 1
min_intptr = -max_intptr - 1
max_uintptr = 2 ** (ptr_size * 8) - 1
min_uintptr = 0
ob = ConversionTest()
assert ob.IntPtrField == IntPtr.Zero
assert ob.UIntPtrField == UIntPtr.Zero
for v in [0, -1, 1024, max_intptr, min_intptr]:
ob.IntPtrField = IntPtr(Int64(v))
assert ob.IntPtrField == IntPtr(v)
assert ob.IntPtrField.ToInt64() == v
for v in [min_intptr - 1, max_intptr + 1]:
with pytest.raises(OverflowError):
IntPtr(v)
for v in [0, 1024, min_uintptr, max_uintptr, max_intptr]:
ob.UIntPtrField = UIntPtr(UInt64(v))
assert ob.UIntPtrField == UIntPtr(v)
assert ob.UIntPtrField.ToUInt64() == v
for v in [min_uintptr - 1, max_uintptr + 1, min_intptr]:
with pytest.raises(OverflowError):
UIntPtr(v)
def test_explicit_conversion():
from operator import index
from System import (
Int64, UInt64, Int32, UInt32, Int16, UInt16, Byte, SByte, Boolean
)
from System import Double, Single
assert int(Boolean(False)) == 0
assert int(Boolean(True)) == 1
for t in [UInt64, UInt32, UInt16, Byte]:
assert index(t(127)) == 127
assert int(t(127)) == 127
assert float(t(127)) == 127.0
for t in [Int64, Int32, Int16, SByte]:
assert index(t(127)) == 127
assert index(t(-127)) == -127
assert int(t(127)) == 127
assert int(t(-127)) == -127
assert float(t(127)) == 127.0
assert float(t(-127)) == -127.0
assert int(Int64(Int64.MaxValue)) == 2**63 - 1
assert int(Int64(Int64.MinValue)) == -2**63
assert int(UInt64(UInt64.MaxValue)) == 2**64 - 1
for t in [Single, Double]:
assert float(t(0.125)) == 0.125
assert int(t(123.4)) == 123
with pytest.raises(TypeError):
index(t(123.4))