Skip to content

generate_random_string_new_markers #5109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ Generate Random String With [LETTERS]
Generate Random String With [NUMBERS]
Check Test Case ${TESTNAME}

Generate Random String With [ARABIC]
Check Test Case ${TESTNAME}

Generate Random String With [POLISH]
Check Test Case ${TESTNAME}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
*** Settings ***
Library String
Library BuiltIn

*** Variables ***
${LOWER} qwertyuiopasdfghjklzxcvbnm
${UPPER} QWERTYUIOPASDFGHJKLZXCVBNM
${LETTERS} ${LOWER}${UPPER}
${NUMBERS} 1234567890
${ARABIC} ؆؇؈؉؊؋،؍؎؏ؘؙؚؐؑؒؓؔؕؖؗ؛؝؞؟ؠءآأؤإئابةتثجحخدذرزسشصضطظعغػؼؽؾؿـفقكلمنهوىيًٌٍَُِّْٕٖٜٟٓٔٗ٘ٙٚٛٝٞ٠١٢٣٤٥٦٧٨٩٪٫٬٭ٮٯٰٱٲٳٴٵٶٷٸٹٺٻټٽپٿڀځڂڃڄڅچڇڈډڊڋڌڍڎڏڐڑڒړڔڕږڗژڙښڛڜڝڞڟڠڡڢڣڤڥڦڧڨکڪګڬڭڮگڰڱڲڳڴڵڶڷڸڹںڻڼڽھڿۀہۂۃۄۅۆۇۈۉۊۋیۍێۏېۑےۓ۔ەۖۗۘۙۚۛۜ۝۞ۣ۟۠ۡۢۤۥۦۧۨ۩۪ۭ۫۬ۮۯ۰۱۲۳۴۵۶۷۸۹ۺۻۼ۽۾ۿ؎
${POLISH} ${LETTERS}ąćęłńóśźżĄĆĘŁŃÓŚŹŻ

*** Test Cases ***
Generate Random String With Defaults
Expand Down Expand Up @@ -48,6 +51,12 @@ Generate Random String With [LETTERS]
Generate Random String With [NUMBERS]
Test Random String With ${NUMBERS} [NUMBERS]

Generate Random String With [ARABIC]
Test Random String With ${ARABIC} [ARABIC]

Generate Random String With [POLISH]
Test Random String With ${POLISH} [POLISH]

*** Keywords ***
String Length Should Be And It Should Consist Of
[Arguments] ${string} ${length} ${allowed chars}
Expand Down
26 changes: 19 additions & 7 deletions src/robot/libraries/String.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,8 @@ def generate_random_string(self, length=8, chars="[LETTERS][NUMBERS]"):
| ``[UPPER]`` | Uppercase ASCII characters from ``A`` to ``Z``. |
| ``[LETTERS]`` | Lowercase and uppercase ASCII characters. |
| ``[NUMBERS]`` | Numbers from 0 to 9. |
| ``[ARABIC]`` | Arabic characters from 0x0600 to 0x0700 unicode |
| ``[POLISH]`` | ASCII characters and Polish diacritical signs. |

Examples:
| ${ret} = | Generate Random String |
Expand All @@ -652,6 +654,21 @@ def generate_random_string(self, length=8, chars="[LETTERS][NUMBERS]"):

Giving ``length`` as a range of values is new in Robot Framework 5.0.
"""

marker_map = {
'[LOWER]': ascii_lowercase,
'[UPPER]': ascii_uppercase,
'[LETTERS]': ascii_lowercase + ascii_uppercase,
'[NUMBERS]': digits,
'[ARABIC]': ''.join(chr(c) for c in range(0x0614, 0x0700)),
'[POLISH]': (
ascii_lowercase + ascii_uppercase +
''.join([
"\u0105", "\u0107", "\u0119", "\u0142", "\u0144", "\u00F3", "\u015B", "\u017A", "\u017C",
"\u0104", "\u0106", "\u0118", "\u0141", "\u0143", "\u00D3", "\u015A", "\u0179", "\u017B"
])
)
}
if length == "":
length = 8
if isinstance(length, str) and re.match(r"^\d+-\d+$", length):
Expand All @@ -661,13 +678,8 @@ def generate_random_string(self, length=8, chars="[LETTERS][NUMBERS]"):
self._convert_to_integer(max_length, "length"),
)
else:
length = self._convert_to_integer(length, "length")
for name, value in [
("[LOWER]", ascii_lowercase),
("[UPPER]", ascii_uppercase),
("[LETTERS]", ascii_lowercase + ascii_uppercase),
("[NUMBERS]", digits),
]:
length = self._convert_to_integer(length, 'length')
for name, value in marker_map.items():
chars = chars.replace(name, value)
maxi = len(chars) - 1
return "".join(chars[randint(0, maxi)] for _ in range(length))
Expand Down