Source

intfprgm / intfprgm / tests.py

#!/usr/bin/env python

import unittest

from intfprgm import interface, abstract, concrete, overrides, private


class InterfaceTest(unittest.TestCase):

    def test_init(self):
        @interface
        class test:
            pass
        self.assertRaises(SyntaxError, test)

    def test_method_raise(self):
        class test:
            def __init__(self):
                pass
        self.assertRaises(SyntaxError, interface, test)

    def test_method_okay(self):
        class test:
            def __init__(self):
                raise NotImplementedError()
        interface(test)

    def test_abstract(self):
        class test1:
            def method(self):
                pass
        class test2:
            @abstract
            def method(self):
                pass
        self.assertRaises(SyntaxError, interface, test1)
        interface(test2)


class AbstractTest(unittest.TestCase):

    def test_init(self):
        @abstract
        class test:
            def method(self):
                raise NotImplementedError()
        self.assertRaises(SyntaxError, test)

    def test_method(self):
        class test:
            pass
        self.assertRaises(SyntaxError, abstract, test)

    def test_okay(self):
        class test1:
            def method(self):
                raise NotImplementedError
        abstract(test1)

        class test2:
            def method(self):
                '''doctring'''
                raise NotImplementedError
        abstract(test2)

        class test3:
            def method(self):
                raise NotImplementedError()
        abstract(test3)

        class test4:
            def method(self):
                '''docstring'''
                raise NotImplementedError()
        abstract(test4)

    def test_function(self):
        @abstract
        def raises():
            pass
        self.assertRaises(NotImplementedError, raises)


class ConcreteTest(unittest.TestCase):

    def test_raise(self):
        class test1:
            def method(self):
                raise NotImplementedError
        self.assertRaises(SyntaxError, concrete, test1)

        class test2:
            def method(self):
                raise NotImplementedError()
        self.assertRaises(SyntaxError, concrete, test2)

    def test_not_raise(self):
        class test:
            def method(self):
                return NotImplementedError()
        concrete(test)

    def test_subclass(self):
        class parent(object):
            def override_me(self):
                raise NotImplementedError()
        class test(parent):
            def leave_it(self):
                pass
        self.assertRaises(SyntaxError, concrete, test)

    def test_abstract(self):
        class test1:
            def method(self):
                pass
        class test2:
            @abstract
            def method(self):
                pass
        concrete(test1)
        self.assertRaises(SyntaxError, concrete, test2)


class OverridesTest(unittest.TestCase):

    def test_found(self):
        class Base(object):
            def method_1(self):
                pass
        class Derived(Base):
            @overrides
            def method_1(self):
                pass
        overrides(Derived)

    def test_not_found(self):
        class Base(object):
            pass
        class Derived(Base):
            @overrides
            def method_1(self):
                pass
        self.assertRaises(RuntimeError, overrides, Derived)

    def test_check_argspec_true(self):
        class Base(object):
            def method_1(self, arg1):
                pass
        class Derived_1(Base):
            @overrides(True)
            def method_1(self):
                pass
        self.assertRaises(RuntimeError, overrides, Derived_1)

    def test_check_argspec_false(self):
        class Base(object):
            def method_1(self, arg1):
                pass
        class Derived_2(Base):
            @overrides(False)
            def method_1(self):
                pass
        overrides(Derived_2)

    def test_matched_signature_up_there_1(self):
        class Base(object):
            def method_1(self, arg1):
                pass
        class Derived_1(Base):
            @overrides
            def method_1(self, *args):
                pass
        class Derived_2(Derived_1):
            @overrides
            def method_1(self, arg1):
                pass
        overrides(Derived_2)


class PrivateTest(unittest.TestCase):

    def test_private_okay(self):
        class Super(object):
            @private('__main__.Super')
            def method(self):
                return 'Super'
        try:
            self.assertEqual('Super', Super().method())
        except TypeError:
            pass

    def test_private_fail(self):
        class Super(object):
            @private('Super')
            def method(self):
                return 'Super'
        self.assertRaises(TypeError, Super().method)

    def test_private_with_args_fail(self):
        class Super(object):
            @private('Super')
            def say(self, msg):
                return msg
        self.assertRaises(TypeError, Super().say, 'hello')

    def test_private_with_args_okay(self):
        class Super(object):
            @private('__main__.Super')
            def say(self, msg):
                return msg
        self.assertEqual('hello', Super().say('hello'))


if __name__ == '__main__':
    unittest.main()