#!/usr/bin/python
# -*- coding: utf-8 -*-
# vi:encoding=utf-8

from TestEnv import TestEnv

te= TestEnv()

te.checkComplainAndAdjustExpected( 0)

def f( self):
 return self

te.test( 1==f( 1))

class A:
 pass

A.f= f

a=A()

te.test( a==a.f())

te.test( f==a.f.im_func)
te.test( a==a.f.im_self)
te.test( A==a.f.im_class)


def g():
 return 1
Ejemplo n.º 2
0
te= TestEnv()

te.checkComplainAndAdjustExpected( 0)

class MyException (Exception):

 def __init__( self, value):
  self.value= value
 
 def __str__( self):
  return "value of MyException is "+ self.value

try:
 raise MyException( "hi")
 te.test( False)
except MyException:
 te.test( isinstance( sys.exc_info()[ 1], MyException))
 te.test( "hi" == sys.exc_info()[ 1].value)

try:
 raise MyException( "hi")
 te.test( False)
except MyException, e:
 te.test( "hi" == e.value)
 te.test( isinstance( e, MyException))
 te.test( "value of MyException is hi" == str( e))

te.checkComplainAndAdjustExpected( 5)

print te.result()
Ejemplo n.º 3
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vi:encoding=utf-8

from TestEnv import TestEnv

te= TestEnv()

te.checkComplainAndAdjustExpected( 0)

def f( a, b):
 te.test( True)
 return ( a, b)

te.test( (1, 2)==f(1, 2))
te.test( (1, 2)==f( b=2, a=1))

te.checkComplainAndAdjustExpected( 4)

te.test( (1, 2)==apply( f, (1, 2)))

te.test( (1, 2)==apply( f, (1, 2), dict()))
te.test( (1, 2)==apply( f, (1, 2), {}))

te.test( (1, 2)==apply( f, (1,), { 'b': 2}))
te.test( (1, 2)==apply( f, [1], { 'b': 2}))

te.test( (1, 2)==apply( f, [], { 'b': 2, 'a': 1}))

te.test( (1, 2)==f( *[1, 2], **{}))
te.test( (1, 2)==f( *[1], **{'b': 2}))
te.checkComplainAndAdjustExpected( 0)


class A(object):
 def f( self):
  te.test( True)
 
class B( A):
 def f( self):
  super( B, self).f()
  te.test( True)

B().f()

te.test( (object,)==A.__bases__)
te.test( (A,)==B.__bases__)

te.checkComplainAndAdjustExpected( 4)

class C:
 def f( self):
  te.test( True)
 
class D( C):
 def f( self):
  C.f( self)
  te.test( True)

D().f()
Ejemplo n.º 5
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vi:encoding=utf-8

import sys
from TestEnv import TestEnv

te = TestEnv()

te.checkComplainAndAdjustExpected(0)

te.test(isinstance(str(), str))
te.test(isinstance("", str))
te.test(isinstance("", str))
te.test(isinstance(int(), int))
te.test(isinstance(1, int))
te.test(isinstance(float(), float))
te.test(isinstance(1.0, float))
te.test(isinstance(long(), long))
te.test(isinstance(1L, long))
te.test(isinstance(complex(), complex))
te.test(isinstance(3 + 7j, complex))

te.test(isinstance(dict(), dict))
te.test(isinstance({}, dict))
te.test(isinstance(list(), list))
te.test(isinstance([], list))
te.test(isinstance(tuple(), tuple))
te.test(isinstance((), tuple))
te.test(not isinstance((1), tuple))
te.test(isinstance((1), int))
Ejemplo n.º 6
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vi:encoding=utf-8

from TestEnv import TestEnv

te= TestEnv()

te.checkComplainAndAdjustExpected( 0)

te.test( True)

te.checkComplainAndAdjustExpected( 1)

print te.result()
Ejemplo n.º 7
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vi:encoding=utf-8

from TestEnv import TestEnv

te= TestEnv()

te.checkComplainAndAdjustExpected( 0)

te.test( 1==(lambda x: x)(1))
te.test( 3==(lambda x, y: x+ y)(1, 2))

te.checkComplainAndAdjustExpected( 2)

print te.result()
Ejemplo n.º 8
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vi:encoding=utf-8

from TestEnv import TestEnv

te= TestEnv()

te.checkComplainAndAdjustExpected( 0)

te.test( [1, 2, 3] == range( 1, 4))
te.test( [0, 2, 4] == range( 0, 5, 2))

te.test( 2 == [1, 2, 3][1])
te.test( 3 == [1, 2, 3][-1])
te.test( [1, 2, 3] == [1, 2, 3][0:3])
te.test( [2, 3] == [1, 2, 3][1:])
te.test( [1] == [1, 2, 3][:1])
te.test( [1, 2] == [1, 2, 3][:-1])

te.test( [1, 2, 3] == [ x for x in [1, 2, 3]])
te.test( [[1, 3], [1, 4], [2, 3], [2, 4]] == [[x,y] for x in [1, 2] for y in [3, 4]])
te.test( [[1, 3], [2, 4]] == [[x,y] for x in [1, 2] for y in [3, 4] if (x+2)==y])

te.checkComplainAndAdjustExpected( 11)

print te.result()
Ejemplo n.º 9
0
te.checkComplainAndAdjustExpected( 0)

class A:
 def __init__( self):
  A_self=self
  class B:
   def getOuter( self):
    return A_self
  A_self.B=B

a1=A()
b11=a1.B()
b12=a1.B()

a2=A()
b21=a2.B()
b22=a2.B()

te.test( a1== b11.getOuter())
te.test( a1== b12.getOuter())

te.test( a2== b21.getOuter())
te.test( a2== b22.getOuter())

te.checkComplainAndAdjustExpected( 4)

# see also: 

print te.result()
Ejemplo n.º 10
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vi:encoding=utf-8

from TestEnv import TestEnv

te= TestEnv()

te.checkComplainAndAdjustExpected( 0)

te.test( "000a" == "a".zfill( 4))
te.test( "a" == "a".zfill( 0))

te.test( "   a" == "a".rjust( 4))
te.test( "a" == "a".rjust( 0))

te.test( "a   " == "a".ljust( 4))
te.test( "a" == "a".ljust( 0))

te.checkComplainAndAdjustExpected( 6)

print te.result()
Ejemplo n.º 11
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vi:encoding=utf-8

from TestEnv import TestEnv

te = TestEnv()

te.checkComplainAndAdjustExpected(0)


te.test("- a -" == "- %s -" % "a")
te.test("- 2 -" == "- %d -" % 2)

te.test("- {'a': 'b'} -" == "- %s -" % {"a": "b"})
te.test("- b -" == "- %(a)s -" % {"a": "b"})

try:
    "- %(c)s -" % {"a": "b"}
    te.test(False)
except KeyError:
    te.test(True)


te.checkComplainAndAdjustExpected(5)

print te.result()
Ejemplo n.º 12
0
if not os.path.isdir( 'work'):
 os.mkdir( 'work')

try:
 os.remove( 'work/dbm_db')
except:
 pass

dbm_db= gdbm.open( 'work/dbm_db', 'c')

te= TestEnv()

te.checkComplainAndAdjustExpected( 0)

try:
 dbm_db2= gdbm.open( 'work/dbm_db', 'c')
 te.test( False)
except gdbm.error, m:
 te.test( isinstance( m, gdbm.error))
 te.test( "(11, 'Resource temporarily unavailable')" == str( m))
 te.test( True)

te.checkComplainAndAdjustExpected( 3)

print te.result()

dbm_db.close()

# see also:

Ejemplo n.º 13
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vi:encoding=utf-8

from TestEnv import TestEnv

te= TestEnv()

te.checkComplainAndAdjustExpected( 0)

te.test( 0==reduce( None, [], 0))
te.test( 0==reduce( (lambda x, y: x+y), [], 0))
te.test( 1==reduce( None, [ 1]))
try:
 te.test( 1==reduce( None, [ 1]), 0)
 te.test( False)
except TypeError: # TypeError: 'NoneType' object is not callable
 te.test( True)
te.test( 1==reduce( (lambda x, y: x+y), [ 1], 0))
te.test( (1+ 1)==reduce( (lambda x, y: x+y), [ 1], 1))
te.test( 1==reduce( (lambda x, y: x+y), [ 1]))
te.test( (1+ 2)==reduce( (lambda x, y: x+y), [ 1, 2]))
te.test( (1+ 2+ 3)==reduce( (lambda x, y: x+y), [ 1, 2, 3]))

te.checkComplainAndAdjustExpected( 9)

# reducing of different types:

te.test( ( 1+ 2+ 3)==reduce( (lambda x, y: x+ int( y)), [ 1, "2", "3"]))
te.test( ( 1+ 1)==reduce( (lambda x, y: x+ int( y)), [ "1"], 1))
Ejemplo n.º 14
0
te= TestEnv()

te.checkComplainAndAdjustExpected( 0)

class Z:
 pass

class A1( Z):
 pass

class A2( Z):
 pass

class B( A1, A2):
 pass

class C:
 pass

te.test( issubclass( B, A1))
te.test( issubclass( B, A2))
te.test( not issubclass( C, A1))
te.test( issubclass( A1, A1))
te.test( not issubclass( A1, A2))
te.test( issubclass( B, Z))

te.checkComplainAndAdjustExpected( 6)

print te.result()
Ejemplo n.º 15
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vi:encoding=utf-8

from TestEnv import TestEnv

te= TestEnv()

te.checkComplainAndAdjustExpected( 0)

te.test( "ab" == "ab")
te.test( "ab" == "a" + "b")
te.test( "ab" == str( "ab"))
te.test( "101" == str( 101))
te.test( "101.0" == str( 101.))

class C:
 def __repr__( self):
  return "aadebdf7-9cbb-494b-a4ea-496aa32c5702"

te.test( "aadebdf7-9cbb-494b-a4ea-496aa32c5702" == str( C()))
te.test( "aadebdf7-9cbb-494b-a4ea-496aa32c5702" == repr( C()))
te.test( not( "aadebdf7-9cbb-494b-a4ea-496aa32c5702" == C()))


te.checkComplainAndAdjustExpected( 8)

print te.result()
Ejemplo n.º 16
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vi:encoding=utf-8

from TestEnv import TestEnv

te= TestEnv()

te.checkComplainAndAdjustExpected( 0)

class A:
 pass

a=A()

te.test( ( '<__main__.A instance at 0x%x>' % id( a)) == repr( a))

A.__name__='B'

te.test( ( '<__main__.B instance at 0x%x>' % id( a)) == repr( a))

class C( object):
 pass

c=C()

te.test( ( '<__main__.C object at 0x%x>' % id( c)) == repr( c))

C.__name__='D'

te.test( ( '<__main__.D object at 0x%x>' % id( c)) == repr( c))
Ejemplo n.º 17
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vi:encoding=utf-8

from TestEnv import TestEnv

te= TestEnv()

te.checkComplainAndAdjustExpected( 0)

te.test( not locals().has_key( 'a'))
te.test( not globals().has_key( 'a'))

a=3

te.test( locals().has_key( 'a'))
te.test( globals().has_key( 'a'))

te.test( 3==locals()['a'])
te.test( 3==globals()['a'])

globals()['a']=4

te.test( 4==locals()['a'])
te.test( 4==globals()['a'])

locals()['a']=5

te.test( 5==locals()['a'])
te.test( 5==globals()['a'])
Ejemplo n.º 18
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vi:encoding=utf-8

from TestEnv import TestEnv

te= TestEnv()

te.checkComplainAndAdjustExpected( 0)

te.test( hex( 17) == "0x11")

te.checkComplainAndAdjustExpected( 1)

# see also: hex.py, oct.py, topic-format-string.py, 

print te.result()
Ejemplo n.º 19
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vi:encoding=utf-8

from TestEnv import TestEnv

te= TestEnv()

te.checkComplainAndAdjustExpected( 0)

exec 'a=3'

te.test( 3==a)

g=dict()

exec 'a=4' in g

te.test( 4==g['a'])

g2=dict()

exec 'b=1+a' in g, g2

te.test( 5==g2['b'])

te.test( not g.has_key( 'b'))
te.test( not g2.has_key( 'a'))

te.checkComplainAndAdjustExpected( 5)
Ejemplo n.º 20
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vi:encoding=utf-8

from TestEnv import TestEnv

te= TestEnv()

te.checkComplainAndAdjustExpected( 0)


te.test( "- a -" == "- %s -" % 'a')
te.test( "- 2 -" == "- %d -" % 2)

te.test( "- {'a': 'b'} -" == "- %s -" % {'a': 'b'})
te.test( "- b -" == "- %(a)s -" % {'a': 'b'})

try:
 "- %(c)s -" % {'a': 'b'}
 te.test( False)
except KeyError:
 te.test( True)
 
te.test( "011" == "%03x" % 0x11)
te.test( hex( 17) == "0x%x" % 0x11)
te.test( oct( 17) == "0%o" % 021)

te.checkComplainAndAdjustExpected( 8)

# see also: hex.py, oct.py, topic-format-string.py, 
Ejemplo n.º 21
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vi:encoding=utf-8

from TestEnv import TestEnv

te = TestEnv()

te.checkComplainAndAdjustExpected(0)

o1 = object()
o2 = object()

# te.test( ( '<object object at 0x%08x>' % id( o1)) == repr( o1))
te.test(("<object object at 0x%x>" % id(o1)) == repr(o1))

te.test(id(o1) == id(o1))
te.test(id(o1) != id(o2))

te.checkComplainAndAdjustExpected(3)

s1 = "string1"
s2 = "string1"

te.test(id(s1) == id(s2))
te.test(id(s1) == id("string1"))

te.checkComplainAndAdjustExpected(2)


class A:
Ejemplo n.º 22
0
# -*- coding: utf-8 -*-
# vi:encoding=utf-8

from TestEnv import TestEnv

te= TestEnv()

te.checkComplainAndAdjustExpected( 0)

f= dict()
f[ 'c']= 'd'
d= { 'a' : 'b'}

e= dict( d)

te.test( e == d)

e.update( f)

te.test( e != d)
te.test( { 'a' : 'b', 'c': 'd'} == e)
te.test( { 'c': 'd', 'a' : 'b'} == e)

tmp= []

for i in e:
 tmp += [i]

tmp.sort()

te.test( [ 'a', 'c'] == tmp)
Ejemplo n.º 23
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vi:encoding=utf-8

from TestEnv import TestEnv

te= TestEnv()

te.checkComplainAndAdjustExpected( 0)

te.test( oct( 17) == "021")

te.checkComplainAndAdjustExpected( 1)

# see also: hex.py, oct.py, topic-format-string.py, 

print te.result()
Ejemplo n.º 24
0
if not os.path.isdir( 'work'):
 os.mkdir( 'work')

try:
 os.remove( 'work/dbm_db')
except:
 pass

dbm_db= gdbm.open( 'work/dbm_db', 'c')

te= TestEnv()

te.checkComplainAndAdjustExpected( 0)

te.test( None == dbm_db.firstkey())

keysPut= []

for x in range( 1, 10): # 8bb46c596f2b47f8a36eae9da5bdbca9
 key= str( x)
 dbm_db[ key]= key # 387533fff7d649cf8d8689cbe9e81307
 keysPut.append( key)

keysCmp= []
key= dbm_db.firstkey()

try: 
 while not None == key:
  if not key == dbm_db[ key]: # 387533fff7d649cf8d8689cbe9e81307
   raise Exception( 'not key == dbm_db[ key]')
Ejemplo n.º 25
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vi:encoding=utf-8

from TestEnv import TestEnv

te= TestEnv()

te.checkComplainAndAdjustExpected( 0)

te.test( 5==eval( '2+3'))

a=7

te.test( 9==eval( '2+a'))

def f(): return 1

te.test( 1==eval( 'f()'))
te.test( 1==eval( 'f')())

g={ 'a': 3} # globals

te.test( 5==eval( '2+a', g))

l={ 'a': 5} # locals

te.test( 7==eval( '2+a', g, l))

try:
 eval( '2+a', {})