tests: Add basic tests for subclassing native types and using special methods.

Even of these, some features do not yet work as expected.
pull/528/head
Paul Sokolovsky 2014-04-29 02:44:13 +03:00
rodzic 6ead0d2fbc
commit 4f46c441ef
2 zmienionych plików z 68 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,31 @@
class mylist(list):
pass
a = mylist([1, 2, 5])
# Test setting instance attr
a.attr = "something"
# Test base type __str__
print(a)
# Test getting instance attr
print(a.attr)
# Test base type ->subscr
print(a[-1])
a[0] = -1
print(a)
# Test another base type unary op
print(len(a))
# Test binary op of base type, with 2nd arg being raw base type
print(a + [20, 30, 40])
# Test binary op of base type, with 2nd arg being same class as 1st arg
# TODO: Faults
#print(a + a)
def foo():
print("hello from foo")
try:
class myfunc(type(foo)):
pass
except TypeError:
print("TypeError")

Wyświetl plik

@ -0,0 +1,37 @@
class Base1:
def __init__(self, *args):
print("Base1.__init__",args)
class Clist1(Base1, list):
pass
class Ctuple1(Base1, tuple):
pass
a = Clist1()
print(len(a))
a = Clist1([1, 2, 3])
print(len(a))
a = Ctuple1()
print(len(a))
a = Ctuple1([1, 2, 3])
# TODO: Faults
#print(len(a))
print("---")
class Clist2(list, Base1):
pass
class Ctuple2(tuple, Base1):
pass
a = Clist2()
print(len(a))
a = Clist2([1, 2, 3])
print(len(a))
#a = Ctuple2()
#print(len(a))
#a = Ctuple2([1, 2, 3])
#print(len(a))