diff options
author | Jaime Resano <gemailpersonal02@gmail.com> | 2022-02-24 01:52:28 +0100 |
---|---|---|
committer | Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io> | 2022-06-29 20:34:31 +0000 |
commit | 168f0c941cefe2fcdaaa12498272f181fe246986 (patch) | |
tree | c2e3247285484f613a3be6ca030705276c408645 /tools | |
parent | 0e8ab25c4ccbd46e62bbb81b169ec0d227cbfc33 (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
Pick-to: 6.2 6.3
Change-Id: Idffbb0aee258522d7855e2ad0e2b8df61a1872c8
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'tools')
-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 5e31f50ef..cf34bc6cd 100644 --- a/tools/snippets_translate/main.py +++ b/tools/snippets_translate/main.py @@ -9,6 +9,7 @@ import sys from enum import Enum from pathlib import Path from textwrap import dedent +from typing import List from converter import snippet_translate @@ -149,28 +150,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 17ab0b449..be46e0c0b 100644 --- a/tools/snippets_translate/tests/test_converter.py +++ b/tools/snippets_translate/tests/test_converter.py @@ -355,10 +355,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 6977b8278..6c451c84e 100644 --- a/tools/snippets_translate/tests/test_snippets.py +++ b/tools/snippets_translate/tests/test_snippets.py @@ -1,17 +1,101 @@ # Copyright (C) 2022 The Qt Company Ltd. # SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only -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"] |