Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 68 additions & 64 deletions mod_test/nicediff/diff.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import difflib
import html
import re
from typing import Any, List, Optional, Tuple, Union
Expand Down Expand Up @@ -121,12 +122,12 @@
return result


def get_html_diff(test_correct_lines: List[str], test_res_lines: List[str], to_view: bool = True) -> str:

Check failure on line 125 in mod_test/nicediff/diff.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 50 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=CCExtractor_sample-platform&issues=AZ0t0H9Rcq4s5iMQkB6C&open=AZ0t0H9Rcq4s5iMQkB6C&pullRequest=1080
"""Return generated difference in HTML formatted table."""
"""Return generated difference in HTML formatted table using smart sequence matching."""
# variable to keep count of diff lines noted
number_of_noted_diff_lines = 0

html = """
html_out = """
<table>
<tr>
<td class="diff-table-td" style="width: 30px;">n&deg;</td>
Expand All @@ -138,81 +139,84 @@
</tr>
</table>"""

res_len = len(test_res_lines)
correct_len = len(test_correct_lines)

if res_len <= correct_len:
use = res_len
till = correct_len

else:
use = correct_len
till = res_len

for line in range(use):
sm = difflib.SequenceMatcher(None, test_res_lines, test_correct_lines)

for tag, i1, i2, j1, j2 in sm.get_opcodes():
if to_view and number_of_noted_diff_lines >= MAX_NUMBER_OF_LINES_TO_VIEW:
break

if test_correct_lines[line] == test_res_lines[line]:
if tag == 'equal':
continue
html += """

if tag == 'replace':
max_len = max(i2 - i1, j2 - j1)
for k in range(max_len):
if to_view and number_of_noted_diff_lines >= MAX_NUMBER_OF_LINES_TO_VIEW:
break

html_out += """
<table>"""

Check failure on line 158 in mod_test/nicediff/diff.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal """ <table>""" 3 times.

See more on https://sonarcloud.io/project/issues?id=CCExtractor_sample-platform&issues=AZ0t0H9Rcq4s5iMQkB6B&open=AZ0t0H9Rcq4s5iMQkB6B&pullRequest=1080
actual, expected = _process(test_res_lines[line], test_correct_lines[line], suffix_id=str(line))

html += f"""

res_idx = i1 + k
corr_idx = j1 + k

res_line = test_res_lines[res_idx] if res_idx < i2 else " "
corr_line = test_correct_lines[corr_idx] if corr_idx < j2 else " "
line_display = str(res_idx + 1) if res_idx < i2 else ""

actual, expected = _process(res_line, corr_line, suffix_id=str(res_idx if res_idx < i2 else corr_idx))

html_out += f"""
<tr>
<td class="diff-table-td" style="width: 30px;">{line + 1}</td>
<td class="diff-table-td" style="width: 30px;">{line_display}</td>
<td class="diff-table-td">{actual}</td>
</tr>
<tr>
<td class="diff-table-td" style="width: 30px;"></td>
<td class="diff-table-td">{expected}</td>
</tr>"""
html += """
html_out += """
</table>"""

Check failure on line 179 in mod_test/nicediff/diff.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal """ </table>""" 3 times.

See more on https://sonarcloud.io/project/issues?id=CCExtractor_sample-platform&issues=AZ0t0H9Rcq4s5iMQkB6A&open=AZ0t0H9Rcq4s5iMQkB6A&pullRequest=1080
number_of_noted_diff_lines += 1

# increase noted diff line by one
number_of_noted_diff_lines += 1

# processing remaining lines
for line in range(use, till):

# stop at 50 lines if test-data for viewing
if to_view and number_of_noted_diff_lines >= MAX_NUMBER_OF_LINES_TO_VIEW:
break

html += """
elif tag == 'delete':
for k in range(i1, i2):
if to_view and number_of_noted_diff_lines >= MAX_NUMBER_OF_LINES_TO_VIEW:
break
html_out += """
<table>"""

if till == res_len:
output, _ = _process(test_res_lines[line], " ", suffix_id=str(line))
html += f"""
<tr>
<td class="diff-table-td" style="width: 30px;">{line + 1}</td>
<td class="diff-table-td">{output}</td>
</tr>
<tr>
<td class="diff-table-td" style="width: 30px;"></td>
<td class="diff-table-td"></td>
</tr>
"""
else:
_, output = _process(" ", test_correct_lines[line], suffix_id=str(line))
html += f"""
<tr>
<td class="diff-table-td" style="width: 30px;">{line + 1}</td>
<td class="diff-table-td"></td>
</tr>
<tr>
<td class="diff-table-td" style="width: 30px;"></td>
<td class="diff-table-td">{output}</td>
</tr>
"""

html += """
output, _ = _process(test_res_lines[k], " ", suffix_id=str(k))
html_out += f"""
<tr>
<td class="diff-table-td" style="width: 30px;">{k + 1}</td>
<td class="diff-table-td">{output}</td>
</tr>
<tr>
<td class="diff-table-td" style="width: 30px;"></td>
<td class="diff-table-td"></td>
</tr>"""
html_out += """
</table>"""
number_of_noted_diff_lines += 1

elif tag == 'insert':
for k in range(j1, j2):
if to_view and number_of_noted_diff_lines >= MAX_NUMBER_OF_LINES_TO_VIEW:
break
html_out += """
<table>"""
_, output = _process(" ", test_correct_lines[k], suffix_id=str(k))
html_out += f"""
<tr>
<td class="diff-table-td" style="width: 30px;"></td>
<td class="diff-table-td"></td>
</tr>
<tr>
<td class="diff-table-td" style="width: 30px;"></td>
<td class="diff-table-td">{output}</td>
</tr>"""
html_out += """
</table>"""
number_of_noted_diff_lines += 1

# increase noted diff line by one
number_of_noted_diff_lines += 1

return html
return html_out