Skip to content

Commit aedbd28

Browse files
committed
ShowIcons: code cleanup, return ordered list, use os.scandir and add a test
1 parent 30f9553 commit aedbd28

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

src/moin/macros/ShowIcons.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111

1212
import os
13-
from os import listdir
14-
from os.path import isfile, join
1513

1614
from flask import url_for
1715

@@ -27,15 +25,13 @@ def macro(self, content, arguments, page_url, alternative):
2725
icon_dir = os.path.join(os.path.split(my_dir)[0], "static", "img", "icons")
2826

2927
headings = (_("Markup"), _("Result"))
30-
files = [f for f in listdir(icon_dir) if isfile(join(icon_dir, f))]
3128
rows = []
32-
for filename in files:
33-
markup = f"<<Icon({filename})>>"
34-
src = url_for("static", filename="img/icons/" + filename)
35-
# TODO: add alt attribute for img and add a macro test module
36-
# reason = _('Icon not rendered, verify name is valid')
37-
# alt = '<<Icon({0})>> - {1}'.format(filename, reason)
38-
rows.append((markup, html.img(attrib={html.src: src, html.alt: filename})))
29+
with os.scandir(icon_dir) as files:
30+
for file in files:
31+
if not file.name.startswith(".") and file.is_file():
32+
markup = f"<<Icon({file.name})>>"
33+
src = url_for("static", filename="img/icons/" + file.name)
34+
rows.append((markup, html.img(attrib={html.src: src, html.alt: file.name})))
3935
table = TableMixin()
40-
ret = table.build_dom_table(rows, head=headings)
36+
ret = table.build_dom_table(sorted(rows), head=headings)
4137
return ret
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright: 2024 MoinMoin:UlrichB
2+
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
3+
4+
"""
5+
Test for macros.ShowIcons
6+
"""
7+
8+
from moin.macros.ShowIcons import Macro
9+
10+
11+
def test_ShowIconsMacro():
12+
"""Call ShowIcons macro and test output"""
13+
test_icons = ["admon-note", "angry", "biggrin", "frown", "moin-rss", "smile3", "star_off"]
14+
expected_namespace = "{http://moinmo.in/namespaces/page}"
15+
expected_tags = set(f"{expected_namespace}{el_name}" for el_name in ["table", "table-header", "table-row"])
16+
expected_texts = set(f"<<Icon({icon_name}.png)>>" for icon_name in test_icons)
17+
expected_paths = set(f"/static/img/icons/{icon_name}.png" for icon_name in test_icons)
18+
macro_obj = Macro()
19+
macro_out = macro_obj.macro("content", None, "page_url", "alternative")
20+
result_tags = set()
21+
result_texts = set()
22+
result_paths = set()
23+
for node in macro_out.iter_elements_tree():
24+
if getattr(node, "tag"):
25+
result_tags.add(str(getattr(node, "tag")))
26+
if getattr(node, "text"):
27+
result_texts.add(getattr(node, "text"))
28+
if getattr(node, "attrib"):
29+
result_paths.update(getattr(node, "attrib").values())
30+
assert expected_tags.issubset(result_tags)
31+
assert expected_texts.issubset(result_texts)
32+
assert expected_paths.issubset(result_paths)

0 commit comments

Comments
 (0)