File: TestDecorators.py

package info (click to toggle)
uranium 5.0.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,304 kB
  • sloc: python: 31,765; sh: 132; makefile: 12
file content (101 lines) | stat: -rw-r--r-- 2,226 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Copyright (c) 2019 Ultimaker B.V.
# Uranium is released under the terms of the LGPLv3 or higher.

import pytest

from UM.Decorators import interface

def test_interface():
    def declare_interface():
        @interface
        class TestInterface:
            def test(self):
                pass

            def test2(self):
                pass

        return TestInterface

    cls = declare_interface()
    assert cls is not None

    def declare_subclass(cls):
        class TestSubclass(cls):
            def __init__(self):
                super().__init__()

            def test(self):
                print("test")

            def test2(self):
                print("test2")

        return TestSubclass

    cls = declare_subclass(cls)
    assert cls is not None

    sub = cls()
    assert sub is not None

    def declare_bad_subclass():
        @interface
        class TestInterface:
            def test(self):
                pass

        class TestSubclass(TestInterface):
            pass

        return TestSubclass()

    with pytest.raises(NotImplementedError):
        declare_bad_subclass()

    def declare_good_signature():
        @interface
        class TestInterface:
            def test(self, one, two, three = None):
                pass

        class TestSubclass(TestInterface):
            def test(self, one, two, three = None):
                pass

        return TestSubclass()

    sub = declare_good_signature()
    assert sub is not None

    def declare_bad_signature():
        @interface
        class TestInterface:
            def test(self, one, two, three = None):
                pass

        class TestSubclass(TestInterface):
            def test(self, one):
                pass

        return TestSubclass()

    with pytest.raises(NotImplementedError):
        declare_bad_signature()

    #
    # private functions should be ignored
    #
    def should_ignore_private_functions():
        @interface
        class TestInterface:
            def __should_be_ignored(self):
                pass

        class TestSubClass(TestInterface):
            pass

        return TestSubClass()

    sub = should_ignore_private_functions()
    assert sub is not None