16 from __future__
import absolute_import
20 from fnmatch
import fnmatchcase
21 from random
import randint
22 from string
import ascii_lowercase, ascii_uppercase, digits
27 unic, Utf8Reader, PY3)
52 ROBOT_LIBRARY_SCOPE =
'GLOBAL'
100 return bytes(string.encode(encoding, errors))
123 raise TypeError(
'Can not decode strings on Python 3.')
124 return bytes.decode(encoding, errors)
150 if os.path.isabs(template)
and os.path.isfile(template):
151 template = template.replace(
'/', os.sep)
152 logger.info(
'Reading template from file <a href="%s">%s</a>.'
153 % (template, template), html=
True)
154 with Utf8Reader(template)
as reader:
155 template = reader.read()
156 return template.format(*positional, **named)
160 count = len(string.splitlines())
161 logger.info(
'%d lines' % count)
187 lines = string.splitlines()[start:end]
188 logger.info(
'%d lines returned' % len(lines))
205 return string.splitlines()[line_number]
232 pattern = pattern.lower()
233 contains =
lambda line: pattern
in line.lower()
235 contains =
lambda line: pattern
in line
268 pattern = pattern.lower()
269 matches =
lambda line: fnmatchcase(line.lower(), pattern)
271 matches =
lambda line: fnmatchcase(line, pattern)
311 pattern =
'^%s$' % pattern
315 lines = string.splitlines()
316 matching = [line
for line
in lines
if matches(line)]
317 logger.info(
'%d out of %d lines matched' % (len(matching), len(lines)))
318 return '\n'.join(matching)
349 regexp = re.compile(pattern)
350 groups = [self.
_parse_group_parse_group(g)
for g
in groups]
351 return [m.group(*groups)
for m
in regexp.finditer(string)]
381 return string.replace(search_for, replace_with, count)
402 return re.sub(pattern, replace_with, string, max(count, 0))
424 for removable
in removables:
438 for pattern
in patterns:
465 return string.split(separator, max_split)
480 return string.rsplit(separator, max_split)
498 return string.split(marker)[0]
508 return string.split(marker)[-1]
533 for name, value
in [(
'[LOWER]', ascii_lowercase),
534 (
'[UPPER]', ascii_uppercase),
535 (
'[LETTERS]', ascii_lowercase + ascii_uppercase),
536 (
'[NUMBERS]', digits)]:
537 chars = chars.replace(name, value)
538 maxi = len(chars) - 1
539 return ''.join(chars[randint(0, maxi)]
for _
in range(length))
557 return string[start:end]
583 method = {
'BOTH': string.strip,
584 'LEFT': string.lstrip,
585 'RIGHT': string.rstrip,
586 'NONE':
lambda characters: string}[mode.upper()]
588 raise ValueError(
"Invalid mode '%s'." % mode)
589 return method(characters)
610 self.
_fail_fail(msg,
"'%s' is not a string.", item)
622 self.
_fail_fail(msg,
"'%s' is a string.", item)
636 self.
_fail_fail(msg,
"'%s' is not a Unicode string.", item)
650 self.
_fail_fail(msg,
"'%s' is not a byte string.", item)
663 if not string.islower():
664 self.
_fail_fail(msg,
"'%s' is not lowercase.", string)
677 if not string.isupper():
678 self.
_fail_fail(msg,
"'%s' is not uppercase.", string)
695 if not string.istitle():
696 self.
_fail_fail(msg,
"'%s' is not titlecase.", string)
709 raise ValueError(
"Cannot convert '%s' argument '%s' to an integer."
712 def _fail(self, message, default_template, *items):
714 message = default_template % tuple(
unic(item)
for item
in items)
A test library for string manipulation and verification.
def get_lines_matching_regexp(self, string, pattern, partial_match=False)
Returns lines of the given string that match the regexp pattern.
def get_regexp_matches(self, string, pattern, *groups)
Returns a list of all non-overlapping matches in the given string.
def get_lines_matching_pattern(self, string, pattern, case_insensitive=False)
Returns lines of the given string that match the pattern.
def format_string(self, template, *positional, **named)
Formats a template using the given positional and named arguments.
def split_string_from_right(self, string, separator=None, max_split=-1)
Splits the string using separator starting from right.
def generate_random_string(self, length=8, chars='[LETTERS][NUMBERS]')
Generates a string with a desired length from the given chars.
def split_string(self, string, separator=None, max_split=-1)
Splits the string using separator as a delimiter string.
def should_be_titlecase(self, string, msg=None)
Fails if given string is not title.
def remove_string_using_regexp(self, string, *patterns)
Removes patterns from the given string.
def replace_string_using_regexp(self, string, pattern, replace_with, count=-1)
Replaces pattern in the given string with replace_with.
def get_line(self, string, line_number)
Returns the specified line from the given string.
def _parse_group(self, group)
def split_to_lines(self, string, start=0, end=None)
Splits the given string to lines.
def get_lines_containing_string(self, string, pattern, case_insensitive=False)
Returns lines of the given string that contain the pattern.
def should_be_unicode_string(self, item, msg=None)
Fails if the given item is not a Unicode string.
def should_be_string(self, item, msg=None)
Fails if the given item is not a string.
def should_not_be_string(self, item, msg=None)
Fails if the given item is a string.
def split_string_to_characters(self, string)
Splits the given string to characters.
def get_line_count(self, string)
Returns and logs the number of lines in the given string.
def replace_string(self, string, search_for, replace_with, count=-1)
Replaces search_for in the given string with replace_with.
def convert_to_lowercase(self, string)
Converts string to lowercase.
def _get_matching_lines(self, string, matches)
def should_be_lowercase(self, string, msg=None)
Fails if the given string is not in lowercase.
def strip_string(self, string, mode='both', characters=None)
Remove leading and/or trailing whitespaces from the given string.
def should_be_byte_string(self, item, msg=None)
Fails if the given item is not a byte string.
def encode_string_to_bytes(self, string, encoding, errors='strict')
Encodes the given Unicode string to bytes using the given encoding.
def convert_to_uppercase(self, string)
Converts string to uppercase.
def _convert_to_integer(self, value, name)
def get_substring(self, string, start, end=None)
Returns a substring from start index to end index.
def should_be_uppercase(self, string, msg=None)
Fails if the given string is not in uppercase.
def remove_string(self, string, *removables)
Removes all removables from the given string.
def fetch_from_right(self, string, marker)
Returns contents of the string after the last occurrence of marker.
def fetch_from_left(self, string, marker)
Returns contents of the string before the first occurrence of marker.
def _convert_to_index(self, value, name)
def _fail(self, message, default_template, *items)
def decode_bytes_to_string(self, bytes, encoding, errors='strict')
Decodes the given bytes to a Unicode string using the given encoding.
def is_truthy(item)
Returns True or False depending is the item considered true or not.
def get_version(naked=False)