Beispiel #1
0
 def test_success(self):
     from typecheck import IsCallable
 
     Xor = self.Xor
     check_type = self.check_type
     
     check_type(Xor(dict, IsCallable()), pow)
     check_type(Xor(dict, IsCallable()), {'a': 5})
Beispiel #2
0
    def test_oldstyle_classes(self):
        from typecheck import IsCallable

        class A:
            pass

        check_type(IsCallable(), A)
Beispiel #3
0
    def test_userdefined_functions(self):
        from typecheck import IsCallable

        def foo(a):
            return a

        check_type(IsCallable(), foo)
Beispiel #4
0
    def test_newstyle_classes(self):
        from typecheck import IsCallable

        class A(object):
            pass

        check_type(IsCallable(), A)
Beispiel #5
0
    def test_accepts_no_args(self):
        from typecheck import IsCallable

        try:
            IsCallable(5, 6, 7)
        except TypeError, e:
            assert str(e) == "__init__() takes exactly 1 argument (4 given)"
Beispiel #6
0
    def test_cope_with_class_changes(self):
        from typecheck import Typeclass, _TC_AttrError, _TC_TypeError
        from typecheck import IsCallable

        class A(object):
            def foo(self):
                pass

        class B(object):
            def foo(self):
                pass

        tc = Typeclass(A)
        b = B()

        # Should pass
        check_type(tc, b)

        B.foo = 5

        # B is still cached as known-good
        check_type(tc, b)

        tc.recalculate_interface()

        try:
            check_type(tc, b)
        except _TC_AttrError, e:
            assert e.attr == 'foo'
            assert isinstance(e.inner, _TC_TypeError)
            assert e.inner.right == IsCallable()
            assert e.inner.wrong == int
Beispiel #7
0
    def test_fail_1(self):
        from typecheck import Typeclass, _TC_AttrError, _TC_TypeError
        from typecheck import IsCallable

        class A(object):
            def foo(self):
                pass

        class B(object):
            def foo(self):
                pass

            def bar(self):
                pass

        class C(object):
            def __init__(self):
                self.foo = 5

        tc = Typeclass(A, B)

        try:
            check_type(tc, C())
        except _TC_AttrError, e:
            assert e.attr == 'foo'
            assert isinstance(e.inner, _TC_TypeError)
            assert e.inner.right == IsCallable()
            assert e.inner.wrong == int
Beispiel #8
0
    def test_callable_instances_oldstyle(self):
        from typecheck import IsCallable

        class A:
            def __call__(self):
                pass

        check_type(IsCallable(), A())
Beispiel #9
0
    def test_args_fail(self):
        from typecheck import accepts, IsCallable
        from typecheck import TypeCheckError, _TC_TypeError

        @accepts(IsCallable())
        def foo(a):
            return a

        try:
            foo(5)
        except TypeCheckError, e:
            assert isinstance(e.internal, _TC_TypeError)
            assert e.internal.right == 'a callable'
            assert e.internal.wrong == int

            self.assertEquals(
                str(e),
                "Argument a: for 5, expected a callable, got <type 'int'>")
Beispiel #10
0
 def IsIterable():
     return HasAttr({'__iter__': IsCallable()})
Beispiel #11
0
    def test_success(self):
        from typecheck import Xor, IsCallable

        check_type(Xor(dict, IsCallable()), pow)
        check_type(Xor(dict, IsCallable()), {'a': 5})
Beispiel #12
0
    def test_hash(self):
        from typecheck import IsCallable

        assert hash(IsCallable()) == hash(IsCallable())
Beispiel #13
0
    def test_equality(self):
        from typecheck import IsCallable

        assert IsCallable() == IsCallable()
        assert not IsCallable() != IsCallable()
Beispiel #14
0
    def test_builtins(self):
        from typecheck import IsCallable

        check_type(IsCallable(), pow)