8000 Fix `Tags.robot()` if reserved tag contains `:`. · robotframework/robotframework@afcbbff · GitHub
[go: up one dir, main page]

Skip to content

Commit afcbbff

Browse files
committed
Fix Tags.robot() if reserved tag contains :.
We don't have such reserved tags now, but may have in the future. Also add public API to `NormalizedDict` for getting normalized keys.
1 parent aac8589 commit afcbbff

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

src/robot/model/tags.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def __init__(self, tags: Sequence[str] = ()):
2626
self._tags, self._reserved = self._init_tags(tags)
2727

2828
def robot(self, name: str) -> bool:
29-
"""Check do tags contain a special tag in format `robot:<name>`.
29+
"""Check do tags contain a reserved tag in format `robot:<name>`.
3030
3131
This is same as `'robot:<name>' in tags` but considerably faster.
3232
"""
@@ -40,14 +40,13 @@ def _init_tags(self, tags) -> 'tuple[tuple[str, ...], tuple[str, ...]]':
4040
return self._normalize(tags)
4141

4242
def _normalize(self, tags):
43-
normalized = NormalizedDict([(str(t), None) for t in tags], ignore='_')
44-
if '' in normalized:
45-
del normalized['']
46-
if 'NONE' in normalized:
47-
del normalized['NONE']
48-
reserved = tuple(tag.split(':')[1] for tag in normalized._keys
49-
if tag[:6] == 'robot:')
50-
return tuple(normalized), reserved
43+
nd = NormalizedDict([(str(t), None) for t in tags], ignore='_')
44+
if '' in nd:
45+
del nd['']
46+
if 'NONE' in nd:
47+
del nd['NONE']
48+
reserved = tuple(tag[6:] for tag in nd.normalized_keys if tag[:6] == 'robot:')
49+
return tuple(nd), reserved
5150

5251
def add(self, tags: Sequence[str]):
5352
self.__init__(tuple(self) + tuple(Tags(tags)))

src/robot/utils/normalizing.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,13 @@ def __init__(self, initial=None, ignore=(), caseless=True, spaceless=True):
6262
self._keys = {}
6363
self._normalize = lambda s: normalize(s, ignore, caseless, spaceless)
6464
if initial:
65-
self._add_initial(initial)
65+
items = initial.items() if hasattr(initial, 'items') else initial
66+
for key, value in items:
67+
self[key] = value
6668

67-
def _add_initial(self, initial):
68-
items = initial.items() if hasattr(initial, 'items') else initial
69-
for key, value in items:
70-
self[key] = value
69+
@property
70+
def normalized_keys(self):
71+
return self._keys
7172

7273
def __getitem__(self, key):
7374
return self._data[self._normalize(key)]

utest/model/test_tags.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ def test_init_with_non_strings(self):
2828
def test_init_with_none(self):
2929
assert_equal(list(Tags(None)), [])
3030

31+
def test_robot(self):
32+
assert_equal(Tags().robot('x'), False)
33+
assert_equal(Tags('robot:x').robot('x'), True)
34+
assert_equal(Tags(['ROBOT : X']).robot('x'), True)
35+
assert_equal(Tags('robot:x:y').robot('x:y'), True)
36+
assert_equal(Tags('robot:x').robot('y'), False)
37+
3138
def test_add_string(self):
3239
tags = Tags(['Y'])
3340
tags.add('x')

0 commit comments

Comments
 (0)
0