|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +"""Test conversions using codecs from client python code""" |
| 4 | +import clr |
| 5 | +import System |
| 6 | +import pytest |
| 7 | +import Python.Runtime |
| 8 | +from Python.Test import ListConversionTester, ListMember |
| 9 | + |
| 10 | +class int_iterable(): |
| 11 | + def __init__(self): |
| 12 | + self.counter = 0 |
| 13 | + def __iter__(self): |
| 14 | + return self |
| 15 | + def __next__(self): |
| 16 | + if self.counter == 3: |
| 17 | + raise StopIteration |
| 18 | + self.counter = self.counter + 1 |
| 19 | + return self.counter |
| 20 | + |
| 21 | +class obj_iterable(): |
| 22 | + def __init__(self): |
| 23 | + self.counter = 0 |
| 24 | + def __iter__(self): |
| 25 | + return self |
| 26 | + def __next__(self): |
| 27 | + if self.counter == 3: |
| 28 | + raise StopIteration |
| 29 | + self.counter = self.counter + 1 |
| 30 | + return ListMember(self.counter, "Number " + str(self.counter)) |
| 31 | + |
| 32 | +def test_iterable(): |
| 33 | + """Test that a python iterable can be passed into a function that takes an IEnumerable<object>""" |
| 34 | + |
| 35 | + #Python.Runtime.Codecs.ListDecoder.Register() |
| 36 | + #Python.Runtime.Codecs.SequenceDecoder.Register() |
| 37 | + Python.Runtime.Codecs.IterableDecoder.Register() |
| 38 | + ob = ListConversionTester() |
| 39 | + |
| 40 | + iterable = int_iterable() |
| 41 | + assert 3 == ob.GetLength(iterable) |
| 42 | + |
| 43 | + iterable2 = obj_iterable() |
| 44 | + assert 3 == ob.GetLength2(iterable2) |
| 45 | + |
| 46 | + Python.Runtime.PyObjectConversions.Reset() |
| 47 | + |
| 48 | +def test_sequence(): |
| 49 | + Python.Runtime.Codecs.SequenceDecoder.Register() |
| 50 | + ob = ListConversionTester() |
| 51 | + |
| 52 | + tup = (1,2,3) |
| 53 | + assert 3 == ob.GetLength(tup) |
| 54 | + |
| 55 | + tup2 = (ListMember(1, "one"), ListMember(2, "two"), ListMember(3, "three")) |
| 56 | + assert 3 == ob.GetLength(tup2) |
| 57 | + |
| 58 | + Python.Runtime.PyObjectConversions.Reset() |
| 59 | + |
| 60 | +def test_list(): |
| 61 | + Python.Runtime.Codecs.SequenceDecoder.Register() |
| 62 | + ob = ListConversionTester() |
| 63 | + |
| 64 | + l = [1,2,3] |
| 65 | + assert 3 == ob.GetLength(l) |
| 66 | + |
| 67 | + l2 = [ListMember(1, "one"), ListMember(2, "two"), ListMember(3, "three")] |
| 68 | + assert 3 == ob.GetLength(l2) |
| 69 | + |
| 70 | + Python.Runtime.PyObjectConversions.Reset() |
0 commit comments