Skip to content

Commit 6b0c6e0

Browse files
committed
tests
1 parent 5972df1 commit 6b0c6e0

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/textual/_slug.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,17 @@ def slug_for_tcss_id(text: str) -> str:
128128
Returns:
129129
A slugified version of text suitable for use as a TCSS id.
130130
"""
131+
131132
slug = "".join(
132133
(
133134
character
134135
if character in VALID_ID_CHARACTERS
135136
else ord(character).__format__("x")
136137
)
137-
for character in text
138+
for character in text.casefold().replace(" ", "-")
138139
)
139140
if not slug:
140-
return "-"
141+
return "_"
141142
if slug[0].isdecimal():
142-
return f"-{slug}"
143+
return f"_{slug}"
143144
return slug

tests/test_slug.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

3-
from textual._slug import TrackedSlugs, slug_for_tcss_id
3+
from textual._slug import TrackedSlugs, slug, slug_for_tcss_id
4+
from textual.dom import check_identifiers
45

56

67
@pytest.mark.xdist_group("group1")
@@ -31,7 +32,7 @@
3132
)
3233
def test_simple_slug(text: str, expected: str) -> None:
3334
"""The simple slug function should produce the expected slug."""
34-
assert slug_for_tcss_id(text) == expected
35+
assert slug(text) == expected
3536

3637

3738
@pytest.fixture(scope="module")
@@ -64,3 +65,24 @@ def tracker() -> TrackedSlugs:
6465
def test_tracked_slugs(tracker: TrackedSlugs, text: str, expected: str) -> None:
6566
"""The tracked slugging class should produce the expected slugs."""
6667
assert tracker.slug(text) == expected
68+
69+
70+
@pytest.mark.parametrize(
71+
"text, expected",
72+
[
73+
("", "_"),
74+
(" ", "-"),
75+
("5", "_5"),
76+
("a", "a"),
77+
("hello world", "hello-world"),
78+
("🙂", "_1f642"),
79+
("🙂🙂", "_1f6421f642"),
80+
("Foo🙂", "foo1f642"),
81+
("ß", "ss"),
82+
],
83+
)
84+
def test_slug_for_tcss_id(text: str, expected: str) -> None:
85+
"""Test the slug_for_tcss_id"""
86+
slug = slug_for_tcss_id(text)
87+
assert slug == expected
88+
check_identifiers(slug)

0 commit comments

Comments
 (0)