Skip to content

Commit 9c7d791

Browse files
authored
RenameTestCases capitalize each word should now work for words in brackets (#498)
1 parent bf35a64 commit 9c7d791

File tree

9 files changed

+55
-2
lines changed

9 files changed

+55
-2
lines changed

docs/releasenotes/4.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ Bugs
301301

302302
- Keyword name will no longer be prefixed with continuation marks (``...``) if name was longer than line length limit (#494)
303303
- It is now safe to use Tasks sections with ``MergeAndOrderSections`` transformer with Robot Framework 6.0+ (#490)
304+
- Test case titles are now capitalized with ``RenameTestCases:capitalize_each_word=True`` even if the word is inside brackets (#485)
304305
- It is now possible to use disabler in the first line and enable it later (previously the whole file was always ignored) (#479)::
305306

306307
# robotidy: off

robotidy/transformers/RenameTestCases.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
import string
23
from typing import Optional
34

45
from robot.api.parsing import Token
@@ -8,6 +9,36 @@
89
from robotidy.transformers import Transformer
910

1011

12+
def cap_string_until_succeed(word: str):
13+
"""
14+
Yield characters from the word and capitalize character until we are able to make char uppercase.
15+
"""
16+
capitalize = True
17+
for char in word:
18+
if capitalize:
19+
char = char.upper()
20+
if char.isupper():
21+
capitalize = False
22+
yield char
23+
24+
25+
def cap_word(word: str):
26+
"""
27+
Capitalize the word. The word can start with ( or contain ':
28+
29+
word -> Word
30+
(word -> (Word
31+
word's -> Word's
32+
33+
"""
34+
if not word or any(c.isupper() for c in word): # ignore JIRA or sOme
35+
return word
36+
new_word = word.capitalize()
37+
if new_word != word:
38+
return new_word
39+
return "".join(cap_string_until_succeed(word))
40+
41+
1142
class RenameTestCases(Transformer):
1243
r"""
1344
Enforce test case naming.
@@ -88,8 +119,8 @@ def visit_TestCaseName(self, node): # noqa
88119
token = node.get_token(Token.TESTCASE_NAME)
89120
if token.value:
90121
if self.capitalize_each_word:
91-
token_value = " ".join(f"{word[0].upper()}{word[1:]}" for word in token.value.split(" "))
92-
token.value = token_value.lstrip()
122+
value = token.value.strip()
123+
token.value = " ".join(cap_word(word) for word in value.split(" "))
93124
else:
94125
token.value = token.value[0].upper() + token.value[1:]
95126
if self.replace_pattern is not None:

tests/atest/transformers/RenameTestCases/expected/replace_pattern_empty.robot

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ Containing replace pattern
1414

1515
Remove special chars: ?$@
1616
No Operation
17+
18+
Capitalize words (after !special that's
19+
No operation

tests/atest/transformers/RenameTestCases/expected/replace_pattern_placeholder.robot

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ Containing replace pattern PLACEHOLDER
1414

1515
Remove special chars: ?$@
1616
No Operation
17+
18+
Capitalize words (after !special that's
19+
No operation

tests/atest/transformers/RenameTestCases/expected/replace_pattern_special_chars.robot

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ Containing replace pattern JIRA-1234
1414

1515
Remove special chars
1616
No Operation
17+
18+
Capitalize words (after !special that's
19+
No operation

tests/atest/transformers/RenameTestCases/expected/selected.robot

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ Containing replace pattern JIRA-1234
1414

1515
Remove special chars: ?$@
1616
No Operation
17+
18+
Capitalize words (after !special that's
19+
No operation

tests/atest/transformers/RenameTestCases/expected/test.robot

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ Containing replace pattern JIRA-1234
1414

1515
Remove special chars: ?$@
1616
No Operation
17+
18+
Capitalize words (after !special that's
19+
No operation

tests/atest/transformers/RenameTestCases/expected/upper_case.robot

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ Containing Replace Pattern JIRA-1234
1414

1515
Remove Special Chars: ?$@
1616
No Operation
17+
18+
Capitalize Words (After !Special That's
19+
No operation

tests/atest/transformers/RenameTestCases/source/test.robot

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ Containing replace pattern JIRA-1234
1414

1515
Remove special chars: ?$@
1616
No Operation
17+
18+
Capitalize words (after !special that's
19+
No operation

0 commit comments

Comments
 (0)