Skip to content

Commit 9d64019

Browse files
committed
Use test_or_task util in new places and refactor it.
1 parent 0fabf9f commit 9d64019

File tree

4 files changed

+35
-41
lines changed

4 files changed

+35
-41
lines changed

src/robot/model/totalstatistics.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
from robot.utils import test_or_task
17+
1618
from .stats import TotalStat
1719
from .visitor import SuiteVisitor
1820

@@ -21,9 +23,8 @@ class TotalStatistics(object):
2123
"""Container for total statistics."""
2224

2325
def __init__(self, rpa=False):
24-
test_or_task = 'Tests' if not rpa else 'Tasks'
2526
#: Instance of :class:`~robot.model.stats.TotalStat` for all the tests.
26-
self._stat = TotalStat('All ' + test_or_task)
27+
self._stat = TotalStat(test_or_task('All {Test}s', rpa))
2728
self._rpa = rpa
2829

2930
def visit(self, visitor):

src/robot/result/merger.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from robot.errors import DataError
1717
from robot.model import SuiteVisitor
18-
from robot.utils import html_escape
18+
from robot.utils import html_escape, test_or_task
1919

2020

2121
class Merger(SuiteVisitor):
@@ -75,12 +75,7 @@ def visit_test(self, test):
7575
self.current.tests[index] = test
7676

7777
def _create_add_message(self, item, suite=False):
78-
if suite:
79-
item_type = 'Suite'
80-
elif self.rpa:
81-
item_type = 'Task'
82-
else:
83-
item_type = 'Test'
78+
item_type = 'Suite' if suite else test_or_task('{Test}', self.rpa)
8479
prefix = '*HTML* %s added from merged output.' % item_type
8580
if not item.message:
8681
return prefix
@@ -93,9 +88,9 @@ def _html_escape(self, message):
9388
return html_escape(message)
9489

9590
def _create_merge_message(self, new, old):
96-
header = ('*HTML* <span class="merge">'
97-
'%s has been re-executed and results merged.'
98-
'</span>' % ('Test' if not self.rpa else 'Task'))
91+
header = test_or_task('*HTML* <span class="merge">'
92+
'{Test} has been re-executed and results merged.'
93+
'</span>', self.rpa)
9994
return ''.join([
10095
header,
10196
'<hr>',

src/robot/utils/misc.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,12 @@ def seq2str2(sequence):
127127
return '[ %s ]' % ' | '.join(unic(item) for item in sequence)
128128

129129

130-
def test_or_task(text, rpa):
131-
"""If `rpa` is True, replaces occurrences of `{test}` in `text` with `task`."""
132-
def t(match):
130+
def test_or_task(text, rpa=False):
131+
"""Replaces `{test}` in `text` with `test` or `task` depending on `rpa`."""
132+
def replace(match):
133+
test = match.group(1)
133134
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)
135+
return test
136+
upper = [c.isupper() for c in test]
137+
return ''.join(c.upper() if up else c for c, up in zip('task', upper))
138+
return re.sub('{(test)}', replace, text, flags=re.IGNORECASE)

utest/utils/test_misc.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -160,24 +160,25 @@ def test_plural_or_not(self):
160160

161161
class TestTestOrTask(unittest.TestCase):
162162

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)
163+
def test_no_match(self):
164+
for inp in ['', 'No match', 'No {match}', '{No} {task} {match}']:
165+
assert_equal(test_or_task(inp), inp)
166+
assert_equal(test_or_task(inp, rpa=True), inp)
167+
168+
def test_match(self):
169+
for test, task in [('test', 'task'),
170+
('Test', 'Task'),
171+
('TEST', 'TASK'),
172+
('tESt', 'tASk')]:
173+
inp = '{%s}' % test
174+
assert_equal(test_or_task(inp, rpa=False), test)
175+
assert_equal(test_or_task(inp, rpa=True), task)
176+
177+
def test_multiple_matches(self):
178+
assert_equal(test_or_task('Contains {test}, {TEST} and {TesT}', False),
179+
'Contains test, TEST and TesT')
180+
assert_equal(test_or_task('Contains {test}, {TEST} and {TesT}', True),
181+
'Contains task, TASK and TasK')
181182

182183

183184
if __name__ == "__main__":

0 commit comments

Comments
 (0)