1
|
|
|
from pathlib import Path |
2
|
|
|
from typing import Callable |
3
|
|
|
from unittest import TestCase |
4
|
|
|
|
5
|
|
|
from barentsz._discover import discover_functions |
6
|
|
|
|
7
|
|
|
import sys |
8
|
|
|
|
9
|
|
|
sys.path.append(str(Path(__file__).parent.parent / 'test_resources')) |
10
|
|
|
from examples_for_tests._private_module import ( |
11
|
|
|
_private_function as private_module_private_function, |
12
|
|
|
public_function, |
13
|
|
|
) |
14
|
|
|
from examples_for_tests.level2 import module1 as module1_level2 |
15
|
|
|
from examples_for_tests.level2.module1 import function1 as function1_level2 |
16
|
|
|
from examples_for_tests.module1 import function1, _private_function |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
class TestDiscoverFunctions(TestCase): |
20
|
|
|
|
21
|
|
|
def test_discover_functions_in_path(self): |
22
|
|
|
# SETUP |
23
|
|
|
path_to_resources = (Path(__file__).parent.parent / 'test_resources' |
24
|
|
|
/ 'examples_for_tests') |
25
|
|
|
|
26
|
|
|
# EXECUTE |
27
|
|
|
functions = discover_functions(path_to_resources) |
28
|
|
|
|
29
|
|
|
# VERIFY |
30
|
|
|
self.assertEqual(2, len(functions)) |
31
|
|
|
self.assertIn(function1, functions) |
32
|
|
|
self.assertIn(function1_level2, functions) |
33
|
|
|
|
34
|
|
|
def test_discover_functions_in_module(self): |
35
|
|
|
# EXECUTE |
36
|
|
|
functions = discover_functions(module1_level2) |
37
|
|
|
|
38
|
|
|
# VERIFY |
39
|
|
|
self.assertEqual(1, len(functions)) |
40
|
|
|
self.assertIn(function1_level2, functions) |
41
|
|
|
|
42
|
|
|
def test_discover_functions_in_class(self): |
43
|
|
|
# SETUP |
44
|
|
|
class C: |
45
|
|
|
def f1(self): |
46
|
|
|
... |
47
|
|
|
|
48
|
|
|
def _f2(self): |
49
|
|
|
... |
50
|
|
|
|
51
|
|
|
@staticmethod |
52
|
|
|
def f3(): |
53
|
|
|
... |
54
|
|
|
|
55
|
|
|
@classmethod |
56
|
|
|
def f4(cls): |
57
|
|
|
... |
58
|
|
|
|
59
|
|
|
# EXECUTE |
60
|
|
|
functions = discover_functions(C, include_privates=True) |
61
|
|
|
|
62
|
|
|
# VERIFY |
63
|
|
|
self.assertEqual(4, len(functions)) |
64
|
|
|
self.assertIn(C.f1, functions) |
65
|
|
|
self.assertIn(C._f2, functions) |
66
|
|
|
self.assertIn(C.f3, functions) |
67
|
|
|
self.assertIn(C.f4, functions) |
68
|
|
|
|
69
|
|
|
def test_discover_functions_in_private_modules(self): |
70
|
|
|
# SETUP |
71
|
|
|
path_to_resources = (Path(__file__).parent.parent / 'test_resources' |
72
|
|
|
/ 'examples_for_tests') |
73
|
|
|
|
74
|
|
|
# EXECUTE |
75
|
|
|
functions = discover_functions(path_to_resources, in_private_modules=True) |
76
|
|
|
|
77
|
|
|
# VERIFY |
78
|
|
|
self.assertEqual(3, len(functions)) |
79
|
|
|
self.assertIn(function1, functions) |
80
|
|
|
self.assertIn(function1_level2, functions) |
81
|
|
|
self.assertIn(public_function, functions) |
82
|
|
|
|
83
|
|
|
def test_discover_private_functions(self): |
84
|
|
|
# SETUP |
85
|
|
|
path_to_resources = (Path(__file__).parent.parent / 'test_resources' |
86
|
|
|
/ 'examples_for_tests') |
87
|
|
|
|
88
|
|
|
# EXECUTE |
89
|
|
|
functions = discover_functions(path_to_resources, include_privates=True) |
90
|
|
|
|
91
|
|
|
# VERIFY |
92
|
|
|
self.assertEqual(3, len(functions)) |
93
|
|
|
self.assertIn(function1, functions) |
94
|
|
|
self.assertIn(function1_level2, functions) |
95
|
|
|
self.assertIn(_private_function, functions) |
96
|
|
|
|
97
|
|
|
def test_discover_functions_with_signature(self): |
98
|
|
|
# SETUP |
99
|
|
|
path_to_resources = (Path(__file__).parent.parent / 'test_resources' |
100
|
|
|
/ 'examples_for_tests') |
101
|
|
|
|
102
|
|
|
# EXECUTE |
103
|
|
|
functions = discover_functions(path_to_resources, |
104
|
|
|
include_privates=True, |
105
|
|
|
in_private_modules=True, |
106
|
|
|
signature=Callable[[int, float], str]) |
107
|
|
|
|
108
|
|
|
# VERIFY |
109
|
|
|
self.assertEqual(1, len(functions)) |
110
|
|
|
self.assertIn(private_module_private_function, functions) |
111
|
|
|
|
112
|
|
|
def test_discover_functions_with_wrong_argument(self): |
113
|
|
|
# EXECUTE & VALIDATE |
114
|
|
|
with self.assertRaises(ValueError): |
115
|
|
|
discover_functions(123) |
116
|
|
|
|