Source

intfprgm / intfprgm / tests.py

#!/usr/bin/env python

import unittest

from intfprgm import interface, abstract, concrete

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 test:
            def method(self):
                raise NotImplementedError
        abstract(test)

    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)


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