aboutsummaryrefslogtreecommitdiffstats
path: root/tools/snippets_translate/tests
diff options
context:
space:
mode:
authorJaime Resano <gemailpersonal02@gmail.com>2022-02-24 01:52:28 +0100
committerCristian Maureira-Fredes <cristian.maureira-fredes@qt.io>2022-06-29 20:34:31 +0000
commit168f0c941cefe2fcdaaa12498272f181fe246986 (patch)
treec2e3247285484f613a3be6ca030705276c408645 /tools/snippets_translate/tests
parent0e8ab25c4ccbd46e62bbb81b169ec0d227cbfc33 (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/snippets_translate/tests')
-rw-r--r--tools/snippets_translate/tests/test_converter.py2
-rw-r--r--tools/snippets_translate/tests/test_snippets.py102
2 files changed, 95 insertions, 9 deletions
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"]