def testTypedIndexer(self): """Test typed indexers.""" object = Test.TypedIndexerTest() from Python.Test import Spam spam = Spam("spam") self.assertTrue(object[spam] == None) object[spam] = "spam" self.assertTrue(object[spam] == "spam") object[spam] = "eggs" self.assertTrue(object[spam] == "eggs") def test(): object = Test.TypedIndexerTest() object["wrong"] self.assertRaises(TypeError, test) def test(): object = Test.TypedIndexerTest() object["wrong"] = "wrong" self.assertRaises(TypeError, test)
def testObjectIndexer(self): """Test object indexers.""" object = Test.ObjectIndexerTest() from Python.Test import Spam spam = Spam("spam") self.assertTrue(object[spam] == None) self.assertTrue(object["spam"] == None) self.assertTrue(object[1] == None) self.assertTrue(object[None] == None) object[spam] = "spam" self.assertTrue(object[spam] == "spam") object["spam"] = "eggs" self.assertTrue(object["spam"] == "eggs") object[1] = "one" self.assertTrue(object[1] == "one") object[1L] = "long" self.assertTrue(object[1L] == "long") def test(): class eggs: pass key = eggs() object = Test.ObjectIndexerTest() object[key] = "wrong" self.assertRaises(TypeError, test)
def test_object_indexer(): """Test ob indexers.""" ob = Test.ObjectIndexerTest() from Python.Test import Spam spam = Spam("spam") assert ob[spam] is None assert ob["spam"] is None assert ob[1] is None assert ob[None] is None ob[spam] = "spam" assert ob[spam] == "spam" ob["spam"] = "eggs" assert ob["spam"] == "eggs" ob[1] = "one" assert ob[1] == "one" ob[1] = "long" assert ob[1] == "long" class Eggs(object): pass key = Eggs() ob = Test.ObjectIndexerTest() ob[key] = "eggs_key" assert ob[key] == "eggs_key"
def testObjectConversion(self): """Test object conversion.""" from Python.Test import Spam object = ConversionTest() self.failUnless(object.ObjectField == None) object.ObjectField = Spam("eggs") self.failUnless(object.ObjectField.__class__.__name__ == "Spam") self.failUnless(object.ObjectField.GetValue() == "eggs") object.ObjectField = None self.failUnless(object.ObjectField == None) object.ObjectField = System.String("spam") self.failUnless(object.ObjectField == "spam") object.ObjectField = System.Int32(1) self.failUnless(object.ObjectField == 1) # need to test subclass here def test(): object = ConversionTest() object.ObjectField = self self.failUnlessRaises(TypeError, test)
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
def testInterfaceIndexer(self): """Test interface indexers.""" object = Test.InterfaceIndexerTest() from Python.Test import Spam spam = Spam("spam") self.failUnless(object[spam] == None) object[spam] = "spam" self.failUnless(object[spam] == "spam") object[spam] = "eggs" self.failUnless(object[spam] == "eggs") def test(): object = Test.InterfaceIndexerTest() object["wrong"] self.failUnlessRaises(TypeError, test) def test(): object = Test.InterfaceIndexerTest() object["wrong"] = "wrong" self.failUnlessRaises(TypeError, test)
def testSequenceArrayConversionTypeChecking(self): """Test error handling for sequence conversion to array arguments.""" from Python.Test import ArrayConversionTest from Python.Test import Spam if six.PY3: from collections import UserList else: from UserList import UserList # This should work, because null / None is a valid value in an # array of reference types. items = UserList() for i in range(10): items.append(Spam(str(i))) items[1] = None result = ArrayConversionTest.EchoRange(items) self.assertTrue(result[0].__class__ == Spam) self.assertTrue(result[1] == None) self.assertTrue(len(result) == 10) def test(items=items): items[1] = 1 result = ArrayConversionTest.EchoRange(items) self.assertRaises(TypeError, test) def test(items=items): items[1] = "spam" result = ArrayConversionTest.EchoRange(items) self.assertRaises(TypeError, test)
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_sequence_array_conversion_type_checking(): """Test error handling for sequence conversion to array arguments.""" from Python.Test import ArrayConversionTest from Python.Test import Spam # This should work, because null / None is a valid value in an # array of reference types. items = UserList() for i in range(10): items.append(Spam(str(i))) items[1] = None result = ArrayConversionTest.EchoRange(items) assert result[0].__class__ == Spam assert result[1] is None assert len(result) == 10 with pytest.raises(TypeError): items[1] = 1 _ = ArrayConversionTest.EchoRange(items) with pytest.raises(TypeError): items[1] = "spam" _ = ArrayConversionTest.EchoRange(items)
def test_object_indexer(): """Test ob indexers.""" ob = Test.ObjectIndexerTest() from Python.Test import Spam spam = Spam("spam") assert ob[spam] is None assert ob["spam"] is None assert ob[1] is None assert ob[None] is None ob[spam] = "spam" assert ob[spam] == "spam" ob["spam"] = "eggs" assert ob["spam"] == "eggs" ob[1] = "one" assert ob[1] == "one" ob[long(1)] = "long" assert ob[long(1)] == "long" with pytest.raises(TypeError): class Eggs(object): pass key = Eggs() ob = Test.ObjectIndexerTest() ob[key] = "wrong"
def testTupleArrayConversionTypeChecking(self): """Test error handling for tuple conversion to array arguments.""" from Python.Test import ArrayConversionTest from Python.Test import Spam # This should work, because null / None is a valid value in an # array of reference types. items = [] for i in range(10): items.append(Spam(str(i))) items[1] = None items = tuple(items) result = ArrayConversionTest.EchoRange(items) self.assertTrue(result[0].__class__ == Spam) self.assertTrue(result[1] == None) self.assertTrue(len(result) == 10) def test(items=items): temp = list(items) temp[1] = 1 result = ArrayConversionTest.EchoRange(tuple(temp)) self.assertRaises(TypeError, test) def test(items=items): temp = list(items) temp[1] = "spam" result = ArrayConversionTest.EchoRange(tuple(temp)) self.assertRaises(TypeError, test)
def testInterfaceConversion(self): """Test interface conversion.""" from Python.Test import Spam, ISpam object = ConversionTest() self.failUnless(ISpam(object.SpamField).GetValue() == "spam") self.failUnless(object.SpamField.GetValue() == "spam") object.SpamField = Spam("eggs") self.failUnless(ISpam(object.SpamField).GetValue() == "eggs") self.failUnless(object.SpamField.GetValue() == "eggs") # need to test spam subclass here. object.SpamField = None self.failUnless(object.SpamField == None) def test(): object = ConversionTest() object.SpamField = System.String("bad") self.failUnlessRaises(TypeError, test) def test(): object = ConversionTest() object.SpamField = System.Int32(1) self.failUnlessRaises(TypeError, test)
def testTypedArray(self): """Test typed arrays.""" from Python.Test import Spam object = Test.TypedArrayTest() items = object.items self.assertTrue(len(items) == 5) self.assertTrue(items[0].GetValue() == "0") self.assertTrue(items[4].GetValue() == "4") items[0] = Spam("4") self.assertTrue(items[0].GetValue() == "4") items[0] = Spam("0") self.assertTrue(items[0].GetValue() == "0") items[-4] = Spam("4") self.assertTrue(items[-4].GetValue() == "4") items[-1] = Spam("0") self.assertTrue(items[-1].GetValue() == "0") items[0] = None self.assertTrue(items[0] == None) def test(): object = Test.TypedArrayTest() object.items[0] = 99 self.assertRaises(TypeError, test) def test(): object = Test.TypedArrayTest() v = object.items["wrong"] self.assertRaises(TypeError, test) def test(): object = Test.TypedArrayTest() object.items["wrong"] = "wrong" self.assertRaises(TypeError, test)
def testInterfaceArray(self): """Test interface arrays.""" from Python.Test import Spam object = Test.InterfaceArrayTest() items = object.items self.failUnless(len(items) == 5) self.failUnless(items[0].GetValue() == "0") self.failUnless(items[4].GetValue() == "4") items[0] = Spam("4") self.failUnless(items[0].GetValue() == "4") items[0] = Spam("0") self.failUnless(items[0].GetValue() == "0") items[-4] = Spam("4") self.failUnless(items[-4].GetValue() == "4") items[-1] = Spam("0") self.failUnless(items[-1].GetValue() == "0") items[0] = None self.failUnless(items[0] == None) def test(): object = Test.InterfaceArrayTest() object.items[0] = 99 self.failUnlessRaises(TypeError, test) def test(): object = Test.InterfaceArrayTest() v = object.items["wrong"] self.failUnlessRaises(TypeError, test) def test(): object = Test.InterfaceArrayTest() object.items["wrong"] = "wrong" self.failUnlessRaises(TypeError, test)
def testInterfaceField(self): """Test interface fields.""" from Python.Test import Spam, ISpam object = FieldTest() self.failUnless(ISpam(object.SpamField).GetValue() == "spam") self.failUnless(object.SpamField.GetValue() == "spam") object.SpamField = Spam("eggs") self.failUnless(ISpam(object.SpamField).GetValue() == "eggs") self.failUnless(object.SpamField.GetValue() == "eggs")
def test_interface_field(): """Test interface fields.""" from Python.Test import Spam, ISpam ob = FieldTest() 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"
def testListArrayConversion(self): """Test conversion of lists to array arguments.""" from Python.Test import ArrayConversionTest from Python.Test import Spam items = [] for i in range(10): items.append(Spam(str(i))) result = ArrayConversionTest.EchoRange(items) self.failUnless(result[0].__class__ == Spam) self.failUnless(len(result) == 10)
def test_list_array_conversion(): """Test conversion of lists to array arguments.""" from Python.Test import ArrayConversionTest from Python.Test import Spam items = [] for i in range(10): items.append(Spam(str(i))) result = ArrayConversionTest.EchoRange(items) assert result[0].__class__ == Spam assert len(result) == 10
def testTupleArrayConversion(self): """Test conversion of tuples to array arguments.""" from Python.Test import ArrayConversionTest from Python.Test import Spam items = [] for i in range(10): items.append(Spam(str(i))) items = tuple(items) result = ArrayConversionTest.EchoRange(items) self.assertTrue(result[0].__class__ == Spam) self.assertTrue(len(result) == 10)
def testSequenceArrayConversion(self): """Test conversion of sequence-like objects to array arguments.""" from Python.Test import ArrayConversionTest from Python.Test import Spam from UserList import UserList items = UserList() for i in range(10): items.append(Spam(str(i))) result = ArrayConversionTest.EchoRange(items) self.failUnless(result[0].__class__ == Spam) self.failUnless(len(result) == 10)
def test_interface_array(): """Test interface arrays.""" from Python.Test import Spam ob = Test.InterfaceArrayTest() items = ob.items assert len(items) == 5 assert items[0].GetValue() == "0" assert items[4].GetValue() == "4" items[0] = Spam("4") assert items[0].GetValue() == "4" items[0] = Spam("0") assert items[0].GetValue() == "0" items[-4] = Spam("4") assert items[-4].GetValue() == "4" items[-1] = Spam("0") assert items[-1].GetValue() == "0" items[0] = None assert items[0] is None with pytest.raises(SystemError): ob = Test.InterfaceArrayTest() ob.items[0] = 99 with pytest.raises(TypeError): ob = Test.InterfaceArrayTest() _ = ob.items["wrong"] with pytest.raises(SystemError): ob = Test.InterfaceArrayTest() ob.items["wrong"] = "wrong"
def test_object_array(): """Test ob arrays.""" from Python.Test import Spam ob = Test.ObjectArrayTest() items = ob.items assert len(items) == 5 assert items[0].GetValue() == "0" assert items[4].GetValue() == "4" items[0] = Spam("4") assert items[0].GetValue() == "4" items[0] = Spam("0") assert items[0].GetValue() == "0" items[-4] = Spam("4") assert items[-4].GetValue() == "4" items[-1] = Spam("0") assert items[-1].GetValue() == "0" items[0] = 99 assert items[0] == 99 items[0] = None assert items[0] is None with pytest.raises(TypeError): ob = Test.ObjectArrayTest() _ = ob.items["wrong"] with pytest.raises(TypeError): ob = Test.ObjectArrayTest() ob.items["wrong"] = "wrong"
def testSequenceArrayConversion(self): """Test conversion of sequence-like objects to array arguments.""" from Python.Test import ArrayConversionTest from Python.Test import Spam if six.PY3: from collections import UserList else: from UserList import UserList items = UserList() for i in range(10): items.append(Spam(str(i))) result = ArrayConversionTest.EchoRange(items) self.assertTrue(result[0].__class__ == Spam) self.assertTrue(len(result) == 10)
def test_sequence_nested_array_conversion(): """Test conversion of sequences to array-of-array arguments.""" from Python.Test import ArrayConversionTest from Python.Test import Spam items = UserList() for i in range(10): subs = UserList() for _ in range(10): subs.append(Spam(str(i))) items.append(subs) result = ArrayConversionTest.EchoRangeAA(items) assert len(result) == 10 assert len(result[0]) == 10 assert result[0][0].__class__ == Spam
def testListNestedArrayConversion(self): """Test conversion of lists to array-of-array arguments.""" from Python.Test import ArrayConversionTest from Python.Test import Spam items = [] for i in range(10): subs = [] for n in range(10): subs.append(Spam(str(i))) items.append(subs) result = ArrayConversionTest.EchoRangeAA(items) self.assertTrue(len(result) == 10) self.assertTrue(len(result[0]) == 10) self.assertTrue(result[0][0].__class__ == Spam)
def testTupleNestedArrayConversion(self): """Test conversion of tuples to array-of-array arguments.""" from Python.Test import ArrayConversionTest from Python.Test import Spam items = [] for i in range(10): subs = [] for n in range(10): subs.append(Spam(str(i))) items.append(tuple(subs)) items = tuple(items) result = ArrayConversionTest.EchoRangeAA(items) self.failUnless(len(result) == 10) self.failUnless(len(result[0]) == 10) self.failUnless(result[0][0].__class__ == Spam)
def test_generic_list_array_conversion(): """Test conversion of lists to generic array arguments.""" from Python.Test import GenericArrayConversionTest from Python.Test import Spam items = [] for i in range(10): items.append(Spam(str(i))) result = GenericArrayConversionTest.EchoRange[Spam](items) assert result[0].__class__ == Spam assert len(result) == 10 # Currently raises an exception because the correct generic type parameter is not inferred with pytest.raises(TypeError): result = GenericArrayConversionTest.EchoRange(items) assert result[0].__class__ == Spam assert len(result) == 10
def testSequenceNestedArrayConversion(self): """Test conversion of sequences to array-of-array arguments.""" from Python.Test import ArrayConversionTest from Python.Test import Spam from UserList import UserList items = UserList() for i in range(10): subs = UserList() for n in range(10): subs.append(Spam(str(i))) items.append(subs) result = ArrayConversionTest.EchoRangeAA(items) self.failUnless(len(result) == 10) self.failUnless(len(result[0]) == 10) self.failUnless(result[0][0].__class__ == Spam)
def test_typed_indexer(): """Test typed indexers.""" ob = Test.TypedIndexerTest() from Python.Test import Spam spam = Spam("spam") assert ob[spam] is None ob[spam] = "spam" assert ob[spam] == "spam" ob[spam] = "eggs" assert ob[spam] == "eggs" with pytest.raises(TypeError): ob = Test.TypedIndexerTest() ob["wrong"] with pytest.raises(TypeError): ob = Test.TypedIndexerTest() ob["wrong"] = "wrong"
def test_md_array_conversion(): """Test passing of multi-dimensional array arguments.""" from Python.Test import ArrayConversionTest from Python.Test import Spam from System import Array # Currently, the runtime does not support automagic conversion of # Python sequences to true multi-dimensional arrays (though it # does support arrays-of-arrays). This test exists mostly as an # example of how a multi-dimensional array can be created and used # with managed code from Python. items = Array.CreateInstance(Spam, 5, 5) for i in range(5): for n in range(5): items.SetValue(Spam(str((i, n))), (i, n)) result = ArrayConversionTest.EchoRangeMD(items) assert len(result) == 25 assert result[0, 0].__class__ == Spam assert result[0, 0].__class__ == Spam