#!/www/python/bin/python

"""$URL: svn+ssh://svn/repos/trunk/dulcinea/lib/test/utest_typeutils.py $
$Id: utest_typeutils.py 25054 2004-09-09 18:04:30Z dbinger $

Test for dulcinea.typeutils.
"""

from types import *
from sancho.utest import UTest
from dulcinea.typeutils import typecheck, typecheck_seq


class TypeUtilsTest (UTest):

    def check_typecheck_seq (self):
        typecheck_seq([4, 2], IntType)
        try:
            typecheck_seq([4.0, 2], IntType)
            assert 0
        except TypeError: pass
        try:
            typecheck_seq([4.0, 2], FloatType)
            assert 0
        except TypeError: pass
        typecheck_seq([4.0, 2.0], FloatType)
        typecheck_seq(['foo', 'bar', 'baz'], str)
        try:
            typecheck_seq(['foo', 'bar', 666], str)
            assert 0
        except TypeError: pass
        typecheck_seq([], str)
        typecheck_seq([], FloatType)
        typecheck_seq([], IntType)
        try:
            typecheck_seq(4, IntType)
            assert 0
        except TypeError: pass
        try:
            typecheck_seq(None, IntType)
            assert 0
        except TypeError: pass
        typecheck_seq(None, IntType, allow_none=1)

    def check_typecheck (self):
        typecheck(4, IntType)
        try:
            typecheck(4.0, IntType, allow_none=1)
            assert 0
        except TypeError: pass
        typecheck(4.0, (FloatType, IntType))
        try:
            typecheck('foo', (FloatType, IntType))
            assert 0
        except TypeError: pass
        try:
            typecheck(None, IntType)
            assert 0
        except TypeError: pass
        typecheck(None, IntType, allow_none=1)

if __name__ == "__main__":
    TypeUtilsTest()
