diff options
author | Jaime Resano <gemailpersonal02@gmail.com> | 2022-02-24 01:52:28 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-06-29 22:22:00 +0000 |
commit | 5ab52b0696bca62039a0c367315df048e7744494 (patch) | |
tree | 0e4dba6e9fc5fc9216bff91258fee0c9150606a3 | |
parent | 5cc0ba1366ccc7855cf92f901b1efeb6ffadb021 (diff) |
snippet translate: fix get_snippets
- Fixed the get_snippets function which did not work properly
when more than one snippet id was on the same line.
- Tests were added
Change-Id: Idffbb0aee258522d7855e2ad0e2b8df61a1872c8
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
(cherry picked from commit 168f0c941cefe2fcdaaa12498272f181fe246986)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | tools/snippets_translate/main.py | 49 | ||||
-rw-r--r-- | tools/snippets_translate/tests/test_converter.py | 2 | ||||
-rw-r--r-- | tools/snippets_translate/tests/test_snippets.py | 102 |
3 files changed, 128 insertions, 25 deletions
diff --git a/tools/snippets_translate/main.py b/tools/snippets_translate/main.py index 0f62ddabd..9e8992d63 100644 --- a/tools/snippets_translate/main.py +++ b/tools/snippets_translate/main.py @@ -45,6 +45,7 @@ import sys from enum import Enum from pathlib import Path from textwrap import dedent +from typing import List from converter import snippet_translate @@ -185,28 +186,44 @@ def is_valid_file(x): return True -def get_snippet_ids(line): - """Extract the snippet ids for a line '//! [1] //! [2]'""" +def get_snippet_ids(line: str) -> List[str]: + # Extract the snippet ids for a line '//! [1] //! [2]' result = [] for m in SNIPPET_PATTERN.finditer(line): result.append(m.group(1)) return result -def get_snippets(data): - """Extract (potentially overlapping) snippets from a C++ file indicated by //! [1]""" - current_snippets = [] # Active ids - snippets = [] - for line in data: - new_ids = get_snippet_ids(line) - for id in new_ids: - if id in current_snippets: # id encountered 2nd time: Snippet ends - current_snippets.remove(id) - else: - current_snippets.append(id) - - if new_ids or current_snippets: - snippets.append(line) +def get_snippets(lines: List[str]) -> List[List[str]]: + # Extract (potentially overlapping) snippets from a C++ file indicated by //! [1] + snippets: List[List[str]] = [] + snippet: List[str] + + i = 0 + while i < len(lines): + line = lines[i] + i += 1 + + start_ids = get_snippet_ids(line) + while start_ids: + # Start of a snippet + start_id = start_ids.pop(0) + snippet = [line] # The snippet starts with his id + + # Find the end of the snippet + j = i + while j < len(lines): + l = lines[j] + j += 1 + + # Add the line to the snippet + snippet.append(l) + + # Check if the snippet is complete + if start_id in get_snippet_ids(l): + # End of snippet + snippets.append(snippet) + break return snippets diff --git a/tools/snippets_translate/tests/test_converter.py b/tools/snippets_translate/tests/test_converter.py index 6f4048bc6..a41f3190e 100644 --- a/tools/snippets_translate/tests/test_converter.py +++ b/tools/snippets_translate/tests/test_converter.py @@ -391,10 +391,12 @@ def test_ternary_operator(): == "if not game.saveGame(json if Game.Json else Game.Binary):" ) + def test_useless_qt_classes(): assert st('result += QLatin1String("; ");') == 'result += "; "' assert st("<< QLatin1Char('\0') << endl;") == "print('\0')" + def test_special_cases(): assert ( st('http->setProxy("proxy.example.com", 3128);') diff --git a/tools/snippets_translate/tests/test_snippets.py b/tools/snippets_translate/tests/test_snippets.py index 0a3f4f021..802bbc662 100644 --- a/tools/snippets_translate/tests/test_snippets.py +++ b/tools/snippets_translate/tests/test_snippets.py @@ -37,17 +37,101 @@ ## ############################################################################# -from main import get_snippets +from main import get_snippets, get_snippet_ids -SNIPPETS = ["pretext", - "//![some name]", "line1", - "//! [some name] [some other name]", - "line2", - "//! [some other name]", - "posttext"] +def test_stacking(): + lines = [ + "//! [A] //! [B] ", + "//! [C] //! [D] //! [E]", + "// Content", + "//! [C] //! [A] ", + "//! [B] //! [D] //! [E]", + ] + snippets = get_snippets(lines) + assert len(snippets) == 5 + assert len(snippets[0]) == 4 # A starts at line 0 and ends at line 3 + assert len(snippets[1]) == 5 # B starts at line 0 and ends at line 4 + assert len(snippets[2]) == 3 # C starts at line 1 and ends at line 3 + assert len(snippets[3]) == 4 # D starts at line 1 and ends at line 4 + assert len(snippets[4]) == 4 # E starts at line 1 and ends at line 4 + + +def test_nesting(): + lines = [ + "//! [A]", + "//! [B]", + "//! [C]", + "// Content", + "//! [A]", + "//! [C]", + "//! [B]", + ] + snippets = get_snippets(lines) + assert len(snippets) == 3 + + assert len(snippets[0]) == 5 + assert snippets[0] == lines[:5] + + assert len(snippets[1]) == 6 + assert snippets[1] == lines[1:] + + assert len(snippets[2]) == 4 + assert snippets[2] == lines[2:6] + + +def test_overlapping(): + lines = [ + "pretext", + "//! [A]", + "l1", + "//! [C]", + "//! [A] //! [B]", + "l2", + "l3 // Comment", + "//! [B]", + "posttext", + "//! [C]", + ] + snippets = get_snippets(lines) + assert len(snippets) == 3 + + assert len(snippets[0]) == 4 + assert snippets[0] == lines[1:5] + + assert len(snippets[1]) == 7 + assert snippets[1] == lines[3:] + + assert len(snippets[2]) == 4 + assert snippets[2] == lines[4:8] def test_snippets(): - extracted = get_snippets(SNIPPETS) - assert len(extracted) == len(SNIPPETS) - 2 + lines = [ + "pretext", + "//! [A]", + "l1", + "//! [A] //! [B]", + "l2", + "l3 // Comment", + "//! [B]", + "posttext" + ] + + snippets = get_snippets(lines) + assert len(snippets) == 2 + + assert len(snippets[0]) == 3 + assert snippets[0] == lines[1:4] + + assert len(snippets[1]) == 4 + assert snippets[1] == lines[3:7] + + +def test_snippet_ids(): + assert get_snippet_ids("") == [] + assert get_snippet_ids("//! ") == [] # Invalid id + assert get_snippet_ids("//! [some name]") == ["some name"] + assert get_snippet_ids("//! [some name] [some other name]") == ["some name"] + assert get_snippet_ids("//! [some name] //! ") == ["some name"] # Invalid id + assert get_snippet_ids("//! [some name] //! [some other name]") == ["some name", "some other name"] |