fastest.code_assets.naive_case_detector   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 281
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 22
eloc 80
dl 0
loc 281
rs 10
c 0
b 0
f 0

9 Functions

Rating   Name   Duplication   Size   Complexity  
A format_imports() 0 21 1
B get_test_from_example_passage() 0 41 5
A get_imports_from_docstring() 0 28 3
A get_test_case_examples() 0 22 2
A get_exception_case_from_examples() 0 38 3
A stack_examples() 0 34 3
A get_return_from_docstring() 0 20 2
A get_variables_from_docstring() 0 27 2
A get_params_from_docstring() 0 23 1
1
import re
2
from fastest.constants import Keys, Patterns
3
4
5
FUNCTION_CALL = 0
6
OUTPUT = 1
7
8
9
def format_imports(import_statements):
10
    """
11
    -----
12
    examples:
13
    @need
14
    from fastest.constants import TestBodies
15
    @end
16
17
    @let
18
    import_input = TestBodies.TEST_STACK_IMPORTS_INPUT
19
    output = TestBodies.TEST_STACK_IMPORTS_OUTPUT
20
    @end
21
    1) format_imports(import_input) -> output
22
    -----
23
    :param import_statements: list
24
    :return: list
25
    """
26
    return [
27
        '{}\n'.format(import_statement.strip())
28
        for import_statement in import_statements
29
        if len(import_statement) > 0
30
    ]
31
32
33
def get_exception_case_from_examples(example_strings):
34
    """
35
    ---
36
    examples:
37
38
    @need
39
    from fastest.constants import TestBodies
40
    @end
41
42
    @let
43
    exception_example_happy_case = TestBodies.EXCEPTION_EXAMPLE_HAPPY_CASE
44
    exception_example_sep_missing = TestBodies.EXCEPTION_EXAMPLE_SEP_MISSING
45
    happy_case_output = TestBodies.EXCEPTION_HAPPY_CASE_OUTPUT
46
    @end
47
48
    1) get_exception_case_from_examples(exception_example_happy_case) -> happy_case_output
49
    2) get_exception_case_from_examples(exception_example_sep_missing) -> []
50
51
    !! get_exception_case_from_examples(None) -> Exception
52
    ---
53
    :param example_strings: str
54
    :return: list
55
    """
56
    exception_example_stack = []
57
    exception_cases = re.findall(Patterns.EXCEPTION_CASE_EXAMPLE, example_strings, re.M)
58
    for example in exception_cases:
59
        function_call_array = re.sub(r'!!\s*', '', example, 1) \
60
            .rsplit(Patterns.TEST_SEP, 1)
61
        if len(function_call_array) != 2:
62
            return []
63
64
        test_function, expectation = function_call_array
65
66
        exception_example_stack.append({
67
            Keys.FROM: test_function,
68
            Keys.EXCEPTION: expectation
69
        })
70
    return exception_example_stack
71
72
73
def get_imports_from_docstring(example_passage):
74
    """
75
    ----
76
    examples:
77
78
    @need
79
    from fastest.constants import TestBodies
80
    @end
81
82
    @let
83
    example_passage = TestBodies.EXAMPLE_WITH_IMPORTS
84
    import_statements = TestBodies.TEST_IMPORT_EXTRACTION
85
    empty_example_passage = ''
86
    @end
87
88
    1) get_imports_from_docstring(example_passage) -> import_statements
89
    2) get_imports_from_docstring(empty_example_passage) -> []
90
    ----
91
    :param example_passage: str
92
    :return: list
93
    """
94
    needed_imports = re.findall(Patterns.NEED_IMPORT, example_passage, re.M)
95
    needed_imports = needed_imports if len(needed_imports) > 0 else None
96
    if needed_imports is None:
97
        return []
98
99
    needed_imports = ''.join(needed_imports).replace(Patterns.IMPORT_DEC, '').split('\n')
100
    return format_imports(needed_imports)
101
102
103
def get_variables_from_docstring(example_passage):
104
    """
105
    ----
106
    examples:
107
    @need
108
    from fastest.constants import TestBodies
109
    @end
110
111
    @let
112
    example_passage = TestBodies.TEST_VARIABLES_FROM_DOCSTRING
113
    empty_example_passage = ''
114
    expected_output = TestBodies.TEST_VARIABLES_FROM_DOCSTRING_RESULT
115
    @end
116
117
    1) get_variables_from_docstring(empty_example_passage) -> []
118
    2) get_variables_from_docstring(example_passage) -> expected_output
119
    ----
120
    :param example_passage: str
121
    :return: list
122
    """
123
    needed_variables = re.findall(Patterns.NEEDED_VARIABLES, example_passage)
124
125
    if len(needed_variables) == 0:
126
        return []
127
    needed_variables = needed_variables[0]
128
    needed_variables = needed_variables.replace('@let', '')
129
    return needed_variables.split('\n')
130
131
132
def stack_examples(examples_strings):
133
    """
134
    ----
135
    examples:
136
137
    @need
138
    from fastest.constants import TestBodies
139
    @end
140
141
    @let
142
    example_strings = TestBodies.STACK_EXAMPLES_TEST
143
    @end
144
145
    1) stack_examples('') -> []
146
    2) stack_examples(example_strings) -> [{'expect': '25', 'from': 'square(5)'}]
147
    3) stack_examples(['1) func_do_work()']) -> []
148
    ----
149
    :param examples_strings: list
150
    :return: list
151
    """
152
    example_stack = []
153
    for example in examples_strings:
154
        function_call_array = re.sub(Patterns.NUMBER_BULLET, '', example, 1) \
155
            .rsplit(Patterns.TEST_SEP, 1)
156
        if len(function_call_array) != 2:
157
            return []
158
159
        test_function, expectation = function_call_array
160
161
        example_stack.append({
162
            Keys.FROM: test_function,
163
            Keys.EXPECT: expectation
164
        })
165
    return example_stack
166
167
168
def get_params_from_docstring(statements):
169
    """
170
    ----
171
    examples:
172
173
    @need
174
    from fastest.constants import TestBodies
175
    @end
176
177
    @let
178
    statements = TestBodies.GET_PARAMS_FROM_DOCSTRING_TEST
179
    @end
180
181
    1) get_params_from_docstring('') -> []
182
    2) get_params_from_docstring(statements) -> TestBodies.EXPECT_PARAMS
183
    ----
184
    :param statements: str
185
    :return: list
186
    """
187
    params = re.findall(r':param .*:(.*)', statements)
188
    return [
189
        param.replace(' ', '')
190
        for param in params
191
    ]
192
193
194
def get_return_from_docstring(statements):
195
    """
196
    ----
197
    examples:
198
    @need
199
    from fastest.constants import TestBodies
200
    @end
201
202
    @let
203
    statements = TestBodies.RETURN_TYPE_TEST
204
    @end
205
206
    1) get_return_from_docstring('') -> ''
207
    2) get_return_from_docstring(statements) -> 'int'
208
    ----
209
    :param statements: str
210
    :return: str
211
    """
212
    return_statement = re.search(r':return: (.*)', statements)
213
    return return_statement.group(1) if return_statement is not None else ''
214
215
216
def get_test_case_examples(example_passage):
217
    """
218
    ----
219
    examples:
220
221
    @need
222
    from fastest.constants import TestBodies
223
    @end
224
225
226
    @let
227
    example_passage = TestBodies.TEST_EXAMPLE_PASSAGE
228
    @end
229
230
    1) get_test_case_examples(example_passage) -> TestBodies.TEST_EXAMPLE_PASSAGE_RESULT
231
    ----
232
    :param example_passage: str
233
    :return: list
234
    """
235
    examples_strings = re.findall(Patterns.TEST_CASE_EXAMPLE, example_passage, re.M)
236
    examples_strings = examples_strings if len(examples_strings) > 0 else []
237
    return stack_examples(examples_strings) + get_exception_case_from_examples(example_passage)
238
239
240
def get_test_from_example_passage(statements):
241
    """
242
    ----
243
    examples:
244
245
    @need
246
    from fastest.constants import TestBodies
247
    @end
248
249
    @let
250
    statements = TestBodies.NAIVE_CASE_TEST_STATEMENT
251
    @end
252
253
    1) get_test_from_example_passage(statements) -> TestBodies.NAIVE_CASE_TEST_RESULT
254
    2) get_test_from_example_passage(None) -> {}
255
    3) get_test_from_example_passage('lorem ipsum') -> {}
256
    ----
257
    :param statements: []
258
    :return: dict
259
    """
260
    if statements is None:
261
        return {}
262
263
    example_passage = re.findall(Patterns.EXAMPLE_PASSAGE, statements, re.I)
264
    example_passage = example_passage[0] if len(example_passage) > 0 else None
265
    if example_passage is None:
266
        return {}
267
    import_statements = get_imports_from_docstring(example_passage)
268
    variables = get_variables_from_docstring(example_passage)
269
    examples = get_test_case_examples(example_passage)
270
    params = get_params_from_docstring(statements)
271
    return_statement = get_return_from_docstring(statements)
272
273
    return {} \
274
        if examples is None \
275
        else {
276
        Keys.IMPORTS: import_statements,
277
        Keys.VARIABLES: variables,
278
        Keys.EXAMPLES: examples,
279
        Keys.PARAMS: params,
280
        Keys.RETURN: return_statement
281
    }
282