Пример #1
0
def native_or_pformat(value):
    """Generic serialization compatible value formatter."""
    if comparison.is_regex(value):
        value = 'REGEX({})'.format(value.pattern)
    elif isinstance(value, comparison.Callable):
        value = str(value)
    elif callable(value):
        value = getattr(value, '__name__', _repr_obj(value))

    result = value if isinstance(value, COMPATIBLE_TYPES) else pprint.pformat(value)
    obj_repr = _repr_obj(result)

    if len(obj_repr) > MAX_LENGTH:
        result = obj_repr[:MAX_LENGTH] + '[truncated]...'
    return result
Пример #2
0
def native_or_pformat(value):
    """Generic serialization compatible value formatter."""
    if comparison.is_regex(value):
        value = "REGEX({})".format(value.pattern)
    elif isinstance(value, comparison.Callable):
        value = str(value)
    elif callable(value):
        value = getattr(value, "__name__", _repr_obj(value))

    # For basic builtin types we return the value unchanged. All other types
    # will be formatted as strings.
    if type(value) in COMPATIBLE_TYPES:
        result = value
    else:
        result = pprint.pformat(value)

    return result
Пример #3
0
def format_cell_data(data, limit):
    """
    Change the str representation of values in data if they represent regex or
    lambda functions. Also limit the length of these strings.

    :param data: List of values to be formatted.
    :type data: ``list``
    :param limit: The number of characters allowed in each string.
    :type limit: ``int``
    :return: List of formatted and limited strings.
    :rtype: ``list``
    """
    for i, value in enumerate(data):
        if is_regex(value):
            data[i] = "REGEX('{}')".format(value.pattern)
        elif "lambda" in str(value):
            data[i] = "<lambda>"

    return _limit_cell_length(data, limit)
Пример #4
0
def native_or_pformat(value):
    """Generic serialization compatible value formatter."""
    if comparison.is_regex(value):
        value = 'REGEX({})'.format(value.pattern)
    elif isinstance(value, comparison.Callable):
        value = str(value)
    elif callable(value):
        value = getattr(value, '__name__', _repr_obj(value))

    # For basic builtin types we return the value unchanged. All other types
    # will be formatted as strings.
    if type(value) in COMPATIBLE_TYPES:
        result = value
    else:
        result = pprint.pformat(value)

    obj_repr = _repr_obj(result)
    if len(obj_repr) > MAX_LENGTH:
        result = obj_repr[:MAX_LENGTH] + '[truncated]...'

    return result
Пример #5
0
    def get_matched_row_data(self, row_comparison, columns, include_columns,
                             exclude_columns, row_idx):
        """
        Return a single row of data in the correct match format and the
        RowStyles indicating which cells need to be coloured red.

        Sample output:

        [{'name': Susan == Susan, 'age': 24 == 24}]

        and

        [RowStyle(...), ...]


        :param row_comparison: RowComparison object containing this rows data.
        :type row_comparison: ``testplan.testing.multitest
                                .entries.assertions.RowComparison``
        :param columns: List of the displayed columns.
        :type columns: ``list``
        :param row_idx: Index of the row being compared. This is not the same as
                        other row_idx parameters in this module. Those refer to
                        the row index of the global table used to display
                        everything in the PDF report.
        :type row_idx: ``int``
        :param include_columns:
        :type include_columns:
        :param exclude_columns:
        :type exclude_columns:
        :return: A single row of the matched data in tabular format and the
                 RowStyles indicating which cells need to be coloured red.
        :rtype: ``list`` of ``dict`` and
                ``list`` of ``testplan.common.exporters.pdf.RowStyle``
        """
        result = {}
        colour_row = []

        for idx, column in enumerate(columns):
            actual = row_comparison.data[idx]
            other, matched = row_comparison.get_comparison_value(column, idx)

            value_limit = int((const.CELL_STRING_LENGTH - 4) / 2)
            other, actual = format_cell_data(
                data=[other, actual],
                limit=value_limit
            )

            other = "REGEX('{}')".format(
                other.pattern) if is_regex(other) else other

            include_columns = include_columns or columns
            exclude_columns = exclude_columns or []

            if (column not in include_columns) or (column in exclude_columns):
                result[column] = '{} .. {}'.format(actual, other)
                colour_row.append('I')
            elif matched:
                result[column] = '{} == {}'.format(actual, other)
                colour_row.append('P')
            else:
                result[column] = '{} != {}'.format(actual, other)
                colour_row.append('F')

        return result, colour_row
Пример #6
0
 def comparison_value(self):
     result = self.error or self.diff or self.extra or self.tag
     if comparison.is_regex(result):
         result = "REGEX('{}')".format(result.pattern)
     return result