Conditions | 3 |
Total Lines | 57 |
Code Lines | 15 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import re |
||
96 | def create_naive_test_case(function_object, test, test_id=None): |
||
97 | """ |
||
98 | Create test cases from the assertions, docstring params and return types |
||
99 | ---- |
||
100 | examples: |
||
101 | |||
102 | @need |
||
103 | from fastest.constants import TestBodies |
||
104 | @end |
||
105 | |||
106 | @let |
||
107 | function_object = { |
||
108 | 'tests': { |
||
109 | 'return': 'str', |
||
110 | 'variables': ['a = 5'] |
||
111 | }, |
||
112 | 'name': 'function_1' |
||
113 | } |
||
114 | |||
115 | test = { |
||
116 | 'from': 'function_1', |
||
117 | 'expect': '2' |
||
118 | } |
||
119 | |||
120 | exception_test = { |
||
121 | 'from': 'function_2(None)', |
||
122 | 'exception': 'TypeError' |
||
123 | } |
||
124 | |||
125 | test_id = 'a55eff11-ed51-ecb37-ccba' |
||
126 | @end |
||
127 | 1) create_naive_test_case(function_object, test, test_id) -> TestBodies.NAIVE_TEST_RESULT |
||
128 | 2) create_naive_test_case(function_object, exception_test, test_id) -> TestBodies.EXCEPTION_TEST_RESULT |
||
129 | ---- |
||
130 | :param function_object: dict |
||
131 | :param test: dict |
||
132 | :param test_id: str |
||
133 | :return: str |
||
134 | """ |
||
135 | test_template = Content.TEST_CASE_TEMPLATE.format( |
||
136 | function_name=function_object.get(Keys.NAME), |
||
137 | case_id=case_generator(test_id), |
||
138 | ) |
||
139 | |||
140 | test_template += create_assertion_test(function_object) |
||
141 | |||
142 | if test.get(Keys.EXPECT): |
||
143 | test_template += Content.ASSERTION_TEMPLATE.format(function=test.get(Keys.FROM), value=test.get(Keys.EXPECT)) |
||
144 | elif test.get(Keys.EXCEPTION): |
||
145 | args = re.search(r'\((.*)\)', test.get(Keys.FROM)) |
||
146 | print('arguments: ', args.group(1)) |
||
147 | test_template += Content.EXCEPTION_TEMPLATE.format( |
||
148 | function=function_object.get(Keys.NAME), |
||
149 | value=test.get(Keys.EXCEPTION), |
||
150 | args=args.group(1) |
||
151 | ) |
||
152 | return test_template |
||
153 |