Skip to content

Commit bec3a19

Browse files
committed
Add helper to resolve test/task in messages
1 parent 2a356d2 commit bec3a19

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

src/robot/utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
from .importer import Importer
5252
from .match import eq, Matcher, MultiMatcher
5353
from .misc import (plural_or_not, printable_name, roundup, seq2str,
54-
seq2str2)
54+
seq2str2, test_or_task)
5555
from .normalizing import lower, normalize, normalize_whitespace, NormalizedDict
5656
from .platform import (IRONPYTHON, JAVA_VERSION, JYTHON, PY_VERSION,
5757
PY2, PY3, PYPY, UNIXY, WINDOWS, RERAISED_EXCEPTIONS)

src/robot/utils/misc.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from __future__ import division
1717

1818
from operator import add, sub
19+
import re
1920

2021
from .platform import PY2
2122
from .robottypes import is_integer
@@ -124,3 +125,17 @@ def seq2str2(sequence):
124125
if not sequence:
125126
return '[ ]'
126127
return '[ %s ]' % ' | '.join(unic(item) for item in sequence)
128+
129+
130+
def test_or_task(text, rpa):
131+
"""If `rpa` is True, replaces occurrences of `{test}` in `text` with `task`."""
132+
def t(match):
133+
if not rpa:
134+
return match.group(1)
135+
try:
136+
return {
137+
'TEST': 'TASK', 'Test': 'Task', 'test': 'task'
138+
}[match.group(1)]
139+
except KeyError:
140+
raise ValueError("Invalid input string '%s'." % text)
141+
return re.sub('{(.*)}', t, text)

utest/utils/test_misc.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import unittest
22

3-
from robot.utils.asserts import assert_equal
4-
from robot.utils import printable_name, seq2str, roundup, plural_or_not, IRONPYTHON
3+
from robot.utils.asserts import assert_equal, assert_raises_with_msg
4+
from robot.utils import (
5+
printable_name, seq2str, roundup, plural_or_not, IRONPYTHON, test_or_task
6+
)
57

68

79
class TestRoundup(unittest.TestCase):
@@ -155,5 +157,28 @@ def test_plural_or_not(self):
155157
assert_equal(plural_or_not(plural), 's')
156158

157159

160+
161+
class TestTestOrTask(unittest.TestCase):
162+
163+
def test_test_or_task(self):
164+
for inp, rpa, exp in [
165+
('{Test}', False, 'Test'),
166+
('{test}', False, 'test'),
167+
('{TEST}', False, 'TEST'),
168+
('{Test}', True, 'Task'),
169+
('{test}', True, 'task'),
170+
('{TEST}', True, 'TASK'),
171+
('Contains {test}', False, 'Contains test'),
172+
('Contains {TEST}', True, 'Contains TASK'),
173+
('Does not contain match', False, 'Does not contain match')
174+
]:
175+
assert_equal(test_or_task(inp, rpa), exp)
176+
177+
def test_test_or_task_fails_with_invalid_pattern_in_braces(self):
178+
assert_raises_with_msg(
179+
ValueError, "Invalid input string '{TeSt}'.",
180+
test_or_task, '{TeSt}', True)
181+
182+
158183
if __name__ == "__main__":
159184
unittest.main()

0 commit comments

Comments
 (0)