| 17 | |
| 18 | |
| 19 | def overwrite_file(file, class_name, test_name, correct_line, done_test): |
| 20 | _id = f"{file}_{class_name}_{test_name}" |
| 21 | done_test[_id] += 1 |
| 22 | |
| 23 | with open(file, "r") as f: |
| 24 | lines = f.readlines() |
| 25 | |
| 26 | class_regex = f"class {class_name}(" |
| 27 | test_regex = f"{4 * ' '}def {test_name}(" |
| 28 | line_begin_regex = f"{8 * ' '}{correct_line.split()[0]}" |
| 29 | another_line_begin_regex = f"{16 * ' '}{correct_line.split()[0]}" |
| 30 | in_class = False |
| 31 | in_func = False |
| 32 | in_line = False |
| 33 | insert_line = False |
| 34 | count = 0 |
| 35 | spaces = 0 |
| 36 | |
| 37 | new_lines = [] |
| 38 | for line in lines: |
| 39 | if line.startswith(class_regex): |
| 40 | in_class = True |
| 41 | elif in_class and line.startswith(test_regex): |
| 42 | in_func = True |
| 43 | elif in_class and in_func and (line.startswith(line_begin_regex) or line.startswith(another_line_begin_regex)): |
| 44 | spaces = len(line.split(correct_line.split()[0])[0]) |
| 45 | count += 1 |
| 46 | |
| 47 | if count == done_test[_id]: |
| 48 | in_line = True |
| 49 | |
| 50 | if in_class and in_func and in_line: |
| 51 | if ")" not in line: |
| 52 | continue |
| 53 | else: |
| 54 | insert_line = True |
| 55 | |
| 56 | if in_class and in_func and in_line and insert_line: |
| 57 | new_lines.append(f"{spaces * ' '}{correct_line}") |
| 58 | in_class = in_func = in_line = insert_line = False |
| 59 | else: |
| 60 | new_lines.append(line) |
| 61 | |
| 62 | with open(file, "w") as f: |
| 63 | for line in new_lines: |
| 64 | f.write(line) |
| 65 | |
| 66 | |
| 67 | def main(correct, fail=None): |