[IronPython] Test Module for IronPython 0.7

Michael Spencer mahs at telcopartners.com
Fri Mar 25 22:43:45 CET 2005


FePyers:

Here is freestanding test module for built-ins, shamelessly appropriated from pypy

Since FePy0.7 lacks assert at this point, its hard to test even the builtins

So, asserts are replaced with if test: pass else: raise AssertionError
(this process used automatic code-generation via the AST- no guarantees of 
correctness)

raises function is provided in the module

2.4 features are commented out

tests is a dict of test functions since introspection of cls dicts doesn't yet work

call with testall()

...try to make sense of the output and file bugs as appropriate at 
http://www.gotdotnet.com/workspaces/bugtracker/home.aspx?id=ad7acff7-ab1e-4bcb-99c0-57ac5a3a9742

Enjoy

Michael



"""Hacked version of pypy/module/test/test_builtin.py
that has no dependencies and does not use assert, which is not implemented on 
FePy 0.7"""


class AppTestBuiltinApp:


     def test_import(self):
         m = __import__('pprint')
         if (m.pformat({ }) == '{}'):
             pass
         else:
             raise AssertionError
         if (m.__name__ == 'pprint'):
             pass
         else:
             raise AssertionError
         raises(ImportError, __import__, 'spamspam')
         raises(TypeError, __import__, 1, 2, 3, 4)

     def test_chr(self):
         if (chr(65) == 'A'):
             pass
         else:
             raise AssertionError
         raises(ValueError, chr, -1)
         raises(TypeError, chr, 'a')

     def test_intern(self):
         raises(TypeError, intern)
         raises(TypeError, intern, 1)
         s = 'never interned before'
         s2 = intern(s)
         if (s == s2):
             pass
         else:
             raise AssertionError
         s3 = s.swapcase()
         if (s3 != s2):
             pass
         else:
             raise AssertionError
         s4 = s3.swapcase()
         if (intern(s4) is s2):
             pass
         else:
             raise AssertionError

     def test_globals(self):
         d = {'foo' : 'bar' }
         exec 'def f(): return globals()' in d
         d2 = d['f']()
         if (d2 is d):
             pass
         else:
             raise AssertionError

     def test_locals(self):

         def f():
             return locals()

         def g(c=0, b=0, a=0):
             return locals()
         if (f() == { }):
             pass
         else:
             raise AssertionError
         if (g() == {'a' : 0, 'b' : 0, 'c' : 0 }):
             pass
         else:
             raise AssertionError

     def test_dir(self):

         def f():
             return dir()

         def g(c=0, b=0, a=0):
             return dir()

         def nosp(x):
             return [y for y in x]
         if (f() == [ ]):
             pass
         else:
             raise AssertionError
         if (g() == ['a','b','c', ]):
             pass
         else:
             raise AssertionError

         class X:

             pass
         if (nosp(dir(X)) == [ ]):
             pass
         else:
             raise AssertionError

         class X:

             a = 23
             c = 45
             b = 67
         if (nosp(dir(X)) == ['a','b','c', ]):
             pass
         else:
             raise AssertionError

     def test_vars(self):

         def f():
             return vars()

         def g(c=0, b=0, a=0):
             return vars()
         if (f() == { }):
             pass
         else:
             raise AssertionError
         if (g() == {'a' : 0, 'b' : 0, 'c' : 0 }):
             pass
         else:
             raise AssertionError

     def test_getattr(self):

         class a:

             i = 5
         if (getattr(a, 'i') == 5):
             pass
         else:
             raise AssertionError
         raises(AttributeError, getattr, a, 'k')
         if (getattr(a, 'k', 42) == 42):
             pass
         else:
             raise AssertionError
         if (getattr(a, u'i') == 5):
             pass
         else:
             raise AssertionError
         raises(AttributeError, getattr, a, u'k')
         if (getattr(a, u'k', 42) == 42):
             pass
         else:
             raise AssertionError

     def test_sum(self):
         if (sum([ ]) == 0):
             pass
         else:
             raise AssertionError
         if (sum([42, ]) == 42):
             pass
         else:
             raise AssertionError
         if (sum([1,2,3, ]) == 6):
             pass
         else:
             raise AssertionError
         if (sum([ ], 5) == 5):
             pass
         else:
             raise AssertionError
         if (sum([1,2,3, ], 4) == 10):
             pass
         else:
             raise AssertionError

     def test_type_selftest(self):
         if (type(type) is type):
             pass
         else:
             raise AssertionError

     def test_iter_sequence(self):
         raises(TypeError, iter, 3)
         x = iter(['a','b','c', ])
         if (x.next() == 'a'):
             pass
         else:
             raise AssertionError
         if (x.next() == 'b'):
             pass
         else:
             raise AssertionError
         if (x.next() == 'c'):
             pass
         else:
             raise AssertionError
         raises(StopIteration, x.next)

     def test_iter___iter__(self):
         mydict = {'a' : 1, 'b' : 2, 'c' : 3 }
         if (list(iter(mydict)) == mydict.keys()):
             pass
         else:
             raise AssertionError

     def test_iter_callable_sentinel(self):

         class count (object):


             def __init__(self):
                 self.value = 0

             def __call__(self):
                 self.value += 1
                 return self.value
         x = iter(count(), 3)
         if (x.next() == 1):
             pass
         else:
             raise AssertionError
         if (x.next() == 2):
             pass
         else:
             raise AssertionError
         raises(StopIteration, x.next)

     def test_enumerate(self):
         seq = range(2, 4)
         enum = enumerate(seq)
         if (enum.next() == (0, 2)):
             pass
         else:
             raise AssertionError
         if (enum.next() == (1, 3)):
             pass
         else:
             raise AssertionError
         raises(StopIteration, enum.next)
         raises(TypeError, enumerate, 1)
         raises(TypeError, enumerate, None)

     def test_xrange_args(self):
         raises(ValueError, xrange, 0, 1, 0)

     def test_xrange_up(self):
         x = xrange(2)
         iter_x = iter(x)
         if (iter_x.next() == 0):
             pass
         else:
             raise AssertionError
         if (iter_x.next() == 1):
             pass
         else:
             raise AssertionError
         raises(StopIteration, iter_x.next)

     def test_xrange_down(self):
         x = xrange(4, 2, -1)
         iter_x = iter(x)
         if (iter_x.next() == 4):
             pass
         else:
             raise AssertionError
         if (iter_x.next() == 3):
             pass
         else:
             raise AssertionError
         raises(StopIteration, iter_x.next)

     def test_xrange_has_type_identity(self):
         if (type(xrange(1)) == type(xrange(1))):
             pass
         else:
             raise AssertionError

     def test_xrange_len(self):
         x = xrange(33)
         if (len(x) == 33):
             pass
         else:
             raise AssertionError
         x = xrange(33.200000000000003)
         if (len(x) == 33):
             pass
         else:
             raise AssertionError
         x = xrange(33, 0, -1)
         if (len(x) == 33):
             pass
         else:
             raise AssertionError
         x = xrange(33, 0)
         if (len(x) == 0):
             pass
         else:
             raise AssertionError
         x = xrange(33, 0.20000000000000001)
         if (len(x) == 0):
             pass
         else:
             raise AssertionError
         x = xrange(0, 33)
         if (len(x) == 33):
             pass
         else:
             raise AssertionError
         x = xrange(0, 33, -1)
         if (len(x) == 0):
             pass
         else:
             raise AssertionError
         x = xrange(0, 33, 2)
         if (len(x) == 17):
             pass
         else:
             raise AssertionError
         x = xrange(0, 32, 2)
         if (len(x) == 16):
             pass
         else:
             raise AssertionError

     def test_xrange_indexing(self):
         x = xrange(0, 33, 2)
         if (x[7] == 14):
             pass
         else:
             raise AssertionError
         if (x[-7] == 20):
             pass
         else:
             raise AssertionError
         raises(IndexError, x.__getitem__, 17)
         raises(IndexError, x.__getitem__, -18)
         raises(TypeError, x.__getitem__, slice(0, 3, 1))

     def test_xrange_bad_args(self):
         raises(TypeError, xrange, '1')
         raises(TypeError, xrange, None)
         raises(TypeError, xrange, (3 + 2j))
         raises(TypeError, xrange, 1, '1')
         raises(TypeError, xrange, 1, (3 + 2j))
         raises(TypeError, xrange, 1, 2, '1')
         raises(TypeError, xrange, 1, 2, (3 + 2j))

##    def test_sorted(self):
##        l = [ ]
##        sorted_l = sorted(l)
##        if (sorted_l is not l):
##            pass
##        else:
##            raise AssertionError
##        if (sorted_l == l):
##            pass
##        else:
##            raise AssertionError
##        l = [1,5,2,3, ]
##        sorted_l = sorted(l)
##        if (sorted_l == [1,2,3,5, ]):
##            pass
##        else:
##            raise AssertionError
##
##    def test_sorted_with_keywords(self):
##        l = ['a','C','b', ]
##        sorted_l = sorted(l, reverse=True)
##        if (sorted_l is not l):
##            pass
##        else:
##            raise AssertionError
##        if (sorted_l == ['b','a','C', ]):
##            pass
##        else:
##            raise AssertionError
##        sorted_l = sorted(l, reverse=True, key=lambda x: x.lower())
##        if (sorted_l is not l):
##            pass
##        else:
##            raise AssertionError
##        if (sorted_l == ['C','b','a', ]):
##            pass
##        else:
##            raise AssertionError

##    def test_reversed_simple_sequences(self):
##        l = range(5)
##        rev = reversed(l)
##        if (list(rev) == [4,3,2,1,0, ]):
##            pass
##        else:
##            raise AssertionError
##        if (list(l.__reversed__()) == [4,3,2,1,0, ]):
##            pass
##        else:
##            raise AssertionError
##        s = 'abcd'
##        if (list(reversed(s)) == ['d','c','b','a', ]):
##            pass
##        else:
##            raise AssertionError

##    def test_reversed_custom_objects(self):
##
##        class SomeClass:
##
##
##            def __reversed__(self):
##                return 42
##        obj = SomeClass()
##        if (reversed(obj) == 42):
##            pass
##        else:
##            raise AssertionError

     def test_cmp(self):
         if (cmp(9, 9) == 0):
             pass
         else:
             raise AssertionError
         if (cmp(0, 9) < 0):
             pass
         else:
             raise AssertionError
         if (cmp(9, 0) > 0):
             pass
         else:
             raise AssertionError

     def test_cmp_more(self):

         class C:


             def __eq__(self, other):
                 return True

             def __cmp__(self, other):
                 raise RuntimeError
         c1 = C()
         c2 = C()
         raises(RuntimeError, cmp, c1, c2)

     def test_cmp_cyclic(self):
         a = [ ]
         a.append(a)
         b = [ ]
         b.append(b)
         from UserList import UserList
         c = UserList()
         c.append(c)
         if (cmp(a, b) == 0):
             pass
         else:
             raise AssertionError
         if (cmp(b, c) == 0):
             pass
         else:
             raise AssertionError
         if (cmp(c, a) == 0):
             pass
         else:
             raise AssertionError
         if (cmp(a, c) == 0):
             pass
         else:
             raise AssertionError
         a.pop()
         b.pop()
         c.pop()

     def test_coerce(self):
         if (coerce(1, 2) == (1, 2)):
             pass
         else:
             raise AssertionError
         if (coerce(1L, 2L) == (1L, 2L)):
             pass
         else:
             raise AssertionError
         if (coerce(1, 2L) == (1L, 2L)):
             pass
         else:
             raise AssertionError
         if (coerce(1L, 2) == (1L, 2L)):
             pass
         else:
             raise AssertionError
         if (coerce(1, 2.0) == (1.0, 2.0)):
             pass
         else:
             raise AssertionError
         if (coerce(1.0, 2L) == (1.0, 2.0)):
             pass
         else:
             raise AssertionError
         if (coerce(1L, 2.0) == (1.0, 2.0)):
             pass
         else:
             raise AssertionError
         raises(TypeError, coerce, 1, 'a')
         raises(TypeError, coerce, u'a', 'a')

     def test_return_None(self):

         class X:

             pass
         x = X()
         if (setattr(x, 'x', 11) == None):
             pass
         else:
             raise AssertionError
         if (delattr(x, 'x') == None):
             pass
         else:
             raise AssertionError

     def test_divmod(self):
         if (divmod(15, 10) == (1, 5)):
             pass
         else:
             raise AssertionError

     def test_callable(self):

         class Call:


             def __call__(self, a):
                 return (a + 2)
         if not not callable(Call()):
             pass
         else:
             raise AssertionError
         if callable(int):
             pass
         else:
             raise AssertionError

     def test_uncallable(self):

         class NoCall (object):

             pass
         a = NoCall()
         if not callable(a):
             pass
         else:
             raise AssertionError
         a.__call__ = lambda : 'foo'
         if not callable(a):
             pass
         else:
             raise AssertionError

     def test_hash(self):
         if (hash(23) == hash(23)):
             pass
         else:
             raise AssertionError
         if (hash(2.2999999999999998) == hash(2.2999999999999998)):
             pass
         else:
             raise AssertionError
         if (hash('23') == hash('23')):
             pass
         else:
             raise AssertionError
         if (hash((23,)) == hash((23,))):
             pass
         else:
             raise AssertionError
         if (hash(22) != hash(23)):
             pass
         else:
             raise AssertionError
         raises(TypeError, hash, [ ])
         raises(TypeError, hash, { })

     def test_eval(self):
         if (eval('1+2') == 3):
             pass
         else:
             raise AssertionError
         if (eval(' \t1+2\n') == 3):
             pass
         else:
             raise AssertionError

     def test_compile(self):
         co = compile('1+2', '?', 'eval')
         if (eval(co) == 3):
             pass
         else:
             raise AssertionError
         raises(SyntaxError, compile, '-', '?', 'eval')
         raises(ValueError, compile, '"\\xt"', '?', 'eval')
         raises(ValueError, compile, '1+2', '?', 'maybenot')
         raises(TypeError, compile, '1+2', 12, 34)

     def test_isinstance(self):
         if isinstance(5, int):
             pass
         else:
             raise AssertionError
         if isinstance(5, object):
             pass
         else:
             raise AssertionError
         if not isinstance(5, float):
             pass
         else:
             raise AssertionError
         if isinstance(True, (int, float)):
             pass
         else:
             raise AssertionError
         if not isinstance(True, (type, float)):
             pass
         else:
             raise AssertionError
         if isinstance(True, ((type, float), bool)):
             pass
         else:
             raise AssertionError
         raises(TypeError, isinstance, 5, 6)
         raises(TypeError, isinstance, 5, (float, 6))

     def test_issubclass(self):
         if issubclass(int, int):
             pass
         else:
             raise AssertionError
         if issubclass(int, object):
             pass
         else:
             raise AssertionError
         if not issubclass(int, float):
             pass
         else:
             raise AssertionError
         if issubclass(bool, (int, float)):
             pass
         else:
             raise AssertionError
         if not issubclass(bool, (type, float)):
             pass
         else:
             raise AssertionError
         if issubclass(bool, ((type, float), bool)):
             pass
         else:
             raise AssertionError
         raises(TypeError, issubclass, 5, int)
         raises(TypeError, issubclass, int, 6)
         raises(TypeError, issubclass, int, (float, 6))

     def test_staticmethod(self):

         class X:


             def f(*args, **kwds):
                 return (args, kwds)
             f = staticmethod(f)
         if (X.f() == ((), { })):
             pass
         else:
             raise AssertionError
         if (X.f(42, x=43) == ((42,), {'x' : 43 })):
             pass
         else:
             raise AssertionError
         if (X().f() == ((), { })):
             pass
         else:
             raise AssertionError
         if (X().f(42, x=43) == ((42,), {'x' : 43 })):
             pass
         else:
             raise AssertionError

     def test_classmethod(self):

         class X:


             def f(*args, **kwds):
                 return (args, kwds)
             f = classmethod(f)

         class Y (X):

             pass
         if (X.f() == ((X,), { })):
             pass
         else:
             raise AssertionError
         if (X.f(42, x=43) == ((X, 42), {'x' : 43 })):
             pass
         else:
             raise AssertionError
         if (X().f() == ((X,), { })):
             pass
         else:
             raise AssertionError
         if (X().f(42, x=43) == ((X, 42), {'x' : 43 })):
             pass
         else:
             raise AssertionError
         if (Y.f() == ((Y,), { })):
             pass
         else:
             raise AssertionError
         if (Y.f(42, x=43) == ((Y, 42), {'x' : 43 })):
             pass
         else:
             raise AssertionError
         if (Y().f() == ((Y,), { })):
             pass
         else:
             raise AssertionError
         if (Y().f(42, x=43) == ((Y, 42), {'x' : 43 })):
             pass
         else:
             raise AssertionError



tests = {AppTestBuiltinApp: [
'test_xrange_indexing', 'test_issubclass', 'test_getattr',
'test_locals', 'test_chr', 'test_hash', 'test_xrange_args',
'test_type_selftest', 'test_xrange_len', 'test_uncallable',
'test_return_None', 'test_divmod', 'test_xrange_down',
'test_iter_sequence', 'test_globals', 'test_compile',
'test_isinstance', 'test_vars', 'test_cmp_more', 'test_sum',
'test_intern', 'test_iter___iter__', 'test_eval', 'test_cmp',
'test_coerce', 'test_iter_callable_sentinel', 'test_import',
'test_xrange_bad_args', 'test_xrange_up', 'test_dir',
'test_cmp_cyclic', 'test_staticmethod', 'test_classmethod',
'test_xrange_has_type_identity', 'test_enumerate', 'test_callable']}


def raises(exc, callable, *args):
     try:
         callable(*args)
     except exc:
         return
     raise AssertionError


def testall():
     for cls in tests:
         inst = cls()
         for attr in tests[cls]:
             func = getattr(inst, attr)
             try:
                 func()
             except AssertionError:
                 print "Failed Assertion: %s.%s" % (cls.__name__,attr)
             except Exception, err:
                 print err




More information about the Ironpython-users mailing list